2020-04-19 05:37:29 +00:00
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 ;
2020-04-23 08:16:16 +00:00
using HeliosPlus.Shared ;
2020-04-27 10:55:44 +00:00
using HeliosPlus.GameLibraries ;
2020-05-17 09:19:55 +00:00
using System.Text.RegularExpressions ;
using System.ServiceModel.Dispatcher ;
2020-04-19 05:37:29 +00:00
2020-04-23 08:16:16 +00:00
namespace HeliosPlus
2020-04-19 05:37:29 +00:00
{
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
2020-05-09 13:02:07 +00:00
var profiles = Profile . LoadAllProfiles ( ) . ToArray ( ) ;
2020-04-19 05:37:29 +00:00
// 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 ;
}
}
2020-05-17 09:19:55 +00:00
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
2020-06-01 10:25:52 +00:00
var shortcuts = ShortcutRepository . AllShortcuts . ToArray ( ) ;
2020-05-17 09:19:55 +00:00
// 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 ;
}
}
2020-04-19 05:37:29 +00:00
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 ;
}
}
}