Techblox Mod Manager / Launcher
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

286 satır
12KB

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