-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacker.cs
More file actions
353 lines (312 loc) · 12.7 KB
/
Packer.cs
File metadata and controls
353 lines (312 loc) · 12.7 KB
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
using System;
using System.IO;
using System.Collections.Generic;
using Serilog;
using Microsoft.CodeAnalysis;
using System.Text;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Emit;
namespace ModShardPackerReference;
public static class FilePacker
{
/// <summary>
/// Pack the content folder into a sml file named <see cref="namePacked"/>.
/// </summary>
/// <param name="namePacked"></param>
/// <param name="folderToPack"></param>
/// <param name="outputfolder"></param>
/// <param name="dllfolder"></param>
/// <param name="frontVersion"></param>
/// <param name="neededType"></param>
/// <returns></returns>
/// <exception cref="DirectoryNotFoundException"></exception>
public static bool Pack(string? namePacked, string folderToPack, string outputfolder, string dllfolder, string frontVersion, Type[] neededType)
{
Log.Information("Starting packing {0}", folderToPack);
DirectoryInfo dir = new(folderToPack);
namePacked ??= dir.Name;
FileInfo[] textures = dir.GetFiles("*.png", SearchOption.AllDirectories);
FileInfo[] scripts = dir.GetFiles("*.lua", SearchOption.AllDirectories);
FileInfo[] codes = dir.GetFiles("*.gml", SearchOption.AllDirectories);
FileInfo[] assemblies = dir.GetFiles("*.asm", SearchOption.AllDirectories);
int offset = 0;
if (!Directory.Exists(outputfolder))
{
throw new DirectoryNotFoundException($"Could not find the given output folder '{outputfolder}'");
}
FileStream fs = new(Path.Join(outputfolder, namePacked + ".sml"), FileMode.Create);
Write(fs, "MSLM");
Log.Information("Writting header...");
Write(fs, frontVersion);
Log.Information("Writting version...");
Write(fs, textures.Length);
foreach (FileInfo tex in textures)
{
string name = dir.Name + tex.FullName.Replace(folderToPack, "");
Write(fs, name.Length);
Write(fs, name);
Write(fs, offset);
int len = CalculateBytesLength(tex);
Write(fs, len);
offset += len;
}
Log.Information("Preparing textures...");
Write(fs, scripts.Length);
foreach (FileInfo scr in scripts)
{
string name = dir.Name + scr.FullName.Replace(folderToPack, "");
Write(fs, name.Length);
Write(fs, name);
Write(fs, offset);
int len = CalculateBytesLength(scr);
Write(fs, len);
offset += len;
}
Log.Information("Preparing scripts...");
Write(fs, codes.Length);
foreach (FileInfo cds in codes)
{
string name = dir.Name + cds.FullName.Replace(folderToPack, "");
Write(fs, name.Length);
Write(fs, name);
Write(fs, offset);
int len = CalculateBytesLength(cds);
Write(fs, len);
offset += len;
}
Log.Information("Preparing codes...");
Write(fs, assemblies.Length);
foreach (FileInfo asm in assemblies)
{
string name = dir.Name + asm.FullName.Replace(folderToPack, "");
Write(fs, name.Length);
Write(fs, name);
Write(fs, offset);
int len = CalculateBytesLength(asm);
Write(fs, len);
offset += len;
}
Log.Information("Preparing assemblies...");
foreach (FileInfo tex in textures)
Write(fs, tex);
Log.Information("Writting textures...");
foreach (FileInfo scr in scripts)
Write(fs, scr);
Log.Information("Writting scripts...");
foreach (FileInfo cds in codes)
Write(fs, cds);
Log.Information("Writting codes...");
foreach (FileInfo asm in assemblies)
Write(fs, asm);
Log.Information("Writting assemblies...");
Log.Information("Starting compilation...");
bool successful;
byte[] code;
try
{
successful = CompileMod(namePacked, folderToPack, dllfolder, neededType, out code, out _);
}
catch
{
throw;
}
if (!successful)
{
fs.Close();
File.Delete(fs.Name);
Log.Information("Failed packing {0}", namePacked);
return false;
}
Write(fs, code.Length);
Write(fs, code);
Log.Information("Successfully packed {0}", namePacked);
fs.Close();
return true;
}
public static void Write(FileStream fs, object obj)
{
Type type = obj.GetType();
if (type == typeof(int))
{
byte[]? bytes = BitConverter.GetBytes((int)obj);
fs.Write(bytes, 0, bytes.Length);
}
else if(type == typeof(string))
{
byte[]? bytes = Encoding.UTF8.GetBytes((string)obj);
fs.Write(bytes, 0, bytes.Length);
}
else if(type == typeof(FileInfo))
{
FileStream stream = new(((FileInfo)obj).FullName, FileMode.Open);
byte[]? bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
fs.Write(bytes, 0, bytes.Length);
stream.Close();
}
else if(type == typeof(byte[]))
{
fs.Write((byte[])obj);
}
}
public static int CalculateBytesLength(FileInfo f)
{
FileStream stream = new(f.FullName, FileMode.Open);
int len = (int)stream.Length;
stream.Close();
return len;
}
public static List<MetadataReference> GetSystemMetadataReferences()
{
string trustedAssemblies = (AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") as string)!;
string[] trustedList = trustedAssemblies.Split(Path.PathSeparator);
List<string> required = new()
{
"System.Runtime.dll",
"System.Linq.dll",
"System.ObjectModel.dll",
"System.Collections.dll",
"System.Private.CoreLib.dll",
"System.Text.RegularExpressions.dll"
};
IEnumerable<string> filteredPath = trustedList.Where(p => required.Exists(r => p.Contains(r)));
return filteredPath.Select(x => MetadataReference.CreateFromFile(x) as MetadataReference).ToList();
}
public static Diagnostic[] RoslynCompile(string name, IEnumerable<string> files, IEnumerable<string> preprocessorSymbols, string dllfolder, Type[] neededType, out byte[] code, out byte[] pdb)
{
NameSyntax[] qualifiedNames = {
SyntaxFactory.ParseName("System"),
SyntaxFactory.ParseName("System.Linq"),
SyntaxFactory.ParseName("System.Collections"),
SyntaxFactory.ParseName("System.Collections.Generic"),
SyntaxFactory.ParseName("System.Runtime")
};
// creating compilation options
CSharpCompilationOptions options = new(
OutputKind.DynamicallyLinkedLibrary,
checkOverflow: true,
optimizationLevel: OptimizationLevel.Release,
allowUnsafe: false
);
// creating parse options
CSharpParseOptions parseOptions = new(LanguageVersion.Preview, preprocessorSymbols: preprocessorSymbols);
// creating emit options
EmitOptions emitOptions = new(debugInformationFormat: DebugInformationFormat.PortablePdb);
// convert string of dll into MetadataReference
IEnumerable<MetadataReference> defaultReferences = Directory.GetFiles(dllfolder, "*.dll")
.Select(x => MetadataReference.CreateFromFile(x));
try
{
defaultReferences = defaultReferences
.Concat(neededType.Select(x => MetadataReference.CreateFromFile(x.Assembly.Location)))
.Concat(GetSystemMetadataReferences());
}
catch(Exception ex)
{
if (ex is ArgumentNullException || ex is ArgumentException)
{
Log.Error(ex, "Something went wrong");
}
else if (ex is IOException)
{
Log.Error(ex, "Error with the dlls, check if they are correctly referenced.");
}
else
{
Log.Error(ex, "Unexpected error");
}
throw;
}
IEnumerable<SyntaxTree> src = files.Select(f => SyntaxFactory.ParseSyntaxTree(File.ReadAllText(f), parseOptions, f, Encoding.UTF8));
// update tree before compilation to add needed using
src = src.Select(tree => (tree.GetRoot() as CompilationUnitSyntax)!
.AddUsings(qualifiedNames.Select(qualifiedNameSpace => SyntaxFactory.UsingDirective(qualifiedNameSpace)).ToArray()).NormalizeWhitespace().SyntaxTree);
Log.Information("Compilation: Writting ast...");
CSharpCompilation comp = CSharpCompilation.Create(name, src, defaultReferences, options);
Log.Information("Compilation: used Assemblies...");
foreach(string? assemblyReferencesDisplay in comp.GetUsedAssemblyReferences().Select(usedAssemblyReferences => usedAssemblyReferences.Display))
{
if (assemblyReferencesDisplay != null)
{
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assemblyReferencesDisplay);
Log.Information("{{{0}}} {{{1}}} {{{2}}}", assemblyReferencesDisplay, fileVersionInfo.FileVersion, fileVersionInfo.ProductName);
}
else
{
Log.Error("Cannot find the assembly");
}
}
foreach(SyntaxTree tree in comp.SyntaxTrees)
{
// get the semantic model for this tree
SemanticModel model = comp.GetSemanticModel(tree);
// find everywhere in the AST that refers to a type
SyntaxNode root = tree.GetRoot();
IEnumerable<TypeSyntax> allTypeNames = root.DescendantNodesAndSelf().OfType<TypeSyntax>();
foreach(TypeSyntax typeName in allTypeNames)
{
// what does roslyn think the type _name_ actually refers to?
Microsoft.CodeAnalysis.TypeInfo effectiveType = model.GetTypeInfo(typeName);
if(effectiveType.Type != null && effectiveType.Type.TypeKind == TypeKind.Error)
{
// if it's an error type (ie. couldn't be resolved), cast and proceed
Log.Error("Cannot understand type {0} of variable {1}", (IErrorTypeSymbol)effectiveType.Type, typeName);
}
}
}
using MemoryStream peStream = new();
using MemoryStream pdbStream = new();
EmitResult results = comp.Emit(peStream, pdbStream, options: emitOptions);
code = peStream.ToArray();
pdb = pdbStream.ToArray();
return results.Diagnostics.ToArray();
}
public static bool CompileMod(string name, string path, string dllfolder, Type[] neededType, out byte[] code, out byte[] pdb)
{
IEnumerable<string> files = Directory
.GetFiles(path, "*.cs", SearchOption.AllDirectories)
.Where(file => !IgnoreCompletely(path, file));
Log.Information("Compilation: Gathering files...");
Diagnostic[] result = Array.Empty<Diagnostic>();
try
{
result = RoslynCompile(name, files, new[] { "FNA" }, dllfolder, neededType, out code, out pdb);
}
catch
{
throw;
}
Log.Information("Compilation: Gathering results...");
foreach(Diagnostic err in result.Where(e => e.Severity == DiagnosticSeverity.Error))
{
Log.Error(err.ToString());
}
foreach(Diagnostic warning in result.Where(e => e.Severity == DiagnosticSeverity.Warning))
{
Log.Warning(warning.ToString());
}
foreach(Diagnostic info in result.Where(e => e.Severity == DiagnosticSeverity.Info))
{
Log.Information(info.ToString());
}
if(Array.Exists(result, e => e.Severity == DiagnosticSeverity.Error))
{
return false;
}
return true;
}
public static bool IgnoreCompletely(string root, string file)
{
string path_from_root = file.Replace(root + Path.DirectorySeparatorChar.ToString(), "");
string[] dirs = path_from_root.Split(Path.DirectorySeparatorChar.ToString());
string dir = "";
if (dirs.Length > 1) // not only a file, but also folders
{
dir = dirs[0]; // topmost directory
}
return dir.StartsWith('.') || dir == "bin" || dir == "obj";
}
}