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.

203 lines
7.0KB

  1. using GCMM.Properties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace GCMM
  14. {
  15. partial class MainForm
  16. {
  17. public void UpdateButton(Button button, bool enabled)
  18. {
  19. if (enabled)
  20. {
  21. button.ForeColor = Color.Lime;
  22. button.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 40, 0);
  23. button.FlatAppearance.MouseDownBackColor = Color.Green;
  24. }
  25. else
  26. {
  27. button.ForeColor = Color.Green;
  28. button.FlatAppearance.MouseOverBackColor = Color.Black;
  29. button.FlatAppearance.MouseDownBackColor = Color.Black;
  30. }
  31. }
  32. public string GetGameFolder()
  33. { //TODO
  34. string libs;
  35. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  36. libs = Settings.Default.SteamConfigFileForAutoLaunch + @"\steamapps\libraryfolders.vdf";
  37. else
  38. return null;
  39. foreach (var line in File.ReadAllLines(libs).Concat(new[] {@"C:\Program Files (x86)\Steam\"}))
  40. {
  41. var regex = new Regex("\\t\"\\d+\"\\t\\t\"(.+)\"");
  42. var match = regex.Match(line);
  43. if (!match.Success)
  44. continue;
  45. string library = match.Groups[1].Value.Replace("\\\\", "\\");
  46. library += @"\steamapps\common\";
  47. if (GetExe(library + "Gamecraft") != null)
  48. return library + "Gamecraft";
  49. if (GetExe(library + "RobocraftX") != null)
  50. return library + "RobocraftX";
  51. }
  52. return null;
  53. }
  54. public string SelectGameFolder()
  55. {
  56. var ofd = new OpenFileDialog();
  57. ofd.Filter = "Gamecraft executable|Gamecraft.exe|Gamecraft Preview executable|GamecraftPreview.exe";
  58. ofd.Title = "Game location";
  59. ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\steamapps\common\"; //TODO
  60. ofd.CheckFileExists = true;
  61. ofd.ShowDialog();
  62. if (string.IsNullOrWhiteSpace(ofd.FileName))
  63. return null;
  64. return Directory.GetParent(ofd.FileName).FullName;
  65. }
  66. public string SelectSteamConfigFile()
  67. {
  68. MessageBox.Show("Please select your Steam config location in the next dialog. It's at Steam\\userdata\\<YourID>\\config\\localconfig.vdf");
  69. var ofd = new OpenFileDialog();
  70. ofd.Filter = "Steam config|localconfig.vdf";
  71. ofd.Title = "Steam config location (Steam\\userdata\\<YourID>\\config\\localconfig.vdf)";
  72. ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\\userdata\"; //TODO
  73. ofd.CheckFileExists = true;
  74. ofd.ShowDialog();
  75. if (string.IsNullOrWhiteSpace(ofd.FileName))
  76. return null;
  77. return ofd.FileName;
  78. }
  79. private void UpdateSteamConfigToAutoStart(bool autoLaunch)
  80. {
  81. }
  82. private void CheckStartGame(object sender, EventArgs e)
  83. {
  84. Action act = () =>
  85. {
  86. if (((sender as Process)?.ExitCode ?? 0) != 0)
  87. {
  88. status.Text = "Status: Patching failed";
  89. return;
  90. }
  91. if (CheckIfPatched() == GameState.Patched || unpatched.Checked)
  92. if (sender is string command)
  93. Process.Start(command);
  94. else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  95. Process.Start("steam://run/1078000/");
  96. else
  97. Process.Start("xdg-open", "steam://run/1078000/");
  98. };
  99. if (InvokeRequired)
  100. Invoke(act);
  101. else
  102. act();
  103. EndWork(false);
  104. }
  105. public WebClient GetClient()
  106. {
  107. var client = new WebClient();
  108. if (!Settings.Default.UseProxy)
  109. client.Proxy = null;
  110. client.Headers.Clear();
  111. client.Headers[HttpRequestHeader.Accept] = "application/json";
  112. client.BaseAddress = "https://git.exmods.org";
  113. return client;
  114. }
  115. private bool working = false;
  116. /// <summary>
  117. /// Some simple "locking", only allow one operation at a time
  118. /// </summary>
  119. /// <returns>Whether the work can begin</returns>
  120. public bool BeginWork()
  121. {
  122. if (working) return false;
  123. working = true;
  124. UpdateButton(playbtn, false);
  125. UpdateButton(installbtn, false);
  126. UpdateButton(uninstallbtn, false);
  127. UpdateButton(settingsbtn, false);
  128. unpatched.Enabled = false;
  129. return true;
  130. }
  131. public void EndWork(bool desc = true)
  132. {
  133. working = false;
  134. UpdateButton(playbtn, true);
  135. UpdateButton(settingsbtn, true);
  136. if (desc)
  137. modlist_SelectedIndexChanged(modlist, null);
  138. unpatched.Enabled = true;
  139. }
  140. /// <summary>
  141. /// Path must start with \
  142. /// </summary>
  143. /// <param name="path"></param>
  144. /// <param name="gamepath"></param>
  145. /// <returns></returns>
  146. public string GamePath(string path, string gamepath = null)
  147. {
  148. return ((gamepath ?? Settings.Default.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar);
  149. }
  150. public string GetExe(string path = null)
  151. {
  152. if (File.Exists(GamePath("\\Gamecraft.exe", path)))
  153. return "Gamecraft.exe";
  154. if (File.Exists(GamePath("\\GamecraftPreview.exe", path)))
  155. return "GamecraftPreview.exe";
  156. return null;
  157. }
  158. private bool CheckNoExe()
  159. {
  160. return CheckNoExe(out _);
  161. }
  162. private bool CheckNoExe(out string path)
  163. {
  164. path = GetExe();
  165. if (path == null)
  166. {
  167. MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
  168. return true;
  169. }
  170. return false;
  171. }
  172. public async Task<DateTime> GetLastGameUpdateTime()
  173. {
  174. /*using (var client = GetClient())
  175. {
  176. string html = await client.DownloadStringTaskAsync("https://api.steamcmd.net/v1/info/1078000");
  177. var regex = new Regex("<i>timeupdated:</i>[^<]*<b>([^<]*)</b>");
  178. var match = regex.Match(html);
  179. if (!match.Success)
  180. return default;
  181. return new DateTime(1970, 1, 1).AddSeconds(long.Parse(match.Groups[1].Value));
  182. }*/
  183. //return new DateTime(2020, 12, 28);
  184. return default;
  185. }
  186. }
  187. }