Techblox Mod Manager / Launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.2KB

  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(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. string backups = GamePath(@"\IPA\Backups\Gamecraft");
  26. if (!Directory.Exists(backups))
  27. {
  28. status.Text = nopatch;
  29. return false;
  30. }
  31. string backup = Directory.EnumerateDirectories(backups).OrderByDescending(s => s).FirstOrDefault();
  32. if (backup == null)
  33. {
  34. status.Text = nopatch;
  35. return false;
  36. }
  37. if (File.GetLastWriteTime(GamePath(@"\Gamecraft_Data\Managed\Assembly-CSharp.dll"))
  38. > //If the file was updated at least 2 minutes after patching
  39. Directory.GetLastWriteTime(backup).AddMinutes(2))
  40. {
  41. status.Text = nopatch;
  42. return false;
  43. }
  44. status.Text = "Status: Patched" + (unpatched.Checked ? "\nClicking Play unpatches it" : "");
  45. return true;
  46. }
  47. public async Task PatchStartGame()
  48. {
  49. if (!BeginWork()) return;
  50. foreach (ListViewItem item in modlist.SelectedItems)
  51. item.Selected = false;
  52. if (!CheckIfPatched().HasValue)
  53. {
  54. if (MessageBox.Show("The patcher (GCIPA) is not found. It's necessary to load the mods. It will be downloaded from https://git.exmods.org/modtainers/GCIPA/releases and ran to patch the game. You can unpatch to run without mods at any time.", "Patcher download needed", MessageBoxButtons.OKCancel)
  55. == DialogResult.Cancel)
  56. {
  57. EndWork();
  58. return;
  59. }
  60. string releases = "/api/v1/repos/modtainers/GCIPA/releases";
  61. string url;
  62. this.status.Text = "Status: Patching...";
  63. using (WebClient client = GetClient())
  64. {
  65. url = JArray.Parse(await client.DownloadStringTaskAsync(releases)).First["assets"].First["browser_download_url"].ToString();
  66. await client.DownloadFileTaskAsync(url, "IPA.zip");
  67. ZipFile.ExtractToDirectory("IPA.zip", Settings.Default.GamePath);
  68. }
  69. }
  70. bool? status = CheckIfPatched();
  71. if (!status.HasValue) //Make sure it actually worked
  72. {
  73. EndWork(false);
  74. return;
  75. }
  76. if (!status.Value ^ unpatched.Checked)
  77. { //TODO: Wine
  78. var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " "
  79. + (unpatched.Checked ? "--revert " : "") + "--nowait")
  80. {
  81. UseShellExecute = false,
  82. RedirectStandardError = true,
  83. RedirectStandardOutput = true,
  84. WorkingDirectory = Settings.Default.GamePath,
  85. CreateNoWindow = true
  86. };
  87. var process = Process.Start(psi);
  88. process.BeginErrorReadLine();
  89. process.BeginOutputReadLine();
  90. process.EnableRaisingEvents = true;
  91. modinfobox.Text = "";
  92. DataReceivedEventHandler onoutput = (sender, e) =>
  93. {
  94. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  95. };
  96. process.OutputDataReceived += onoutput;
  97. process.ErrorDataReceived += onoutput;
  98. process.Exited += CheckStartGame;
  99. }
  100. else
  101. CheckStartGame(null, null);
  102. }
  103. }
  104. }