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.

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