private static bool CompileMod(string modDir, BuildProperties properties) { CompilerParameters compileOptions = new CompilerParameters(); compileOptions.GenerateExecutable = false; compileOptions.GenerateInMemory = false; compileOptions.OutputAssembly = ModPath + Path.DirectorySeparatorChar + Path.GetFileName(modDir) + ".tmod"; foreach (string reference in buildReferences) { compileOptions.ReferencedAssemblies.Add(reference); } Directory.CreateDirectory(DllPath); foreach (string reference in properties.dllReferences) { compileOptions.ReferencedAssemblies.Add(DllPath + Path.DirectorySeparatorChar + reference + ".dll"); } foreach (string reference in properties.modReferences) { compileOptions.ReferencedAssemblies.Add(ModSourcePath + Path.DirectorySeparatorChar + reference + ".dll"); } CodeDomProvider codeProvider = new CSharpCodeProvider(); CompilerResults results = codeProvider.CompileAssemblyFromFile(compileOptions, Directory.GetFiles(modDir, "*.cs", SearchOption.AllDirectories)); CompilerErrorCollection errors = results.Errors; foreach (string reference in properties.modReferences) { File.Delete(ModSourcePath + Path.DirectorySeparatorChar + reference + ".dll"); } if (errors.HasErrors) { ErrorLogger.LogCompileErrors(errors); return(false); } return(true); }
private static void CompileMod(BuildingMod mod, List <LoadingMod> refMods, bool forWindows, ref byte[] dll, ref byte[] pdb) { LoadReferences(); bool generatePDB = mod.properties.includePDB && forWindows; if (generatePDB && !windows) { Console.WriteLine("PDB files can only be generated for windows, on windows."); generatePDB = false; } //collect all dll references var tempDir = Path.Combine(ModPath, "compile_temp"); Directory.CreateDirectory(tempDir); var refs = new List <string>(); //everything used to compile the tModLoader for the target platform refs.AddRange(GetTerrariaReferences(tempDir, forWindows)); //libs added by the mod refs.AddRange(mod.properties.dllReferences.Select(refDll => Path.Combine(mod.path, "lib/" + refDll + ".dll"))); //all dlls included in all referenced mods foreach (var refMod in refMods) { var path = Path.Combine(tempDir, refMod + ".dll"); File.WriteAllBytes(path, refMod.modFile.GetMainAssembly(forWindows)); refs.Add(path); foreach (var refDll in refMod.properties.dllReferences) { path = Path.Combine(tempDir, refDll + ".dll"); File.WriteAllBytes(path, refMod.modFile.GetFile("lib/" + refDll + ".dll")); refs.Add(path); } } var compileOptions = new CompilerParameters { OutputAssembly = Path.Combine(tempDir, mod + ".dll"), GenerateExecutable = false, GenerateInMemory = false, TempFiles = new TempFileCollection(tempDir, true), IncludeDebugInformation = generatePDB }; compileOptions.ReferencedAssemblies.AddRange(refs.ToArray()); var files = Directory.GetFiles(mod.path, "*.cs", SearchOption.AllDirectories).Where(file => !mod.properties.ignoreFile(file.Substring(mod.path.Length + 1))).ToArray(); try { CompilerResults results; if (mod.properties.languageVersion == 6) { if (Environment.Version.Revision < 10000) { ErrorLogger.LogBuildError(".NET Framework 4.5 must be installed to compile C# 6.0"); return; } results = RoslynCompile(compileOptions, files); } else { var options = new Dictionary <string, string> { { "CompilerVersion", "v" + mod.properties.languageVersion + ".0" } }; results = new CSharpCodeProvider(options).CompileAssemblyFromFile(compileOptions, files); } if (results.Errors.HasErrors) { ErrorLogger.LogCompileErrors(results.Errors, forWindows); return; } dll = File.ReadAllBytes(compileOptions.OutputAssembly); dll = PostProcess(dll, forWindows); if (generatePDB) { pdb = File.ReadAllBytes(Path.Combine(tempDir, mod + ".pdb")); } } finally { int numTries = 10; while (numTries > 0) { try { Directory.Delete(tempDir, true); numTries = 0; } catch { System.Threading.Thread.Sleep(1); numTries--; } } } }
private static byte[] CompileMod(string modDir, BuildProperties properties, bool forWindows) { CompilerParameters compileOptions = new CompilerParameters(); compileOptions.GenerateExecutable = false; compileOptions.GenerateInMemory = false; string outFile = ModPath + Path.DirectorySeparatorChar + Path.GetFileName(modDir) + ".dll"; compileOptions.OutputAssembly = outFile; bool flag = false; foreach (string reference in buildReferences) { if (forWindows) { if (reference.IndexOf("FNA.dll") >= 0) { compileOptions.ReferencedAssemblies.Add("Microsoft.Xna.Framework.dll"); compileOptions.ReferencedAssemblies.Add("Microsoft.Xna.Framework.Game.dll"); compileOptions.ReferencedAssemblies.Add("Microsoft.Xna.Framework.Graphics.dll"); compileOptions.ReferencedAssemblies.Add("Microsoft.Xna.Framework.Xact.dll"); continue; } else if (!windows && reference.IndexOf("Terraria.exe") >= 0) { compileOptions.ReferencedAssemblies.Add("TerrariaWindows.exe"); continue; } } else { if (reference.IndexOf("Microsoft.Xna.Framework") >= 0) { if (!flag) { compileOptions.ReferencedAssemblies.Add("FNA.dll"); flag = true; } continue; } else if (windows && reference.IndexOf(System.AppDomain.CurrentDomain.FriendlyName) >= 0) { compileOptions.ReferencedAssemblies.Add("TerrariaMac.exe"); continue; } } compileOptions.ReferencedAssemblies.Add(reference); } Directory.CreateDirectory(DllPath); foreach (string reference in properties.dllReferences) { compileOptions.ReferencedAssemblies.Add(DllPath + Path.DirectorySeparatorChar + reference + ".dll"); } foreach (string reference in properties.modReferences) { string refFile = ModSourcePath + Path.DirectorySeparatorChar + reference; string realRefFile = refFile + ".dll"; refFile += forWindows ? "1.dll" : "2.dll"; if (File.Exists(realRefFile)) { File.Delete(realRefFile); } File.Move(refFile, realRefFile); compileOptions.ReferencedAssemblies.Add(realRefFile); } CodeDomProvider codeProvider = new CSharpCodeProvider(); CompilerResults results = codeProvider.CompileAssemblyFromFile(compileOptions, Directory.GetFiles(modDir, "*.cs", SearchOption.AllDirectories)); CompilerErrorCollection errors = results.Errors; foreach (string reference in properties.modReferences) { File.Delete(ModSourcePath + Path.DirectorySeparatorChar + reference + ".dll"); } if (errors.HasErrors) { ErrorLogger.LogCompileErrors(errors); return(null); } byte[] data = File.ReadAllBytes(outFile); File.Delete(outFile); return(data); }
private static void CompileMod(BuildingMod mod, List <LoadingMod> refMods, bool forWindows, ref byte[] dll, ref byte[] pdb) { LoadReferences(); var terrariaModule = Assembly.GetExecutingAssembly(); bool generatePDB = mod.properties.includePDB && forWindows; if (generatePDB && !windows) { Console.WriteLine("PDB files can only be generated for windows, on windows."); generatePDB = false; } var refs = new List <string>(terrariaReferences); if (forWindows == windows) { refs.Add(terrariaModule.Location); } else { refs = refs.Where(path => { var name = Path.GetFileName(path); return(name != "FNA.dll" && !name.StartsWith("Microsoft.Xna.Framework")); }).ToList(); var names = forWindows ? new[] { "tModLoaderWindows.exe", "Microsoft.Xna.Framework.dll", "Microsoft.Xna.Framework.Game.dll", "Microsoft.Xna.Framework.Graphics.dll", "Microsoft.Xna.Framework.Xact.dll" } : new[] { "tModLoaderMac.exe", "FNA.dll" }; refs.AddRange(names.Select(f => Path.Combine(modCompileDir, f))); } refs.AddRange(mod.properties.dllReferences.Select(refDll => Path.Combine(mod.path, "lib/" + refDll + ".dll"))); var tempDir = Path.Combine(ModPath, "compile_temp"); Directory.CreateDirectory(tempDir); foreach (var resName in terrariaModule.GetManifestResourceNames().Where(n => n.EndsWith(".dll"))) { var path = Path.Combine(tempDir, Path.GetFileName(resName)); using (Stream res = terrariaModule.GetManifestResourceStream(resName), file = File.Create(path)) res.CopyTo(file); refs.Add(path); } foreach (var refMod in refMods) { var path = Path.Combine(tempDir, refMod + ".dll"); File.WriteAllBytes(path, refMod.modFile.GetMainAssembly(forWindows)); refs.Add(path); foreach (var refDll in refMod.properties.dllReferences) { path = Path.Combine(tempDir, refDll + ".dll"); File.WriteAllBytes(path, refMod.modFile.GetFile("lib/" + refDll + ".dll")); refs.Add(path); } } var compileOptions = new CompilerParameters { OutputAssembly = Path.Combine(tempDir, mod + ".dll"), GenerateExecutable = false, GenerateInMemory = false, TempFiles = new TempFileCollection(tempDir, true), IncludeDebugInformation = generatePDB }; compileOptions.ReferencedAssemblies.AddRange(refs.ToArray()); var files = Directory.GetFiles(mod.path, "*.cs", SearchOption.AllDirectories).Where(file => !mod.properties.ignoreFile(file.Substring(mod.path.Length + 1))).ToArray(); try { CompilerResults results; if (mod.properties.languageVersion == 6) { if (Environment.Version.Revision < 10000) { ErrorLogger.LogBuildError(".NET Framework 4.5 must be installed to compile C# 6.0"); return; } results = RoslynCompile(compileOptions, files); } else { var options = new Dictionary <string, string> { { "CompilerVersion", "v" + mod.properties.languageVersion + ".0" } }; results = new CSharpCodeProvider(options).CompileAssemblyFromFile(compileOptions, files); } if (results.Errors.HasErrors) { ErrorLogger.LogCompileErrors(results.Errors, forWindows); return; } dll = File.ReadAllBytes(compileOptions.OutputAssembly); if (generatePDB) { pdb = File.ReadAllBytes(Path.Combine(tempDir, mod + ".pdb")); } } finally { int numTries = 10; while (numTries > 0) { try { Directory.Delete(tempDir, true); numTries = 0; } catch { System.Threading.Thread.Sleep(1); numTries--; } } } }