Techblox Mod Manager / Launcher
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

174 satır
7.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<bool?> PatchStartGame(string command = null)
  68. {
  69. if (!BeginWork()) return false;
  70. foreach (ListViewItem item in modlist.SelectedItems)
  71. item.Selected = false;
  72. bool? retOpenedWindowShouldStay = null;
  73. void EnsureShown(bool stay)
  74. {
  75. if (!Visible)
  76. {
  77. Show();
  78. retOpenedWindowShouldStay = stay;
  79. TopMost = true; //It opens in the background otherwise - should be fine since it only shows for a couple seconds
  80. }
  81. }
  82. var status = CheckIfPatched();
  83. switch (status)
  84. {
  85. case GameState.NotFound:
  86. MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
  87. EndWork(false);
  88. return retOpenedWindowShouldStay;
  89. case GameState.NoPatcher:
  90. case GameState.OldPatcher:
  91. {
  92. if (MessageBox.Show((status == GameState.NoPatcher
  93. ? "The patcher (GCIPA) is not found. It's necessary to load the mods."
  94. : "There is a patcher update available!"
  95. ) + "\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.",
  96. "Patcher download needed", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  97. {
  98. EndWork();
  99. return retOpenedWindowShouldStay;
  100. }
  101. this.status.Text = "Status: Patching...";
  102. EnsureShown(false);
  103. using (WebClient client = GetClient())
  104. {
  105. string url = gcipa.DownloadURL;
  106. await client.DownloadFileTaskAsync(url, "IPA.zip");
  107. using (var fs = new FileStream("IPA.zip", FileMode.Open))
  108. using (var za = new ZipArchive(fs))
  109. za.ExtractToDirectory(Settings.Default.GamePath, true); //Overwrite files that were left from a previous install of the patcher
  110. File.Delete("IPA.zip");
  111. }
  112. }
  113. GetInstalledMods(); //Update patcher state, should be fine for this rare event
  114. status = CheckIfPatched();
  115. break;
  116. }
  117. switch (status)
  118. {
  119. case GameState.NoPatcher: //Make sure it actually worked
  120. case GameState.OldPatcher:
  121. EndWork(false);
  122. return retOpenedWindowShouldStay;
  123. case GameState.Unpatched:
  124. { //TODO: Wine
  125. EnsureShown(false);
  126. var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " --nowait")
  127. {
  128. UseShellExecute = false,
  129. RedirectStandardError = true,
  130. RedirectStandardOutput = true,
  131. WorkingDirectory = Settings.Default.GamePath,
  132. CreateNoWindow = true
  133. };
  134. var process = Process.Start(psi);
  135. process.BeginErrorReadLine();
  136. process.BeginOutputReadLine();
  137. process.EnableRaisingEvents = true;
  138. modinfobox.Text = "";
  139. DataReceivedEventHandler onoutput = (sender, e) =>
  140. {
  141. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  142. };
  143. process.OutputDataReceived += onoutput;
  144. process.ErrorDataReceived += onoutput;
  145. var (handler, task) = CheckStartGame(command);
  146. process.Exited += handler;
  147. await task;
  148. }
  149. break;
  150. case GameState.Patched:
  151. {
  152. //CheckStartGame(command)(null, null);
  153. var (handler, task) = CheckStartGame(command);
  154. handler(null, null);
  155. await task;
  156. }
  157. break;
  158. }
  159. return retOpenedWindowShouldStay;
  160. }
  161. public enum GameState
  162. {
  163. NotFound,
  164. NoPatcher,
  165. OldPatcher,
  166. Unpatched,
  167. Patched
  168. }
  169. }
  170. }