Techblox Mod Manager / Launcher
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

140 řádky
5.7KB

  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(name => Directory.GetLastWriteTimeUtc(name)).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. using (var fs = new FileStream("IPA.zip", FileMode.Open))
  87. using (var za = new ZipArchive(fs))
  88. za.ExtractToDirectory(Settings.Default.GamePath, true); //Overwrite files that were left from a previous install of the patcher
  89. }
  90. }
  91. status = CheckIfPatched();
  92. break;
  93. }
  94. switch (status)
  95. {
  96. case GameState.NoPatcher: //Make sure it actually worked
  97. EndWork(false);
  98. return;
  99. case GameState.Unpatched:
  100. { //TODO: Wine
  101. var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " --nowait")
  102. {
  103. UseShellExecute = false,
  104. RedirectStandardError = true,
  105. RedirectStandardOutput = true,
  106. WorkingDirectory = Settings.Default.GamePath,
  107. CreateNoWindow = true
  108. };
  109. var process = Process.Start(psi);
  110. process.BeginErrorReadLine();
  111. process.BeginOutputReadLine();
  112. process.EnableRaisingEvents = true;
  113. modinfobox.Text = "";
  114. DataReceivedEventHandler onoutput = (sender, e) =>
  115. {
  116. Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
  117. };
  118. process.OutputDataReceived += onoutput;
  119. process.ErrorDataReceived += onoutput;
  120. process.Exited += CheckStartGame;
  121. }
  122. break;
  123. case GameState.Patched:
  124. CheckStartGame(null, null);
  125. break;
  126. }
  127. }
  128. public enum GameState
  129. {
  130. NotFound,
  131. NoPatcher,
  132. Unpatched,
  133. Patched
  134. }
  135. }
  136. }