DisplayMagician/HeliosPlus/Validators.cs
temacdonald 9dab00bdb4 [WIP] Sort out the ShortcutForm controls
The logic I chose for the ShortcutForm controls is
really not suited towards distributed logic. I am
going to have to centralise the logic into a single
function that will evaulate when to enable the
save button.
2020-05-17 21:19:55 +12:00

122 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Validation;
using System.ComponentModel.DataAnnotations;
using HeliosPlus.Shared;
using HeliosPlus.GameLibraries;
using System.Text.RegularExpressions;
using System.ServiceModel.Dispatcher;
namespace HeliosPlus
{
class ProfileMustExistValidator : IOptionValidator
{
public ValidationResult GetValidationResult(CommandOption optionProfile, ValidationContext context)
{
// This validator only runs if there is a value
if (!optionProfile.HasValue()) return ValidationResult.Success;
var profile = optionProfile.Value();
// Create an array of display profiles we have
var profiles = Profile.LoadAllProfiles().ToArray();
// Check if the user supplied a --profile option using the profiles' ID
var profileIndex = profiles.Length > 0 ? Array.FindIndex(profiles, p => p.Id.Equals(profile, StringComparison.InvariantCultureIgnoreCase)) : -1;
// If the profileID wasn't there, maybe they used the profile name?
if (profileIndex == -1)
{
// Try and lookup the profile in the profiles' Name fields
profileIndex = profiles.Length > 0 ? Array.FindIndex(profiles, p => p.Name.StartsWith(profile, StringComparison.InvariantCultureIgnoreCase)) : -1;
}
// If the profileID still isn't there, then raise the alarm
if (profileIndex == -1)
{
return new ValidationResult($"Couldn't find Profile Name or ID supplied via command line: '{optionProfile.LongName}'. Please check the Profile Name or ID you supplied on the command line is correct.");
}
Console.WriteLine($"Using Profile: '{profiles[profileIndex].Name}' (ID:{profiles[profileIndex].Id})");
return ValidationResult.Success;
}
}
class ShortcutMustExistValidator : IArgumentValidator
{
public ValidationResult GetValidationResult(CommandArgument argumentShortcutName, ValidationContext context)
{
// This validator only runs if there is a string provided
if (argumentShortcutName.Value == "") return ValidationResult.Success;
string shortcutNameProvided = (string) argumentShortcutName.Value;
string shortcutName = "";
// check if the shortcut name is surrounded by speech marks
int shortcutNameIndexLeft = shortcutNameProvided.IndexOf('"');
int shortcutNameIndexRight = shortcutNameProvided.LastIndexOf('"');
if (shortcutNameIndexLeft != -1 && shortcutNameIndexRight != -1 && shortcutNameIndexLeft != shortcutNameIndexRight)
{
MatchCollection matches = Regex.Matches(shortcutNameProvided, @"'(.*?)'");
shortcutName = matches[0].Groups[1].Value; // (Index 1 is the first group)
}
else
{
shortcutName = shortcutNameProvided;
}
// Create an array of shortcuts we have
var shortcuts = Shortcut.LoadAllShortcuts().ToArray();
// Check if the user supplied a valid shortcut name
int profileIndex = shortcuts.Length > 0 ? Array.FindIndex(shortcuts, p => p.Name.Contains(shortcutName)) : -1;
// If the profileID still isn't there, then raise the alarm
if (profileIndex == -1)
{
return new ValidationResult($"Couldn't find Shortcut Name supplied via command line: '{shortcutName}'. Please check the Shortcut Name you supplied on the command line is correct.");
}
Console.WriteLine($"Using Shortcut: '{shortcuts[profileIndex].Name}'");
return ValidationResult.Success;
}
}
class FileOptionMustExistValidator : IOptionValidator
{
public ValidationResult GetValidationResult(CommandOption optionFullFileName, ValidationContext context)
{
// This validator only runs if there is a string provided
if (optionFullFileName.Value() == "") return ValidationResult.Success;
var fileNameAndPath = optionFullFileName.Value();
// Check that the file exists
if (!File.Exists(fileNameAndPath))
{
return new ValidationResult($"Couldn't find the file '{optionFullFileName.Value()}' supplied to '{optionFullFileName.LongName}'. Please check you specified the full path to the file on the command line.");
}
return ValidationResult.Success;
}
}
class FileArgumentMustExistValidator : IArgumentValidator
{
public ValidationResult GetValidationResult(CommandArgument argumentFullFileName, ValidationContext context)
{
// This validator only runs if there is a string provided
if (argumentFullFileName.Value == "") return ValidationResult.Success;
var fileNameAndPath = argumentFullFileName.Value;
// Check that the file exists
if (!File.Exists(fileNameAndPath))
{
return new ValidationResult($"Couldn't find the file '{argumentFullFileName.Value}' supplied to '{argumentFullFileName.Name}'. Please check you specified the full path to the file on the command line.");
}
return ValidationResult.Success;
}
}
}