DisplayMagician/HeliosPlus/Validators.cs
terrymacdonald 71928a9b44 [WIP] Created SteamLibrary class to handle library
Pulled out the the library list mgmt from SteamGame
and put it in a new SteamLibrary class. This means I
can replicate the learnings from the ShortcutRepo
amd Profile Repo, and can save separate JSON files
in the future if I so desire.

There is a little bit outstanding to make the SteamGame
Properties to be writeable as well as readable, otherwise
the SteamGame.CopyTo function won't work.
2020-07-21 23:40:33 +12:00

95 lines
3.9 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 profileName = (string) optionProfile.Value();
// Try to find the Profile Name
if (!ProfileRepository.ContainsProfile(profileName))
{
return new ValidationResult($"Couldn't find Profile Name or ID supplied via command line: '{profileName}'. Please check the Profile Name or ID you supplied on the command line is correct.");
}
ProfileItem profile = ProfileRepository.GetProfile(profileName);
Console.WriteLine($"Using Profile: '{profile.Name}' (ID:{profile.UUID})");
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 shortcutName = (string) argumentShortcutName.Value;
// Check if the UUID or ShortcutName are provided
if (!ShortcutRepository.ContainsShortcut(shortcutName))
{
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.");
}
ShortcutItem shortcut = ShortcutRepository.GetShortcut(shortcutName);
Console.WriteLine($"Using Shortcut: '{shortcut.Name}' (ID: {shortcut.UUID})");
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;
}
}
}