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.

154 lines
6.4KB

  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. using System.Reflection;
  14. namespace GCMM
  15. {
  16. partial class MainForm
  17. {
  18. public GameState CheckIfPatched()
  19. {
  20. if (GetExe() == null)
  21. {
  22. status.Text = "Status: Game not found";
  23. return GameState.NotFound;
  24. }
  25. string pnp = "Patch && Play";
  26. if (!File.Exists(GamePath(@"\IPA.exe")))
  27. {
  28. status.Text = "Status: Patcher missing\nClicking Play will install it";
  29. playbtn.Text = pnp;
  30. return GameState.NoPatcher;
  31. }
  32. if (gcipa.Updatable && !(gcipa.Version == new Version(1, 0, 0, 0) && gcipa.LatestVersion == new Version(4, 0, 0, 0)))
  33. {
  34. status.Text = "Status: Patcher outdated\nClicking play will update it";
  35. playbtn.Text = pnp;
  36. return GameState.OldPatcher;
  37. }
  38. string nopatch = "Status: Unpatched\nClicking Play patches it";
  39. string gc = GetExe().Replace(".exe", "");
  40. string backups = GamePath(@"\IPA\Backups\" + gc);
  41. if (!Directory.Exists(backups))
  42. {
  43. status.Text = nopatch;
  44. playbtn.Text = pnp;
  45. return GameState.Unpatched;
  46. }
  47. string backup = Directory.EnumerateDirectories(backups).OrderByDescending(name => Directory.GetLastWriteTimeUtc(name)).FirstOrDefault();
  48. if (backup == null)
  49. {
  50. status.Text = nopatch;
  51. playbtn.Text = pnp;
  52. return GameState.Unpatched;
  53. }
  54. if (File.GetLastWriteTime(GamePath($@"\{gc}_Data\Managed\Assembly-CSharp.dll"))
  55. > //If the file was updated at least 2 minutes after patching
  56. Directory.GetLastWriteTime(backup).AddMinutes(2)
  57. || !File.Exists(GamePath($@"\{gc}_Data\Managed\IllusionInjector.dll")))
  58. {
  59. status.Text = nopatch;
  60. playbtn.Text = pnp;
  61. return GameState.Unpatched;
  62. }
  63. status.Text = "Status: " + (unpatched.Checked ? "Mods disabled" : "Patched");
  64. playbtn.Text = "Play" + (unpatched.Checked ? " unmodded" : "");
  65. return GameState.Patched;
  66. }
  67. public async Task PatchStartGame()
  68. {
  69. if (!BeginWork()) return;
  70. foreach (ListViewItem item in modlist.SelectedItems)
  71. item.Selected = false;
  72. var status = CheckIfPatched();
  73. switch (status)
  74. {
  75. case GameState.NotFound:
  76. MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
  77. EndWork(false);
  78. return;
  79. case GameState.NoPatcher:
  80. case GameState.OldPatcher:
  81. {
  82. if (MessageBox.Show((status == GameState.NoPatcher
  83. ? "The patcher (GCIPA) is not found. It's necessary to load the mods."
  84. : "There is a patcher update available!"
  85. ) + "\n\nIt will be downloaded from https://git.exmods.org/modtainers/GCIPA/releases and ran to patch the game. You can validate the game to restore the original game files or simply disable mods at any time.",
  86. "Patcher download needed", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  87. {
  88. EndWork();
  89. return;
  90. }
  91. this.status.Text = "Status: Patching...";
  92. using (WebClient client = GetClient())
  93. {
  94. string url = gcipa.DownloadURL;
  95. await client.DownloadFileTaskAsync(url, "IPA.zip");
  96. using (var fs = new FileStream("IPA.zip", FileMode.Open))
  97. using (var za = new ZipArchive(fs))
  98. za.ExtractToDirectory(Settings.Default.GamePath, true); //Overwrite files that were left from a previous install of the patcher
  99. File.Delete("IPA.zip");
  100. }
  101. }
  102. GetInstalledMods(); //Update patcher state, should be fine for this rare event
  103. status = CheckIfPatched();
  104. break;
  105. }
  106. switch (status)
  107. {
  108. case GameState.NoPatcher: //Make sure it actually worked
  109. case GameState.OldPatcher:
  110. EndWork(false);
  111. return;
  112. case GameState.Unpatched:
  113. { //TODO: Wine
  114. var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " --nowait")
  115. {
  116. UseShellExecute = false,
  117. RedirectStandardError = true,
  118. RedirectStandardOutput = true,
  119. WorkingDirectory = Settings.Default.GamePath,
  120. CreateNoWindow = true
  121. };
  122. var process = Process.Start(psi);
  123. process.BeginErrorReadLine();
  124. process.BeginOutputReadLine();
  125. process.EnableRaisingEvents = true;
  126. modinfobox.Text = "";
  127. DataReceivedEventHandler onoutput = (sender, e) =>
  128. {
  129. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  130. };
  131. process.OutputDataReceived += onoutput;
  132. process.ErrorDataReceived += onoutput;
  133. process.Exited += CheckStartGame;
  134. }
  135. break;
  136. case GameState.Patched:
  137. CheckStartGame(null, null);
  138. break;
  139. }
  140. }
  141. public enum GameState
  142. {
  143. NotFound,
  144. NoPatcher,
  145. OldPatcher,
  146. Unpatched,
  147. Patched
  148. }
  149. }
  150. }