Only execute scripts if they exists on the disk

pull/33/head
Pat Hartl 2023-11-16 14:06:40 -06:00
parent 986fb87db1
commit a9f3b7a39d
3 changed files with 95 additions and 52 deletions

View File

@ -7,6 +7,7 @@ using Playnite.SDK.Models;
using Playnite.SDK.Plugins; using Playnite.SDK.Plugins;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
namespace LANCommander.PlaynitePlugin namespace LANCommander.PlaynitePlugin
@ -139,6 +140,10 @@ namespace LANCommander.PlaynitePlugin
private int RunInstallScript(string installDirectory) private int RunInstallScript(string installDirectory)
{ {
var manifest = ManifestHelper.Read(installDirectory); var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(path))
{
var script = new PowerShellScript(); var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("InstallDirectory", installDirectory);
@ -151,9 +156,16 @@ namespace LANCommander.PlaynitePlugin
return script.Execute(); return script.Execute();
} }
return 0;
}
private int RunNameChangeScript(string installDirectory) private int RunNameChangeScript(string installDirectory)
{ {
var manifest = ManifestHelper.Read(installDirectory); var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange);
if (File.Exists(path))
{
var script = new PowerShellScript(); var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("InstallDirectory", installDirectory);
@ -163,14 +175,21 @@ namespace LANCommander.PlaynitePlugin
script.AddVariable("OldPlayerAlias", ""); script.AddVariable("OldPlayerAlias", "");
script.AddVariable("NewPlayerAlias", Plugin.Settings.PlayerName); script.AddVariable("NewPlayerAlias", Plugin.Settings.PlayerName);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange)); script.UseFile(path);
return script.Execute(); return script.Execute();
} }
return 0;
}
private int RunKeyChangeScript(string installDirectory) private int RunKeyChangeScript(string installDirectory)
{ {
var manifest = ManifestHelper.Read(installDirectory); var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange);
if (File.Exists(path))
{
var script = new PowerShellScript(); var script = new PowerShellScript();
var key = Plugin.LANCommanderClient.GetAllocatedKey(manifest.Id); var key = Plugin.LANCommanderClient.GetAllocatedKey(manifest.Id);
@ -181,9 +200,12 @@ namespace LANCommander.PlaynitePlugin
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress); script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.AddVariable("AllocatedKey", key); script.AddVariable("AllocatedKey", key);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange)); script.UseFile(path);
return script.Execute(); return script.Execute();
} }
return 0;
}
} }
} }

View File

@ -538,6 +538,10 @@ namespace LANCommander.PlaynitePlugin
private int RunInstallScript(string installDirectory) private int RunInstallScript(string installDirectory)
{ {
var manifest = ManifestHelper.Read(installDirectory); var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(path))
{
var script = new PowerShellScript(); var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("InstallDirectory", installDirectory);
@ -545,14 +549,21 @@ namespace LANCommander.PlaynitePlugin
script.AddVariable("DefaultInstallDirectory", Settings.InstallDirectory); script.AddVariable("DefaultInstallDirectory", Settings.InstallDirectory);
script.AddVariable("ServerAddress", Settings.ServerAddress); script.AddVariable("ServerAddress", Settings.ServerAddress);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install)); script.UseFile(path);
return script.Execute(); return script.Execute();
} }
return 0;
}
private int RunNameChangeScript(string installDirectory, string oldPlayerAlias, string newPlayerAlias) private int RunNameChangeScript(string installDirectory, string oldPlayerAlias, string newPlayerAlias)
{ {
var manifest = ManifestHelper.Read(installDirectory); var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange);
if (File.Exists(path))
{
var script = new PowerShellScript(); var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory); script.AddVariable("InstallDirectory", installDirectory);
@ -562,14 +573,21 @@ namespace LANCommander.PlaynitePlugin
script.AddVariable("OldPlayerAlias", oldPlayerAlias); script.AddVariable("OldPlayerAlias", oldPlayerAlias);
script.AddVariable("NewPlayerAlias", newPlayerAlias); script.AddVariable("NewPlayerAlias", newPlayerAlias);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange)); script.UseFile(path);
return script.Execute(); return script.Execute();
} }
return 0;
}
private int RunKeyChangeScript(string installDirectory, string key = "") private int RunKeyChangeScript(string installDirectory, string key = "")
{ {
var manifest = ManifestHelper.Read(installDirectory); var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange);
if (File.Exists(path))
{
var script = new PowerShellScript(); var script = new PowerShellScript();
if (String.IsNullOrEmpty(key)) if (String.IsNullOrEmpty(key))
@ -581,9 +599,12 @@ namespace LANCommander.PlaynitePlugin
script.AddVariable("ServerAddress", Settings.ServerAddress); script.AddVariable("ServerAddress", Settings.ServerAddress);
script.AddVariable("AllocatedKey", key); script.AddVariable("AllocatedKey", key);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange)); script.UseFile(path);
return script.Execute(); return script.Execute();
} }
return 0;
}
} }
} }

View File

@ -31,7 +31,7 @@ namespace LANCommander.PlaynitePlugin
{ {
var scriptPath = ScriptHelper.GetScriptFilePath(Game.InstallDirectory, SDK.Enums.ScriptType.Uninstall); var scriptPath = ScriptHelper.GetScriptFilePath(Game.InstallDirectory, SDK.Enums.ScriptType.Uninstall);
if (!String.IsNullOrEmpty(scriptPath)) if (!String.IsNullOrEmpty(scriptPath) && File.Exists(scriptPath))
{ {
var manifest = ManifestHelper.Read(Game.InstallDirectory); var manifest = ManifestHelper.Read(Game.InstallDirectory);
var script = new PowerShellScript(); var script = new PowerShellScript();