|
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Resources;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace TBMM
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- resources = new ResourceManager("TBMM.Localization", Assembly.GetExecutingAssembly());
- Configuration = Configuration.Load();
- }
-
- public Configuration Configuration { get; }
-
- private readonly ResourceManager resources;
- private readonly Dictionary<string, ModInfo> mods = new();
- private readonly ModInfo gcipa = new ModInfo { Author = "modtainers", Name = "GCIPA" };
- private readonly ModInfo tbmm = new ModInfo { Author = "NorbiPeti", Name = "TBMM" };
- private DateTime lastGameUpdateTime;
- private const string defaultInfo = @"
- Techblox Mod Manager
-
- If you click on a mod it will show some info about it. The install instructions there are usually for manual installs.
- To get started, click on a mod and select Install mod. Most mods need TechbloxModdingAPI as well so it'll be installed.
- Then launch Techblox by clicking on the Play button below. Mods are only loaded if you start the game from here.
- This will first download and run the patcher (GCIPA) if needed. If all goes well, after some time a modded Techblox should launch.
-
- 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.
- Until updated versions are released, launch the game without mods through its own launcher.
-
- Disclaimer:
- 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.
-
- 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.
- ";
-
- private async void Form1_Load(object sender, EventArgs e)
- {
- if (mods.Values.All(mod => mod.LatestVersion == null)) //Not (fully) loaded yet
- await LoadEverything(true);
- }
-
- public async Task LoadEverything(bool evenMods)
- {
- modlist.Items.Clear();
- mods.Clear(); //This method may get called twice when ran from the command line
- UpdateButton(installbtn, false);
- modinfobox.Text = defaultInfo;
- if (string.IsNullOrWhiteSpace(Configuration.GamePath) || GetExe() == null)
- {
- Configuration.GamePath = GetGameFolder();
- if (string.IsNullOrWhiteSpace(Configuration.GamePath))
- {
- DialogUtils.ShowWarning(resources.GetString("Game_not_found"), "");
- Configuration.GamePath = SelectGameFolder();
- }
- else
- DialogUtils.ShowInfo(string.Format(resources.GetString("Found_game_at"), Configuration.GamePath), "");
- Configuration.Save();
- }
- if(string.IsNullOrWhiteSpace(Configuration.GamePath))
- {
- status.Text = resources.GetString("Status_Game_not_found");
- return;
- }
- DeleteEmptyPluginsDir(out _, out _);
- await RefreshEverything(evenMods);
- }
-
- private async void playbtn_Click(object sender, EventArgs e)
- {
- if (playbtn.ForeColor == Color.Green) return; //Disabled
- await UpdateAPI();
- await PatchStartGame(); //It will call EndWork();
- }
-
- private void settingsbtn_Click(object sender, EventArgs e)
- {
- if (settingsbtn.ForeColor == Color.Green) return; //Disabled
- var sf = new SettingsForm();
- sf.ShowDialog(this);
- }
-
- private void modlist_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (working) return;
- modinfobox.Clear();
- switch (modlist.SelectedItems.Count)
- {
- case 0:
- modinfobox.Text = defaultInfo;
- UpdateButton(installbtn, false);
- UpdateButton(uninstallbtn, false);
- break;
- case 1:
- default:
- installbtn.Text = "Install mod";
- UpdateButton(installbtn, false);
- UpdateButton(uninstallbtn, false);
- bool install = false, update = false;
- Action<string, Color> addText = (txt, color) =>
- {
- int start = modinfobox.Text.Length;
- modinfobox.AppendText(txt + Environment.NewLine + Environment.NewLine);
- modinfobox.Select(start, txt.Length);
- modinfobox.SelectionColor = color;
- modinfobox.DeselectAll();
- modinfobox.SelectionColor = modinfobox.ForeColor;
- };
- foreach (ListViewItem item in modlist.SelectedItems)
- {
- var mod = mods[item.Name];
- if (modlist.SelectedItems.Count == 1)
- {
- if (mod.Updatable)
- addText("New version available! " + mod.UpdateDetails, Color.Aqua);
- if (mod.Broken)
- addText("Outdated mod! It has been confirmed that the mod is broken on the current version of the game.", Color.Red);
- else if (mod.LastUpdated != default && mod.LastUpdated < lastGameUpdateTime)
- addText("Outdated mod! It may not work properly on the current version of the game.", Color.DarkOrange);
- if (mod.Description != null)
- modinfobox.AppendText(mod.Description.Replace("\n", Environment.NewLine));
- }
- else
- modinfobox.Text = modlist.SelectedItems.Count + " mods selected";
- if (mod.DownloadURL != null && !(mod.LatestVersion <= mod.Version))
- {
- UpdateButton(installbtn, true);
- if (mod.Version != null)
- update = true;
- else
- install = true;
- }
- if (mod.Version != null)
- UpdateButton(uninstallbtn, true);
- }
- if (install && update)
- installbtn.Text = "Install and update mod";
- else if (update)
- installbtn.Text = "Update mod";
- else
- installbtn.Text = "Install mod";
- break;
- }
- }
-
- private async void installbtn_Click(object sender, EventArgs e)
- {
- if (installbtn.ForeColor == Color.Green) return; //Disabled
- if (!BeginWork()) return;
- foreach (ListViewItem item in modlist.SelectedItems)
- {
- var mod = mods[item.Name];
- if (item.Group.Name == "installed" && (mod.DownloadURL == null || mod.LatestVersion <= mod.Version)) continue;
- await InstallMod(mod);
- }
- EndWork(CheckIfPatched());
- }
-
- private void uninstallbtn_Click(object sender, EventArgs e)
- {
- if (uninstallbtn.ForeColor == Color.Green) return; //Disabled
- foreach (ListViewItem item in modlist.SelectedItems)
- {
- if (item.Group.Name != "installed") continue;
- UninstallMod(mods[item.Name]);
- }
- EndWork(CheckIfPatched()); //Update button states
- }
-
- private void findlog_Click(object sender, EventArgs e)
- {
- if (Environment.OSVersion.Platform == PlatformID.Win32NT)
- {
- if (CheckNoExe(out string exe))
- return;
- Process.Start("explorer.exe", $@"/select,{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}Low\Freejam\{exe.Replace(".exe", "")}\Player.log");
- }
- }
-
- private void DeleteEmptyPluginsDir(out bool pexists, out bool dexists)
- {
- string plugins = GamePath("\\Plugins");
- string disabled = GamePath("\\Plugins_Disabled");
- pexists = Directory.Exists(plugins);
- dexists = Directory.Exists(disabled);
- if (pexists && !Directory.EnumerateFiles(plugins).Any())
- {
- Directory.Delete(plugins);
- pexists = false;
- }
- if (dexists && !Directory.EnumerateFiles(disabled).Any())
- {
- Directory.Delete(disabled);
- dexists = false;
- }
- }
-
- private async void refreshbtn_Click(object sender, EventArgs e)
- {
- await RefreshEverything(true);
- }
-
- private async Task RefreshEverything(bool evenMods)
- {
- if (CheckIfPatched() == GameState.Patched) //Set from placeholder & unpatch if game was patched
- HandleGameExit(null, EventArgs.Empty);
- lastGameUpdateTime = GetGameVersionAsDate();
- var mods = GetInstalledMods();
- if (evenMods)
- await GetAvailableMods();
- CheckUninstalledMods(mods);
- CheckIfPatched(); //Check after getting the available mods to show GCIPA updates
- }
-
- private void MainForm_Shown(object sender, EventArgs e)
- {
- Focus();
- }
-
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (e.Cancel) return;
- if (Configuration.KeepPatched || CheckIfPatched(out bool patched) != GameState.InGame || !patched) return;
- if (MessageBox.Show("The game is still running. The mod manager needs to be running until the game closes to restore the game files." +
- " If you proceed you won't be able to play online until you start the mod manager again.\n\n" +
- "Are you sure you want TBMM to exit before the game does?", "Game still running",
- MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
- e.Cancel = true;
- }
-
- private void MainForm_Activated(object sender, EventArgs e)
- {
- CheckIfPatched();
- }
- }
- }
|