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.

139 lines
5.6KB

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