LANCommander/LANCommander.PowerShell/Cmdlets/Install-Game.cs

133 lines
4.3 KiB
C#
Raw Permalink Normal View History

2023-11-16 07:01:17 +00:00
using LANCommander.SDK;
2023-11-16 07:44:21 +00:00
using LANCommander.SDK.Helpers;
using LANCommander.SDK.PowerShell;
2023-11-16 07:01:17 +00:00
using System;
using System.Diagnostics;
using System.IO;
2023-11-16 07:01:17 +00:00
using System.Linq;
using System.Management.Automation;
using System.Windows.Forms;
namespace LANCommander.PowerShell.Cmdlets
{
2023-11-16 18:04:26 +00:00
[Cmdlet(VerbsLifecycle.Install, "Game")]
2023-11-16 07:01:17 +00:00
[OutputType(typeof(string))]
2023-11-16 21:21:50 +00:00
public class InstallGameCmdlet : Cmdlet
2023-11-16 07:01:17 +00:00
{
[Parameter(Mandatory = true)]
public Client Client { get; set; }
[Parameter(Mandatory = true)]
public Guid Id { get; set; }
[Parameter(Mandatory = false)]
public string InstallDirectory { get; set; } = "C:\\Games";
protected override void ProcessRecord()
{
var gameManager = new GameManager(Client, InstallDirectory);
var game = Client.GetGame(Id);
var progress = new ProgressRecord(1, $"Installing {game.Title}", "Progress:");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
gameManager.OnArchiveExtractionProgress += (long position, long length) =>
{
// Only update a max of every 500ms
if (stopwatch.ElapsedMilliseconds > 500)
{
progress.PercentComplete = (int)Math.Ceiling((position / (decimal)length) * 100);
WriteProgress(progress);
stopwatch.Restart();
}
};
2023-11-16 07:44:21 +00:00
var installDirectory = gameManager.Install(Id);
2023-11-16 07:01:17 +00:00
stopwatch.Stop();
2023-11-16 07:44:21 +00:00
RunInstallScript(installDirectory);
RunNameChangeScript(installDirectory);
RunKeyChangeScript(installDirectory);
WriteObject(installDirectory);
2023-11-16 07:44:21 +00:00
}
private int RunInstallScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(path))
{
var script = new PowerShellScript();
2023-11-16 07:44:21 +00:00
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", InstallDirectory);
script.AddVariable("ServerAddress", Client.BaseUrl);
2023-11-16 07:44:21 +00:00
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install));
2023-11-16 07:44:21 +00:00
return script.Execute();
}
return 0;
2023-11-16 07:44:21 +00:00
}
private int RunNameChangeScript(string installDirectory)
{
var user = Client.GetProfile();
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
2023-11-16 07:44:21 +00:00
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", InstallDirectory);
script.AddVariable("ServerAddress", Client.BaseUrl);
script.AddVariable("OldPlayerAlias", "");
script.AddVariable("NewPlayerAlias", user.UserName);
2023-11-16 07:44:21 +00:00
script.UseFile(path);
2023-11-16 07:44:21 +00:00
return script.Execute();
}
return 0;
2023-11-16 07:44:21 +00:00
}
private int RunKeyChangeScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
var key = Client.GetAllocatedKey(manifest.Id);
2023-11-16 07:44:21 +00:00
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", InstallDirectory);
script.AddVariable("ServerAddress", Client.BaseUrl);
script.AddVariable("AllocatedKey", key);
2023-11-16 07:44:21 +00:00
script.UseFile(path);
2023-11-16 07:44:21 +00:00
return script.Execute();
}
2023-11-16 07:44:21 +00:00
return 0;
2023-11-16 07:01:17 +00:00
}
}
}