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.

298 lines
13KB

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