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.

114 line
4.5KB

  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. string pnp = unpatched.Checked ? "Play" : "Patch && Play";
  20. if (!File.Exists(GamePath(@"\IPA.exe")))
  21. {
  22. status.Text = "Status: Patcher missing\nClicking Play will install it";
  23. playbtn.Text = pnp;
  24. return null;
  25. }
  26. string nopatch = "Status: Unpatched" + (unpatched.Checked ? "" : "\nClicking Play patches it");
  27. string gc = GetExe().Replace(".exe", "");
  28. string backups = GamePath(@"\IPA\Backups\" + gc);
  29. if (!Directory.Exists(backups))
  30. {
  31. status.Text = nopatch;
  32. playbtn.Text = pnp;
  33. return false;
  34. }
  35. string backup = Directory.EnumerateDirectories(backups).OrderByDescending(s => s).FirstOrDefault();
  36. if (backup == null)
  37. {
  38. status.Text = nopatch;
  39. playbtn.Text = pnp;
  40. return false;
  41. }
  42. if (File.GetLastWriteTime(GamePath($@"\{gc}_Data\Managed\Assembly-CSharp.dll"))
  43. > //If the file was updated at least 2 minutes after patching
  44. Directory.GetLastWriteTime(backup).AddMinutes(2))
  45. {
  46. status.Text = nopatch;
  47. playbtn.Text = pnp;
  48. return false;
  49. }
  50. status.Text = "Status: Patched" + (unpatched.Checked ? "\nClicking Play unpatches it" : "");
  51. playbtn.Text = unpatched.Checked ? "Unpatch && Play" : "Play";
  52. return true;
  53. }
  54. public async Task PatchStartGame()
  55. {
  56. if (!BeginWork()) return;
  57. foreach (ListViewItem item in modlist.SelectedItems)
  58. item.Selected = false;
  59. if (!CheckIfPatched().HasValue)
  60. {
  61. 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)
  62. == DialogResult.Cancel)
  63. {
  64. EndWork();
  65. return;
  66. }
  67. string releases = "/api/v1/repos/modtainers/GCIPA/releases";
  68. string url;
  69. this.status.Text = "Status: Patching...";
  70. using (WebClient client = GetClient())
  71. {
  72. url = JArray.Parse(await client.DownloadStringTaskAsync(releases)).First["assets"].First["browser_download_url"].ToString();
  73. await client.DownloadFileTaskAsync(url, "IPA.zip");
  74. ZipFile.ExtractToDirectory("IPA.zip", Settings.Default.GamePath);
  75. }
  76. }
  77. bool? status = CheckIfPatched();
  78. if (!status.HasValue) //Make sure it actually worked
  79. {
  80. EndWork(false);
  81. return;
  82. }
  83. if (!status.Value ^ unpatched.Checked)
  84. { //TODO: Wine
  85. var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " "
  86. + (unpatched.Checked ? "--revert " : "") + "--nowait")
  87. {
  88. UseShellExecute = false,
  89. RedirectStandardError = true,
  90. RedirectStandardOutput = true,
  91. WorkingDirectory = Settings.Default.GamePath,
  92. CreateNoWindow = true
  93. };
  94. var process = Process.Start(psi);
  95. process.BeginErrorReadLine();
  96. process.BeginOutputReadLine();
  97. process.EnableRaisingEvents = true;
  98. modinfobox.Text = "";
  99. DataReceivedEventHandler onoutput = (sender, e) =>
  100. {
  101. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  102. };
  103. process.OutputDataReceived += onoutput;
  104. process.ErrorDataReceived += onoutput;
  105. process.Exited += CheckStartGame;
  106. }
  107. else
  108. CheckStartGame(null, null);
  109. }
  110. }
  111. }