private static byte[] PropertiesToBytes(BuildProperties properties) { byte[] data; using (MemoryStream memoryStream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memoryStream)) { if (properties.dllReferences.Length > 0) { writer.Write("dllReferences"); foreach (string reference in properties.dllReferences) { writer.Write(reference); } writer.Write(""); } if (properties.modReferences.Length > 0) { writer.Write("modReferences"); foreach (string reference in properties.modReferences) { writer.Write(reference); } writer.Write(""); } if (properties.author.Length > 0) { writer.Write("author"); writer.Write(properties.author); } if (properties.version.Length > 0) { writer.Write("version"); writer.Write(properties.version); } if (properties.displayName.Length > 0) { writer.Write("displayName"); writer.Write(properties.displayName); } writer.Write(""); writer.Flush(); data = memoryStream.ToArray(); } } return(data); }
private static bool CompileMod(string modDir, BuildProperties properties) { CompilerParameters compileOptions = new CompilerParameters(); compileOptions.GenerateExecutable = false; compileOptions.GenerateInMemory = false; compileOptions.OutputAssembly = ModSavePath + Path.DirectorySeparatorChar + Path.GetFileName(modDir) + ".tea"; foreach (string reference in buildReferences) { compileOptions.ReferencedAssemblies.Add(reference); } Directory.CreateDirectory(ReferencePath); foreach (string reference in properties.dllReferences) { compileOptions.ReferencedAssemblies.Add(ReferencePath + Path.DirectorySeparatorChar + reference + ".dll"); } foreach (string reference in properties.modReferences) { compileOptions.ReferencedAssemblies.Add(ModSourcesPath + 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(ModSourcesPath + Path.DirectorySeparatorChar + reference + ".dll"); } if (errors.HasErrors) { ErrorLogger.LogCompileErrors(errors); return(false); } return(true); }
private static void LoadMod(string modFile, BuildProperties properties) { Directory.CreateDirectory(ReferencePath); foreach (string dllReference in properties.dllReferences) { string dllFile = ReferencePath + Path.DirectorySeparatorChar + dllReference + ".dll"; Assembly.Load(File.ReadAllBytes(dllFile)); } Assembly modCode; using (FileStream fileStream = File.OpenRead(modFile)) { using (BinaryReader reader = new BinaryReader(fileStream)) { fileStream.Seek(reader.ReadInt32(), SeekOrigin.Current); modCode = Assembly.Load(reader.ReadBytes(reader.ReadInt32())); for (string texturePath = reader.ReadString(); texturePath != "end"; texturePath = reader.ReadString()) { byte[] imageData = reader.ReadBytes(reader.ReadInt32()); using (MemoryStream buffer = new MemoryStream(imageData)) { textures[texturePath] = Texture2D.FromStream(Main.instance.GraphicsDevice, buffer); } } } } Type[] classes = modCode.GetTypes(); foreach (Type type in classes) { if (type.IsSubclassOf(typeof(Mod))) { Mod mod = (Mod)Activator.CreateInstance(type); mod.filePath = modFile; mod.ModAssembly = modCode; mod.Initialize(); mods[mod.Name] = mod; } } }
private static BuildProperties ReadBuildProperties(string modDir) { string propertiesFile = modDir + Path.DirectorySeparatorChar + "build.txt"; BuildProperties properties = new BuildProperties(); if (!File.Exists(propertiesFile)) { return(properties); } string[] lines = File.ReadAllLines(propertiesFile); foreach (string line in lines) { if (line.Length == 0) { continue; } int split = line.IndexOf('='); string property = line.Substring(0, split).Trim(); string value = line.Substring(split + 1).Trim(); if (value.Length == 0) { continue; } switch (property) { case "dllReferences": string[] dllReferences = value.Split(','); for (int k = 0; k < dllReferences.Length; k++) { string dllReference = dllReferences[k].Trim(); if (dllReference.Length > 0) { dllReferences[k] = dllReference; } } properties.dllReferences = dllReferences; break; case "modReferences": string[] modReferences = value.Split(','); for (int k = 0; k < modReferences.Length; k++) { string modReference = modReferences[k].Trim(); if (modReference.Length > 0) { modReferences[k] = modReference; } } properties.modReferences = modReferences; break; case "author": properties.author = value; break; case "version": properties.version = value; break; case "displayName": properties.displayName = value; break; } } foreach (string modReference in properties.modReferences) { string path = ModSavePath + Path.DirectorySeparatorChar + modReference + ".tea"; if (!File.Exists(path)) { ErrorLogger.LogModReferenceError(modReference); return(null); } byte[] data; using (FileStream fileStream = File.OpenRead(path)) { using (BinaryReader reader = new BinaryReader(fileStream)) { fileStream.Seek(reader.ReadInt32(), SeekOrigin.Current); data = reader.ReadBytes(reader.ReadInt32()); } } using (FileStream writeStream = File.Create(ModSourcesPath + Path.DirectorySeparatorChar + modReference + ".dll")) { using (BinaryWriter writer = new BinaryWriter(writeStream)) { writer.Write(data); } } } return(properties); }
internal static bool do_BuildMod(object threadContext) { LoaderMenus.buildMod.SetReading(); BuildProperties properties = ReadBuildProperties(modToBuild); if (properties == null) { if (!buildAll) { Main.menuMode = LoaderMenus.errorMessageID; } return(false); } LoadReferences(); LoaderMenus.buildMod.SetCompiling(); if (!CompileMod(modToBuild, properties)) { if (!buildAll) { Main.menuMode = LoaderMenus.errorMessageID; } return(false); } LoaderMenus.buildMod.SetBuildText(); byte[] propertiesData = PropertiesToBytes(properties); string file = ModSavePath + Path.DirectorySeparatorChar + Path.GetFileName(modToBuild) + ".tea"; byte[] buffer = File.ReadAllBytes(file); using (FileStream fileStream = File.Create(file)) { using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(propertiesData.Length); writer.Write(propertiesData); writer.Write(buffer.Length); writer.Write(buffer); string[] images = Directory.GetFiles(modToBuild, "*.png", SearchOption.AllDirectories); foreach (string image in images) { string texturePath = image.Replace(ModSourcesPath + Path.DirectorySeparatorChar, null); texturePath = Path.ChangeExtension(texturePath.Replace(Path.DirectorySeparatorChar, '/'), null); buffer = File.ReadAllBytes(image); writer.Write(texturePath); writer.Write(buffer.Length); writer.Write(buffer); } writer.Write("end"); } } EnableMod(file); if (!buildAll) { Main.menuMode = reloadAfterBuild ? LoaderMenus.loadModsID : 0; } return(true); }
internal static BuildProperties LoadBuildProperties(string modFile) { BuildProperties properties = new BuildProperties(); byte[] data; using (FileStream fileStream = File.OpenRead(modFile)) { using (BinaryReader reader = new BinaryReader(fileStream)) { data = reader.ReadBytes(reader.ReadInt32()); } } if (data.Length == 0) { return(properties); } using (MemoryStream memoryStream = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(memoryStream)) { for (string tag = reader.ReadString(); tag.Length > 0; tag = reader.ReadString()) { if (tag == "dllReferences") { List <string> dllReferences = new List <string>(); for (string reference = reader.ReadString(); reference.Length > 0; reference = reader.ReadString()) { dllReferences.Add(reference); } properties.dllReferences = dllReferences.ToArray(); } if (tag == "modReferences") { List <string> modReferences = new List <string>(); for (string reference = reader.ReadString(); reference.Length > 0; reference = reader.ReadString()) { modReferences.Add(reference); } properties.modReferences = modReferences.ToArray(); } if (tag == "author") { properties.author = reader.ReadString(); } if (tag == "version") { properties.version = reader.ReadString(); } if (tag == "displayName") { properties.displayName = reader.ReadString(); } } } } return(properties); }