TryGet variants /w out parameter

This commit is contained in:
Justin Swanson 2020-04-12 14:47:28 -05:00
parent c4cce47399
commit cf765942e1
4 changed files with 27 additions and 9 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Alphaleonis.Win32.Filesystem;
using Microsoft.Win32;
@ -85,13 +86,12 @@ namespace Wabbajack.Common
{
get
{
AbsolutePath? gameLoc = TryGetGameLocation();
if (gameLoc == null)
if (!TryGetGameLocation(out var gameLoc))
throw new GameNotInstalledException(this);
if (MainExecutable == null)
throw new NotImplementedException();
return FileVersionInfo.GetVersionInfo((string)gameLoc.Value.Combine(MainExecutable)).ProductVersion;
return FileVersionInfo.GetVersionInfo((string)gameLoc.Combine(MainExecutable)).ProductVersion;
}
}
@ -104,6 +104,21 @@ namespace Wabbajack.Common
return Consts.TestMode ? AbsolutePath.GetCurrentDirectory() : StoreHandler.Instance.TryGetGamePath(Game);
}
public bool TryGetGameLocation(out AbsolutePath path)
{
var ret = TryGetGameLocation();
if (ret != null)
{
path = ret.Value;
return true;
}
else
{
path = default;
return false;
}
}
public AbsolutePath GameLocation()
{
var ret = TryGetGameLocation();
@ -189,6 +204,12 @@ namespace Wabbajack.Common
return int.TryParse(someName, out int id) ? GetBySteamID(id) : null;
}
public static bool TryGetByFuzzyName(string someName, [MaybeNullWhen(false)] out GameMetaData gameMetaData)
{
gameMetaData = TryGetByFuzzyName(someName);
return gameMetaData != null;
}
public static IReadOnlyDictionary<Game, GameMetaData> Games = new Dictionary<Game, GameMetaData>
{
{

View File

@ -226,8 +226,7 @@ namespace Wabbajack.Common
return (Game)i;
}
GameMetaData? game = GameRegistry.TryGetByFuzzyName(str);
if (game == null)
if (!GameRegistry.TryGetByFuzzyName(str, out var game))
{
throw new ArgumentException($"Could not convert {str} to a Game type.");
}

View File

@ -17,8 +17,7 @@ namespace Wabbajack.Lib.Downloaders
if (gameName == null || gameFile == null)
return null;
var game = GameRegistry.TryGetByFuzzyName(gameName);
if (game == null) return null;
if (!GameRegistry.TryGetByFuzzyName(gameName, out var game)) return null;
var path = game.TryGetGameLocation();
var filePath = path?.Combine(gameFile);

View File

@ -26,8 +26,7 @@ namespace Wabbajack.Lib.NexusApi
{
var parts = link.Uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
var foundGame = GameRegistry.TryGetByFuzzyName(parts[0]);
if (foundGame == null)
if (!GameRegistry.TryGetByFuzzyName(parts[0], out var foundGame))
{
game = Game.Oblivion;
modId = 0;