From 81bad5e0325d31191e65971e8abf943bce7f1922 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 15 Jan 2023 04:56:56 -0600 Subject: [PATCH] Started basic implementation of allowing name/key changes in UI --- .../InstallController.cs | 12 +--- .../LANCommanderLibraryPlugin.cs | 55 +++++++++++++++++++ .../PowerShellRuntime.cs | 23 +++++--- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/LANCommander.Playnite.Extension/InstallController.cs b/LANCommander.Playnite.Extension/InstallController.cs index cf83a09..da2b5ea 100644 --- a/LANCommander.Playnite.Extension/InstallController.cs +++ b/LANCommander.Playnite.Extension/InstallController.cs @@ -187,17 +187,7 @@ namespace LANCommander.PlaynitePlugin if (script.RequiresAdmin) script.Contents = "# Requires Admin" + "\r\n\r\n" + script.Contents; - Dictionary filenames = new Dictionary() { - { ScriptType.Install, "_install.ps1" }, - { ScriptType.Uninstall, "_uninstall.ps1" }, - { ScriptType.NameChange, "_changename.ps1" }, - { ScriptType.KeyChange, "_changekey.ps1" } - }; - - if (!filenames.ContainsKey(type)) - return; - - var filename = Path.Combine(installationDirectory, filenames[type]); + var filename = PowerShellRuntime.GetScriptFilePath(PlayniteGame, type); if (File.Exists(filename)) File.Delete(filename); diff --git a/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs b/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs index d88a998..df1a693 100644 --- a/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs +++ b/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs @@ -21,6 +21,7 @@ namespace LANCommander.PlaynitePlugin public static readonly ILogger Logger = LogManager.GetLogger(); internal LANCommanderSettingsViewModel Settings { get; set; } internal LANCommanderClient LANCommander { get; set; } + internal PowerShellRuntime PowerShellRuntime { get; set; } public override Guid Id { get; } = Guid.Parse("48e1bac7-e0a0-45d7-ba83-36f5e9e959fc"); public override string Name => "LANCommander"; @@ -40,6 +41,8 @@ namespace LANCommander.PlaynitePlugin AccessToken = Settings.AccessToken, RefreshToken = Settings.RefreshToken, }; + + PowerShellRuntime = new PowerShellRuntime(); } public override IEnumerable GetGames(LibraryGetGamesArgs args) @@ -124,6 +127,58 @@ namespace LANCommander.PlaynitePlugin yield return new LANCommanderUninstallController(this, args.Game); } + public override IEnumerable GetGameMenuItems(GetGameMenuItemsArgs args) + { + if (args.Games.Count == 1 && args.Games.First().IsInstalled && !String.IsNullOrWhiteSpace(args.Games.First().InstallDirectory)) + { + var nameChangeScriptPath = PowerShellRuntime.GetScriptFilePath(args.Games.First(), SDK.Enums.ScriptType.NameChange); + var keyChangeScriptPath = PowerShellRuntime.GetScriptFilePath(args.Games.First(), SDK.Enums.ScriptType.KeyChange); + + if (File.Exists(nameChangeScriptPath)) + yield return new GameMenuItem + { + Description = "Change Player Name", + Action = (nameChangeArgs) => + { + PowerShellRuntime.RunScript(nameChangeArgs.Games.First(), SDK.Enums.ScriptType.NameChange); + } + }; + + if (File.Exists(keyChangeScriptPath)) + yield return new GameMenuItem + { + Description = "Change Game Key", + Action = (keyChangeArgs) => + { + PowerShellRuntime.RunScript(keyChangeArgs.Games.First(), SDK.Enums.ScriptType.KeyChange); + } + }; + } + } + + // To add new main menu items override GetMainMenuItems + public override IEnumerable GetMainMenuItems(GetMainMenuItemsArgs args) + { + yield return new MainMenuItem + { + Description = "Change Player Name (All Games)", + Action = (args2) => + { + var result = PlayniteApi.Dialogs.SelectString("Enter your new player name. This will change your name across all installed games!", "Enter Name", ""); + + if (result.Result == true) + { + var games = PlayniteApi.Database.Games.Where(g => g.IsInstalled).ToList(); + + foreach (var game in games) + { + PowerShellRuntime.RunScript(game, SDK.Enums.ScriptType.NameChange); + } + } + } + }; + } + public override ISettings GetSettings(bool firstRunSettings) { return Settings; diff --git a/LANCommander.Playnite.Extension/PowerShellRuntime.cs b/LANCommander.Playnite.Extension/PowerShellRuntime.cs index a78a13e..050bc28 100644 --- a/LANCommander.Playnite.Extension/PowerShellRuntime.cs +++ b/LANCommander.Playnite.Extension/PowerShellRuntime.cs @@ -35,14 +35,7 @@ namespace LANCommander.PlaynitePlugin public void RunScript(Game game, ScriptType type) { - Dictionary filenames = new Dictionary() { - { ScriptType.Install, "_install.ps1" }, - { ScriptType.Uninstall, "_uninstall.ps1" }, - { ScriptType.NameChange, "_changename.ps1" }, - { ScriptType.KeyChange, "_changekey.ps1" } - }; - - var path = Path.Combine(game.InstallDirectory, filenames[type]); + var path = GetScriptFilePath(game, type); if (File.Exists(path)) { @@ -54,5 +47,19 @@ namespace LANCommander.PlaynitePlugin RunScript(path); } } + + public static string GetScriptFilePath(Game game, ScriptType type) + { + Dictionary filenames = new Dictionary() { + { ScriptType.Install, "_install.ps1" }, + { ScriptType.Uninstall, "_uninstall.ps1" }, + { ScriptType.NameChange, "_changename.ps1" }, + { ScriptType.KeyChange, "_changekey.ps1" } + }; + + var filename = filenames[type]; + + return Path.Combine(game.InstallDirectory, filename); + } } }