Techblox Mod Manager / Launcher
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

307 行
13KB

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