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.

330 satır
13KB

  1. using GCMM.Properties;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace GCMM
  15. {
  16. partial class MainForm
  17. {
  18. public void UpdateButton(Button button, bool enabled)
  19. {
  20. if (enabled)
  21. {
  22. button.ForeColor = Color.Lime;
  23. button.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 40, 0);
  24. button.FlatAppearance.MouseDownBackColor = Color.Green;
  25. }
  26. else
  27. {
  28. button.ForeColor = Color.Green;
  29. button.FlatAppearance.MouseOverBackColor = Color.Black;
  30. button.FlatAppearance.MouseDownBackColor = Color.Black;
  31. }
  32. }
  33. public string GetGameFolder()
  34. {
  35. string libs;
  36. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  37. libs = steamPath + @"\steamapps\libraryfolders.vdf";
  38. else
  39. return null;
  40. foreach (var line in File.ReadAllLines(libs).Concat(new[] { "\t\"19\"\t\t\"" + steamPath + "\"" }))
  41. {
  42. var regex = new Regex("\\t\"\\d+\"\\t\\t\"(.+)\"");
  43. var match = regex.Match(line);
  44. if (!match.Success)
  45. continue;
  46. string library = match.Groups[1].Value.Replace("\\\\", "\\");
  47. library += @"\steamapps\common\";
  48. if (GetExe(library + "Techblox") != null)
  49. return library + "Techblox";
  50. }
  51. return null;
  52. }
  53. public string SelectGameFolder()
  54. {
  55. var ofd = new OpenFileDialog();
  56. ofd.Filter = "Techblox executable|Techblox.exe|Techblox Preview executable|TechbloxPreview.exe";
  57. ofd.Title = "Game location";
  58. ofd.InitialDirectory = steamPath + @"\steamapps\common\";
  59. ofd.CheckFileExists = true;
  60. ofd.ShowDialog();
  61. if (string.IsNullOrWhiteSpace(ofd.FileName))
  62. return null;
  63. return Directory.GetParent(ofd.FileName).FullName;
  64. }
  65. public (string, int) AskForSteamLogin()
  66. {
  67. while (MessageBox.Show("Couid not find your Steam configuration to set launch options.\n\n" +
  68. "Please make sure you are logged into Steam and click Retry or click Cancel to skip setting this up.",
  69. "Steam config not found", MessageBoxButtons.RetryCancel) != DialogResult.Cancel)
  70. {
  71. var ret = GetSteamLocationAndUser();
  72. if (ret != (null, 0))
  73. return ret;
  74. }
  75. return (null, 0);
  76. }
  77. /// <summary>
  78. /// Does not return the current value if setting it is not possible because Steam is running.
  79. /// </summary>
  80. /// <param name="autoLaunch">The value to set or null to keep as is</param>
  81. /// <returns>The current value, unless setting it while Steam is running</returns>
  82. public bool UpdateOrGetSteamConfigToAutoStart(bool? autoLaunch)
  83. {
  84. string commandToUse = Application.ExecutablePath + " -start %command%";
  85. if (autoLaunch.HasValue && Process.GetProcessesByName("steam").Length > 0)
  86. { //Setting it while Steam is running
  87. if (MessageBox.Show("Cannot set launch options while Steam is running." +
  88. (autoLaunch.Value
  89. ? " Do you want to do it manually? If so, it will be copied on your clipboard." +
  90. " Right click the game, select Properties and paste it in the launch options field."
  91. : " Do you want to do it manually?" +
  92. " If so, right click the game, select Properties and remove the text from the launch options field."),
  93. "Launch options in Steam", MessageBoxButtons.YesNo) == DialogResult.Yes && autoLaunch.Value)
  94. Clipboard.SetText(commandToUse);
  95. return false;
  96. }
  97. var regex = new Regex(@"(\t{6}""LaunchOptions""\t+"")(.*)("")");
  98. string path = steamPath + @"\userdata\" + Settings.Default.SteamUserID + @"\config\localconfig.vdf";
  99. var lines = File.ReadAllLines(path);
  100. bool shouldMatch = false;
  101. bool ret = false;
  102. for (int i = 0; i < lines.Length; i++)
  103. {
  104. if (lines[i] == "\t\t\t\t\t\"1078000\"")
  105. shouldMatch = true; //Found the game
  106. else if(shouldMatch)
  107. {
  108. var match = regex.Match(lines[i]);
  109. if (!match.Success)
  110. continue;
  111. ret = match.Groups[2].Value.Contains("GCMM.exe");
  112. string enabledCommand = match.Groups[1].Value + commandToUse.Replace("\\", "\\\\") + match.Groups[3].Value;
  113. if (autoLaunch.HasValue)
  114. {
  115. if (autoLaunch.Value)
  116. lines[i] = enabledCommand;
  117. else
  118. lines[i] = match.Groups[1].Value + match.Groups[3].Value;
  119. File.WriteAllLines(path, lines);
  120. }
  121. else if (ret && lines[i] != enabledCommand) //GCMM got moved or something and it's only queried, not set
  122. {
  123. lines[i] = enabledCommand;
  124. File.WriteAllLines(path, lines);
  125. }
  126. break;
  127. }
  128. }
  129. return ret;
  130. }
  131. public (string, int) GetSteamLocationAndUser()
  132. {
  133. if (Environment.OSVersion.Platform != PlatformID.Win32NT) return (null, 0);
  134. using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Valve\Steam\ActiveProcess"))
  135. {
  136. string path = (string)key?.GetValue("SteamClientDll");
  137. path = path != null ? Directory.GetParent(path).FullName : null;
  138. return (path, (int)(key?.GetValue("ActiveUser") ?? 0));
  139. }
  140. }
  141. public void DetectConfigLocationAndAutoStart(string steamPath, ref int user)
  142. {
  143. string path = steamPath + @"\userdata";
  144. if (user == 0)
  145. {
  146. var dirs = Directory.GetDirectories(path);
  147. var goodPaths = (from dir in dirs
  148. where File.Exists(dir + @"\config\localconfig.vdf")
  149. select dir).ToArray();
  150. if (goodPaths.Length != 1)
  151. {
  152. (_, user) = AskForSteamLogin();
  153. path += user;
  154. }
  155. else
  156. {
  157. path = goodPaths[0];
  158. user = int.Parse(Path.GetFileName(path));
  159. }
  160. }
  161. else
  162. path += "\\" + user;
  163. path += @"\config\localconfig.vdf";
  164. if (path != null && user != 0 && File.Exists(path))
  165. {
  166. Settings.Default.SteamUserID = user;
  167. UpdateOrGetSteamConfigToAutoStart(true);
  168. }
  169. else
  170. Settings.Default.AutoLaunch = false;
  171. }
  172. private (EventHandler, Task) CheckStartGame(string command)
  173. {
  174. var tcs = new TaskCompletionSource<object>();
  175. return ((sender, e) =>
  176. {
  177. Action act = async () =>
  178. {
  179. if (((sender as Process)?.ExitCode ?? 0) != 0)
  180. {
  181. status.Text = "Status: Patching failed";
  182. return;
  183. }
  184. if (CheckIfPatched() == GameState.Patched || unpatched.Checked)
  185. if (command != null)
  186. {
  187. if (sender is Process) //Patched just now
  188. CheckCompatibilityAndDisableMods();
  189. await CheckModUpdatesAsync();
  190. Process.Start(command);
  191. }
  192. else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  193. Process.Start("steam://run/1078000/");
  194. else
  195. Process.Start("xdg-open", "steam://run/1078000/");
  196. EndWork(false);
  197. tcs.SetResult(null);
  198. };
  199. if (InvokeRequired)
  200. Invoke(act);
  201. else
  202. act();
  203. }, tcs.Task);
  204. }
  205. private void CheckCompatibilityAndDisableMods()
  206. {
  207. if (!unpatched.Checked && MessageBox.Show("If the game updated just now, some mods may be incompatible or they may work just fine." +
  208. " Do you want to try running with mods?" +
  209. "\n\nClick Yes to start the game with mods (after a small update or if you just installed GCMM)" +
  210. "\nClick No to disable mods before starting the game (after a major update)" +
  211. "\n\nYou can always enable/disable mods by launching GCMM.",
  212. "Possible incompatibility warning", MessageBoxButtons.YesNo) == DialogResult.No)
  213. unpatched.Checked = true;
  214. }
  215. private async Task CheckModUpdatesAsync()
  216. {
  217. var updatable = mods.Values.Where(mod => mod.Updatable).ToArray();
  218. if (updatable.Length == 0)
  219. return;
  220. if (MessageBox.Show("Mod update(s) available!\n\n"
  221. + updatable.Select(mod => mod.Name + " " + mod.LatestVersion).Aggregate((a, b) => a + "\n")
  222. + "\n\nDo you want to update them now? You can also update later by opening GCMM.",
  223. "Update(s) available", MessageBoxButtons.YesNo) == DialogResult.No)
  224. return;
  225. foreach (var mod in updatable)
  226. await InstallMod(mod);
  227. MessageBox.Show("Mods updated");
  228. }
  229. public WebClient GetClient()
  230. {
  231. var client = new WebClient();
  232. if (!Settings.Default.UseProxy)
  233. client.Proxy = null;
  234. client.Headers.Clear();
  235. client.Headers[HttpRequestHeader.Accept] = "application/json";
  236. client.BaseAddress = "https://git.exmods.org";
  237. return client;
  238. }
  239. private bool working = false;
  240. /// <summary>
  241. /// Some simple "locking", only allow one operation at a time
  242. /// </summary>
  243. /// <returns>Whether the work can begin</returns>
  244. public bool BeginWork()
  245. {
  246. if (working) return false;
  247. working = true;
  248. UpdateButton(playbtn, false);
  249. UpdateButton(installbtn, false);
  250. UpdateButton(uninstallbtn, false);
  251. UpdateButton(settingsbtn, false);
  252. unpatched.Enabled = false;
  253. return true;
  254. }
  255. public void EndWork(bool desc = true)
  256. {
  257. working = false;
  258. UpdateButton(playbtn, true);
  259. UpdateButton(settingsbtn, true);
  260. if (desc)
  261. modlist_SelectedIndexChanged(modlist, null);
  262. unpatched.Enabled = true;
  263. }
  264. /// <summary>
  265. /// Path must start with \
  266. /// </summary>
  267. /// <param name="path"></param>
  268. /// <param name="gamepath"></param>
  269. /// <returns></returns>
  270. public string GamePath(string path, string gamepath = null)
  271. {
  272. return ((gamepath ?? Settings.Default.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar);
  273. }
  274. public string GetExe(string path = null)
  275. {
  276. if (File.Exists(GamePath("\\Techblox.exe", path)))
  277. return "Techblox.exe";
  278. if (File.Exists(GamePath("\\TechbloxPreview.exe", path)))
  279. return "TechbloxPreview.exe";
  280. return null;
  281. }
  282. private bool CheckNoExe()
  283. {
  284. return CheckNoExe(out _);
  285. }
  286. private bool CheckNoExe(out string path)
  287. {
  288. path = GetExe();
  289. if (path == null)
  290. {
  291. MessageBox.Show("Techblox not found! Set the correct path in Settings.");
  292. return true;
  293. }
  294. return false;
  295. }
  296. public async Task<DateTime> GetLastGameUpdateTime()
  297. {
  298. /*using (var client = GetClient())
  299. {
  300. string html = await client.DownloadStringTaskAsync("https://api.steamcmd.net/v1/info/1078000");
  301. var regex = new Regex("<i>timeupdated:</i>[^<]*<b>([^<]*)</b>");
  302. var match = regex.Match(html);
  303. if (!match.Success)
  304. return default;
  305. return new DateTime(1970, 1, 1).AddSeconds(long.Parse(match.Groups[1].Value));
  306. }*/
  307. //return new DateTime(2020, 12, 28);
  308. return default;
  309. }
  310. }
  311. }