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.

260 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 readonly Dictionary<string, ModInfo> mods = new Dictionary<string, ModInfo>();
  24. private readonly ModInfo gcipa = new ModInfo { Author = "modtainers", Name = "GCIPA" };
  25. private const string defaultInfo = @"
  26. Gamecraft Mod Manager
  27. If you click on a mod it will show some info about it. The install instructions there are usually for manual installs.
  28. To get started, click on a mod and select Install mod. Most mods need GamecraftModdingAPI as well.
  29. Then, simply click Play. This will first download and run the patcher (GCIPA) if needed.
  30. If all goes well, after some time a modded Gamecraft should launch.
  31. 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.
  32. Until updated versions are released, use the ""Disable mods"" checkbox at the bottom to launch the game without mods.
  33. 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.
  34. However, you need to run it and click ""Patch & Play"" each time there's a Gamecraft update.
  35. Disclaimer:
  36. 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.
  37. 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.
  38. 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.
  39. ";
  40. private void Form1_Load(object sender, EventArgs e)
  41. {
  42. if (Settings.Default.NeedsUpdate)
  43. {
  44. Settings.Default.Upgrade();
  45. Settings.Default.NeedsUpdate = false;
  46. Settings.Default.Save();
  47. }
  48. modlist.Items.Clear();
  49. UpdateButton(installbtn, false);
  50. modinfobox.Text = defaultInfo;
  51. if (string.IsNullOrWhiteSpace(Settings.Default.GamePath) || GetExe() == null)
  52. {
  53. Settings.Default.GamePath = GetGameFolder();
  54. if (string.IsNullOrWhiteSpace(Settings.Default.GamePath))
  55. Settings.Default.GamePath = SelectGameFolder();
  56. else
  57. MessageBox.Show("Found game at " + Settings.Default.GamePath);
  58. Settings.Default.Save();
  59. }
  60. if(string.IsNullOrWhiteSpace(Settings.Default.GamePath))
  61. {
  62. status.Text = "Status: Game not found";
  63. return;
  64. }
  65. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  66. if (!pexists && dexists)
  67. unpatched.Checked = true; //It will call the event but that won't do anything
  68. refreshbtn_Click(refreshbtn, null);
  69. }
  70. private async void playbtn_Click(object sender, EventArgs e)
  71. {
  72. if (playbtn.ForeColor == Color.Green) return; //Disabled
  73. await UpdateAPI();
  74. await PatchStartGame(); //It will call EndWork();
  75. }
  76. private void settingsbtn_Click(object sender, EventArgs e)
  77. {
  78. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  79. var sf = new SettingsForm();
  80. sf.ShowDialog(this);
  81. }
  82. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  83. {
  84. if (working) return;
  85. modinfobox.Clear();
  86. switch (modlist.SelectedItems.Count)
  87. {
  88. case 0:
  89. modinfobox.Text = defaultInfo;
  90. UpdateButton(installbtn, false);
  91. UpdateButton(uninstallbtn, false);
  92. break;
  93. case 1:
  94. default:
  95. installbtn.Text = "Install mod";
  96. UpdateButton(installbtn, false);
  97. UpdateButton(uninstallbtn, false);
  98. bool install = false, update = false;
  99. foreach (ListViewItem item in modlist.SelectedItems)
  100. {
  101. var mod = mods[item.Name];
  102. if (modlist.SelectedItems.Count == 1)
  103. {
  104. bool up = mod.Updatable;
  105. modinfobox.Text = ((up ? "New version available! " + mod.UpdateDetails + "\n\n"
  106. : "") + mod.Description).Replace("\n", Environment.NewLine);
  107. if(up)
  108. {
  109. modinfobox.Select(0, "New version available!".Length);
  110. modinfobox.SelectionColor = Color.Aqua;
  111. modinfobox.DeselectAll();
  112. modinfobox.SelectionColor = modinfobox.ForeColor;
  113. }
  114. }
  115. else
  116. modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
  117. if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
  118. {
  119. UpdateButton(installbtn, true);
  120. if (mod.Version != null)
  121. update = true;
  122. else
  123. install = true;
  124. }
  125. if (mod.Version != null)
  126. UpdateButton(uninstallbtn, true);
  127. }
  128. if (install && update)
  129. installbtn.Text = "Install and update mod";
  130. else if (update)
  131. installbtn.Text = "Update mod";
  132. else
  133. installbtn.Text = "Install mod";
  134. break;
  135. }
  136. if (unpatched.Checked)
  137. { //Don't allow (un)installing mods if mods are disabled
  138. UpdateButton(installbtn, false);
  139. UpdateButton(uninstallbtn, false);
  140. modlist.Enabled = false;
  141. }
  142. else
  143. modlist.Enabled = true;
  144. }
  145. private async void installbtn_Click(object sender, EventArgs e)
  146. {
  147. if (installbtn.ForeColor == Color.Green) return; //Disabled
  148. if (!BeginWork()) return;
  149. foreach (ListViewItem item in modlist.SelectedItems)
  150. {
  151. var mod = mods[item.Name];
  152. if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
  153. await InstallMod(mod);
  154. }
  155. EndWork();
  156. }
  157. private void uninstallbtn_Click(object sender, EventArgs e)
  158. {
  159. if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
  160. foreach (ListViewItem item in modlist.SelectedItems)
  161. {
  162. if (item.Group.Name != "installed") continue;
  163. UninstallMod(mods[item.Name]);
  164. }
  165. EndWork(); //Update button states
  166. }
  167. private void findlog_Click(object sender, EventArgs e)
  168. {
  169. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  170. {
  171. if (CheckNoExe(out string exe))
  172. return;
  173. Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
  174. }
  175. }
  176. private void unpatched_CheckedChanged(object sender, EventArgs e)
  177. { //Not using the patcher's revert option because sometimes it restores the wrong files - the game can be patched without mods
  178. if (CheckNoExe())
  179. return;
  180. CheckIfPatched();
  181. modlist_SelectedIndexChanged(modlist, null);
  182. string plugins = GamePath("\\Plugins");
  183. string disabled = GamePath("\\Plugins_Disabled");
  184. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  185. if (unpatched.Checked)
  186. {
  187. if (pexists)
  188. {
  189. if (dexists)
  190. Directory.Delete(disabled, true); //Resolving conflicts would be complicated so delete the other mods - this shouldn't happen normally
  191. Directory.Move(plugins, disabled);
  192. }
  193. }
  194. else
  195. {
  196. if (dexists)
  197. {
  198. if (pexists)
  199. Directory.Delete(plugins, true);
  200. Directory.Move(disabled, plugins);
  201. }
  202. }
  203. }
  204. private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  205. {
  206. string plugins = GamePath("\\Plugins");
  207. string disabled = GamePath("\\Plugins_Disabled");
  208. pexists = Directory.Exists(plugins);
  209. dexists = Directory.Exists(disabled);
  210. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  211. {
  212. Directory.Delete(plugins);
  213. pexists = false;
  214. }
  215. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  216. {
  217. Directory.Delete(disabled);
  218. dexists = false;
  219. }
  220. }
  221. private async void refreshbtn_Click(object sender, EventArgs e)
  222. {
  223. CheckIfPatched(); //Set from placeholder
  224. var mods = GetInstalledMods();
  225. await GetAvailableMods();
  226. CheckUninstalledMods(mods);
  227. CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
  228. }
  229. private void validatebtn_Click(object sender, EventArgs e)
  230. {
  231. if (CheckNoExe())
  232. return;
  233. 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)
  234. return;
  235. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  236. Process.Start("steam://validate/1078000/");
  237. else
  238. Process.Start("xdg-open", "steam://validate/1078000/");
  239. }
  240. }
  241. }