Techblox Mod Manager / Launcher
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

165 рядки
5.4KB

  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 = @"C:\Program Files (x86)\Steam\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. private void CheckStartGame(object sender, EventArgs e)
  67. {
  68. Action act = () =>
  69. {
  70. if (((sender as Process)?.ExitCode ?? 0) != 0)
  71. {
  72. status.Text = "Status: Patching failed";
  73. return;
  74. }
  75. if (CheckIfPatched() == GameState.Patched || unpatched.Checked)
  76. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  77. Process.Start("steam://run/1078000/");
  78. else
  79. Process.Start("xdg-open", "steam://run/1078000/");
  80. };
  81. if (InvokeRequired)
  82. Invoke(act);
  83. else
  84. act();
  85. EndWork(false);
  86. }
  87. public WebClient GetClient()
  88. {
  89. var client = new WebClient();
  90. if (!Settings.Default.UseProxy)
  91. client.Proxy = null;
  92. client.Headers.Clear();
  93. client.Headers[HttpRequestHeader.Accept] = "application/json";
  94. client.BaseAddress = "https://git.exmods.org";
  95. return client;
  96. }
  97. private bool working = false;
  98. /// <summary>
  99. /// Some simple "locking", only allow one operation at a time
  100. /// </summary>
  101. /// <returns>Whether the work can begin</returns>
  102. public bool BeginWork()
  103. {
  104. if (working) return false;
  105. working = true;
  106. UpdateButton(playbtn, false);
  107. UpdateButton(installbtn, false);
  108. UpdateButton(uninstallbtn, false);
  109. UpdateButton(settingsbtn, false);
  110. return true;
  111. }
  112. public void EndWork(bool desc = true)
  113. {
  114. working = false;
  115. UpdateButton(playbtn, true);
  116. UpdateButton(settingsbtn, true);
  117. if (desc)
  118. modlist_SelectedIndexChanged(modlist, null);
  119. }
  120. /// <summary>
  121. /// Path must start with \
  122. /// </summary>
  123. /// <param name="path"></param>
  124. /// <param name="gamepath"></param>
  125. /// <returns></returns>
  126. public string GamePath(string path, string gamepath = null)
  127. {
  128. return ((gamepath ?? Settings.Default.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar);
  129. }
  130. public string GetExe(string path = null)
  131. {
  132. if (File.Exists(GamePath("\\Gamecraft.exe", path)))
  133. return "Gamecraft.exe";
  134. if (File.Exists(GamePath("\\GamecraftPreview.exe", path)))
  135. return "GamecraftPreview.exe";
  136. return null;
  137. }
  138. private bool CheckNoExe()
  139. {
  140. return CheckNoExe(out _);
  141. }
  142. private bool CheckNoExe(out string path)
  143. {
  144. path = GetExe();
  145. if (path == null)
  146. {
  147. MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
  148. return true;
  149. }
  150. return false;
  151. }
  152. }
  153. }