Add support for running from the command line

To be used from Steam
This commit is contained in:
Norbi Peti 2021-01-20 00:49:11 +01:00
parent 890a650093
commit ac6d07df59
7 changed files with 88 additions and 12 deletions

View file

@ -273,6 +273,7 @@
this.Name = "MainForm";
this.Text = "Gamecraft Mod Manager";
this.Load += new System.EventHandler(this.Form1_Load);
this.Shown += new System.EventHandler(this.MainForm_Shown);
this.ResumeLayout(false);
this.PerformLayout();

View file

@ -47,7 +47,12 @@ If you encounter an issue while any mods are installed, report it to us. If you
You may also want to verify the game's files by right clicking the game in Steam and choosing Properties, going to Local files and clicking Verify integrity of game files.
";
private void Form1_Load(object sender, EventArgs e)
private async void Form1_Load(object sender, EventArgs e)
{
await LoadEverything();
}
public async Task LoadEverything()
{
if (Settings.Default.NeedsUpdate)
{
@ -56,6 +61,7 @@ You may also want to verify the game's files by right clicking the game in Steam
Settings.Default.Save();
}
modlist.Items.Clear();
mods.Clear(); //This method may get called twice when ran from the command line
UpdateButton(installbtn, false);
modinfobox.Text = defaultInfo;
if (string.IsNullOrWhiteSpace(Settings.Default.GamePath) || GetExe() == null)
@ -75,7 +81,7 @@ You may also want to verify the game's files by right clicking the game in Steam
DeleteEmptyPluginsDir(out bool pexists, out bool dexists);
if (!pexists && dexists)
unpatched.Checked = true; //It will call the event but that won't do anything
refreshbtn_Click(refreshbtn, null);
await RefreshEverything();
}
private async void playbtn_Click(object sender, EventArgs e)
@ -243,6 +249,11 @@ You may also want to verify the game's files by right clicking the game in Steam
}
private async void refreshbtn_Click(object sender, EventArgs e)
{
await RefreshEverything();
}
private async Task RefreshEverything()
{
CheckIfPatched(); //Set from placeholder
lastGameUpdateTime = await GetLastGameUpdateTime();
@ -265,5 +276,10 @@ You may also want to verify the game's files by right clicking the game in Steam
else
Process.Start("xdg-open", "steam://validate/1078000/");
}
private void MainForm_Shown(object sender, EventArgs e)
{
Focus();
}
}
}

View file

@ -190,7 +190,7 @@ namespace GCMM
}
if (mod.LatestVersion != null && mod.Version != null && mod.Version < mod.LatestVersion)
item.ForeColor = Color.Blue;
else if (mod.LastUpdated < lastGameUpdateTime)
else if (mod.LastUpdated != default && mod.LastUpdated < lastGameUpdateTime)
item.ForeColor = Color.DarkOrange;
else
item.ForeColor = modlist.ForeColor;

View file

@ -66,18 +66,27 @@ namespace GCMM
return GameState.Patched;
}
public async Task PatchStartGame()
public async Task<bool?> PatchStartGame(string command = null)
{
if (!BeginWork()) return;
if (!BeginWork()) return false;
foreach (ListViewItem item in modlist.SelectedItems)
item.Selected = false;
bool? retOpenedWindowShouldStay = null;
void EnsureShown(bool stay)
{
if (!Visible)
{
Show();
retOpenedWindowShouldStay = stay;
}
}
var status = CheckIfPatched();
switch (status)
{
case GameState.NotFound:
MessageBox.Show("Gamecraft not found! Set the correct path in Settings.");
EndWork(false);
return;
return retOpenedWindowShouldStay;
case GameState.NoPatcher:
case GameState.OldPatcher:
{
@ -88,9 +97,10 @@ namespace GCMM
"Patcher download needed", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
EndWork();
return;
return retOpenedWindowShouldStay;
}
this.status.Text = "Status: Patching...";
EnsureShown(false);
using (WebClient client = GetClient())
{
string url = gcipa.DownloadURL;
@ -110,9 +120,10 @@ namespace GCMM
case GameState.NoPatcher: //Make sure it actually worked
case GameState.OldPatcher:
EndWork(false);
return;
return retOpenedWindowShouldStay;
case GameState.Unpatched:
{ //TODO: Wine
EnsureShown(false);
var psi = new ProcessStartInfo(GamePath(@"\IPA.exe"), GetExe() + " --nowait")
{
UseShellExecute = false,
@ -132,13 +143,23 @@ namespace GCMM
};
process.OutputDataReceived += onoutput;
process.ErrorDataReceived += onoutput;
process.Exited += CheckStartGame;
process.Exited += command != null ? (EventHandler) ((sender, e) => StartGameUsingCommand(command)) : CheckStartGame; //target-typed conditional expression - C# 9.0
}
break;
case GameState.Patched:
if (command != null)
StartGameUsingCommand(command);
else
CheckStartGame(null, null);
break;
}
return retOpenedWindowShouldStay;
}
private void StartGameUsingCommand(string command)
{
Process.Start(command);
EndWork(false);
}
public enum GameState

View file

@ -113,6 +113,7 @@ namespace GCMM
UpdateButton(installbtn, false);
UpdateButton(uninstallbtn, false);
UpdateButton(settingsbtn, false);
unpatched.Enabled = false;
return true;
}
@ -123,6 +124,7 @@ namespace GCMM
UpdateButton(settingsbtn, true);
if (desc)
modlist_SelectedIndexChanged(modlist, null);
unpatched.Enabled = true;
}
/// <summary>
@ -172,7 +174,8 @@ namespace GCMM
return default;
return new DateTime(1970, 1, 1).AddSeconds(long.Parse(match.Groups[1].Value));
}*/
return new DateTime(2020, 12, 28);
//return new DateTime(2020, 12, 28);
return default;
}
}
}

View file

@ -12,12 +12,40 @@ namespace GCMM
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
static void Main(string[] args)
{
//Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
DealWithCommandLineAsync(args);
Application.Run();
return;
}
Application.Run(new MainForm());
}
private static async void DealWithCommandLineAsync(string[] args)
{
var form = new MainForm();
await form.LoadEverything();
bool? shouldStay = null;
if (args[0] == "-start" && args.Length > 1)
shouldStay = await form.PatchStartGame(args[1]);
else
MessageBox.Show("Supported options:\n-start <command> - Starts the game using the given command");
if (shouldStay.HasValue)
{
form.FormClosed += (sender, e) => Application.Exit();
if (!shouldStay.Value)
{
await Task.Delay(2000);
form.Close();
}
}
else
Application.Exit();
}
}
}

View file

@ -0,0 +1,7 @@
{
"profiles": {
"GCMM": {
"commandName": "Project"
}
}
}