Skip to content

Commit

Permalink
More null safety checks just in case, fixed test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed May 29, 2024
1 parent ea401b4 commit 3e8490a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Bannerlord.ModuleManager.Models/ModuleInfoExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ record ModuleInfoExtended
var isNative = moduleId.Equals(NativeModuleId);

// Override any existing metadata
if (dependentModuleMetadatas.Find(dmm => dmm.Id.Equals(moduleId, StringComparison.Ordinal)) is { } module)
if (dependentModuleMetadatas.Find(dmm => moduleId.Equals(dmm.Id, StringComparison.Ordinal)) is { } module)
{
dependentModuleMetadatas.Remove(module);
}
Expand Down Expand Up @@ -291,7 +291,7 @@ public ModuleInfoExtended(string id, string name, bool isOfficial, ApplicationVe
Url = url;
}

public bool IsNative() => Id.Equals(NativeModuleId, StringComparison.OrdinalIgnoreCase);
public bool IsNative() => NativeModuleId.Equals(Id, StringComparison.OrdinalIgnoreCase);

public override string ToString() => $"{Id} - {Version}";

Expand Down
26 changes: 13 additions & 13 deletions src/Bannerlord.ModuleManager/ModuleInfoExtendedExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ static class ModuleInfoExtendedExtensions
public static IEnumerable<DependentModuleMetadata> DependenciesAllDistinct(this ModuleInfoExtended module) => DependenciesAll(module).DistinctBy(x => x.Id);
public static IEnumerable<DependentModuleMetadata> DependenciesAll(this ModuleInfoExtended module)
{
foreach (var metadata in module.DependentModuleMetadatas)
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x is not null))
{
yield return metadata;
}
foreach (var metadata in module.DependentModules)
foreach (var metadata in module.DependentModules.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -71,7 +71,7 @@ public static IEnumerable<DependentModuleMetadata> DependenciesAll(this ModuleIn
Version = metadata.Version
};
}
foreach (var metadata in module.ModulesToLoadAfterThis)
foreach (var metadata in module.ModulesToLoadAfterThis.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -81,7 +81,7 @@ public static IEnumerable<DependentModuleMetadata> DependenciesAll(this ModuleIn
Version = metadata.Version
};
}
foreach (var metadata in module.IncompatibleModules)
foreach (var metadata in module.IncompatibleModules.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -96,11 +96,11 @@ public static IEnumerable<DependentModuleMetadata> DependenciesAll(this ModuleIn
public static IEnumerable<DependentModuleMetadata> DependenciesToLoadDistinct(this ModuleInfoExtended module) => DependenciesToLoad(module).DistinctBy(x => x.Id);
public static IEnumerable<DependentModuleMetadata> DependenciesToLoad(this ModuleInfoExtended module)
{
foreach (var metadata in module.DependentModuleMetadatas.Where(x => !x.IsIncompatible))
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x is not null).Where(x => !x.IsIncompatible))
{
yield return metadata;
}
foreach (var metadata in module.DependentModules)
foreach (var metadata in module.DependentModules.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -110,7 +110,7 @@ public static IEnumerable<DependentModuleMetadata> DependenciesToLoad(this Modul
Version = metadata.Version
};
}
foreach (var metadata in module.ModulesToLoadAfterThis)
foreach (var metadata in module.ModulesToLoadAfterThis.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -125,11 +125,11 @@ public static IEnumerable<DependentModuleMetadata> DependenciesToLoad(this Modul
public static IEnumerable<DependentModuleMetadata> DependenciesLoadBeforeThisDistinct(this ModuleInfoExtended module) => DependenciesLoadBeforeThis(module).DistinctBy(x => x.Id);
public static IEnumerable<DependentModuleMetadata> DependenciesLoadBeforeThis(this ModuleInfoExtended module)
{
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x.LoadType == LoadType.LoadBeforeThis))
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x is not null).Where(x => x.LoadType == LoadType.LoadBeforeThis))
{
yield return metadata;
}
foreach (var metadata in module.DependentModules)
foreach (var metadata in module.DependentModules.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -144,11 +144,11 @@ public static IEnumerable<DependentModuleMetadata> DependenciesLoadBeforeThis(th
public static IEnumerable<DependentModuleMetadata> DependenciesLoadAfterThisDistinct(this ModuleInfoExtended module) => DependenciesLoadAfterThis(module).DistinctBy(x => x.Id);
public static IEnumerable<DependentModuleMetadata> DependenciesLoadAfterThis(this ModuleInfoExtended module)
{
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x.LoadType == LoadType.LoadAfterThis))
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x is not null).Where(x => x.LoadType == LoadType.LoadAfterThis))
{
yield return metadata;
}
foreach (var metadata in module.ModulesToLoadAfterThis)
foreach (var metadata in module.ModulesToLoadAfterThis.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand All @@ -163,11 +163,11 @@ public static IEnumerable<DependentModuleMetadata> DependenciesLoadAfterThis(thi
public static IEnumerable<DependentModuleMetadata> DependenciesIncompatiblesDistinct(this ModuleInfoExtended module) => DependenciesIncompatibles(module).DistinctBy(x => x.Id);
public static IEnumerable<DependentModuleMetadata> DependenciesIncompatibles(this ModuleInfoExtended module)
{
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x.IsIncompatible))
foreach (var metadata in module.DependentModuleMetadatas.Where(x => x is not null).Where(x => x.IsIncompatible))
{
yield return metadata;
}
foreach (var metadata in module.IncompatibleModules)
foreach (var metadata in module.IncompatibleModules.Where(x => x is not null))
{
yield return new DependentModuleMetadata
{
Expand Down
9 changes: 5 additions & 4 deletions src/Bannerlord.ModuleManager/ModuleUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleCommonData(ModuleInfoExtend
Reason = $"Module Name is missing in '{module.Id}'"
};
}
foreach (var dependentModule in module.DependentModules)
foreach (var dependentModule in module.DependentModules.Where(x => x is not null))
{
if (dependentModule is null)
{
Expand All @@ -221,7 +221,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleCommonData(ModuleInfoExtend
};
}
}
foreach (var dependentModuleMetadata in module.DependentModuleMetadatas)
foreach (var dependentModuleMetadata in module.DependentModuleMetadatas.Where(x => x is not null))
{
if (dependentModuleMetadata is null)
{
Expand Down Expand Up @@ -249,6 +249,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleCommonData(ModuleInfoExtend
public static IEnumerable<ModuleIssue> ValidateModuleDependenciesDeclarations(IReadOnlyList<ModuleInfoExtended> modules, ModuleInfoExtended targetModule)
{
// Any Incompatible module is depended on
// TODO: Will a null Id break things?
foreach (var moduleId in targetModule.DependenciesToLoadDistinct().Select(x => x.Id).Intersect(targetModule.DependenciesIncompatiblesDistinct().Select(x => x.Id)))
{
yield return new ModuleIssue(targetModule, moduleId, ModuleIssueType.DependencyConflictDependentAndIncompatible)
Expand All @@ -257,7 +258,7 @@ public static IEnumerable<ModuleIssue> ValidateModuleDependenciesDeclarations(IR
};
}
// Check raw metadata too
foreach (var dependency in targetModule.DependentModuleMetadatas.Where(x => x.IsIncompatible && x.LoadType != LoadType.None))
foreach (var dependency in targetModule.DependentModuleMetadatas.Where(x => x is not null).Where(x => x.IsIncompatible && x.LoadType != LoadType.None))
{
yield return new ModuleIssue(targetModule, dependency.Id, ModuleIssueType.DependencyConflictDependentAndIncompatible)
{
Expand Down Expand Up @@ -505,7 +506,7 @@ public static IEnumerable<ModuleIssue> ValidateLoadOrder(IReadOnlyList<ModuleInf
// Check that all dependencies are present
foreach (var metadata in targetModule.DependenciesToLoad().DistinctBy(x => x.Id))
{
var metadataIdx = CollectionsExtensions.IndexOf(modules, x => x.Id == metadata.Id);
var metadataIdx = CollectionsExtensions.IndexOf(modules, x => string.Equals(x.Id, metadata.Id, StringComparison.Ordinal));
if (metadataIdx == -1)
{
if (!metadata.IsOptional)
Expand Down
4 changes: 2 additions & 2 deletions test/Bannerlord.ModuleManager.Native.Tests/Utils2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static partial class Utils2
public static int LibraryAliveCount() => common_alloc_alive_count();

public static unsafe ReadOnlySpan<char> ToSpan(param_string* value) => new SafeStringMallocHandle((char*) value, false).ToSpan();
public static SafeStringMallocHandle ToJson<T>(T value) => Utils.SerializeJsonCopy(value, (JsonTypeInfo<T>) CustomSourceGenerationContext.GetTypeInfo(typeof(T)), true);
public static SafeStringMallocHandle ToJson<T>(T value) where T : class => Utils.SerializeJsonCopy(value, (JsonTypeInfo<T>) CustomSourceGenerationContext.GetTypeInfo(typeof(T)), true);
private static TValue DeserializeJson<TValue>(SafeStringMallocHandle json, JsonTypeInfo<TValue> jsonTypeInfo, [CallerMemberName] string? caller = null)
{
if (json.DangerousGetHandle() == IntPtr.Zero)
Expand All @@ -66,7 +66,7 @@ private static TValue DeserializeJson<TValue>([StringSyntax(StringSyntaxAttribut
}
}

public static unsafe T? GetResult<T>(return_value_json* ret)
public static unsafe T? GetResult<T>(return_value_json* ret) where T : class
{
using var result = SafeStructMallocHandle.Create(ret, true);
return result.ValueAsJson((JsonTypeInfo<T>) CustomSourceGenerationContext.GetTypeInfo(typeof(T)));
Expand Down

0 comments on commit 3e8490a

Please sign in to comment.