Started basic implementation of allowing name/key changes in UI

This commit is contained in:
Pat Hartl 2023-01-15 04:56:56 -06:00
parent 0681d0a75b
commit 81bad5e032
3 changed files with 71 additions and 19 deletions

View file

@ -187,17 +187,7 @@ namespace LANCommander.PlaynitePlugin
if (script.RequiresAdmin)
script.Contents = "# Requires Admin" + "\r\n\r\n" + script.Contents;
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
{ 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);

View file

@ -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<GameMetadata> GetGames(LibraryGetGamesArgs args)
@ -124,6 +127,58 @@ namespace LANCommander.PlaynitePlugin
yield return new LANCommanderUninstallController(this, args.Game);
}
public override IEnumerable<GameMenuItem> 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<MainMenuItem> 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;

View file

@ -35,14 +35,7 @@ namespace LANCommander.PlaynitePlugin
public void RunScript(Game game, ScriptType type)
{
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
{ 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<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
{ 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);
}
}
}