TBMM/GCMM/MainForm.cs

227 lines
8.5 KiB
C#
Raw Normal View History

2020-06-15 19:11:05 +00:00
using GCMM.Properties;
using Newtonsoft.Json.Linq;
2020-06-15 19:11:05 +00:00
using System;
using System.Diagnostics;
2020-06-15 19:11:05 +00:00
using System.Drawing;
using System.IO;
using System.IO.Compression;
2020-06-15 19:11:05 +00:00
using System.Linq;
using System.Net;
using System.Reflection;
2020-06-15 19:11:05 +00:00
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GCMM
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
modlist.Items.Clear();
UpdateButton(installbtn, false);
modinfobox.Text = "";
if (string.IsNullOrWhiteSpace(Settings.Default.GamePath))
{
Settings.Default.GamePath = GetGameFolder();
if (string.IsNullOrWhiteSpace(Settings.Default.GamePath))
Settings.Default.GamePath = SelectGameFolder();
else
MessageBox.Show("Found game at " + Settings.Default.GamePath);
Settings.Default.Save();
}
if(string.IsNullOrWhiteSpace(Settings.Default.GamePath))
{
status.Text = "Status: Game not found";
return;
}
CheckIfPatched();
GetInstalledMods();
2020-06-15 19:11:05 +00:00
}
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()
{
string libs;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
libs = @"C:\Program Files (x86)\Steam\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 (File.Exists(library + @"Gamecraft\Gamecraft.exe"))
return library + "Gamecraft";
if (File.Exists(library + @"RobocraftX\Gamecraft.exe"))
return library + "RobocraftX";
}
return libs;
}
public string SelectGameFolder()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Gamecraft executable|Gamecraft.exe";
ofd.Title = "Game location";
ofd.InitialDirectory = @"C:\Program Files (x86)\Steam\steamapps\common\";
ofd.CheckFileExists = true;
ofd.ShowDialog();
if (string.IsNullOrWhiteSpace(ofd.FileName))
return null;
return Directory.GetParent(ofd.FileName).FullName;
}
public void GetInstalledMods()
2020-06-15 19:11:05 +00:00
{
foreach (var mod in Directory.GetFiles(Settings.Default.GamePath + @"\Plugins", "*.dll"))
{
try
{
var an = AssemblyName.GetAssemblyName(mod);
if (an.Name == "0Harmony") continue;
modlist.Items.Add(new ListViewItem(new[] { an.Name, an.Version.ToString(), File.GetLastWriteTime(mod).ToString(), "" }, modlist.Groups["installed"]));
}
catch (BadImageFormatException)
{ //Not a .NET assembly
}
}
}
2020-06-15 19:11:05 +00:00
public bool? CheckIfPatched()
{
if (!File.Exists(Settings.Default.GamePath + @"\IPA.exe"))
{
status.Text = "Status: Patcher missing\nInstalling a mod downloads it";
return null;
}
string nopatch = "Status: Unpatched\nClick play";
if (!Directory.Exists(Settings.Default.GamePath + @"\IPA\Backups\Gamecraft"))
{
status.Text = nopatch;
return false;
}
string backup = Directory.EnumerateDirectories(Settings.Default.GamePath + @"\IPA\Backups\Gamecraft").OrderByDescending(s => s).FirstOrDefault();
if (backup == null)
{
status.Text = nopatch;
return false;
}
if (File.GetLastWriteTime(Settings.Default.GamePath + @"\Gamecraft_Data\Managed\Assembly-CSharp.dll")
> //If the file was updated at least a minute after patching
Directory.GetLastWriteTime(backup).AddMinutes(1))
{
status.Text = nopatch;
return false;
}
status.Text = "Status: Patched";
return true;
}
public async void PatchGame()
{
UpdateButton(playbtn, false);
UpdateButton(installbtn, false);
UpdateButton(uninstallbtn, false);
UpdateButton(settingsbtn, false);
if (!CheckIfPatched().HasValue)
{
if (MessageBox.Show("The patcher (GCIPA) is not found. It will be downloaded from https://git.exmods.org/modtainers/GCIPA/releases and ran to patch the game.", "Patcher download needed", MessageBoxButtons.OKCancel)
== DialogResult.Cancel)
return;
string releases = "https://git.exmods.org/api/v1/repos/modtainers/GCIPA/releases";
string url;
this.status.Text = "Status: Patching...";
await Task.Run(() =>
{
using (WebClient client = new WebClient())
{
url = JArray.Parse(client.DownloadString(releases)).First["assets"].First["browser_download_url"].ToString();
client.DownloadFile(url, "IPA.zip");
ZipFile.ExtractToDirectory("IPA.zip", Settings.Default.GamePath);
}
});
}
bool? status = CheckIfPatched();
if (!status.HasValue) //Make sure it actually worked
return;
if (!status.Value)
{
var psi = new ProcessStartInfo(Settings.Default.GamePath + @"\IPA.exe", "Gamecraft.exe --nowait");
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.WorkingDirectory = Settings.Default.GamePath;
psi.CreateNoWindow = true;
var process = Process.Start(psi);
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.EnableRaisingEvents = true;
modinfobox.Text = "";
DataReceivedEventHandler onoutput = (sender, e) =>
{
Invoke((Action)(() => modinfobox.Text += e.Data + Environment.NewLine));
};
process.OutputDataReceived += onoutput;
process.ErrorDataReceived += onoutput;
process.Exited += CheckStartGame;
}
else
CheckStartGame(null, null);
}
private void CheckStartGame(object sender, EventArgs e)
{
Action act = () =>
{
if ((sender as Process).ExitCode != 0)
{
status.Text = "Status: Patching failed";
return;
}
if (CheckIfPatched() ?? false)
Process.Start("steam://run/1078000/");
};
if (InvokeRequired)
Invoke(act);
}
private void playbtn_Click(object sender, EventArgs e)
{
if (playbtn.ForeColor == Color.Green) return; //Disabled
PatchGame();
2020-06-15 19:11:05 +00:00
}
private void settingsbtn_Click(object sender, EventArgs e)
{
if (settingsbtn.ForeColor == Color.Green) return; //Disabled
2020-06-15 19:11:05 +00:00
var sf = new SettingsForm();
sf.ShowDialog(this);
}
}
}