Skip to content

Commit

Permalink
Merge pull request #209 from AlamoEngine-Tools/develop
Browse files Browse the repository at this point in the history
Update to spec 3.0.2
  • Loading branch information
AnakinRaW authored Dec 3, 2024
2 parents c940ef0 + 9388d98 commit b405a87
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
51 changes: 42 additions & 9 deletions src/EawModinfo.Tests/ModinfoValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,22 @@ public static IEnumerable<object[]> GetSteamData()
},
false
];
yield return
[
new JsonSteamData
{
Id = long.MaxValue.ToString(), Tags = ["EAW"], ContentFolder = "testFolder",
Description = "Some description", Title = "MyTitle"
},
false
];
}

public static IEnumerable<object[]> GetInvalidSteamData()
{
yield return [new JsonSteamData(), true];
yield return [new JsonSteamData { Id = null! }, true];
yield return [new JsonSteamData {Id = "asd", Tags = ["EAW"], ContentFolder = "testFolder"}, true];
yield return [new JsonSteamData {Id = "1234", Tags = ["EAW"], ContentFolder = "testFolder"}, true];
yield return [new JsonSteamData {Id = "0", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title"}, true];
yield return [new JsonSteamData {Id = "-123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return
[
new JsonSteamData {Id = "129381209812430981329048", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title"},
true
];
yield return [new JsonSteamData {Id = "1234", Tags = Array.Empty<string>(), ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1234", Tags = null!, ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1234", Tags = null!, ContentFolder = "testFolder", Title = "Title" }, true];
Expand All @@ -192,9 +193,41 @@ public static IEnumerable<object[]> GetInvalidSteamData()
yield return [new JsonSteamData { Id = "1234312", Tags = ["FOC", new string('a', 256)], Metadata = "bla", ContentFolder = "testFolder", Title = "Title" }, true];
}

public static IEnumerable<object[]> GetInvalidSteamIDs()
{
var random = new Random();
yield return [new JsonSteamData { Id = "NaN", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1abc", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "abc1", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "abc", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1d", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1f", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "0x1", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "0", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1AF", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1+", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1e2", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1E2", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = random.Next(int.MinValue, -1).ToString(), Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "+123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "-+123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1_23", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1-23", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1.23", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1,23", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "0123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "00123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = " 00123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = " 123", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "123 ", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1 23", Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
yield return [new JsonSteamData { Id = "1" + ulong.MaxValue, Tags = ["EAW"], ContentFolder = "testFolder", Title = "Title" }, true];
}

[Theory]
[MemberData(nameof(GetSteamData))]
[MemberData(nameof(GetInvalidSteamData))]
[MemberData(nameof(GetInvalidSteamIDs))]
public void Validate_SteamData(ISteamData steamData, bool shallThrow)
{
if (!shallThrow)
Expand Down
2 changes: 1 addition & 1 deletion src/EawModinfo/EawModinfo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JsonSchema.Net" Version="7.2.3" />
<PackageReference Include="JsonSchema.Net" Version="7.3.0" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
9 changes: 7 additions & 2 deletions src/EawModinfo/Spec/IModReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ namespace EawModinfo.Spec;
public interface IModReference : IEquatable<IModReference>, IConvertibleToJson
{
/// <summary>
/// Gets the unique identifier as a textual representation. The <see cref="Type"/> property my indicate how the data can be interpreted.
/// Gets the unique, predictable identifier of the mod.
/// </summary>
/// <remarks>
/// The identifier can hold any data to uniquely identify a mod, however the concrete value shall be predictable,
/// so that it can be used to code it into modinfo JSON files to reference mod dependencies.
/// For Steam Workshop mods it is therefore recommended to use the Steam Workshops ID and for local
/// The <see cref="Type"/> property may indicate how the data can be interpreted.
/// </remarks>
string Identifier { get; }

/// <summary>
Expand All @@ -25,5 +31,4 @@ public interface IModReference : IEquatable<IModReference>, IConvertibleToJson
/// As stated in the specification this property is not used for equality matching.
/// </remarks>
SemVersionRange? VersionRange { get; }

}
2 changes: 0 additions & 2 deletions src/EawModinfo/Spec/ModType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ public enum ModType
{
/// <summary>
/// The mod îs stored on the file system at a well known file system location (e.g. the game's 'Mods' directory).
/// The associated <see cref="IModReference.Identifier"/> should be able to parse into a relative or absolute file system path.
/// </summary>
Default = 0,
/// <summary>
/// The mod is a Steam Workshops Item.
/// The associated <see cref="IModReference.Identifier"/> <b>must</b> be able to parse into an <see cref="uint"/> which represents a Steam Workshops identifier.
/// </summary>
Workshops = 1,
/// <summary>
Expand Down
11 changes: 6 additions & 5 deletions src/EawModinfo/Utilities/ModinfoValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using EawModinfo.Model.Json;
using EawModinfo.Spec;
using EawModinfo.Spec.Steam;

Expand All @@ -16,7 +15,7 @@ public static class ModinfoValidator
{
/// <summary>
/// Validates an <see cref="IModinfo"/> data. Throws an <see cref="ModinfoException"/> when validation failed.
/// Also throws is subsequent data such as <see cref="ISteamData"/> are invalid, if present.
/// Also throws if subsequent data such as <see cref="ISteamData"/> are invalid, if present.
/// </summary>
/// <param name="modinfo">The data to validate.</param>
/// <exception cref="ModinfoException">The validation failed.</exception>
Expand Down Expand Up @@ -47,7 +46,7 @@ public static void Validate(this ISteamData steamData)
{
if (string.IsNullOrEmpty(steamData.Id))
throw new ModinfoException("Steam data is invalid: Identifier is missing.");
ValidateSteamId(steamData.Id, "Steam data is invalid: ");
ValidateSteamId(steamData.Id, $"Steam data is invalid: '{steamData.Id}'");

if (string.IsNullOrEmpty(steamData.ContentFolder))
throw new ModinfoException("Steam data is invalid: ContentFolder is missing.");
Expand Down Expand Up @@ -81,7 +80,6 @@ private static bool ContainsInvalidCharacter(ReadOnlySpan<char> value)
if (c == ',' || (uint)(c - '\x0020') > '\x007F' - '\x0020') // (c >= '\x0020' && c <= '\x007F'
return true;
}

return false;
}

Expand Down Expand Up @@ -152,7 +150,10 @@ private static void ValidateSteamId(string data, string errorSourceMessage)

private static (bool Valid, string Error) ValidateSteamId(string data)
{
if (!uint.TryParse(data, out var id))
// Leading spaces, etc. is not a problem, because this case is handled with NumberStyles.None
if (data[0] == '0')
return (false, "Workshops ID cannot have leading zeros.");
if (!ulong.TryParse(data, NumberStyles.None, CultureInfo.InvariantCulture, out var id))
return (false, "Workshops ID must be an uint number.");
if (id == 0)
return (false, "Workshops ID cannot be 0.");
Expand Down

0 comments on commit b405a87

Please sign in to comment.