public static bool Mod(this InstallerWindow ins, string file) { string inPath = Path.Combine(ins.MainModDir, file); string outPath = Path.Combine(ins.MainModDir, file + ".tmp.dll"); MonoModder monomod = new MonoModder() { InputPath = inPath, OutputPath = outPath }; monomod.SetupETGModder(); using (FileStream fileStream = File.Open(LogPath, FileMode.Append)) { using (StreamWriter streamWriter = new StreamWriter(fileStream)) { monomod.Logger = (string s) => ins.OnActivity(); monomod.Logger += (string s) => streamWriter.WriteLine(s); // Unity wants .mdbs // monomod.WriterParameters.SymbolWriterProvider = new Mono.Cecil.Mdb.MdbWriterProvider(); string db = Path.ChangeExtension(inPath, "pdb"); string dbTmp = Path.ChangeExtension(outPath, "pdb"); if (!File.Exists(db)) { db = inPath + ".mdb"; dbTmp = outPath + ".mdb"; } #if !DEBUG RETRY: try { #endif monomod.Read(); // Read main module first monomod.ReadMod(Directory.GetParent(inPath).FullName); // ... then mods monomod.MapDependencies(); // ... then all dependencies monomod.AutoPatch(); // Patch, monomod.Write(); // and write. monomod.Dispose(); // Finally, dispose, because file access happens now. File.Delete(inPath); File.Move(outPath, inPath); if (File.Exists(db)) { File.Delete(db); } if (File.Exists(dbTmp)) { File.Move(dbTmp, db); } return(true); #if !DEBUG } catch (ArgumentException e) { monomod.Dispose(); if (File.Exists(db)) { File.Delete(db); if (File.Exists(dbTmp)) { File.Delete(dbTmp); } goto RETRY; } ins.LogLine(e.ToString()); throw; return(false); } catch (Exception e) { monomod.Dispose(); ins.LogLine(e.ToString()); throw; return(false); } #endif } } }
public static void Uninstall(this InstallerWindow ins) { if (ins.MainMod == null) { return; } ins.MainMod.Dispose(); // Uninstall can be invoked without the installer running ins.Invoke(() => ExePath = ins.ExePathBox.Text).Wait(); if (ETGFinder.IsBinaryWrapped) { ExePath = Path.Combine(Directory.GetParent(ExePath).FullName, ETGFinder.MainName); } string pathGame = ins.MainModDir; string pathBackup = Path.Combine(pathGame, "ModBackup"); if (!Directory.Exists(pathBackup)) { return; } string[] files = Directory.GetFiles(pathGame); ins.InitProgress("Removing leftover files", files.Length + 1); for (int i = 0; i < files.Length; i++) { string file = Path.GetFileName(files[i]); if (!file.Contains(".mm.")) { continue; } ins.Log("Removing: ").LogLine(file); ins.SetProgress("Removing: " + file, i); File.Delete(files[i]); } if (ins.ModVersion != null) { ins.Log("Found previous mod installation: ").LogLine(ins.ModVersion); ins.LogLine("Reverting to unmodded backup..."); } else { ins.LogLine("No previous mod installation found."); ins.LogLine("Still reverting to unmodded backup..."); } string etgBackup = Path.Combine(pathBackup, ETGFinder.MainName); ins.Log("Reverting: ").LogLine(ETGFinder.MainName); if (File.Exists(etgBackup)) { File.Delete(ExePath); File.Move(etgBackup, ExePath); } else { ins.Log("WARNING: Backup not found for ").LogLine(ETGFinder.MainName); } files = Directory.GetFiles(pathBackup); ins.InitProgress("Uninstalling ETGMod", files.Length + 1); for (int i = 0; i < files.Length; i++) { string file = Path.GetFileName(files[i]); ins.Log("Reverting: ").LogLine(file); ins.SetProgress("Reverting: " + file, i); string origPath = Path.Combine(pathGame, file); File.Delete(origPath); File.Move(files[i], origPath); } ins.LogLine("Reloading Assembly-CSharp.dll"); ins.SetProgress("Reloading Assembly-CSharp.dll", files.Length); ins.MainMod = new MonoModder() { InputPath = ins.MainModIn }; ins.MainMod.SetupETGModder(); #if DEBUG if (LogPath == null) { ins.MainMod.Read(); // Read main module first ins.MainMod.ReadMod(ins.MainModDir); // ... then mods ins.MainMod.MapDependencies(); // ... then all dependencies } else using (FileStream fileStream = File.Open(LogPath, FileMode.Append)) { using (StreamWriter streamWriter = new StreamWriter(fileStream)) { ins.MainMod.Logger = (string s) => ins.OnActivity(); ins.MainMod.Logger += (string s) => streamWriter.WriteLine(s); // MonoMod.MonoModSymbolReader.MDBDEBUG = true; #endif ins.MainMod.Read(); // Read main module first ins.MainMod.ReadMod(ins.MainModDir); // ... then mods ins.MainMod.MapDependencies(); // ... then all dependencies #if DEBUG Mono.Cecil.TypeDefinition etgMod = ins.MainMod.Module.GetType("ETGMod"); if (etgMod != null) { for (int i = 0; i < etgMod.Methods.Count; i++) { Mono.Cecil.Cil.MethodBody body = etgMod.Methods[i].Body; } } } } ins.MainMod.Logger = null; #endif ins.EndProgress("Uninstalling complete."); }