private bool InstallDoorstop(Actions action, bool write = true) { var gameConfigPath = GameInfo.filepathInGame; var success = false; switch (action) { case Actions.Install: try { Log.Print("======================================="); if (!Directory.Exists(managerPath)) { Directory.CreateDirectory(managerPath); } Utils.MakeBackup(doorstopPath); Utils.MakeBackup(doorstopConfigPath); Utils.MakeBackup(libraryPaths); if (!InstallDoorstop(Actions.Remove, false)) { Log.Print("Installation failed. Can't uninstall the previous version."); goto EXIT; } Log.Print($"Copying files to game..."); var arch = Utils.UnmanagedDllIs64Bit(gameExePath); var filename = arch == true ? "winhttp_x64.dll" : "winhttp_x86.dll"; Log.Print($" '{filename}'"); File.Copy(filename, doorstopPath, true); Log.Print($" '{doorstopConfigFilename}'"); File.WriteAllText(doorstopConfigPath, "[UnityDoorstop]" + Environment.NewLine + "enabled = true" + Environment.NewLine + "targetAssembly = " + managerAssemblyPath); DoactionLibraries(Actions.Install); DoactionGameConfig(Actions.Install); Log.Print("Installation was successful."); success = true; } catch (Exception e) { Log.Print(e.ToString()); Utils.RestoreBackup(doorstopPath); Utils.RestoreBackup(doorstopConfigPath); Utils.RestoreBackup(libraryPaths); Utils.RestoreBackup(gameConfigPath); Log.Print("Installation failed."); } break; case Actions.Remove: try { if (write) { Log.Print("======================================="); } Utils.MakeBackup(gameConfigPath); if (write) { Utils.MakeBackup(doorstopPath); Utils.MakeBackup(doorstopConfigPath); Utils.MakeBackup(libraryPaths); } Log.Print($"Deleting files from game..."); Log.Print($" '{doorstopFilename}'"); File.Delete(doorstopPath); Log.Print($" '{doorstopConfigFilename}'"); File.Delete(doorstopConfigPath); if (write) { DoactionLibraries(Actions.Remove); DoactionGameConfig(Actions.Remove); Log.Print("Removal was successful."); } success = true; } catch (Exception e) { Log.Print(e.ToString()); if (write) { Utils.RestoreBackup(doorstopPath); Utils.RestoreBackup(doorstopConfigPath); Utils.RestoreBackup(libraryPaths); Utils.RestoreBackup(gameConfigPath); Log.Print("Removal failed."); } } break; } EXIT: if (write) { try { Utils.DeleteBackup(doorstopPath); Utils.DeleteBackup(doorstopConfigPath); Utils.DeleteBackup(libraryPaths); Utils.DeleteBackup(gameConfigPath); } catch (Exception) { } } return(success); }
private bool InstallDoorstop(Actions action, bool write = true) { var gameConfigPath = GameInfo.filepathInGame; var success = false; switch (action) { case Actions.Install: try { Log.Print("======================================="); if (!Directory.Exists(managerPath)) { Directory.CreateDirectory(managerPath); } Utils.MakeBackup(doorstopPath); Utils.MakeBackup(doorstopConfigPath); Utils.MakeBackup(libraryPaths); if (!InstallDoorstop(Actions.Remove, false)) { Log.Print("安装管理器模块到游戏失败,不能卸载上一个版本!"); goto EXIT; } Log.Print("正在复制文件到游戏……"); var arch = Utils.UnmanagedDllIs64Bit(gameExePath); var filename = arch == true ? doorstopFilenameX64 : doorstopFilenameX86; Log.Print($" '{filename}'"); File.Copy(filename, doorstopPath, true); Log.Print($" '{doorstopConfigFilename}'"); File.WriteAllText(doorstopConfigPath, $@"[UnityDoorstop]{Environment.NewLine}enabled = true{Environment.NewLine}targetAssembly = {managerAssemblyPath}"); DoactionLibraries(Actions.Install); DoactionGameConfig(Actions.Install); Log.Print("安装管理器模块到游戏成功!"); success = true; } catch (Exception e) { Log.Print(e.ToString()); Utils.RestoreBackup(doorstopPath); Utils.RestoreBackup(doorstopConfigPath); Utils.RestoreBackup(libraryPaths); Utils.RestoreBackup(gameConfigPath); Log.Print("安装管理器模块到游戏失败!"); } break; case Actions.Remove: try { if (write) { Log.Print("======================================="); } Utils.MakeBackup(gameConfigPath); if (write) { Utils.MakeBackup(doorstopPath); Utils.MakeBackup(doorstopConfigPath); Utils.MakeBackup(libraryPaths); } Log.Print("正在从游戏目录删除文件……"); Log.Print($" '{doorstopFilename}'"); File.Delete(doorstopPath); Log.Print($" '{doorstopConfigFilename}'"); File.Delete(doorstopConfigPath); if (write) { DoactionLibraries(Actions.Remove); DoactionGameConfig(Actions.Remove); Log.Print("从游戏目录删除文件成功!"); } success = true; } catch (Exception e) { Log.Print(e.ToString()); if (write) { Utils.RestoreBackup(doorstopPath); Utils.RestoreBackup(doorstopConfigPath); Utils.RestoreBackup(libraryPaths); Utils.RestoreBackup(gameConfigPath); Log.Print("从游戏目录删除文件失败!"); } } break; } EXIT: if (write) { Utils.DeleteBackup(doorstopPath); Utils.DeleteBackup(doorstopConfigPath); Utils.DeleteBackup(libraryPaths); Utils.DeleteBackup(gameConfigPath); } return(success); }