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.

214 lines
7.5KB

  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. { //TODO
  35. string libs;
  36. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  37. libs = Settings.Default.SteamConfigFileForAutoLaunch + @"\steamapps\libraryfolders.vdf"; //TODO: Not the Steam folder anymore!
  38. else
  39. return null;
  40. foreach (var line in File.ReadAllLines(libs).Concat(new[] {@"C:\Program Files (x86)\Steam\"}))
  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 + "Gamecraft") != null)
  49. return library + "Gamecraft";
  50. if (GetExe(library + "RobocraftX") != null)
  51. return library + "RobocraftX";
  52. }
  53. return null;
  54. }
  55. public string SelectGameFolder()
  56. {
  57. var ofd = new OpenFileDialog();
  58. ofd.Filter = "Gamecraft executable|Gamecraft.exe|Gamecraft Preview executable|GamecraftPreview.exe";
  59. ofd.Title = "Game location";
  60. ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\steamapps\common\"; //TODO
  61. ofd.CheckFileExists = true;
  62. ofd.ShowDialog();
  63. if (string.IsNullOrWhiteSpace(ofd.FileName))
  64. return null;
  65. return Directory.GetParent(ofd.FileName).FullName;
  66. }
  67. public string SelectSteamConfigFile()
  68. {
  69. MessageBox.Show("Please select your Steam config location in the next dialog. It's at Steam\\userdata\\<YourID>\\config\\localconfig.vdf");
  70. var ofd = new OpenFileDialog();
  71. ofd.Filter = "Steam config|localconfig.vdf";
  72. ofd.Title = "Steam config location (Steam\\userdata\\<YourID>\\config\\localconfig.vdf)";
  73. ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\\userdata\"; //TODO
  74. ofd.CheckFileExists = true;
  75. ofd.ShowDialog();
  76. if (string.IsNullOrWhiteSpace(ofd.FileName))
  77. return null;
  78. return ofd.FileName;
  79. }
  80. private void UpdateSteamConfigToAutoStart(bool autoLaunch)
  81. {
  82. //TODO
  83. }
  84. private (string, int) GetSteamLocationAndUser()
  85. {
  86. if (Environment.OSVersion.Platform != PlatformID.Win32NT) return (null, 0);
  87. using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Valve\Steam\ActiveProcess"))
  88. {
  89. string path = Directory.GetParent((string)key.GetValue("SteamClientDll")).FullName;
  90. return (path, (int)key.GetValue("ActiveUser"));
  91. }
  92. }
  93. private void CheckStartGame(object sender, EventArgs e)
  94. {
  95. Action act = () =>
  96. {
  97. if (((sender as Process)?.ExitCode ?? 0) != 0)
  98. {
  99. status.Text = "Status: Patching failed";
  100. return;
  101. }
  102. if (CheckIfPatched() == GameState.Patched || unpatched.Checked)
  103. if (sender is string command)
  104. Process.Start(command);
  105. else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  106. Process.Start("steam://run/1078000/");
  107. else
  108. Process.Start("xdg-open", "steam://run/1078000/");
  109. };
  110. if (InvokeRequired)
  111. Invoke(act);
  112. else
  113. act();
  114. EndWork(false);
  115. }
  116. public WebClient GetClient()
  117. {
  118. var client = new WebClient();
  119. if (!Settings.Default.UseProxy)
  120. client.Proxy = null;
  121. client.Headers.Clear();
  122. client.Headers[HttpRequestHeader.Accept] = "application/json";
  123. client.BaseAddress = "https://git.exmods.org";
  124. return client;
  125. }
  126. private bool working = false;
  127. /// <summary>
  128. /// Some simple "locking", only allow one operation at a time
  129. /// </summary>
  130. /// <returns>Whether the work can begin</returns>
  131. public bool BeginWork()
  132. {
  133. if (working) return false;
  134. working = true;
  135. UpdateButton(playbtn, false);
  136. UpdateButton(installbtn, false);
  137. UpdateButton(uninstallbtn, false);
  138. UpdateButton(settingsbtn, false);
  139. unpatched.Enabled = false;
  140. return true;
  141. }
  142. public void EndWork(bool desc = true)
  143. {
  144. working = false;
  145. UpdateButton(playbtn, true);
  146. UpdateButton(settingsbtn, true);
  147. if (desc)
  148. modlist_SelectedIndexChanged(modlist, null);
  149. unpatched.Enabled = true;
  150. }
  151. /// <summary>
  152. /// Path must start with \
  153. /// </summary>
  154. /// <param name="path"></param>
  155. /// <param name="gamepath"></param>
  156. /// <returns></returns>
  157. public string GamePath(string path, string gamepath = null)
  158. {
  159. return ((gamepath ?? Settings.Default.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar);
  160. }
  161. public string GetExe(string path = null)
  162. {
  163. if (File.Exists(GamePath("\\Gamecraft.exe", path)))
  164. return "Gamecraft.exe";
  165. if (File.Exists(GamePath("\\GamecraftPreview.exe", path)))
  166. return "GamecraftPreview.exe";
  167. return null;
  168. }
  169. private bool CheckNoExe()
  170. {
  171. return CheckNoExe(out _);
  172. }
  173. private bool CheckNoExe(out string path)
  174. {
  175. path = GetExe();
  176. if (path == null)
  177. {
  178. MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
  179. return true;
  180. }
  181. return false;
  182. }
  183. public async Task<DateTime> GetLastGameUpdateTime()
  184. {
  185. /*using (var client = GetClient())
  186. {
  187. string html = await client.DownloadStringTaskAsync("https://api.steamcmd.net/v1/info/1078000");
  188. var regex = new Regex("<i>timeupdated:</i>[^<]*<b>([^<]*)</b>");
  189. var match = regex.Match(html);
  190. if (!match.Success)
  191. return default;
  192. return new DateTime(1970, 1, 1).AddSeconds(long.Parse(match.Groups[1].Value));
  193. }*/
  194. //return new DateTime(2020, 12, 28);
  195. return default;
  196. }
  197. }
  198. }