Move script path helpers to ScriptHelper. Execute post-install scripts in Playnite extension.

pull/33/head
Pat Hartl 2023-11-15 23:42:54 -06:00
parent 29dcebb70f
commit bf2c9ea45a
3 changed files with 55 additions and 14 deletions

View File

@ -1,6 +1,7 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.Models;
using LANCommander.SDK.PowerShell;
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
@ -116,6 +117,9 @@ namespace LANCommander.PlaynitePlugin
InstallDirectory = installDirectory,
};
RunInstallScript(installDirectory);
RunNameChangeScript(installDirectory);
InvokeOnInstalled(new GameInstalledEventArgs(installInfo));
}
else if (result.Canceled)
@ -130,5 +134,37 @@ namespace LANCommander.PlaynitePlugin
else if (result.Error != null)
throw result.Error;
}
private int RunInstallScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Plugin.Settings.InstallDirectory);
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install));
return script.Execute();
}
private int RunNameChangeScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Plugin.Settings.InstallDirectory);
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.AddVariable("OldPlayerAlias", "");
script.AddVariable("NewPlayerAlias", Plugin.Settings.PlayerName);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange));
return script.Execute();
}
}
}

View File

@ -91,20 +91,6 @@ namespace LANCommander.SDK
ScriptHelper.SaveScript(game, ScriptType.NameChange);
ScriptHelper.SaveScript(game, ScriptType.KeyChange);
try
{
PowerShellRuntime.RunScript(game, ScriptType.Install);
PowerShellRuntime.RunScript(game, ScriptType.NameChange, /* Plugin.Settings.PlayerName */ "");
var key = Client.GetAllocatedKey(game.Id);
PowerShellRuntime.RunScript(game, ScriptType.KeyChange, $"\"{key}\"");
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not execute post-install scripts");
}
return result.Directory;
}

View File

@ -55,5 +55,24 @@ namespace LANCommander.SDK.Helpers
File.WriteAllText(filename, script.Contents);
}
public static string GetScriptFilePath(Game game, ScriptType type)
{
return GetScriptFilePath(game.InstallDirectory, type);
}
public static string GetScriptFilePath(string installDirectory, ScriptType type)
{
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
{ ScriptType.Install, "_install.ps1" },
{ ScriptType.Uninstall, "_uninstall.ps1" },
{ ScriptType.NameChange, "_changename.ps1" },
{ ScriptType.KeyChange, "_changekey.ps1" }
};
var filename = filenames[type];
return Path.Combine(installDirectory, filename);
}
}
}