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.

MainPatcher.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\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";
  44. return true;
  45. }
  46. public async void PatchGame()
  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)
  67. {
  68. var psi = new ProcessStartInfo(Settings.Default.GamePath + @"\IPA.exe", "Gamecraft.exe --nowait");
  69. psi.UseShellExecute = false;
  70. psi.RedirectStandardError = true;
  71. psi.RedirectStandardOutput = true;
  72. psi.WorkingDirectory = Settings.Default.GamePath;
  73. psi.CreateNoWindow = true;
  74. var process = Process.Start(psi);
  75. process.BeginErrorReadLine();
  76. process.BeginOutputReadLine();
  77. process.EnableRaisingEvents = true;
  78. modinfobox.Text = "";
  79. DataReceivedEventHandler onoutput = (sender, e) =>
  80. {
  81. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  82. };
  83. process.OutputDataReceived += onoutput;
  84. process.ErrorDataReceived += onoutput;
  85. process.Exited += CheckStartGame;
  86. }
  87. else
  88. CheckStartGame(null, null);
  89. }
  90. }
  91. }