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.

126 lines
4.1KB

  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. {
  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 (File.Exists(library + @"Gamecraft\Gamecraft.exe"))
  48. return library + "Gamecraft";
  49. if (File.Exists(library + @"RobocraftX\Gamecraft.exe"))
  50. return library + "RobocraftX";
  51. }
  52. return libs;
  53. }
  54. public string SelectGameFolder()
  55. {
  56. var ofd = new OpenFileDialog();
  57. ofd.Filter = "Gamecraft executable|Gamecraft.exe";
  58. ofd.Title = "Game location";
  59. ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\steamapps\common\";
  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() ?? false) || unpatched.Checked)
  76. Process.Start("steam://run/1078000/");
  77. };
  78. if (InvokeRequired)
  79. Invoke(act);
  80. else
  81. act();
  82. EndWork(false);
  83. }
  84. public WebClient GetClient()
  85. {
  86. var client = new WebClient();
  87. if (!Settings.Default.UseProxy)
  88. client.Proxy = null;
  89. client.Headers.Clear();
  90. client.Headers[HttpRequestHeader.Accept] = "application/json";
  91. client.BaseAddress = "https://git.exmods.org";
  92. return client;
  93. }
  94. private bool working = false;
  95. /// <summary>
  96. /// Some simple "locking", only allow one operation at a time
  97. /// </summary>
  98. /// <returns>Whether the work can begin</returns>
  99. public bool BeginWork()
  100. {
  101. if (working) return false;
  102. working = true;
  103. UpdateButton(playbtn, false);
  104. UpdateButton(installbtn, false);
  105. UpdateButton(uninstallbtn, false);
  106. UpdateButton(settingsbtn, false);
  107. return true;
  108. }
  109. public void EndWork(bool desc = true)
  110. {
  111. working = false;
  112. UpdateButton(playbtn, true);
  113. UpdateButton(settingsbtn, true);
  114. if (desc)
  115. modlist_SelectedIndexChanged(modlist, null);
  116. }
  117. }
  118. }