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.

308 lines
14KB

  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 readonly ModInfo gcmm = new ModInfo { Author = "NorbiPeti", Name = "GCMM" };
  26. private DateTime lastGameUpdateTime;
  27. private string steamPath;
  28. private const string defaultInfo = @"
  29. Gamecraft Mod Manager
  30. If you click on a mod it will show some info about it. The install instructions there are usually for manual installs.
  31. To get started, click on a mod and select Install mod. Most mods need GamecraftModdingAPI as well.
  32. Then, simply click Play. This will first download and run the patcher (GCIPA) if needed.
  33. If all goes well, after some time a modded Gamecraft should launch.
  34. 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.
  35. Until updated versions are released, use the ""Disable mods"" checkbox at the bottom to launch the game without mods.
  36. 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.
  37. However, you need to run it and click ""Patch & Play"" each time there's a Gamecraft update.
  38. Disclaimer:
  39. 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.
  40. 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.
  41. 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.
  42. ";
  43. private async void Form1_Load(object sender, EventArgs e)
  44. {
  45. await LoadEverything();
  46. }
  47. public async Task LoadEverything()
  48. {
  49. if (Settings.Default.NeedsUpdate)
  50. {
  51. Settings.Default.Upgrade();
  52. Settings.Default.NeedsUpdate = false;
  53. Settings.Default.Save();
  54. }
  55. modlist.Items.Clear();
  56. mods.Clear(); //This method may get called twice when ran from the command line
  57. UpdateButton(installbtn, false);
  58. modinfobox.Text = defaultInfo;
  59. var (steamPath, user) = GetSteamLocationAndUser(); //TODO: If user is 0 then start Steam
  60. if (steamPath != null)
  61. this.steamPath = steamPath;
  62. else
  63. {
  64. MessageBox.Show("Steam not found! If you have Steam installed, please report this to ExMods.\n\nThe Steam install is checked to autodetect where the game is installed and to optionally configure auto-patching.", "Steam not found");
  65. status.Text = "Status: Steam not found";
  66. return;
  67. }
  68. if (Settings.Default.SteamUserID == 0 && Settings.Default.AutoLaunch)
  69. {
  70. if (MessageBox.Show("Do you want GCMM to change the game's launch settings so it can ensure the game is patched?\n\n" +
  71. "If you say yes, GCMM will do a quick check before the game is launched and patches if necessary. " +
  72. "This way you (hopefully) won't see crashes after a Gamecraft update.\n\n" +
  73. "Note that this also means that if you (re)move GCMM without disabling this in the settings then you won't be able to launch Gamecraft.",
  74. "GCMM auto-patching", MessageBoxButtons.YesNo) == DialogResult.Yes)
  75. DetectConfigLocationAndAutoStart(steamPath, ref user);
  76. else
  77. Settings.Default.AutoLaunch = false;
  78. Settings.Default.Save();
  79. }
  80. if (string.IsNullOrWhiteSpace(Settings.Default.GamePath) || GetExe() == null)
  81. {
  82. Settings.Default.GamePath = GetGameFolder();
  83. if (string.IsNullOrWhiteSpace(Settings.Default.GamePath))
  84. Settings.Default.GamePath = SelectGameFolder();
  85. else
  86. MessageBox.Show("Found game at " + Settings.Default.GamePath);
  87. Settings.Default.Save();
  88. }
  89. if(string.IsNullOrWhiteSpace(Settings.Default.GamePath))
  90. {
  91. status.Text = "Status: Game not found";
  92. return;
  93. }
  94. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  95. if (!pexists && dexists)
  96. unpatched.Checked = true; //It will call the event but that won't do anything
  97. await RefreshEverything();
  98. }
  99. private async void playbtn_Click(object sender, EventArgs e)
  100. {
  101. if (playbtn.ForeColor == Color.Green) return; //Disabled
  102. await UpdateAPI();
  103. await PatchStartGame(); //It will call EndWork();
  104. }
  105. private void settingsbtn_Click(object sender, EventArgs e)
  106. {
  107. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  108. var sf = new SettingsForm();
  109. sf.ShowDialog(this);
  110. }
  111. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  112. {
  113. if (working) return;
  114. modinfobox.Clear();
  115. switch (modlist.SelectedItems.Count)
  116. {
  117. case 0:
  118. modinfobox.Text = defaultInfo;
  119. UpdateButton(installbtn, false);
  120. UpdateButton(uninstallbtn, false);
  121. break;
  122. case 1:
  123. default:
  124. installbtn.Text = "Install mod";
  125. UpdateButton(installbtn, false);
  126. UpdateButton(uninstallbtn, false);
  127. bool install = false, update = false;
  128. Action<string, Color> addText = (txt, color) =>
  129. {
  130. int start = modinfobox.Text.Length;
  131. modinfobox.AppendText(txt + Environment.NewLine + Environment.NewLine);
  132. modinfobox.Select(start, txt.Length);
  133. modinfobox.SelectionColor = color;
  134. modinfobox.DeselectAll();
  135. modinfobox.SelectionColor = modinfobox.ForeColor;
  136. };
  137. foreach (ListViewItem item in modlist.SelectedItems)
  138. {
  139. var mod = mods[item.Name];
  140. if (modlist.SelectedItems.Count == 1)
  141. {
  142. if (mod.Updatable)
  143. addText("New version available! " + mod.UpdateDetails, Color.Aqua);
  144. if (mod.LastUpdated < lastGameUpdateTime)
  145. addText("Outdated mod! It may not work properly on the latest version of the game.", Color.DarkOrange);
  146. if (mod.Description != null)
  147. modinfobox.AppendText(mod.Description.Replace("\n", Environment.NewLine));
  148. }
  149. else
  150. modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
  151. if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
  152. {
  153. UpdateButton(installbtn, true);
  154. if (mod.Version != null)
  155. update = true;
  156. else
  157. install = true;
  158. }
  159. if (mod.Version != null)
  160. UpdateButton(uninstallbtn, true);
  161. }
  162. if (install && update)
  163. installbtn.Text = "Install and update mod";
  164. else if (update)
  165. installbtn.Text = "Update mod";
  166. else
  167. installbtn.Text = "Install mod";
  168. break;
  169. }
  170. if (unpatched.Checked)
  171. { //Don't allow (un)installing mods if mods are disabled
  172. UpdateButton(installbtn, false);
  173. UpdateButton(uninstallbtn, false);
  174. modlist.Enabled = false;
  175. }
  176. else
  177. modlist.Enabled = true;
  178. }
  179. private async void installbtn_Click(object sender, EventArgs e)
  180. {
  181. if (installbtn.ForeColor == Color.Green) return; //Disabled
  182. if (!BeginWork()) return;
  183. foreach (ListViewItem item in modlist.SelectedItems)
  184. {
  185. var mod = mods[item.Name];
  186. if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
  187. await InstallMod(mod);
  188. }
  189. EndWork();
  190. }
  191. private void uninstallbtn_Click(object sender, EventArgs e)
  192. {
  193. if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
  194. foreach (ListViewItem item in modlist.SelectedItems)
  195. {
  196. if (item.Group.Name != "installed") continue;
  197. UninstallMod(mods[item.Name]);
  198. }
  199. EndWork(); //Update button states
  200. }
  201. private void findlog_Click(object sender, EventArgs e)
  202. {
  203. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  204. {
  205. if (CheckNoExe(out string exe))
  206. return;
  207. Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
  208. }
  209. }
  210. private void unpatched_CheckedChanged(object sender, EventArgs e)
  211. { //Not using the patcher's revert option because sometimes it restores the wrong files - the game can be patched without mods
  212. if (CheckNoExe())
  213. return;
  214. CheckIfPatched();
  215. modlist_SelectedIndexChanged(modlist, null);
  216. string plugins = GamePath("\\Plugins");
  217. string disabled = GamePath("\\Plugins_Disabled");
  218. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  219. if (unpatched.Checked)
  220. {
  221. if (pexists)
  222. {
  223. if (dexists)
  224. Directory.Delete(disabled, true); //Resolving conflicts would be complicated so delete the other mods - this shouldn't happen normally
  225. Directory.Move(plugins, disabled);
  226. }
  227. }
  228. else
  229. {
  230. if (dexists)
  231. {
  232. if (pexists)
  233. Directory.Delete(plugins, true);
  234. Directory.Move(disabled, plugins);
  235. }
  236. }
  237. }
  238. private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  239. {
  240. string plugins = GamePath("\\Plugins");
  241. string disabled = GamePath("\\Plugins_Disabled");
  242. pexists = Directory.Exists(plugins);
  243. dexists = Directory.Exists(disabled);
  244. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  245. {
  246. Directory.Delete(plugins);
  247. pexists = false;
  248. }
  249. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  250. {
  251. Directory.Delete(disabled);
  252. dexists = false;
  253. }
  254. }
  255. private async void refreshbtn_Click(object sender, EventArgs e)
  256. {
  257. await RefreshEverything();
  258. }
  259. private async Task RefreshEverything()
  260. {
  261. CheckIfPatched(); //Set from placeholder
  262. lastGameUpdateTime = await GetLastGameUpdateTime();
  263. var mods = GetInstalledMods();
  264. await GetAvailableMods();
  265. CheckUninstalledMods(mods);
  266. CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
  267. }
  268. private void validatebtn_Click(object sender, EventArgs e)
  269. {
  270. if (CheckNoExe())
  271. return;
  272. 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)
  273. return;
  274. string exe = GetExe();
  275. File.Delete(GamePath($@"\{exe.Replace(".exe", "")}_Data\Managed\IllusionInjector.dll")); //Used to check if game is patched
  276. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  277. Process.Start("steam://validate/1078000/");
  278. else
  279. Process.Start("xdg-open", "steam://validate/1078000/");
  280. }
  281. private void MainForm_Shown(object sender, EventArgs e)
  282. {
  283. Focus();
  284. }
  285. }
  286. }