Skip to content

Commit

Permalink
Replacing EnumBase with Intellenum, so that there is no more reflecti…
Browse files Browse the repository at this point in the history
…on being directly used by the project.
  • Loading branch information
tpill90 committed Dec 23, 2023
1 parent 0a9ac5a commit 592e08e
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 98 deletions.
4 changes: 2 additions & 2 deletions SteamPrefill.Test/DepotHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ public async Task OperatingSystemMatches_DepotIsIncluded(string supportedOS, str
{
DepotId = 123,
ManifestId = 5555,
SupportedOperatingSystems = supportedOS.Split(" ").Select(e => OperatingSystem.Parse(e)).ToList()
SupportedOperatingSystems = supportedOS.Split(" ").Select(e => OperatingSystem.FromValue(e)).ToList()
}
};

var downloadArguments = new DownloadArguments
{
OperatingSystems = downloadOS.Split(" ").Select(e => OperatingSystem.Parse(e)).ToList()
OperatingSystems = downloadOS.Split(" ").Select(e => OperatingSystem.FromValue(e)).ToList()
};
var filteredDepots = await _depotHandler.FilterDepotsToDownloadAsync(downloadArguments, depotList);
Assert.Single(filteredDepots);
Expand Down
4 changes: 2 additions & 2 deletions SteamPrefill/CliCommands/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public sealed class OperatingSystemConverter : BindingConverter<OperatingSystem>
{
public override OperatingSystem Convert(string rawValue)
{
if (!OperatingSystem.IsValidEnumValue(rawValue))
if (!OperatingSystem.TryFromValue(rawValue, out var _))
{
AnsiConsole.MarkupLine(Red($"{White(rawValue)} is not a valid operating system!"));
AnsiConsole.Markup(Red($"Valid operating systems include : {LightYellow("windows/linux/macos")}"));
throw new CommandException(".", 1, true);
}
return OperatingSystem.Parse(rawValue);
return OperatingSystem.FromValue(rawValue);
}
}

Expand Down
12 changes: 2 additions & 10 deletions SteamPrefill/Extensions/KeyValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ public static class KeyValueExtensions
return DateTimeOffset.FromUnixTimeSeconds(seconds).DateTime;
}

/// <summary>
/// Attempts to convert and return the value of this instance as an enum.
/// If the conversion is invalid, null is returned.
/// </summary>
public static T AsEnum<T>(this KeyValue keyValue, bool toLower = false) where T : EnumBase<T>
public static string ToLowerCaseString(this KeyValue keyValue)
{
if (keyValue == KeyValue.Invalid)
{
Expand All @@ -62,11 +58,7 @@ public static T AsEnum<T>(this KeyValue keyValue, bool toLower = false) where T
{
return null;
}
if (toLower)
{
return EnumBase<T>.Parse(keyValue.Value.ToLower());
}
return EnumBase<T>.Parse(keyValue.Value);
return keyValue.Value.ToLower();
}

public static List<string> SplitCommaDelimited(this KeyValue keyValue)
Expand Down
11 changes: 8 additions & 3 deletions SteamPrefill/Models/AppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ public DateTime? ReleaseDate
public AppInfo(Steam3Session steamSession, uint appId, KeyValue rootKeyValue)
{
AppId = appId;

Name = rootKeyValue["common"]["name"].Value.EscapeMarkup();
Type = rootKeyValue["common"]["type"].AsEnum<AppType>(toLower: true);


AppType.TryFromValue(rootKeyValue["common"]["type"].ToLowerCaseString(), out var appType);
Type = appType;

OSList = rootKeyValue["common"]["oslist"].SplitCommaDelimited();

SteamReleaseDate = rootKeyValue["common"]["steam_release_date"].AsDateTimeUtc();
OriginalReleaseDate = rootKeyValue["common"]["original_release_date"].AsDateTimeUtc();
ReleaseState = rootKeyValue["common"]["releasestate"].AsEnum<ReleaseState>(toLower: true);

ReleaseState.TryFromValue(rootKeyValue["common"]["releasestate"].ToLowerCaseString(), out var releaseState);
ReleaseState = releaseState;

if (rootKeyValue["depots"] != KeyValue.Invalid)
{
Expand Down
8 changes: 5 additions & 3 deletions SteamPrefill/Models/DepotInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ public DepotInfo(KeyValue rootKey, uint appId)
{
SupportedOperatingSystems = rootKey["config"]["oslist"].Value
.Split(',')
.Select(e => OperatingSystem.Parse(e))
.Select(e => OperatingSystem.FromValue(e))
.ToList();
}
Architecture = rootKey["config"]["osarch"].AsEnum<Architecture>();

Architecture.TryFromValue(rootKey["config"]["osarch"].ToLowerCaseString(), out var appType);
Architecture = appType;

Languages = rootKey["config"]["language"].SplitCommaDelimited()
.Select(e => Language.Parse(e))
.Select(e => Language.FromValue(e))
.ToList();

if (rootKey["config"]["lowviolence"].Value is "1")
Expand Down
9 changes: 3 additions & 6 deletions SteamPrefill/Models/Enums/AppType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/// https://partner.steamgames.com/doc/api/steam_api?#EAppType
/// </summary>
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public class AppType : EnumBase<AppType>
[Intellenum(typeof(string))]
public sealed partial class AppType
{
public static readonly AppType Application = new AppType("application");
public static readonly AppType Beta = new AppType("beta");
Expand All @@ -20,9 +21,5 @@ public class AppType : EnumBase<AppType>
public static readonly AppType Series = new AppType("series");
public static readonly AppType Tool = new AppType("tool");
public static readonly AppType Video = new AppType("video");

private AppType(string name) : base(name)
{
}
}
}
}
13 changes: 5 additions & 8 deletions SteamPrefill/Models/Enums/Architecture.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
namespace SteamPrefill.Models.Enums
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public class Architecture : EnumBase<Architecture>
[Intellenum(typeof(string))]
public sealed partial class Architecture
{
public static readonly Architecture unknown = new Architecture("unknown");
public static readonly Architecture x86 = new Architecture("32");
public static readonly Architecture x64 = new Architecture("64");

private Architecture(string name) : base(name)
{
}
public static readonly Architecture unknown = new("unknown");
public static readonly Architecture x86 = new("32");
public static readonly Architecture x64 = new("64");
}
}
81 changes: 39 additions & 42 deletions SteamPrefill/Models/Enums/Language.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
namespace SteamPrefill.Models.Enums
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public class Language : EnumBase<Language>
[Intellenum(typeof(string))]
public sealed partial class Language
{
public static readonly Language English = new Language("english");

public static readonly Language Arabic = new Language("arabic");
public static readonly Language Brazilian = new Language("brazilian");
public static readonly Language Bulgarian = new Language("bulgarian");

public static readonly Language Chinese = new Language("chinese");
public static readonly Language ChineseSimplified = new Language("schinese");
public static readonly Language ChineseTraditional = new Language("tchinese");
public static readonly Language Czech = new Language("czech");

public static readonly Language Danish = new Language("danish");
public static readonly Language Dutch = new Language("dutch");

public static readonly Language Finnish = new Language("finnish");
public static readonly Language French = new Language("french");
public static readonly Language German = new Language("german");
public static readonly Language Greek = new Language("greek");

public static readonly Language Hungarian = new Language("hungarian");
public static readonly Language Italian = new Language("italian");
public static readonly Language Japanese = new Language("japanese");
public static readonly Language Korean = new Language("koreana");
public static readonly Language Latam = new Language("latam");

public static readonly Language Norwegian = new Language("norwegian");
public static readonly Language Polish = new Language("polish");
public static readonly Language Portuguese = new Language("portuguese");
public static readonly Language Spanish = new Language("spanish");

public static readonly Language Romanian = new Language("romanian");
public static readonly Language Russian = new Language("russian");
public static readonly Language Thai = new Language("thai");
public static readonly Language Turkish = new Language("turkish");
public static readonly Language Swedish = new Language("swedish");
public static readonly Language Ukrainian = new Language("ukrainian");
public static readonly Language Vietnamese = new Language("vietnamese");

private Language(string name) : base(name)
{
}
public static readonly Language English = new("english");

public static readonly Language Arabic = new("arabic");
public static readonly Language Brazilian = new("brazilian");
public static readonly Language Bulgarian = new("bulgarian");

public static readonly Language Chinese = new("chinese");
public static readonly Language ChineseSimplified = new("schinese");
public static readonly Language ChineseTraditional = new("tchinese");
public static readonly Language Czech = new("czech");

public static readonly Language Danish = new("danish");
public static readonly Language Dutch = new("dutch");

public static readonly Language Finnish = new("finnish");
public static readonly Language French = new("french");
public static readonly Language German = new("german");
public static readonly Language Greek = new("greek");

public static readonly Language Hungarian = new("hungarian");
public static readonly Language Italian = new("italian");
public static readonly Language Japanese = new("japanese");
public static readonly Language Korean = new("koreana");
public static readonly Language Latam = new("latam");

public static readonly Language Norwegian = new("norwegian");
public static readonly Language Polish = new("polish");
public static readonly Language Portuguese = new("portuguese");
public static readonly Language Spanish = new("spanish");

public static readonly Language Romanian = new("romanian");
public static readonly Language Russian = new("russian");
public static readonly Language Thai = new("thai");
public static readonly Language Turkish = new("turkish");
public static readonly Language Swedish = new("swedish");
public static readonly Language Ukrainian = new("ukrainian");
public static readonly Language Vietnamese = new("vietnamese");
}
}
15 changes: 6 additions & 9 deletions SteamPrefill/Models/Enums/OperatingSystem.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
namespace SteamPrefill.Models.Enums
{
public class OperatingSystem : EnumBase<OperatingSystem>
[Intellenum(typeof(string))]
public sealed partial class OperatingSystem
{
public static readonly OperatingSystem Windows = new OperatingSystem("windows");
public static readonly OperatingSystem MacOS = new OperatingSystem("macos");
public static readonly OperatingSystem Linux = new OperatingSystem("linux");

private OperatingSystem(string name) : base(name)
{
}
public static readonly OperatingSystem Windows = new("windows");
public static readonly OperatingSystem MacOS = new("macos");
public static readonly OperatingSystem Linux = new("linux");
}
}
}
21 changes: 9 additions & 12 deletions SteamPrefill/Models/Enums/ReleaseState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,34 @@
/// https://partner.steamgames.com/doc/api/steam_api?#EAppReleaseState
/// </summary>
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class ReleaseState : EnumBase<ReleaseState>
[Intellenum(typeof(string))]
public sealed partial class ReleaseState
{
/// <summary>
/// Unknown, can't get application information, or license info is missing.
/// </summary>
public static readonly ReleaseState Unknown = new ReleaseState("unknown");
public static readonly ReleaseState Unknown = new("unknown");

/// <summary>
/// Even if user owns it, they can't see game at all.
/// </summary>
public static readonly ReleaseState Unavailable = new ReleaseState("unavailable");
public static readonly ReleaseState Unavailable = new("unavailable");

/// <summary>
/// Can be purchased and is visible in games list, nothing else.
/// </summary>
public static readonly ReleaseState Prerelease = new ReleaseState("prerelease");
public static readonly ReleaseState Prerelease = new("prerelease");

/// <summary>
/// Owners can preload app, not play it.
/// </summary>
public static readonly ReleaseState PreloadOnly = new ReleaseState("preloadonly");
public static readonly ReleaseState PreloadOnly = new("preloadonly");

/// <summary>
/// Owners can download and play app.
/// </summary>
public static readonly ReleaseState Released = new ReleaseState("released");
public static readonly ReleaseState Released = new("released");

public static readonly ReleaseState Disabled = new ReleaseState("disabled");

private ReleaseState(string name) : base(name)
{
}
public static readonly ReleaseState Disabled = new("disabled");
}
}
}
1 change: 1 addition & 0 deletions SteamPrefill/Properties/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
global using LancachePrefill.Common.Exceptions;
global using LancachePrefill.Common.Extensions;
global using LancachePrefill.Common.SelectAppsTui;
global using Intellenum;
global using ProtoBuf;
global using Spectre.Console;
global using SteamKit2;
Expand Down
1 change: 1 addition & 0 deletions SteamPrefill/SteamPrefill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<!-- External Packages -->
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="HexMate" Version="0.0.3" />
<PackageReference Include="Intellenum" Version="1.0.0-beta.3" />
<PackageReference Include="protobuf-net" Version="3.2.16" />
<PackageReference Include="SteamKit2" Version="2.5.0-Beta.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />
Expand Down

0 comments on commit 592e08e

Please sign in to comment.