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.

234 lines
10KB

  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 _, out _);
  70. await RefreshEverything(evenMods);
  71. }
  72. private async void playbtn_Click(object sender, EventArgs e)
  73. {
  74. if (playbtn.ForeColor == Color.Green) return; //Disabled
  75. await UpdateAPI();
  76. await PatchStartGame(); //It will call EndWork();
  77. }
  78. private void settingsbtn_Click(object sender, EventArgs e)
  79. {
  80. if (settingsbtn.ForeColor == Color.Green) return; //Disabled
  81. var sf = new SettingsForm();
  82. sf.ShowDialog(this);
  83. }
  84. private void modlist_SelectedIndexChanged(object sender, EventArgs e)
  85. {
  86. if (working) return;
  87. modinfobox.Clear();
  88. switch (modlist.SelectedItems.Count)
  89. {
  90. case 0:
  91. modinfobox.Text = defaultInfo;
  92. UpdateButton(installbtn, false);
  93. UpdateButton(uninstallbtn, false);
  94. break;
  95. case 1:
  96. default:
  97. installbtn.Text = "Install mod";
  98. UpdateButton(installbtn, false);
  99. UpdateButton(uninstallbtn, false);
  100. bool install = false, update = false;
  101. Action<string, Color> addText = (txt, color) =>
  102. {
  103. int start = modinfobox.Text.Length;
  104. modinfobox.AppendText(txt + Environment.NewLine + Environment.NewLine);
  105. modinfobox.Select(start, txt.Length);
  106. modinfobox.SelectionColor = color;
  107. modinfobox.DeselectAll();
  108. modinfobox.SelectionColor = modinfobox.ForeColor;
  109. };
  110. foreach (ListViewItem item in modlist.SelectedItems)
  111. {
  112. var mod = mods[item.Name];
  113. if (modlist.SelectedItems.Count == 1)
  114. {
  115. if (mod.Updatable)
  116. addText("New version available! " + mod.UpdateDetails, Color.Aqua);
  117. if (mod.Broken)
  118. addText("Outdated mod! It has been confirmed that the mod is broken on the current version of the game.", Color.Red);
  119. else 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. }
  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(CheckIfPatched());
  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(CheckIfPatched()); //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 DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
  178. {
  179. string plugins = GamePath("\\Plugins");
  180. string disabled = GamePath("\\Plugins_Disabled");
  181. pexists = Directory.Exists(plugins);
  182. dexists = Directory.Exists(disabled);
  183. if (pexists && !Directory.EnumerateFiles(plugins).Any())
  184. {
  185. Directory.Delete(plugins);
  186. pexists = false;
  187. }
  188. if (dexists && !Directory.EnumerateFiles(disabled).Any())
  189. {
  190. Directory.Delete(disabled);
  191. dexists = false;
  192. }
  193. }
  194. private async void refreshbtn_Click(object sender, EventArgs e)
  195. {
  196. await RefreshEverything(true);
  197. }
  198. private async Task RefreshEverything(bool evenMods)
  199. {
  200. if (CheckIfPatched() == GameState.Patched) //Set from placeholder
  201. HandleGameExit(null, EventArgs.Empty);
  202. lastGameUpdateTime = GetGameVersionAsDate();
  203. var mods = GetInstalledMods();
  204. if (evenMods)
  205. await GetAvailableMods();
  206. CheckUninstalledMods(mods);
  207. CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
  208. }
  209. private void MainForm_Shown(object sender, EventArgs e)
  210. {
  211. Focus();
  212. }
  213. }
  214. }