|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- 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.Substring(0, Math.Max(line.LastIndexOf("TBMM", StringComparison.Ordinal), line.Length))}{(enable ? "TBMM/" : "")}";
- }
-
- return line;
- }).ToArray(); //Need to close the file
- File.WriteAllLines(launcherConfig, lines);
- }
-
- return gamePathSet;
- }
-
- 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 File.ReadLines(launcherConfig).FirstOrDefault(line => line.StartsWith("133062..GAME_PATH="))
- ?.Substring("133062..GAME_PATH=".Length);
- 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<object>();
- 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;
- /// <summary>
- /// Some simple "locking", only allow one operation at a time
- /// </summary>
- /// <returns>Whether the work can begin</returns>
- 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;
- }
-
- /// <summary>
- /// Path must start with \
- /// </summary>
- /// <param name="path"></param>
- /// <param name="gamepath"></param>
- /// <returns></returns>
- 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<DateTime> GetLastGameUpdateTime()
- {
- /*using (var client = GetClient())
- {
- string html = await client.DownloadStringTaskAsync("https://api.steamcmd.net/v1/info/1078000");
- var regex = new Regex("<i>timeupdated:</i>[^<]*<b>([^<]*)</b>");
- 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;
- }
- }
- }
|