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.

153 lines
6.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. 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)
  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. {
  58. status.Text = nopatch;
  59. playbtn.Text = pnp;
  60. return GameState.Unpatched;
  61. }
  62. status.Text = "Status: " + (unpatched.Checked ? "Mods disabled" : "Patched");
  63. playbtn.Text = "Play" + (unpatched.Checked ? " unmodded" : "");
  64. return GameState.Patched;
  65. }
  66. public async Task PatchStartGame()
  67. {
  68. if (!BeginWork()) return;
  69. foreach (ListViewItem item in modlist.SelectedItems)
  70. item.Selected = false;
  71. var status = CheckIfPatched();
  72. switch (status)
  73. {
  74. case GameState.NotFound:
  75. MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
  76. EndWork(false);
  77. return;
  78. case GameState.NoPatcher:
  79. case GameState.OldPatcher:
  80. {
  81. if (MessageBox.Show((status == GameState.NoPatcher
  82. ? "The patcher (GCIPA) is not found. It's necessary to load the mods."
  83. : "There is a patcher update available!"
  84. ) + "\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.",
  85. "Patcher download needed", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  86. {
  87. EndWork();
  88. return;
  89. }
  90. string releases = "/api/v1/repos/modtainers/GCIPA/releases";
  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. }
  100. }
  101. GetInstalledMods(); //Update patcher state, should be fine for this rare event
  102. status = CheckIfPatched();
  103. break;
  104. }
  105. switch (status)
  106. {
  107. case GameState.NoPatcher: //Make sure it actually worked
  108. case GameState.OldPatcher:
  109. EndWork(false);
  110. return;
  111. case GameState.Unpatched:
  112. { //TODO: Wine
  113. var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " --nowait")
  114. {
  115. UseShellExecute = false,
  116. RedirectStandardError = true,
  117. RedirectStandardOutput = true,
  118. WorkingDirectory = Settings.Default.GamePath,
  119. CreateNoWindow = true
  120. };
  121. var process = Process.Start(psi);
  122. process.BeginErrorReadLine();
  123. process.BeginOutputReadLine();
  124. process.EnableRaisingEvents = true;
  125. modinfobox.Text = "";
  126. DataReceivedEventHandler onoutput = (sender, e) =>
  127. {
  128. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  129. };
  130. process.OutputDataReceived += onoutput;
  131. process.ErrorDataReceived += onoutput;
  132. process.Exited += CheckStartGame;
  133. }
  134. break;
  135. case GameState.Patched:
  136. CheckStartGame(null, null);
  137. break;
  138. }
  139. }
  140. public enum GameState
  141. {
  142. NotFound,
  143. NoPatcher,
  144. OldPatcher,
  145. Unpatched,
  146. Patched
  147. }
  148. }
  149. }