Techblox Mod Manager / Launcher
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

97 行
3.9KB

  1. using GCMM.Properties;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO.Compression;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace GCMM
  14. {
  15. partial class MainForm
  16. {
  17. public bool? CheckIfPatched()
  18. {
  19. if (!File.Exists(Settings.Default.GamePath + @"\IPA.exe"))
  20. {
  21. status.Text = "Status: Patcher missing\nClicking Play will install it";
  22. return null;
  23. }
  24. string nopatch = "Status: Unpatched" + (unpatched.Checked ? "" : "\nClicking Play patches it");
  25. if (!Directory.Exists(Settings.Default.GamePath + @"\IPA\Backups\Gamecraft"))
  26. {
  27. status.Text = nopatch;
  28. return false;
  29. }
  30. string backup = Directory.EnumerateDirectories(Settings.Default.GamePath + @"\IPA\Backups\Gamecraft").OrderByDescending(s => s).FirstOrDefault();
  31. if (backup == null)
  32. {
  33. status.Text = nopatch;
  34. return false;
  35. }
  36. if (File.GetLastWriteTime(Settings.Default.GamePath + @"\Gamecraft_Data\Managed\Assembly-CSharp.dll")
  37. > //If the file was updated at least 2 minutes after patching
  38. Directory.GetLastWriteTime(backup).AddMinutes(2))
  39. {
  40. status.Text = nopatch;
  41. return false;
  42. }
  43. status.Text = "Status: Patched" + (unpatched.Checked ? "\nClicking Play unpatches it" : "");
  44. return true;
  45. }
  46. public async void PatchStartGame()
  47. {
  48. if (!CheckIfPatched().HasValue)
  49. {
  50. if (MessageBox.Show("The patcher (GCIPA) is not found. It will be downloaded from https://git.exmods.org/modtainers/GCIPA/releases and ran to patch the game.", "Patcher download needed", MessageBoxButtons.OKCancel)
  51. == DialogResult.Cancel)
  52. return;
  53. string releases = "/api/v1/repos/modtainers/GCIPA/releases";
  54. string url;
  55. this.status.Text = "Status: Patching...";
  56. using (WebClient client = GetClient())
  57. {
  58. url = JArray.Parse(await client.DownloadStringTaskAsync(releases)).First["assets"].First["browser_download_url"].ToString();
  59. await client.DownloadFileTaskAsync(url, "IPA.zip");
  60. ZipFile.ExtractToDirectory("IPA.zip", Settings.Default.GamePath);
  61. }
  62. }
  63. bool? status = CheckIfPatched();
  64. if (!status.HasValue) //Make sure it actually worked
  65. return;
  66. if (!status.Value ^ unpatched.Checked)
  67. {
  68. var psi = new ProcessStartInfo(Settings.Default.GamePath + @"\IPA.exe", "Gamecraft.exe "
  69. + (unpatched.Checked ? "--revert " : "") + "--nowait")
  70. {
  71. UseShellExecute = false,
  72. RedirectStandardError = true,
  73. RedirectStandardOutput = true,
  74. WorkingDirectory = Settings.Default.GamePath,
  75. CreateNoWindow = true
  76. };
  77. var process = Process.Start(psi);
  78. process.BeginErrorReadLine();
  79. process.BeginOutputReadLine();
  80. process.EnableRaisingEvents = true;
  81. modinfobox.Text = "";
  82. DataReceivedEventHandler onoutput = (sender, e) =>
  83. {
  84. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  85. };
  86. process.OutputDataReceived += onoutput;
  87. process.ErrorDataReceived += onoutput;
  88. process.Exited += CheckStartGame;
  89. }
  90. else
  91. CheckStartGame(null, null);
  92. }
  93. }
  94. }