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; namespace GCMM { 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; } } private bool EnableDisableAutoPatching(bool enable) { string launcherConfig = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Techblox Launcher", "launcher_settings.ini"); bool gamePathSet = false; if (File.Exists(launcherConfig)) { string[] lines = File.ReadLines(launcherConfig).Select(line => { if (line.StartsWith("133062..GAME_PATH=") && line.Contains('/')) { gamePathSet = true; return $"{line.Replace("/TBMM/", "/")}{(enable ? "TBMM/" : "")}"; } return line; }).ToArray(); //Need to close the file File.WriteAllLines(launcherConfig, lines); } return gamePathSet; } //TODO: Setting the game path might not be a good idea because of updates... public void EnableDisableAutoPatchingWithDialog(bool enable) { if (EnableDisableAutoPatching(enable)) { DialogUtils.ShowInfo(resources.GetString("Change_launch_settings_done"), resources.GetString("Change_launch_settings_title")); Configuration.AutoPatch = enable ? AutoPatchingState.Enabled : AutoPatchingState.Disabled; } else DialogUtils.ShowError(resources.GetString("Change_launch_settings_error"), resources.GetString("Change_launch_settings_title")); } public string GetGameFolder() { string launcherConfig = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Techblox Launcher", "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/", "/") + "StandaloneWindows64"; 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); } 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 GCMM)" + "\nClick No to disable mods before starting the game (after a major update)" + "\n\nYou can always enable/disable mods by launching GCMM.", "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 GCMM.", "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 async Task GetLastGameUpdateTime() { /*using (var client = GetClient()) { string html = await client.DownloadStringTaskAsync("https://api.steamcmd.net/v1/info/1078000"); var regex = new Regex("timeupdated:[^<]*([^<]*)"); var match = regex.Match(html); if (!match.Success) return default; return new DateTime(1970, 1, 1).AddSeconds(long.Parse(match.Groups[1].Value)); }*/ //return new DateTime(2020, 12, 28); return default; } } }