From c52950c9b50d7189f6818ff32a48617dc44bc4a9 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Sun, 22 Nov 2020 21:27:29 +1300 Subject: [PATCH] Fixed ChangeProfile command line ChangeProfile command wasn't working, but now that you can use the Desktop Background context menu to change profiles I needed to fix this so that the menu runs this command. --- HeliosPlus.Shared/ProfileItem.cs | 3 +-- HeliosPlus/Program.cs | 27 ++++++++++++++++++--------- HeliosPlus/Validators.cs | 8 ++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/HeliosPlus.Shared/ProfileItem.cs b/HeliosPlus.Shared/ProfileItem.cs index 0b41e3d..65e9471 100644 --- a/HeliosPlus.Shared/ProfileItem.cs +++ b/HeliosPlus.Shared/ProfileItem.cs @@ -30,6 +30,7 @@ namespace HeliosPlus.Shared private static List _unavailableDisplays; internal static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeliosPlus"); + private static string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$"; private string _uuid = ""; private Version _version; @@ -125,7 +126,6 @@ namespace HeliosPlus.Shared } set { - string uuidV4Regex = @"[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}"; Match match = Regex.Match(value, uuidV4Regex, RegexOptions.IgnoreCase); if (match.Success) _uuid = value; @@ -272,7 +272,6 @@ namespace HeliosPlus.Shared public static bool IsValidUUID(string testId) { - string uuidV4Regex = @"/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i"; Match match = Regex.Match(testId, uuidV4Regex, RegexOptions.IgnoreCase); if (match.Success) return true; diff --git a/HeliosPlus/Program.cs b/HeliosPlus/Program.cs index 9103e41..91c0b70 100644 --- a/HeliosPlus/Program.cs +++ b/HeliosPlus/Program.cs @@ -103,20 +103,29 @@ namespace HeliosPlus { }); }); - // This is the ApplyProfile command - app.Command(HeliosStartupAction.ChangeProfile.ToString(), (runShortcutCmd) => + // This is the ChangeProfile command + app.Command(HeliosStartupAction.ChangeProfile.ToString(), (runProfileCmd) => { - var argumentShortcut = runShortcutCmd.Argument("\"SHORTCUT_UUID\"", "(required) The UUID of the shortcut to run from those stored in the shortcut library.").IsRequired(); - argumentShortcut.Validators.Add(new ShortcutMustExistValidator()); + var argumentProfile = runProfileCmd.Argument("\"Profile_UUID\"", "(required) The UUID of the profile to run from those stored in the profile file.").IsRequired(); + argumentProfile.Validators.Add(new ProfileMustExistValidator()); //description and help text of the command. - runShortcutCmd.Description = "Use this command to run favourite game or application with a display profile of your choosing."; + runProfileCmd.Description = "Use this command to change to a display profile of your choosing."; - runShortcutCmd.OnExecute(() => + runProfileCmd.OnExecute(() => { - // - RunShortcut(argumentShortcut.Value); - return 0; + try + { + // Lookup the profile + ProfileItem profileToUse = ProfileRepository.AllProfiles.Where(p => p.UUID.Equals(argumentProfile.Value)).First(); + + ApplyProfile(profileToUse); + return 0; + } + catch (Exception) + { + return 1; + } }); }); diff --git a/HeliosPlus/Validators.cs b/HeliosPlus/Validators.cs index 6729ca9..1ce057a 100644 --- a/HeliosPlus/Validators.cs +++ b/HeliosPlus/Validators.cs @@ -14,13 +14,13 @@ using System.ServiceModel.Dispatcher; namespace HeliosPlus { - class ProfileMustExistValidator : IOptionValidator + class ProfileMustExistValidator : IArgumentValidator { - public ValidationResult GetValidationResult(CommandOption optionProfile, ValidationContext context) + public ValidationResult GetValidationResult(CommandArgument argumentProfileName, ValidationContext context) { // This validator only runs if there is a value - if (!optionProfile.HasValue()) return ValidationResult.Success; - var profileName = (string) optionProfile.Value(); + if (argumentProfileName.Value == "") return ValidationResult.Success; + var profileName = (string)argumentProfileName.Value; // Try to find the Profile Name if (!ProfileRepository.ContainsProfile(profileName))