|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using GCMM.Properties;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- 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;
- }
- }
-
- public string GetGameFolder()
- { //TODO
- string libs;
- if (Environment.OSVersion.Platform == PlatformID.Win32NT)
- libs = Settings.Default.SteamConfigFileForAutoLaunch + @"\steamapps\libraryfolders.vdf";
- else
- return null;
- foreach (var line in File.ReadAllLines(libs).Concat(new[] {@"C:\Program Files (x86)\Steam\"}))
- {
- var regex = new Regex("\\t\"\\d+\"\\t\\t\"(.+)\"");
- var match = regex.Match(line);
- if (!match.Success)
- continue;
- string library = match.Groups[1].Value.Replace("\\\\", "\\");
- library += @"\steamapps\common\";
- if (GetExe(library + "Gamecraft") != null)
- return library + "Gamecraft";
- if (GetExe(library + "RobocraftX") != null)
- return library + "RobocraftX";
- }
- return null;
- }
-
- public string SelectGameFolder()
- {
- var ofd = new OpenFileDialog();
- ofd.Filter = "Gamecraft executable|Gamecraft.exe|Gamecraft Preview executable|GamecraftPreview.exe";
- ofd.Title = "Game location";
- ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\steamapps\common\"; //TODO
- ofd.CheckFileExists = true;
- ofd.ShowDialog();
- if (string.IsNullOrWhiteSpace(ofd.FileName))
- return null;
- return Directory.GetParent(ofd.FileName).FullName;
- }
-
- public string SelectSteamConfigFile()
- {
- MessageBox.Show("Please select your Steam config location in the next dialog. It's at Steam\\userdata\\<YourID>\\config\\localconfig.vdf");
- var ofd = new OpenFileDialog();
- ofd.Filter = "Steam config|localconfig.vdf";
- ofd.Title = "Steam config location (Steam\\userdata\\<YourID>\\config\\localconfig.vdf)";
- ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\\userdata\"; //TODO
- ofd.CheckFileExists = true;
- ofd.ShowDialog();
- if (string.IsNullOrWhiteSpace(ofd.FileName))
- return null;
- return ofd.FileName;
- }
-
- private void UpdateSteamConfigToAutoStart(bool autoLaunch)
- {
-
- }
-
- private void CheckStartGame(object sender, EventArgs e)
- {
- Action act = () =>
- {
- if (((sender as Process)?.ExitCode ?? 0) != 0)
- {
- status.Text = "Status: Patching failed";
- return;
- }
- if (CheckIfPatched() == GameState.Patched || unpatched.Checked)
- if (sender is string command)
- Process.Start(command);
- else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
- Process.Start("steam://run/1078000/");
- else
- Process.Start("xdg-open", "steam://run/1078000/");
- };
- if (InvokeRequired)
- Invoke(act);
- else
- act();
- EndWork(false);
- }
-
- public WebClient GetClient()
- {
- var client = new WebClient();
- if (!Settings.Default.UseProxy)
- client.Proxy = null;
- 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 ?? Settings.Default.GamePath) + path).Replace('\\', Path.DirectorySeparatorChar);
- }
-
- public string GetExe(string path = null)
- {
- if (File.Exists(GamePath("\\Gamecraft.exe", path)))
- return "Gamecraft.exe";
- if (File.Exists(GamePath("\\GamecraftPreview.exe", path)))
- return "GamecraftPreview.exe";
- return null;
- }
-
- private bool CheckNoExe()
- {
- return CheckNoExe(out _);
- }
-
- private bool CheckNoExe(out string path)
- {
- path = GetExe();
- if (path == null)
- {
- MessageBox.Show("Gamecraft 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;
- }
- }
- }
|