Techblox Mod Manager / Launcher
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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