Allow user to change player name across all installed games

dashboard
Pat Hartl 2023-01-19 20:02:34 -06:00
parent 2278c85539
commit ee35c9bd13
2 changed files with 106 additions and 12 deletions

View File

@ -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<TopPanelItem> 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;

View File

@ -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<string> 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<Game> games, ScriptType type, string arguments = null)
{
List<string> scripts = new List<string>();
List<string> adminScripts = new List<string>();
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<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {