using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; namespace TBMM { partial class MainForm { public void UpdateButton(Button button, bool enabled) { if (enabled) { button.ForeColor = Color.Lime; button.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 40, 0); button.FlatAppearance.MouseDownBackColor = Color.Green; } else { button.ForeColor = Color.Green; button.FlatAppearance.MouseOverBackColor = Color.Black; button.FlatAppearance.MouseDownBackColor = Color.Black; } } public string GetGameFolder() { using var key = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Techblox Launcher") ?? Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Techblox Launcher"); string launcherPath = key?.GetValue("DisplayIcon") is string launcherExecutable ? Directory.GetParent(launcherExecutable)?.FullName : null; launcherPath ??= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Techblox Launcher"); string launcherConfig = Path.Combine(launcherPath, "launcher_settings.ini"); if (!File.Exists(launcherConfig)) return null; string path = File.ReadLines(launcherConfig) .FirstOrDefault(line => line.StartsWith("133062..GAME_PATH=")) ?.Substring("133062..GAME_PATH=".Length).Replace("/TBMM/", "/"); if (path != null) path = (path + "StandaloneWindows64").Replace('/', Path.DirectorySeparatorChar); if (path != null && GetExe(path) != null) return path; return null; } public string SelectGameFolder() { var ofd = new OpenFileDialog(); ofd.Filter = "Techblox executable|Techblox.exe|Techblox Preview executable|TechbloxPreview.exe"; ofd.Title = "Game location"; ofd.CheckFileExists = true; ofd.ShowDialog(); return string.IsNullOrWhiteSpace(ofd.FileName) ? null : Directory.GetParent(ofd.FileName)?.FullName; } private (EventHandler, Task) CheckStartGame(string command) { var tcs = new TaskCompletionSource(); return ((sender, e) => { Action act = async () => { if (((sender as Process)?.ExitCode ?? 0) != 0) { status.Text = "Status: Patching failed"; return; } if (CheckIfPatched() == GameState.Patched || unpatched.Checked) if (command != null) { if (sender is Process) //Patched just now CheckCompatibilityAndDisableMods(); await CheckModUpdatesAsync(); Process.Start(command); } else if (Environment.OSVersion.Platform == PlatformID.Win32NT) Process.Start(new ProcessStartInfo(GamePath("\\" + GetExe())) { WorkingDirectory = GamePath("\\") //Mods are only loaded if the working directory is correct }); EndWork(false); tcs.SetResult(null); }; if (InvokeRequired) Invoke(act); else act(); }, tcs.Task); } private void CheckCompatibilityAndDisableMods() { if (!unpatched.Checked && MessageBox.Show("If the game updated just now, some mods may be incompatible or they may work just fine." + " Do you want to try running with mods?" + "\n\nClick Yes to start the game with mods (after a small update or if you just installed TBMM)" + "\nClick No to disable mods before starting the game (after a major update)" + "\n\nYou can always enable/disable mods by launching TBMM.", "Possible incompatibility warning", MessageBoxButtons.YesNo) == DialogResult.No) unpatched.Checked = true; } private async Task CheckModUpdatesAsync() { var updatable = mods.Values.Where(mod => mod.Updatable).ToArray(); if (updatable.Length == 0) return; if (MessageBox.Show("Mod update(s) available!\n\n" + updatable.Select(mod => mod.Name + " " + mod.LatestVersion).Aggregate((a, b) => a + "\n") + "\n\nDo you want to update them now? You can also update later by opening TBMM.", "Update(s) available", MessageBoxButtons.YesNo) == DialogResult.No) return; foreach (var mod in updatable) await InstallMod(mod); MessageBox.Show("Mods updated"); } public WebClient GetClient() { var client = new WebClient(); client.Headers.Clear(); client.Headers[HttpRequestHeader.Accept] = "application/json"; client.BaseAddress = "https://git.exmods.org"; return client; } private bool working = false; /// /// Some simple "locking", only allow one operation at a time /// /// Whether the work can begin public bool BeginWork() { if (working) return false; working = true; UpdateButton(playbtn, false); UpdateButton(installbtn, false); UpdateButton(uninstallbtn, false); UpdateButton(settingsbtn, false); unpatched.Enabled = false; return true; } public void EndWork(bool desc = true) { working = false; UpdateButton(playbtn, true); UpdateButton(settingsbtn, true); if (desc) modlist_SelectedIndexChanged(modlist, null); unpatched.Enabled = true; } /// /// Path must start with \ /// /// /// /// public string GamePath(string path, string gamepath = null) { return ((gamepath ?? Configuration.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar); } public string GetExe(string path = null) { if (File.Exists(GamePath("\\Techblox.exe", path))) return "Techblox.exe"; if (File.Exists(GamePath("\\TechbloxPreview.exe", path))) return "TechbloxPreview.exe"; return null; } private bool CheckNoExe() { return CheckNoExe(out _); } private bool CheckNoExe(out string path) { path = GetExe(); if (path == null) { MessageBox.Show("Techblox not found! Set the correct path in Settings."); return true; } return false; } public DateTime GetGameVersionAsDate() { if (Configuration.GamePath == null) return default; using var fs = File.OpenRead(GamePath("\\TechbloxPreview_Data\\globalgamemanagers")); using var sr = new StreamReader(fs); char[] data = new char[512]; while(!sr.EndOfStream) { Array.Copy(data, 256, data, 0, 256); int read = sr.ReadBlock(data, 256, 256); for (int i = 0; i < data.Length - 11; i++) { if (data[i] == '2') { string date = new string(data, i, 11); if (date.StartsWith("202") && DateTime.TryParse(date, out var ret)) return ret; } } } return default; } } }