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.

MainForm.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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();
  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 through the mod manager.
  34. Until updated versions are released, launch the game without mods through its own launcher.
  35. Disclaimer:
  36. 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.
  37. 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 by launching the game through the official launcher before reporting to Freejam.
  38. ";
  39. private async void Form1_Load(object sender, EventArgs e)
  40. {
  41. if (mods.Values.All(mod => mod.LatestVersion == null)) //Not (fully) loaded yet
  42. await LoadEverything(true);
  43. }
  44. public async Task LoadEverything(bool evenMods)
  45. {
  46. modlist.Items.Clear();
  47. mods.Clear(); //This method may get called twice when ran from the command line
  48. UpdateButton(installbtn, false);
  49. modinfobox.Text = defaultInfo;
  50. if (string.IsNullOrWhiteSpace(Configuration.GamePath) || GetExe() == null)
  51. {
  52. Configuration.GamePath = GetGameFolder();
  53. if (string.IsNullOrWhiteSpace(Configuration.GamePath))
  54. {
  55. DialogUtils.ShowWarning(resources.GetString("Game_not_found"), "");
  56. Configuration.GamePath = SelectGameFolder();
  57. }
  58. else
  59. DialogUtils.ShowInfo(string.Format(resources.GetString("Found_game_at"), Configuration.GamePath), "");
  60. Configuration.Save();
  61. }
  62. if(string.IsNullOrWhiteSpace(Configuration.GamePath))
  63. {
  64. status.Text = resources.GetString("Status_Game_not_found");
  65. return;
  66. }
  67. DeleteEmptyPluginsDir(out _, out _);
  68. await RefreshEverything(evenMods);
  69. }
  70. private async void playbtn_Click(object sender, EventArgs e)
  71. {
  72. if (playbtn.ForeColor == Color.Green) return; //Disabled
  73. await UpdateAPI();
  74. await PatchStartGame(); //It will call EndWork();
  75. }
  76. private void settingsbtn_Click(object sender, EventArgs e)
  77. {
  78. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  79. var sf = new SettingsForm();
  80. sf.ShowDialog(this);
  81. }
  82. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  83. {
  84. if (working) return;
  85. modinfobox.Clear();
  86. switch (modlist.SelectedItems.Count)
  87. {
  88. case 0:
  89. modinfobox.Text = defaultInfo;
  90. UpdateButton(installbtn, false);
  91. UpdateButton(uninstallbtn, false);
  92. break;
  93. case 1:
  94. default:
  95. installbtn.Text = "Install mod";
  96. UpdateButton(installbtn, false);
  97. UpdateButton(uninstallbtn, false);
  98. bool install = false, update = false;
  99. Action<string, Color> addText = (txt, color) =>
  100. {
  101. int start = modinfobox.Text.Length;
  102. modinfobox.AppendText(txt + Environment.NewLine + Environment.NewLine);
  103. modinfobox.Select(start, txt.Length);
  104. modinfobox.SelectionColor = color;
  105. modinfobox.DeselectAll();
  106. modinfobox.SelectionColor = modinfobox.ForeColor;
  107. };
  108. foreach (ListViewItem item in modlist.SelectedItems)
  109. {
  110. var mod = mods[item.Name];
  111. if (modlist.SelectedItems.Count == 1)
  112. {
  113. if (mod.Updatable)
  114. addText("New version available! " + mod.UpdateDetails, Color.Aqua);
  115. if (mod.Broken)
  116. addText("Outdated mod! It has been confirmed that the mod is broken on the current version of the game.", Color.Red);
  117. else if (mod.LastUpdated != default && mod.LastUpdated < lastGameUpdateTime)
  118. addText("Outdated mod! It may not work properly on the current version of the game.", Color.DarkOrange);
  119. if (mod.Description != null)
  120. modinfobox.AppendText(mod.Description.Replace("\n", Environment.NewLine));
  121. }
  122. else
  123. modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
  124. if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
  125. {
  126. UpdateButton(installbtn, true);
  127. if (mod.Version != null)
  128. update = true;
  129. else
  130. install = true;
  131. }
  132. if (mod.Version != null)
  133. UpdateButton(uninstallbtn, true);
  134. }
  135. if (install && update)
  136. installbtn.Text = "Install and update mod";
  137. else if (update)
  138. installbtn.Text = "Update mod";
  139. else
  140. installbtn.Text = "Install mod";
  141. break;
  142. }
  143. }
  144. private async void installbtn_Click(object sender, EventArgs e)
  145. {
  146. if (installbtn.ForeColor == Color.Green) return; //Disabled
  147. if (!BeginWork()) return;
  148. foreach (ListViewItem item in modlist.SelectedItems)
  149. {
  150. var mod = mods[item.Name];
  151. if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
  152. await InstallMod(mod);
  153. }
  154. EndWork(CheckIfPatched());
  155. }
  156. private void uninstallbtn_Click(object sender, EventArgs e)
  157. {
  158. if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
  159. foreach (ListViewItem item in modlist.SelectedItems)
  160. {
  161. if (item.Group.Name != "installed") continue;
  162. UninstallMod(mods[item.Name]);
  163. }
  164. EndWork(CheckIfPatched()); //Update button states
  165. }
  166. private void findlog_Click(object sender, EventArgs e)
  167. {
  168. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  169. {
  170. if (CheckNoExe(out string exe))
  171. return;
  172. Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
  173. }
  174. }
  175. private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  176. {
  177. string plugins = GamePath("\\Plugins");
  178. string disabled = GamePath("\\Plugins_Disabled");
  179. pexists = Directory.Exists(plugins);
  180. dexists = Directory.Exists(disabled);
  181. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  182. {
  183. Directory.Delete(plugins);
  184. pexists = false;
  185. }
  186. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  187. {
  188. Directory.Delete(disabled);
  189. dexists = false;
  190. }
  191. }
  192. private async void refreshbtn_Click(object sender, EventArgs e)
  193. {
  194. await RefreshEverything(true);
  195. }
  196. private async Task RefreshEverything(bool evenMods)
  197. {
  198. if (CheckIfPatched() == GameState.Patched) //Set from placeholder & unpatch if game was patched
  199. HandleGameExit(null, EventArgs.Empty);
  200. lastGameUpdateTime = GetGameVersionAsDate();
  201. var mods = GetInstalledMods();
  202. if (evenMods)
  203. await GetAvailableMods();
  204. CheckUninstalledMods(mods);
  205. CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
  206. }
  207. private void MainForm_Shown(object sender, EventArgs e)
  208. {
  209. Focus();
  210. }
  211. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  212. {
  213. if (e.Cancel) return;
  214. if (Configuration.KeepPatched || CheckIfPatched(out bool patched) != GameState.InGame || !patched) return;
  215. if (MessageBox.Show("The game is still running. The mod manager needs to be running until the game closes to restore the game files." +
  216. " If you proceed you won't be able to play online until you start the mod manager again.\n\n" +
  217. "Are you sure you want TBMM to exit before the game does?", "Game still running",
  218. MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
  219. e.Cancel = true;
  220. }
  221. private void MainForm_Activated(object sender, EventArgs e)
  222. {
  223. CheckIfPatched();
  224. }
  225. }
  226. }