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.

270 lines
11KB

  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 TBMM
  12. {
  13. public partial class MainForm : Form
  14. {
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. resources = new ResourceManager("TBMM.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 tbmm = new ModInfo { Author = "NorbiPeti", Name = "TBMM" };
  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 by clicking on the Play button below. Mods are only loaded if you start the game from here.
  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 launch the game through the launcher after an update and encounter an error, either repair the game or launch it through the mod manager.
  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. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  70. if (!pexists && dexists)
  71. unpatched.Checked = true; //It will call the event but that won't do anything
  72. await RefreshEverything(evenMods);
  73. }
  74. private async void playbtn_Click(object sender, EventArgs e)
  75. {
  76. if (playbtn.ForeColor == Color.Green) return; //Disabled
  77. await UpdateAPI();
  78. await PatchStartGame(); //It will call EndWork();
  79. }
  80. private void settingsbtn_Click(object sender, EventArgs e)
  81. {
  82. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  83. var sf = new SettingsForm();
  84. sf.ShowDialog(this);
  85. }
  86. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  87. {
  88. if (working) return;
  89. modinfobox.Clear();
  90. switch (modlist.SelectedItems.Count)
  91. {
  92. case 0:
  93. modinfobox.Text = defaultInfo;
  94. UpdateButton(installbtn, false);
  95. UpdateButton(uninstallbtn, false);
  96. break;
  97. case 1:
  98. default:
  99. installbtn.Text = "Install mod";
  100. UpdateButton(installbtn, false);
  101. UpdateButton(uninstallbtn, false);
  102. bool install = false, update = false;
  103. Action<string, Color> addText = (txt, color) =>
  104. {
  105. int start = modinfobox.Text.Length;
  106. modinfobox.AppendText(txt + Environment.NewLine + Environment.NewLine);
  107. modinfobox.Select(start, txt.Length);
  108. modinfobox.SelectionColor = color;
  109. modinfobox.DeselectAll();
  110. modinfobox.SelectionColor = modinfobox.ForeColor;
  111. };
  112. foreach (ListViewItem item in modlist.SelectedItems)
  113. {
  114. var mod = mods[item.Name];
  115. if (modlist.SelectedItems.Count == 1)
  116. {
  117. if (mod.Updatable)
  118. addText("New version available! " + mod.UpdateDetails, Color.Aqua);
  119. if (mod.LastUpdated != default && mod.LastUpdated < lastGameUpdateTime)
  120. addText("Outdated mod! It may not work properly on the current version of the game.", Color.DarkOrange);
  121. if (mod.Description != null)
  122. modinfobox.AppendText(mod.Description.Replace("\n", Environment.NewLine));
  123. }
  124. else
  125. modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
  126. if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
  127. {
  128. UpdateButton(installbtn, true);
  129. if (mod.Version != null)
  130. update = true;
  131. else
  132. install = true;
  133. }
  134. if (mod.Version != null)
  135. UpdateButton(uninstallbtn, true);
  136. }
  137. if (install && update)
  138. installbtn.Text = "Install and update mod";
  139. else if (update)
  140. installbtn.Text = "Update mod";
  141. else
  142. installbtn.Text = "Install mod";
  143. break;
  144. }
  145. if (unpatched.Checked)
  146. { //Don't allow (un)installing mods if mods are disabled
  147. UpdateButton(installbtn, false);
  148. UpdateButton(uninstallbtn, false);
  149. modlist.Enabled = false;
  150. }
  151. else
  152. modlist.Enabled = true;
  153. }
  154. private async void installbtn_Click(object sender, EventArgs e)
  155. {
  156. if (installbtn.ForeColor == Color.Green) return; //Disabled
  157. if (!BeginWork()) return;
  158. foreach (ListViewItem item in modlist.SelectedItems)
  159. {
  160. var mod = mods[item.Name];
  161. if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
  162. await InstallMod(mod);
  163. }
  164. EndWork();
  165. }
  166. private void uninstallbtn_Click(object sender, EventArgs e)
  167. {
  168. if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
  169. foreach (ListViewItem item in modlist.SelectedItems)
  170. {
  171. if (item.Group.Name != "installed") continue;
  172. UninstallMod(mods[item.Name]);
  173. }
  174. EndWork(); //Update button states
  175. }
  176. private void findlog_Click(object sender, EventArgs e)
  177. {
  178. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  179. {
  180. if (CheckNoExe(out string exe))
  181. return;
  182. Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
  183. }
  184. }
  185. private void unpatched_CheckedChanged(object sender, EventArgs e)
  186. { //Not using the patcher's revert option because sometimes it restores the wrong files - the game can be patched without mods
  187. if (CheckNoExe())
  188. return;
  189. CheckIfPatched();
  190. modlist_SelectedIndexChanged(modlist, null);
  191. string plugins = GamePath("\\Plugins");
  192. string disabled = GamePath("\\Plugins_Disabled");
  193. DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
  194. if (unpatched.Checked)
  195. {
  196. if (pexists)
  197. {
  198. if (dexists)
  199. Directory.Delete(disabled, true); //Resolving conflicts would be complicated so delete the other mods - this shouldn't happen normally
  200. Directory.Move(plugins, disabled);
  201. }
  202. }
  203. else
  204. {
  205. if (dexists)
  206. {
  207. if (pexists)
  208. Directory.Delete(plugins, true);
  209. Directory.Move(disabled, plugins);
  210. }
  211. }
  212. }
  213. private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  214. {
  215. string plugins = GamePath("\\Plugins");
  216. string disabled = GamePath("\\Plugins_Disabled");
  217. pexists = Directory.Exists(plugins);
  218. dexists = Directory.Exists(disabled);
  219. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  220. {
  221. Directory.Delete(plugins);
  222. pexists = false;
  223. }
  224. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  225. {
  226. Directory.Delete(disabled);
  227. dexists = false;
  228. }
  229. }
  230. private async void refreshbtn_Click(object sender, EventArgs e)
  231. {
  232. await RefreshEverything(true);
  233. }
  234. private async Task RefreshEverything(bool evenMods)
  235. {
  236. CheckIfPatched(); //Set from placeholder
  237. lastGameUpdateTime = GetGameVersionAsDate();
  238. var mods = GetInstalledMods();
  239. if (evenMods)
  240. await GetAvailableMods();
  241. CheckUninstalledMods(mods);
  242. CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
  243. }
  244. private void MainForm_Shown(object sender, EventArgs e)
  245. {
  246. Focus();
  247. }
  248. }
  249. }