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.

275 lines
10KB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using Microsoft.Win32;
  10. namespace TBMM
  11. {
  12. partial class MainForm
  13. {
  14. public void UpdateButton(Button button, bool enabled)
  15. {
  16. if (enabled)
  17. {
  18. button.ForeColor = Color.Lime;
  19. button.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 40, 0);
  20. button.FlatAppearance.MouseDownBackColor = Color.Green;
  21. }
  22. else
  23. {
  24. button.ForeColor = Color.Green;
  25. button.FlatAppearance.MouseOverBackColor = Color.Black;
  26. button.FlatAppearance.MouseDownBackColor = Color.Black;
  27. }
  28. }
  29. public string GetGameFolder()
  30. {
  31. using var key =
  32. Registry.LocalMachine.OpenSubKey(
  33. @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Techblox Launcher") ??
  34. Registry.LocalMachine.OpenSubKey(
  35. @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Techblox Launcher");
  36. string launcherPath = key?.GetValue("DisplayIcon") is string launcherExecutable
  37. ? Directory.GetParent(launcherExecutable)?.FullName
  38. : null;
  39. launcherPath ??= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
  40. "Techblox Launcher");
  41. string launcherConfig = Path.Combine(launcherPath, "launcher_settings.ini");
  42. if (!File.Exists(launcherConfig)) return null;
  43. string path = File.ReadLines(launcherConfig)
  44. .FirstOrDefault(line => line.StartsWith("133062..GAME_PATH="))
  45. ?.Substring("133062..GAME_PATH=".Length).Replace("/TBMM/", "/");
  46. if (path != null) path = (path + "StandaloneWindows64").Replace('/', Path.DirectorySeparatorChar);
  47. if (path != null && GetExe(path) != null) return path;
  48. return null;
  49. }
  50. public string SelectGameFolder()
  51. {
  52. var ofd = new OpenFileDialog();
  53. ofd.Filter = "Techblox executable|Techblox.exe|Techblox Preview executable|TechbloxPreview.exe";
  54. ofd.Title = "Game location";
  55. ofd.CheckFileExists = true;
  56. ofd.ShowDialog();
  57. return string.IsNullOrWhiteSpace(ofd.FileName) ? null : Directory.GetParent(ofd.FileName)?.FullName;
  58. }
  59. private (EventHandler, Task) CheckStartGame(string command)
  60. {
  61. var tcs = new TaskCompletionSource<object>();
  62. return ((sender, e) =>
  63. {
  64. Action act = async () =>
  65. {
  66. if (((sender as Process)?.ExitCode ?? 0) != 0)
  67. {
  68. status.Text = "Status: Patching failed";
  69. return;
  70. }
  71. var patched = CheckIfPatched();
  72. if (patched == GameState.Patched)
  73. {
  74. Process process = null;
  75. if (command != null)
  76. {
  77. await CheckModUpdatesAsync();
  78. process = Process.Start(command);
  79. }
  80. else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  81. {
  82. process = Process.Start(new ProcessStartInfo(GamePath("\\" + GetExe()))
  83. {
  84. WorkingDirectory = GamePath("\\") //Mods are only loaded if the working directory is correct
  85. });
  86. }
  87. if (process is null)
  88. throw new NullReferenceException("Game process is null");
  89. process.EnableRaisingEvents = true;
  90. process.Exited += HandleGameExit;
  91. _gameProcess = process;
  92. patched = CheckIfPatched(); // Set in-game status
  93. }
  94. EndWork(patched, false);
  95. tcs.SetResult(null);
  96. };
  97. if (InvokeRequired)
  98. Invoke(act);
  99. else
  100. act();
  101. }, tcs.Task);
  102. }
  103. private void HandleGameExit(object sender, EventArgs e)
  104. {
  105. _gameProcess = null;
  106. if (InvokeMethod(CheckIfPatched) != GameState.Patched)
  107. return;
  108. InvokeMethod(() => ExecutePatcher(false)).Exited += (o, args) =>
  109. {
  110. if (InvokeMethod(CheckIfPatched) == GameState.Patched)
  111. {
  112. MessageBox.Show("Failed to unpatch game, launching through the launcher will fail because of anticheat. " +
  113. "Check the output in the panel on the right.\n\n" +
  114. "Please try starting the game again by clicking Play.", "Patcher error",
  115. MessageBoxButtons.OK, MessageBoxIcon.Error);
  116. }
  117. };
  118. }
  119. private Process _gameProcess;
  120. private bool CheckIfGameIsRunning()
  121. {
  122. switch (_gameProcess)
  123. {
  124. case { HasExited: false }:
  125. return true;
  126. case { HasExited: true }:
  127. MessageBox.Show("Game has exited without handling... Please report.");
  128. HandleGameExit(null, EventArgs.Empty);
  129. return false;
  130. default:
  131. _gameProcess = Process.GetProcessesByName(GetExe(withExtension: false)).FirstOrDefault();
  132. if (_gameProcess == null) return false;
  133. if (_gameProcess.HasExited)
  134. {
  135. HandleGameExit(null, EventArgs.Empty);
  136. return false;
  137. }
  138. else
  139. {
  140. _gameProcess.Exited += HandleGameExit;
  141. _gameProcess.EnableRaisingEvents = true;
  142. return true;
  143. }
  144. }
  145. }
  146. private async Task CheckModUpdatesAsync()
  147. {
  148. var updatable = mods.Values.Where(mod => mod.Updatable).ToArray();
  149. if (updatable.Length == 0)
  150. return;
  151. if (MessageBox.Show("Mod update(s) available!\n\n"
  152. + updatable.Select(mod => mod.Name + " " + mod.LatestVersion).Aggregate((a, b) => a + "\n")
  153. + "\n\nDo you want to update them now? You can also update later by opening TBMM.",
  154. "Update(s) available", MessageBoxButtons.YesNo) == DialogResult.No)
  155. return;
  156. foreach (var mod in updatable)
  157. await InstallMod(mod);
  158. MessageBox.Show("Mods updated");
  159. }
  160. public WebClient GetClient()
  161. {
  162. var client = new WebClient();
  163. client.Headers.Clear();
  164. client.Headers[HttpRequestHeader.Accept] = "application/json";
  165. client.BaseAddress = "https://git.exmods.org";
  166. return client;
  167. }
  168. public T InvokeMethod<T>(Func<T> func)
  169. {
  170. if (InvokeRequired)
  171. return (T)Invoke(func);
  172. else
  173. return func();
  174. }
  175. private bool working = false;
  176. /// <summary>
  177. /// Some simple "locking", only allow one operation at a time
  178. /// </summary>
  179. /// <returns>Whether the work can begin</returns>
  180. public bool BeginWork()
  181. {
  182. if (working) return false;
  183. working = true;
  184. UpdateButton(playbtn, false);
  185. UpdateButton(installbtn, false);
  186. UpdateButton(uninstallbtn, false);
  187. UpdateButton(settingsbtn, false);
  188. return true;
  189. }
  190. public void EndWork(GameState state, bool desc = true)
  191. {
  192. working = false;
  193. if (state != GameState.InGame)
  194. UpdateButton(playbtn, true);
  195. UpdateButton(settingsbtn, true);
  196. if (desc)
  197. modlist_SelectedIndexChanged(modlist, null);
  198. }
  199. /// <summary>
  200. /// Path must start with \
  201. /// </summary>
  202. /// <param name="path"></param>
  203. /// <param name="gamepath"></param>
  204. /// <returns></returns>
  205. public string GamePath(string path, string gamepath = null)
  206. {
  207. return ((gamepath ?? Configuration.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar);
  208. }
  209. public string GetExe(string path = null, bool withExtension = true)
  210. {
  211. if (File.Exists(GamePath("\\Techblox.exe", path)))
  212. return "Techblox" + (withExtension ? ".exe" : "");
  213. if (File.Exists(GamePath("\\TechbloxPreview.exe", path)))
  214. return "TechbloxPreview" + (withExtension ? ".exe" : "");
  215. return null;
  216. }
  217. private bool CheckNoExe()
  218. {
  219. return CheckNoExe(out _);
  220. }
  221. private bool CheckNoExe(out string path)
  222. {
  223. path = GetExe();
  224. if (path == null)
  225. {
  226. MessageBox.Show("Techblox not found! Set the correct path in Settings.");
  227. return true;
  228. }
  229. return false;
  230. }
  231. public DateTime GetGameVersionAsDate()
  232. {
  233. if (Configuration.GamePath == null) return default;
  234. using var fs = File.OpenRead(GamePath($"\\{GetExe(withExtension: false)}_Data\\globalgamemanagers"));
  235. using var sr = new StreamReader(fs);
  236. char[] data = new char[512];
  237. while(!sr.EndOfStream)
  238. {
  239. Array.Copy(data, 256, data, 0, 256);
  240. int read = sr.ReadBlock(data, 256, 256);
  241. for (int i = 0; i < data.Length - 11; i++)
  242. {
  243. if (data[i] == '2')
  244. {
  245. string date = new string(data, i, 11);
  246. if (date.StartsWith("202") && DateTime.TryParse(date, out var ret))
  247. return ret;
  248. }
  249. }
  250. }
  251. return default;
  252. }
  253. }
  254. }