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.

106 lines
4.3KB

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