DisplayMagician/HeliosDisplayManagement/Validators.cs
temacdonald a9bb295d1f Renamed app to HeliosPlus namespace and more
Renamed app to HeliosPlus namespace so that the updated
changes don't interfere with HeliosDisplayManagement if
that is also installed. The fact that I've changed so much of
the app means that my changes would be unlikely to be
accepted by Soroush, so I'm best to release this work in a
similar way to other projects like Notepad++, by keeping
the same root name, and adding a plus.
    I've also changed the Shortcut form to put all the games
in a single list to reduce the number of clicks a user has to
do in order for them to create a shortcut. I have begun to
prepare the form so that it will support multiple game
libraries, but now I am at a point that I need to fix the Steam
and Uplay game detection mechanisms so that they report
the correct information for the lv_games list view.
2020-04-23 20:16:16 +12:00

119 lines
5.2 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.Steam;
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.GetAllProfiles().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 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;
}
}
class SteamArgumentMustExistValidator : IArgumentValidator
{
public ValidationResult GetValidationResult(CommandArgument argumentSteamGameId, ValidationContext context)
{
// This validator only runs if there is a string provided
if (argumentSteamGameId.Value == "") return ValidationResult.Success;
var steamGameId = argumentSteamGameId.Value;
// Check that the Steam Game exists
/*if (!Steam.IsInstalled(steamGameId))
{
return new ValidationResult($"Couldn't find a Steam Game with ID '{argumentSteamGameId.Value}' within Steam. Please check you specified the correct Steam Game ID you supplied in the --steam '{argumentSteamGameId.Name}' command line argument.");
}*/
return ValidationResult.Success;
}
}
class UplayArgumentMustExistValidator : IArgumentValidator
{
public ValidationResult GetValidationResult(CommandArgument argumentUplayGameId, ValidationContext context)
{
// This validator only runs if there is a string provided
if (argumentUplayGameId.Value == "") return ValidationResult.Success;
var steamGameId = argumentUplayGameId.Value;
// Check that the Uplay Game exists
/*if (!Steam.IsInstalled(uplayGameId))
{
return new ValidationResult($"Couldn't find a Uplay Game with ID '{argumentUplayGameId.Value}' within Uplay. Please check you specified the correct Uplay Game ID you supplied in the --steam '{argumentUplayGameId.Name}' command line argument.");
}*/
return ValidationResult.Success;
}
}
}