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.

99 lines
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\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. UpdateButton(playbtn, false);
  49. UpdateButton(installbtn, false);
  50. UpdateButton(uninstallbtn, false);
  51. UpdateButton(settingsbtn, false);
  52. if (!CheckIfPatched().HasValue)
  53. {
  54. 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)
  55. == DialogResult.Cancel)
  56. return;
  57. string releases = "/api/v1/repos/modtainers/GCIPA/releases";
  58. string url;
  59. this.status.Text = "Status: Patching...";
  60. using (WebClient client = GetClient())
  61. {
  62. url = JArray.Parse(await client.DownloadStringTaskAsync(releases)).First["assets"].First["browser_download_url"].ToString();
  63. await client.DownloadFileTaskAsync(url, "IPA.zip");
  64. ZipFile.ExtractToDirectory("IPA.zip", Settings.Default.GamePath);
  65. }
  66. }
  67. bool? status = CheckIfPatched();
  68. if (!status.HasValue) //Make sure it actually worked
  69. return;
  70. if (!status.Value)
  71. {
  72. var psi = new ProcessStartInfo(Settings.Default.GamePath + @"\IPA.exe", "Gamecraft.exe --nowait");
  73. psi.UseShellExecute = false;
  74. psi.RedirectStandardError = true;
  75. psi.RedirectStandardOutput = true;
  76. psi.WorkingDirectory = Settings.Default.GamePath;
  77. psi.CreateNoWindow = true;
  78. var process = Process.Start(psi);
  79. process.BeginErrorReadLine();
  80. process.BeginOutputReadLine();
  81. process.EnableRaisingEvents = true;
  82. modinfobox.Text = "";
  83. DataReceivedEventHandler onoutput = (sender, e) =>
  84. {
  85. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  86. };
  87. process.OutputDataReceived += onoutput;
  88. process.ErrorDataReceived += onoutput;
  89. process.Exited += CheckStartGame;
  90. }
  91. else
  92. CheckStartGame(null, null);
  93. }
  94. }
  95. }