Allow user to change player name across all installed games
parent
2278c85539
commit
ee35c9bd13
|
@ -6,6 +6,7 @@ using Playnite.SDK.Plugins;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
|
@ -13,6 +14,7 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Media;
|
||||||
using YamlDotNet.Serialization;
|
using YamlDotNet.Serialization;
|
||||||
using YamlDotNet.Serialization.NamingConventions;
|
using YamlDotNet.Serialization.NamingConventions;
|
||||||
using PN = Playnite;
|
using PN = Playnite;
|
||||||
|
@ -219,17 +221,25 @@ namespace LANCommander.PlaynitePlugin
|
||||||
Description = "Change Player Name (All Games)",
|
Description = "Change Player Name (All Games)",
|
||||||
Action = (args2) =>
|
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)
|
public override IEnumerable<TopPanelItem> GetTopPanelItems()
|
||||||
{
|
{
|
||||||
var games = PlayniteApi.Database.Games.Where(g => g.IsInstalled).ToList();
|
yield return new TopPanelItem
|
||||||
|
{
|
||||||
foreach (var game in games)
|
Title = "Click to change your name (All Games)",
|
||||||
{
|
Icon = new TextBlock
|
||||||
PowerShellRuntime.RunScript(game, SDK.Enums.ScriptType.NameChange);
|
{
|
||||||
}
|
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);
|
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()
|
public Window ShowAuthenticationWindow()
|
||||||
{
|
{
|
||||||
Window window = null;
|
Window window = null;
|
||||||
|
|
|
@ -18,8 +18,6 @@ namespace LANCommander.PlaynitePlugin
|
||||||
var process = new Process();
|
var process = new Process();
|
||||||
process.StartInfo.FileName = "powershell.exe";
|
process.StartInfo.FileName = "powershell.exe";
|
||||||
process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{path}""";
|
process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{path}""";
|
||||||
process.StartInfo.RedirectStandardOutput = true;
|
|
||||||
process.StartInfo.UseShellExecute = false;
|
|
||||||
|
|
||||||
if (arguments != null)
|
if (arguments != null)
|
||||||
process.StartInfo.Arguments += " " + arguments;
|
process.StartInfo.Arguments += " " + arguments;
|
||||||
|
@ -44,6 +42,38 @@ namespace LANCommander.PlaynitePlugin
|
||||||
process.WaitForExit();
|
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)
|
public void RunScript(Game game, ScriptType type, string arguments = null)
|
||||||
{
|
{
|
||||||
var path = GetScriptFilePath(game, type);
|
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)
|
public static string GetScriptFilePath(Game game, ScriptType type)
|
||||||
{
|
{
|
||||||
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
|
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
|
||||||
|
|
Loading…
Reference in New Issue