From ee35c9bd137f67633e9f58a009b18187f6ca758b Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Thu, 19 Jan 2023 20:02:34 -0600 Subject: [PATCH] Allow user to change player name across all installed games --- .../LANCommanderLibraryPlugin.cs | 56 ++++++++++++++--- .../PowerShellRuntime.cs | 62 ++++++++++++++++++- 2 files changed, 106 insertions(+), 12 deletions(-) diff --git a/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs b/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs index d88b6bf..1b76050 100644 --- a/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs +++ b/LANCommander.Playnite.Extension/LANCommanderLibraryPlugin.cs @@ -6,6 +6,7 @@ using Playnite.SDK.Plugins; using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.IO; using System.Linq; using System.Net.NetworkInformation; @@ -13,6 +14,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Media; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; using PN = Playnite; @@ -219,17 +221,25 @@ namespace LANCommander.PlaynitePlugin 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", ""); + ShowNameChangeWindow(); + } + }; + } - 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 IEnumerable GetTopPanelItems() + { + yield return new TopPanelItem + { + Title = "Click to change your name (All Games)", + Icon = new TextBlock + { + Text = Settings.PlayerName, + FontSize = 16, + FontFamily = ResourceProvider.GetResource("FontIcoFont") as FontFamily, + Padding = new Thickness(10, 0, 10, 0), + }, + Activated = () => { + ShowNameChangeWindow(); } }; } @@ -244,6 +254,32 @@ namespace LANCommander.PlaynitePlugin return new LANCommanderSettingsView(this); } + public void ShowNameChangeWindow() + { + var result = PlayniteApi.Dialogs.SelectString("Enter your new player name. This will change your name across all installed games!", "Enter Name", Settings.PlayerName); + + if (result.Result == true) + { + // Check to make sure they're staying in ASCII encoding + if (String.IsNullOrEmpty(result.SelectedString) || !result.SelectedString.All(c => c > 127)) + { + PlayniteApi.Dialogs.ShowErrorMessage("The name you supplied is invalid. Try again."); + + ShowNameChangeWindow(); + } + else + { + Settings.PlayerName = result.SelectedString; + + SavePluginSettings(Settings); + + var games = PlayniteApi.Database.Games.Where(g => g.IsInstalled).ToList(); + + PowerShellRuntime.RunScripts(games, SDK.Enums.ScriptType.NameChange, Settings.PlayerName); + } + } + } + public Window ShowAuthenticationWindow() { Window window = null; diff --git a/LANCommander.Playnite.Extension/PowerShellRuntime.cs b/LANCommander.Playnite.Extension/PowerShellRuntime.cs index ba92224..22e1dc0 100644 --- a/LANCommander.Playnite.Extension/PowerShellRuntime.cs +++ b/LANCommander.Playnite.Extension/PowerShellRuntime.cs @@ -18,8 +18,6 @@ namespace LANCommander.PlaynitePlugin var process = new Process(); process.StartInfo.FileName = "powershell.exe"; process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{path}"""; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; if (arguments != null) process.StartInfo.Arguments += " " + arguments; @@ -44,6 +42,38 @@ namespace LANCommander.PlaynitePlugin process.WaitForExit(); } + public void RunScriptsAsAdmin(IEnumerable paths, string arguments = null) + { + // Concatenate scripts + var sb = new StringBuilder(); + + foreach (var path in paths) + { + var contents = File.ReadAllText(path); + + sb.AppendLine(contents); + } + + if (sb.Length > 0) + { + var scriptPath = Path.GetTempFileName(); + + File.WriteAllText(scriptPath, sb.ToString()); + + var process = new Process(); + process.StartInfo.FileName = "powershell.exe"; + process.StartInfo.UseShellExecute = true; + process.StartInfo.Verb = "runas"; + process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{scriptPath}"""; + + if (arguments != null) + process.StartInfo.Arguments += " " + arguments; + + process.Start(); + process.WaitForExit(); + } + } + public void RunScript(Game game, ScriptType type, string arguments = null) { var path = GetScriptFilePath(game, type); @@ -59,6 +89,34 @@ namespace LANCommander.PlaynitePlugin } } + public void RunScripts(IEnumerable games, ScriptType type, string arguments = null) + { + List scripts = new List(); + List adminScripts = new List(); + + foreach (var game in games) + { + var path = GetScriptFilePath(game, type); + + if (!File.Exists(path)) + continue; + + var contents = File.ReadAllText(path); + + if (contents.StartsWith("# Requires Admin")) + adminScripts.Add(path); + else + scripts.Add(path); + } + + RunScriptsAsAdmin(adminScripts, arguments); + + foreach (var script in scripts) + { + RunScript(script, arguments); + } + } + public static string GetScriptFilePath(Game game, ScriptType type) { Dictionary filenames = new Dictionary() {