Skip to content

Commit

Permalink
Added ApplicationVersion comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Oct 9, 2023
1 parent 52e097b commit 6483e6a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/Bannerlord.ModuleManager.DotNetJS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ static Program()
JS.Runtime.ConfigureJson(options => options.Converters.Add(new JsonStringEnumConverter()));
}

private static readonly ApplicationVersionComparer _applicationVersionComparer = new();

public static void Main() { }

[JSInvokable]
Expand Down Expand Up @@ -92,7 +90,7 @@ public static void DisableModule(ModuleInfoExtended[] modules, ModuleInfoExtende
}

[JSInvokable]
public static int CompareVersions(ApplicationVersion x, ApplicationVersion y) => _applicationVersionComparer.Compare(x, y);
public static int CompareVersions(ApplicationVersion x, ApplicationVersion y) => ApplicationVersionComparer.CompareStandard(x, y);

[JSInvokable]
public static DependentModuleMetadata[] GetDependenciesAll(ModuleInfoExtended module) => module.DependenciesAllDistinct().ToArray();
Expand Down
4 changes: 1 addition & 3 deletions src/Bannerlord.ModuleManager.Native/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public static unsafe partial class Bindings
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate return_value_void* N_SetDisabled(param_ptr* p_owner, param_string* p_module_id, param_bool value);

private static readonly ApplicationVersionComparer _applicationVersionComparer = new();

[UnmanagedCallersOnly(EntryPoint = "bmm_sort", CallConvs = new[] { typeof(CallConvCdecl) }), IsNotConst<IsPtrConst>]
public static return_value_json* Sort([IsConst<IsPtrConst>] param_json* p_source)
{
Expand Down Expand Up @@ -414,7 +412,7 @@ public static unsafe partial class Bindings
var x = Utils.DeserializeJson(p_x, CustomSourceGenerationContext.ApplicationVersion);
var y = Utils.DeserializeJson(p_y, CustomSourceGenerationContext.ApplicationVersion);

var result = _applicationVersionComparer.Compare(x, y);
var result = ApplicationVersionComparer.CompareStandard(x, y);

Logger.LogOutput(result);
return return_value_int32.AsValue(result, false);
Expand Down
3 changes: 1 addition & 2 deletions src/Bannerlord.ModuleManager.WASM/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Bannerlord.ModuleManager.WASM
{
public static partial class Program
{
private static readonly ApplicationVersionComparer _applicationVersionComparer = new();
private static readonly JsonSerializerOptions _options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
Expand Down Expand Up @@ -127,6 +126,6 @@ public static string GetSubModuleInfo([JSMarshalAs<JSType.String>] string xmlCon
[JSExport]
[return: JSMarshalAs<JSType.Number>]
public static int CompareVersions([JSMarshalAs<JSType.String>] string x, [JSMarshalAs<JSType.String>] string y) =>
_applicationVersionComparer.Compare(AsApplicationVersion(x), AsApplicationVersion(y));
ApplicationVersionComparer.CompareStandard(AsApplicationVersion(x), AsApplicationVersion(y));
}
}
11 changes: 10 additions & 1 deletion src/Bannerlord.ModuleManager/ApplicationVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@

namespace Bannerlord.ModuleManager
{
using global::System;

#if !BANNERLORDBUTRMODULEMANAGER_PUBLIC
internal
#else
public
# endif
record ApplicationVersion
record ApplicationVersion : IComparable<ApplicationVersion>
{
public static ApplicationVersion Empty { get; } = new();

Expand Down Expand Up @@ -76,6 +78,13 @@ public bool IsSameWithChangeSet(ApplicationVersion? other) =>

public override string ToString() => $"{GetPrefix(ApplicationVersionType)}{Major}.{Minor}.{Revision}.{ChangeSet}";

public int CompareTo(ApplicationVersion? other) => ApplicationVersionComparer.CompareStandard(this, other);

public static bool operator <(ApplicationVersion left, ApplicationVersion right) => left.CompareTo(right) < 0;
public static bool operator >(ApplicationVersion left, ApplicationVersion right) => left.CompareTo(right) > 0;
public static bool operator <=(ApplicationVersion left, ApplicationVersion right) => left.CompareTo(right) <= 0;
public static bool operator >=(ApplicationVersion left, ApplicationVersion right) => left.CompareTo(right) >= 0;

public static char GetPrefix(ApplicationVersionType applicationVersionType) => applicationVersionType switch
{
ApplicationVersionType.Alpha => 'a',
Expand Down
4 changes: 3 additions & 1 deletion src/Bannerlord.ModuleManager/ApplicationVersionComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class ApplicationVersionComparer : IComparer<ApplicationVersion?>, IComparer
public int Compare(object? x, object? y) => Compare(x as ApplicationVersion, y as ApplicationVersion);

/// <inheritdoc/>
public virtual int Compare(ApplicationVersion? x, ApplicationVersion? y)
public virtual int Compare(ApplicationVersion? x, ApplicationVersion? y) => CompareStandard(x, y);

public static int CompareStandard(ApplicationVersion? x, ApplicationVersion? y)
{
if (x is null && y is null)
return 0;
Expand Down
7 changes: 3 additions & 4 deletions src/Bannerlord.ModuleManager/ModuleUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ public static IEnumerable<ModuleIssue> ValidateModuleDependencies(IReadOnlyList<
}

// Check that the dependencies have the minimum required version set by DependedModuleMetadatas
var comparer = new ApplicationVersionComparer();
foreach (var metadata in targetModule.DependenciesToLoadDistinct())
{
// Ignore the check for empty versions
Expand All @@ -387,7 +386,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleDependencies(IReadOnlyList<
if (metadata.Version != ApplicationVersion.Empty)
{
// dependedModuleMetadata.Version > dependedModule.Version
if (!metadata.IsOptional && (comparer.Compare(metadata.Version, metadataModule.Version) > 0))
if (!metadata.IsOptional && (ApplicationVersionComparer.CompareStandard(metadata.Version, metadataModule.Version) > 0))
{
yield return new ModuleIssue(targetModule, metadataModule.Id, ModuleIssueType.VersionMismatchLessThanOrEqual)
{
Expand All @@ -403,7 +402,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleDependencies(IReadOnlyList<
// dependedModuleMetadata.Version < dependedModule.VersionRange.Max
if (!metadata.IsOptional)
{
if (comparer.Compare(metadata.VersionRange.Min, metadataModule.Version) > 0)
if (ApplicationVersionComparer.CompareStandard(metadata.VersionRange.Min, metadataModule.Version) > 0)
{
yield return new ModuleIssue(targetModule, metadataModule.Id, ModuleIssueType.VersionMismatchLessThan)
{
Expand All @@ -412,7 +411,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleDependencies(IReadOnlyList<
};
continue;
}
if (comparer.Compare(metadata.VersionRange.Max, metadataModule.Version) < 0)
if (ApplicationVersionComparer.CompareStandard(metadata.VersionRange.Max, metadataModule.Version) < 0)
{
yield return new ModuleIssue(targetModule, metadataModule.Id, ModuleIssueType.VersionMismatchGreaterThan)
{
Expand Down

0 comments on commit 6483e6a

Please sign in to comment.