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.

279 lines
12KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Resources;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace GCMM
  12. {
  13. public partial class MainForm : Form
  14. {
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. resources = new ResourceManager("GCMM.Localization", Assembly.GetExecutingAssembly());
  19. Configuration = Configuration.Load();
  20. }
  21. public Configuration Configuration { get; }
  22. private readonly ResourceManager resources;
  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. Techblox 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 TechbloxModdingAPI as well so it'll be installed.
  31. Then launch Techblox: if you enabled auto-patching then you can use the launcher but if you didn't then you must use the Play button.
  32. This will first download and run the patcher (GCIPA) if needed. If all goes well, after some time a modded Techblox should launch.
  33. After a Techblox update there's a good chance that mods will break. If this happens you may get errors when trying to start Techblox.
  34. Until updated versions are released, use the ""Disable mods"" checkbox at the bottom to launch the game without mods.
  35. If you enabled auto-patching you will get a warning about this.
  36. If you don't have auto-patching enabled then you will need to run the mod manager each time Techblox updates and click ""Patch & Play"" or the game may not function properly.
  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 Techblox. Modify Techblox 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 in the launcher.
  41. ";
  42. private async void Form1_Load(object sender, EventArgs e)
  43. {
  44. if (mods.Values.All(mod => mod.LatestVersion == null)) //Not (fully) loaded yet
  45. await LoadEverything(true);
  46. }
  47. public async Task LoadEverything(bool evenMods)
  48. {
  49. modlist.Items.Clear();
  50. mods.Clear(); //This method may get called twice when ran from the command line
  51. UpdateButton(installbtn, false);
  52. modinfobox.Text = defaultInfo;
  53. if (string.IsNullOrWhiteSpace(Configuration.GamePath) || GetExe() == null)
  54. {
  55. Configuration.GamePath = GetGameFolder();
  56. if (string.IsNullOrWhiteSpace(Configuration.GamePath))
  57. {
  58. DialogUtils.ShowWarning(resources.GetString("Game_not_found"), "");
  59. Configuration.GamePath = SelectGameFolder();
  60. }
  61. else
  62. DialogUtils.ShowInfo(string.Format(resources.GetString("Found_game_at"), Configuration.GamePath), "");
  63. Configuration.Save();
  64. }
  65. if(string.IsNullOrWhiteSpace(Configuration.GamePath))
  66. {
  67. status.Text = resources.GetString("Status_Game_not_found");
  68. return;
  69. }
  70. if (Configuration.AutoPatch == AutoPatchingState.Unspecified)
  71. {
  72. Configuration.AutoPatch = AutoPatchingState.Disabled;
  73. if (DialogUtils.ShowYesNoQuestion(resources.GetString("Change_launch_settings_question"),
  74. resources.GetString("Change_launch_settings_title")))
  75. EnableDisableAutoPatchingWithDialog(true);
  76. }
  77. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  78. if (!pexists && dexists)
  79. unpatched.Checked = true; //It will call the event but that won't do anything
  80. await RefreshEverything(evenMods);
  81. }
  82. private async void playbtn_Click(object sender, EventArgs e)
  83. {
  84. if (playbtn.ForeColor == Color.Green) return; //Disabled
  85. await UpdateAPI();
  86. await PatchStartGame(); //It will call EndWork();
  87. }
  88. private void settingsbtn_Click(object sender, EventArgs e)
  89. {
  90. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  91. var sf = new SettingsForm();
  92. sf.ShowDialog(this);
  93. }
  94. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  95. {
  96. if (working) return;
  97. modinfobox.Clear();
  98. switch (modlist.SelectedItems.Count)
  99. {
  100. case 0:
  101. modinfobox.Text = defaultInfo;
  102. UpdateButton(installbtn, false);
  103. UpdateButton(uninstallbtn, false);
  104. break;
  105. case 1:
  106. default:
  107. installbtn.Text = "Install mod";
  108. UpdateButton(installbtn, false);
  109. UpdateButton(uninstallbtn, false);
  110. bool install = false, update = false;
  111. Action<string, Color> addText = (txt, color) =>
  112. {
  113. int start = modinfobox.Text.Length;
  114. modinfobox.AppendText(txt + Environment.NewLine + Environment.NewLine);
  115. modinfobox.Select(start, txt.Length);
  116. modinfobox.SelectionColor = color;
  117. modinfobox.DeselectAll();
  118. modinfobox.SelectionColor = modinfobox.ForeColor;
  119. };
  120. foreach (ListViewItem item in modlist.SelectedItems)
  121. {
  122. var mod = mods[item.Name];
  123. if (modlist.SelectedItems.Count == 1)
  124. {
  125. if (mod.Updatable)
  126. addText("New version available! " + mod.UpdateDetails, Color.Aqua);
  127. if (mod.LastUpdated < lastGameUpdateTime)
  128. addText("Outdated mod! It may not work properly on the latest version of the game.", Color.DarkOrange);
  129. if (mod.Description != null)
  130. modinfobox.AppendText(mod.Description.Replace("\n", Environment.NewLine));
  131. }
  132. else
  133. modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
  134. if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
  135. {
  136. UpdateButton(installbtn, true);
  137. if (mod.Version != null)
  138. update = true;
  139. else
  140. install = true;
  141. }
  142. if (mod.Version != null)
  143. UpdateButton(uninstallbtn, true);
  144. }
  145. if (install && update)
  146. installbtn.Text = "Install and update mod";
  147. else if (update)
  148. installbtn.Text = "Update mod";
  149. else
  150. installbtn.Text = "Install mod";
  151. break;
  152. }
  153. if (unpatched.Checked)
  154. { //Don't allow (un)installing mods if mods are disabled
  155. UpdateButton(installbtn, false);
  156. UpdateButton(uninstallbtn, false);
  157. modlist.Enabled = false;
  158. }
  159. else
  160. modlist.Enabled = true;
  161. }
  162. private async void installbtn_Click(object sender, EventArgs e)
  163. {
  164. if (installbtn.ForeColor == Color.Green) return; //Disabled
  165. if (!BeginWork()) return;
  166. foreach (ListViewItem item in modlist.SelectedItems)
  167. {
  168. var mod = mods[item.Name];
  169. if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
  170. await InstallMod(mod);
  171. }
  172. EndWork();
  173. }
  174. private void uninstallbtn_Click(object sender, EventArgs e)
  175. {
  176. if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
  177. foreach (ListViewItem item in modlist.SelectedItems)
  178. {
  179. if (item.Group.Name != "installed") continue;
  180. UninstallMod(mods[item.Name]);
  181. }
  182. EndWork(); //Update button states
  183. }
  184. private void findlog_Click(object sender, EventArgs e)
  185. {
  186. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  187. {
  188. if (CheckNoExe(out string exe))
  189. return;
  190. Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
  191. }
  192. }
  193. private void unpatched_CheckedChanged(object sender, EventArgs e)
  194. { //Not using the patcher's revert option because sometimes it restores the wrong files - the game can be patched without mods
  195. if (CheckNoExe())
  196. return;
  197. CheckIfPatched();
  198. modlist_SelectedIndexChanged(modlist, null);
  199. string plugins = GamePath("\\Plugins");
  200. string disabled = GamePath("\\Plugins_Disabled");
  201. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  202. if (unpatched.Checked)
  203. {
  204. if (pexists)
  205. {
  206. if (dexists)
  207. Directory.Delete(disabled, true); //Resolving conflicts would be complicated so delete the other mods - this shouldn't happen normally
  208. Directory.Move(plugins, disabled);
  209. }
  210. }
  211. else
  212. {
  213. if (dexists)
  214. {
  215. if (pexists)
  216. Directory.Delete(plugins, true);
  217. Directory.Move(disabled, plugins);
  218. }
  219. }
  220. }
  221. private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  222. {
  223. string plugins = GamePath("\\Plugins");
  224. string disabled = GamePath("\\Plugins_Disabled");
  225. pexists = Directory.Exists(plugins);
  226. dexists = Directory.Exists(disabled);
  227. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  228. {
  229. Directory.Delete(plugins);
  230. pexists = false;
  231. }
  232. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  233. {
  234. Directory.Delete(disabled);
  235. dexists = false;
  236. }
  237. }
  238. private async void refreshbtn_Click(object sender, EventArgs e)
  239. {
  240. await RefreshEverything(true);
  241. }
  242. private async Task RefreshEverything(bool evenMods)
  243. {
  244. CheckIfPatched(); //Set from placeholder
  245. lastGameUpdateTime = await GetLastGameUpdateTime();
  246. var mods = GetInstalledMods();
  247. if (evenMods)
  248. await GetAvailableMods();
  249. CheckUninstalledMods(mods);
  250. CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
  251. }
  252. private void MainForm_Shown(object sender, EventArgs e)
  253. {
  254. Focus();
  255. }
  256. }
  257. }