-
Notifications
You must be signed in to change notification settings - Fork 7
/
ScriptBuilder.cs
405 lines (345 loc) · 18.3 KB
/
ScriptBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/////////////////////////////////////////////////////////////////////////////////
// CodeLab for Paint.NET
// Copyright ©2006 Rick Brewster, Tom Jackson. All Rights Reserved.
// Portions Copyright ©2018 BoltBait. All Rights Reserved.
// Portions Copyright ©2018 Jason Wendt. All Rights Reserved.
// Portions Copyright ©Microsoft Corporation. All Rights Reserved.
//
// THE CODELAB DEVELOPERS MAKE NO WARRANTY OF ANY KIND REGARDING THE CODE. THEY
// SPECIFICALLY DISCLAIM ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE OR
// ANY OTHER WARRANTY. THE CODELAB DEVELOPERS DISCLAIM ALL LIABILITY RELATING
// TO THE USE OF THIS CODE. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR
// OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED HEREIN.
//
// Latest distribution: https://www.BoltBait.com/pdn/codelab
/////////////////////////////////////////////////////////////////////////////////
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using PaintDotNet;
using PaintDotNet.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text.RegularExpressions;
namespace PdnCodeLab
{
internal static class ScriptBuilder
{
private static IEffect builtEffect;
private static FileType builtFileType;
private static int lineOffset;
private static string exceptionMsg;
private static IEnumerable<MetadataReference> references = Array.Empty<MetadataReference>();
private static readonly CSharpParseOptions parseOptions = CSharpParseOptions.Default;
private static CSharpCompilationOptions compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true, optimizationLevel: OptimizationLevel.Release, deterministic: false);
private static IEnumerable<string> warningsToIgnore = Array.Empty<string>();
private static IEnumerable<Diagnostic> errors;
#region Properties
internal static string CSharpVersion => parseOptions.LanguageVersion.ToDisplayString();
internal static IEffect BuiltEffect => builtEffect;
internal static FileType BuiltFileType => builtFileType;
internal static int LineOffset => lineOffset;
internal static int ColumnOffset => 8;
internal static string Exception => exceptionMsg;
internal static IReadOnlyCollection<Error> Errors
{
get
{
List<Error> errorList = new List<Error>();
if (!exceptionMsg.IsNullOrEmpty())
{
errorList.Add(Error.NewInternalError(exceptionMsg));
}
if (errors != null)
{
errorList.AddRange(errors.Select(diag => Error.NewCodeError(diag)));
}
errorList.Sort();
return errorList;
}
}
#endregion
internal static void SetReferences(IEnumerable<string> dllFilePaths)
{
references = dllFilePaths.Select(a => MetadataReference.CreateFromFile(a));
}
internal static void SetWarningLevel(int level)
{
compilationOptions = compilationOptions.WithWarningLevel(level);
}
internal static void SetWarningsToIgnore(IEnumerable<string> warnings)
{
warningsToIgnore = warnings;
}
internal static bool BuildEffect<TEffect>(string fullSourceCode, bool debug = false)
where TEffect : IEffect
{
builtEffect = null;
Assembly assembly = CreateAssembly(fullSourceCode, debug);
if (assembly == null)
{
return false;
}
Intelli.UserDefinedTypes.Clear();
foreach (Type type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(TEffect)) && !type.IsAbstract)
{
builtEffect = (TEffect)type.GetConstructor(Type.EmptyTypes).Invoke(null);
Intelli.UserScript = type;
}
else if (type.DeclaringType != null && type.DeclaringType.FullName.StartsWith(Intelli.UserScriptFullName, StringComparison.Ordinal) && !Intelli.UserDefinedTypes.ContainsKey(type.Name))
{
Intelli.UserDefinedTypes.Add(type.Name, type);
}
}
return builtEffect != null;
}
internal static bool BuildEffectDll(string fullSourceCode, string scriptPath, string iconPath, HelpType helpType)
{
string projectName = Path.GetFileNameWithoutExtension(scriptPath);
List<string> resourceFiles = new List<string>();
if (File.Exists(iconPath))
{
// If an icon is specified and exists, add it to the build as an embedded resource to the same namespace as the effect.
resourceFiles.Add(iconPath);
}
string samplePath = Path.ChangeExtension(scriptPath, ".sample.png");
if (File.Exists(samplePath))
{
// If an image exists in the icon directory with a ".sample.png" extension, add it to the build as an embedded resource.
resourceFiles.Add(samplePath);
}
string helpPath = Path.ChangeExtension(scriptPath, ".rtz");
if (helpType == HelpType.RichText && File.Exists(helpPath))
{
// If an help file exists in the source directory with a ".rtz" extension, add it to the build as an embedded resource.
resourceFiles.Add(helpPath);
}
return BuildDll(projectName, resourceFiles, fullSourceCode, true);
}
internal static bool BuildFileType(string fullSourceCode, bool debug = false)
{
builtFileType = null;
Assembly assembly = CreateAssembly(fullSourceCode, debug);
if (assembly == null)
{
return false;
}
Intelli.UserDefinedTypes.Clear();
foreach (Type type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(FileType)) && !type.IsAbstract)
{
builtFileType = (FileType)type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0].Invoke(null);
return true;
}
}
return false;
}
internal static bool BuildFileTypeDll(string fullSourceCode, string projectName)
{
return BuildDll(projectName, Array.Empty<string>(), fullSourceCode, false);
}
private static bool BuildDll(string projectName, IEnumerable<string> resources, string sourceCode, bool isEffect)
{
lineOffset = CalculateLineOffset(sourceCode);
exceptionMsg = null;
errors = null;
try
{
string sanitizedProjectName = Regex.Replace(projectName, @"[^\w]", ""); // Remove non-alpha characters from namespace
// Calculate output path
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string dllPath = Path.Combine(desktopPath, sanitizedProjectName);
dllPath = Path.ChangeExtension(dllPath, ".dll");
IEnumerable<SyntaxTree> syntaxTree = new[] { CSharpSyntaxTree.ParseText(sourceCode, options: parseOptions) };
CSharpCompilation compilation = CSharpCompilation.Create(sanitizedProjectName, syntaxTree, references, compilationOptions);
IEnumerable<ResourceDescription> resourceDescriptions = resources.Select(res => new ResourceDescription(
sanitizedProjectName + "Effect." + Path.GetFileName(res),
() => File.OpenRead(res),
true));
using (FileStream dllStream = new FileStream(dllPath, FileMode.Create))
using (Stream win32resStream = compilation.CreateDefaultWin32Resources(true, true, null, null))
{
EmitResult result = compilation.Emit(peStream: dllStream, manifestResources: resourceDescriptions, win32Resources: win32resStream);
errors = result.Diagnostics.Where(ErrorFilter);
if (!result.Success)
{
return false;
}
}
string zipPath = Path.ChangeExtension(dllPath, ".zip");
string batPath = Path.Combine(desktopPath, "Install_" + sanitizedProjectName);
batPath = Path.ChangeExtension(batPath, ".bat");
// Create install bat file
if (File.Exists(batPath))
{
File.Delete(batPath);
}
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
string pluginDirName = isEffect ? "Effects" : "FileTypes";
// Try this in Russian for outputting Russian characters to the install batch file:
//System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("windows-1251");
//var stream = new FileStream(installPath, FileMode.Create);
//StreamWriter sw = new StreamWriter(stream, encoding);
StreamWriter sw = new StreamWriter(batPath);
sw.WriteLine("@echo off");
sw.WriteLine(":: Get ADMIN Privs");
sw.WriteLine("mkdir \"%windir%\\BatchGotAdmin\"");
sw.WriteLine("if '%errorlevel%' == '0' (");
sw.WriteLine(" rmdir \"%windir%\\BatchGotAdmin\" & goto gotAdmin");
sw.WriteLine(") else ( goto UACPrompt )");
sw.WriteLine("");
sw.WriteLine(":UACPrompt");
sw.WriteLine(" echo Set UAC = CreateObject^(\"Shell.Application\"^) > \"%temp%\\getadmin.vbs\"");
sw.WriteLine(" echo UAC.ShellExecute %0, \"\", \"\", \"runas\", 1 >> \"%temp%\\getadmin.vbs\"");
sw.WriteLine(" \"%temp%\\getadmin.vbs\"");
sw.WriteLine(" exit /B");
sw.WriteLine("");
sw.WriteLine(":gotAdmin");
sw.WriteLine(" if exist \"%temp%\\getadmin.vbs\" ( del \"%temp%\\getadmin.vbs\" )");
sw.WriteLine(" pushd \"%CD%\"");
sw.WriteLine(" CD /D \"%~dp0\"");
sw.WriteLine("");
sw.WriteLine(":: End Get ADMIN Privs");
sw.WriteLine(":: Read registry to find Paint.NET install directory");
sw.WriteLine("reg query HKLM\\SOFTWARE\\Paint.NET /v TARGETDIR 2>nul || (echo Sorry, I can't find Paint.NET! & goto store)");
sw.WriteLine("set PDN_DIR=");
sw.WriteLine("for /f \"tokens=2,*\" %%a in ('reg query HKLM\\SOFTWARE\\Paint.NET /v TARGETDIR ^| findstr TARGETDIR') do (");
sw.WriteLine(" set PDN_DIR=%%b");
sw.WriteLine(")");
sw.WriteLine("if not defined PDN_DIR (echo Sorry, I can't find Paint.NET! & goto store)");
sw.WriteLine(":: End read registry");
sw.WriteLine(":: Now do install");
sw.WriteLine("@echo off");
sw.WriteLine("cls");
sw.WriteLine("echo.");
sw.WriteLine("echo Installing " + Path.GetFileName(dllPath) + " to %PDN_DIR%\\" + pluginDirName + "\\");
sw.WriteLine("echo.");
sw.WriteLine("copy /y \"" + Path.GetFileName(dllPath) + "\" \"%PDN_DIR%\\" + pluginDirName + "\\\"");
sw.WriteLine("if '%errorlevel%' == '0' (");
sw.WriteLine("goto success");
sw.WriteLine(") else (");
sw.WriteLine("goto fail");
sw.WriteLine(")");
sw.WriteLine(":store");
sw.WriteLine("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\" /v Personal 2>nul || (echo Sorry, I can't find Paint.NET! & goto fail)");
sw.WriteLine("set PDN_DIR=");
sw.WriteLine("for /f \"tokens=2,*\" %%a in ('reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\" /v Personal ^| findstr Personal') do (");
sw.WriteLine(" set PDN_DIR=%%b");
sw.WriteLine(")");
sw.WriteLine("if not defined PDN_DIR (echo Sorry, I can't find Paint.NET! & goto fail)");
sw.WriteLine("@echo off");
sw.WriteLine("cls");
sw.WriteLine("setlocal enabledelayedexpansion");
sw.WriteLine("set PDN_DIR=!PDN_DIR:%%USERPROFILE%%=%USERPROFILE%!");
sw.WriteLine("setlocal disabledelayedexpansion");
sw.WriteLine("echo.");
sw.WriteLine("echo I could not find the standard installation of Paint.NET.");
sw.WriteLine("echo I will install this effect in your Documents folder instead");
sw.WriteLine("echo in case you are using the store version.");
sw.WriteLine("echo.");
sw.WriteLine("echo Installing " + Path.GetFileName(dllPath) + " to %PDN_DIR%\\paint.net App Files\\" + pluginDirName + "\\");
sw.WriteLine("echo.");
sw.WriteLine("mkdir \"%PDN_DIR%\\paint.net App Files\\" + pluginDirName + "\\\" 2>nul");
sw.WriteLine("copy /y \"" + Path.GetFileName(dllPath) + "\" \"%PDN_DIR%\\paint.net App Files\\" + pluginDirName + "\\\"");
sw.WriteLine("if '%errorlevel%' == '0' (");
sw.WriteLine("goto success");
sw.WriteLine(") else (");
sw.WriteLine("goto fail");
sw.WriteLine(")");
sw.WriteLine(":success");
sw.WriteLine("echo.");
sw.WriteLine(@"echo _____ _ _ _____ _____ ______ _____ _____ _ ");
sw.WriteLine(@"echo / ____) ! ! !/ ___) ___) ____)/ ____) ____)! !");
sw.WriteLine(@"echo ( (___ ! ! ! ! / / / ! (__ ( (___( (___ ! !");
sw.WriteLine(@"echo \___ \! ! ! ! ( ( ( ! __) \___ \\___ \ ! !");
sw.WriteLine(@"echo ____) ) !__! ! \__\ \___! (____ ____) )___) )!_!");
sw.WriteLine(@"echo (_____/ \____/ \_____)_____)______)_____/_____/ (_)");
//sw.WriteLine(@"echo _ _ ___ _____ ____ _ _ _ ");
//sw.WriteLine(@"echo ( \/ )/ __)( _ )( __)( \/ )/ \");
//sw.WriteLine(@"echo ) /( (__ )( )( ) _) ) ( \_/");
//sw.WriteLine(@"echo (__/ \___)(_) (_)(____)(_/\_)(_)");
sw.WriteLine("goto done");
sw.WriteLine(":fail");
sw.WriteLine("echo.");
sw.WriteLine(@"echo _____ _____ _ _ ");
sw.WriteLine(@"echo ! ___)/\ (_ _) ! ! !");
sw.WriteLine(@"echo ! (__ / \ ! ! ! ! ! !");
sw.WriteLine(@"echo ! __) /\ \ ! ! ! ! ! !");
sw.WriteLine(@"echo ! ! / ____ \ _! !_! !___ !_!");
sw.WriteLine(@"echo !_!/_/ \_\_____)_____)(_)");
//sw.WriteLine(@"echo __ ____ __ _ ___ ____ _");
//sw.WriteLine(@"echo / \(_ _)! / /(_ \( __ ) / \");
//sw.WriteLine(@"echo ( O ) )( ! ( / _ )(__ \ \_/");
//sw.WriteLine(@"echo \__/ (__) !__\_\\___/(____/ (_)");
sw.WriteLine("echo.");
sw.WriteLine("echo Close Paint.NET and try installing again.");
sw.WriteLine(":done");
sw.WriteLine("echo.");
sw.WriteLine("pause");
sw.Flush();
sw.Close();
// Create .zip file on user's desktop
using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(dllPath, Path.GetFileName(dllPath)); // add .dll file
archive.CreateEntryFromFile(batPath, Path.GetFileName(batPath)); // add install.bat
}
if (File.Exists(zipPath))
{
// if the zip file was successfully built, delete temp files
File.Delete(dllPath);
File.Delete(batPath);
}
return true;
}
catch (Exception ex)
{
exceptionMsg = ex.Message;
}
return false;
}
private static int CalculateLineOffset(string sourceCode)
{
return sourceCode.Substring(0, sourceCode.IndexOf("#region User Entered Code", StringComparison.Ordinal)).CountLines() + 1;
}
private static Assembly CreateAssembly(string sourceCode, bool debug)
{
lineOffset = CalculateLineOffset(sourceCode);
exceptionMsg = null;
errors = null;
string assemblyName = Path.GetRandomFileName();
IEnumerable<SyntaxTree> syntaxTree = new[] { CSharpSyntaxTree.ParseText(sourceCode, options: debug ? parseOptions.WithPreprocessorSymbols("DEBUG") : parseOptions) };
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName, syntaxTree, references,
compilationOptions.WithOptimizationLevel(debug ? OptimizationLevel.Debug : OptimizationLevel.Release));
using (MemoryStream ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
errors = result.Diagnostics.Where(ErrorFilter);
if (!result.Success)
{
return null;
}
AssemblyLoadContext loadContext = new AssemblyLoadContext(null, true);
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = loadContext.LoadFromStream(ms);
return assembly;
}
}
private static bool ErrorFilter(Diagnostic diagnostic)
{
return !(diagnostic.Severity == DiagnosticSeverity.Hidden || warningsToIgnore.Contains(diagnostic.Id));
}
}
}