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.

258 lines
11KB

  1. using GCMM.Properties;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.IO.Compression;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Reflection;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace GCMM
  16. {
  17. public partial class MainForm : Form
  18. {
  19. public MainForm()
  20. {
  21. InitializeComponent();
  22. }
  23. private Dictionary<string, ModInfo> mods = new Dictionary<string, ModInfo>();
  24. private string defaultInfo = @"
  25. Gamecraft Mod Manager
  26. If you click on a mod it will show some info about it. The install instructions there are usually for manual installs.
  27. To get started, click on a mod and select Install mod. Most mods need GamecraftModdingAPI as well.
  28. Then, simply click Play. This will first download and run the patcher (GCIPA) if needed.
  29. If all goes well, after some time a modded Gamecraft should launch.
  30. After a Gamecraft update there's a good chance that mods will break. If this happens you may get errors when trying to start Gamecraft.
  31. Until updated versions are released, use the ""Disable mods"" checkbox at the bottom to launch the game without mods.
  32. You don't have to use the mod manager to run the game each time, though it will tell you about mod updates when they come.
  33. However, you need to run it and click ""Patch & Play"" each time there's a Gamecraft update.
  34. Disclaimer:
  35. This mod manager and the mods in the list are made by the ExMods developers. We are not associated with Freejam or Gamecraft. Modify Gamecraft at your own risk.
  36. If you encounter an issue while any mods are installed, report it to us. If you think it's an issue with the game, test again with the ""Disable mods"" option checked before reporting to Freejam.
  37. You may also want to verify the game's files by right clicking the game in Steam and choosing Properties, going to Local files and clicking Verify integrity of game files.
  38. ";
  39. private void Form1_Load(object sender, EventArgs e)
  40. {
  41. if (Settings.Default.NeedsUpdate)
  42. {
  43. Settings.Default.Upgrade();
  44. Settings.Default.NeedsUpdate = false;
  45. Settings.Default.Save();
  46. }
  47. modlist.Items.Clear();
  48. UpdateButton(installbtn, false);
  49. modinfobox.Text = defaultInfo;
  50. if (string.IsNullOrWhiteSpace(Settings.Default.GamePath) || GetExe() == null)
  51. {
  52. Settings.Default.GamePath = GetGameFolder();
  53. if (string.IsNullOrWhiteSpace(Settings.Default.GamePath))
  54. Settings.Default.GamePath = SelectGameFolder();
  55. else
  56. MessageBox.Show("Found game at " + Settings.Default.GamePath);
  57. Settings.Default.Save();
  58. }
  59. if(string.IsNullOrWhiteSpace(Settings.Default.GamePath))
  60. {
  61. status.Text = "Status: Game not found";
  62. return;
  63. }
  64. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  65. if (!pexists && dexists)
  66. unpatched.Checked = true; //It will call the event but that won't do anything
  67. refreshbtn_Click(refreshbtn, null);
  68. }
  69. private async void playbtn_Click(object sender, EventArgs e)
  70. {
  71. if (playbtn.ForeColor == Color.Green) return; //Disabled
  72. await UpdateAPI();
  73. await PatchStartGame(); //It will call EndWork();
  74. }
  75. private void settingsbtn_Click(object sender, EventArgs e)
  76. {
  77. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  78. var sf = new SettingsForm();
  79. sf.ShowDialog(this);
  80. }
  81. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  82. {
  83. if (working) return;
  84. modinfobox.Clear();
  85. switch (modlist.SelectedItems.Count)
  86. {
  87. case 0:
  88. modinfobox.Text = defaultInfo;
  89. UpdateButton(installbtn, false);
  90. UpdateButton(uninstallbtn, false);
  91. break;
  92. case 1:
  93. default:
  94. installbtn.Text = "Install mod";
  95. UpdateButton(installbtn, false);
  96. UpdateButton(uninstallbtn, false);
  97. bool install = false, update = false;
  98. foreach (ListViewItem item in modlist.SelectedItems)
  99. {
  100. var mod = mods[item.Name];
  101. if (modlist.SelectedItems.Count == 1)
  102. {
  103. bool up = mod.Updatable;
  104. modinfobox.Text = ((up ? "New version available! " + mod.UpdateDetails + "\n\n"
  105. : "") + mod.Description).Replace("\n", Environment.NewLine);
  106. if(up)
  107. {
  108. modinfobox.Select(0, "New version available!".Length);
  109. modinfobox.SelectionColor = Color.Aqua;
  110. modinfobox.DeselectAll();
  111. modinfobox.SelectionColor = modinfobox.ForeColor;
  112. }
  113. }
  114. else
  115. modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
  116. if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
  117. {
  118. UpdateButton(installbtn, true);
  119. if (mod.Version != null)
  120. update = true;
  121. else
  122. install = true;
  123. }
  124. if (mod.Version != null)
  125. UpdateButton(uninstallbtn, true);
  126. }
  127. if (install && update)
  128. installbtn.Text = "Install and update mod";
  129. else if (update)
  130. installbtn.Text = "Update mod";
  131. else
  132. installbtn.Text = "Install mod";
  133. break;
  134. }
  135. if (unpatched.Checked)
  136. { //Don't allow (un)installing mods if mods are disabled
  137. UpdateButton(installbtn, false);
  138. UpdateButton(uninstallbtn, false);
  139. modlist.Enabled = false;
  140. }
  141. else
  142. modlist.Enabled = true;
  143. }
  144. private async void installbtn_Click(object sender, EventArgs e)
  145. {
  146. if (installbtn.ForeColor == Color.Green) return; //Disabled
  147. if (!BeginWork()) return;
  148. foreach (ListViewItem item in modlist.SelectedItems)
  149. {
  150. var mod = mods[item.Name];
  151. if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
  152. await InstallMod(mod);
  153. }
  154. EndWork();
  155. }
  156. private void uninstallbtn_Click(object sender, EventArgs e)
  157. {
  158. if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
  159. foreach (ListViewItem item in modlist.SelectedItems)
  160. {
  161. if (item.Group.Name != "installed") continue;
  162. UninstallMod(mods[item.Name]);
  163. }
  164. EndWork(); //Update button states
  165. }
  166. private void findlog_Click(object sender, EventArgs e)
  167. {
  168. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  169. {
  170. if (CheckNoExe(out string exe))
  171. return;
  172. Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
  173. }
  174. }
  175. private void unpatched_CheckedChanged(object sender, EventArgs e)
  176. { //Not using the patcher's revert option because sometimes it restores the wrong files - the game can be patched without mods
  177. if (CheckNoExe())
  178. return;
  179. CheckIfPatched();
  180. modlist_SelectedIndexChanged(modlist, null);
  181. string plugins = GamePath("\\Plugins");
  182. string disabled = GamePath("\\Plugins_Disabled");
  183. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  184. if (unpatched.Checked)
  185. {
  186. if (pexists)
  187. {
  188. if (dexists)
  189. Directory.Delete(disabled, true); //Resolving conflicts would be complicated so delete the other mods - this shouldn't happen normally
  190. Directory.Move(plugins, disabled);
  191. }
  192. }
  193. else
  194. {
  195. if (dexists)
  196. {
  197. if (pexists)
  198. Directory.Delete(plugins, true);
  199. Directory.Move(disabled, plugins);
  200. }
  201. }
  202. }
  203. private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  204. {
  205. string plugins = GamePath("\\Plugins");
  206. string disabled = GamePath("\\Plugins_Disabled");
  207. pexists = Directory.Exists(plugins);
  208. dexists = Directory.Exists(disabled);
  209. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  210. {
  211. Directory.Delete(plugins);
  212. pexists = false;
  213. }
  214. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  215. {
  216. Directory.Delete(disabled);
  217. dexists = false;
  218. }
  219. }
  220. private void refreshbtn_Click(object sender, EventArgs e)
  221. {
  222. CheckIfPatched();
  223. var mods = GetInstalledMods();
  224. GetAvailableMods();
  225. CheckUninstalledMods(mods);
  226. }
  227. private void validatebtn_Click(object sender, EventArgs e)
  228. {
  229. if (CheckNoExe())
  230. return;
  231. if (MessageBox.Show("Validating the game's files is useful if the game doesn't start even without mods. Make sure to click Refresh once Steam finished verifying the game. The Steam window that shows the progress might open in the background. Note that you will need to patch the game again using the Play button in order to use mods.\n\nContinue?", "Verify game files", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  232. return;
  233. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  234. Process.Start("steam://validate/1078000/");
  235. else
  236. Process.Start("xdg-open", "steam://validate/1078000/");
  237. }
  238. }
  239. }