Skip to content

Commit

Permalink
Added common data validation, since we are able to construct the mode…
Browse files Browse the repository at this point in the history
…l from raw serialization
  • Loading branch information
Aragas committed May 28, 2024
1 parent 5dca8d9 commit 424007d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Bannerlord.ModuleManager.Models/ModuleIssueType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ enum ModuleIssueType

DependencyNotLoadedBeforeThis,
DependencyNotLoadedAfterThis,

MissingModuleId,
MissingModuleName,
DependencyIsNull,
DependencyMissingModuleId,
}
}
#nullable restore
Expand Down
66 changes: 64 additions & 2 deletions src/Bannerlord.ModuleManager/ModuleUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public static IEnumerable<ModuleIssue> ValidateModule(IReadOnlyList<ModuleInfoEx
/// <returns>Any error that were detected during inspection</returns>
public static IEnumerable<ModuleIssue> ValidateModule(IReadOnlyList<ModuleInfoExtended> modules, ModuleInfoExtended targetModule, HashSet<ModuleInfoExtended> visitedModules, Func<ModuleInfoExtended, bool> isSelected, Func<ModuleInfoExtended, bool> isValid)
{
// Validate common data
foreach (var issue in ValidateModuleCommonData(targetModule))
yield return issue;

// Validate dependency declaration
foreach (var issue in ValidateModuleDependenciesDeclarations(modules, targetModule))
yield return issue;
Expand All @@ -179,6 +183,64 @@ public static IEnumerable<ModuleIssue> ValidateModule(IReadOnlyList<ModuleInfoEx
foreach (var issue in ValidateModuleDependencies(modules, targetModule, visitedModules, isSelected, isValid))
yield return issue;
}

/// <summary>
/// Validates a module's common data
/// </summary>
public static IEnumerable<ModuleIssue> ValidateModuleCommonData(ModuleInfoExtended module)
{
if (string.IsNullOrWhiteSpace(module.Id))
{
yield return new ModuleIssue(module, "UNKNOWN", ModuleIssueType.MissingModuleId)
{
Reason = $"Module Id is missing for '{module.Name}'"
};
}
if (string.IsNullOrWhiteSpace(module.Name))
{
yield return new ModuleIssue(module, module.Id, ModuleIssueType.MissingModuleName)
{
Reason = $"Module Name is missing in '{module.Id}'"
};
}
foreach (var dependentModule in module.DependentModules)
{
if (dependentModule is null)
{
yield return new ModuleIssue(module, "UNKNOWN", ModuleIssueType.DependencyIsNull)
{
Reason = $"Found a null dependency in '{module.Id}'",
};
break;
}
if (string.IsNullOrWhiteSpace(dependentModule.Id))
{
yield return new ModuleIssue(module, "UNKNOWN", ModuleIssueType.DependencyMissingModuleId)
{
Reason = $"Module Id is missing for one if the dependencies of '{module.Id}'",
};
}
}
foreach (var dependentModuleMetadata in module.DependentModuleMetadatas)
{
if (dependentModuleMetadata is null)
{
yield return new ModuleIssue(module, "UNKNOWN", ModuleIssueType.DependencyIsNull)
{
Reason = $"Found a null dependency in '{module.Id}'",
};
break;
}
if (string.IsNullOrWhiteSpace(dependentModuleMetadata.Id))
{
yield return new ModuleIssue(module, "UNKNOWN", ModuleIssueType.DependencyMissingModuleId)
{
Reason = $"Module Id is missing for one if the dependencies of '{module.Id}'",
};
}
}
}

/// <summary>
/// Validates a module metadata
/// </summary>
Expand Down Expand Up @@ -475,15 +537,15 @@ public static IEnumerable<ModuleIssue> ValidateLoadOrder(IReadOnlyList<ModuleInf
{
yield return new ModuleIssue(targetModule, metadata.Id, ModuleIssueType.DependencyNotLoadedBeforeThis)
{
Reason = $"'{targetModule.Id}' should be loaded before '{metadata.Id}'"
Reason = $"'{metadata.Id}' should be loaded before '{targetModule.Id}'"
};
}

if (metadata.LoadType == LoadType.LoadAfterThis && metadataIdx < targetModuleIdx)
{
yield return new ModuleIssue(targetModule, metadata.Id, ModuleIssueType.DependencyNotLoadedAfterThis)
{
Reason = $"'{targetModule.Id}' should be loaded after '{metadata.Id}'"
Reason = $"'{metadata.Id}' should be loaded after '{targetModule.Id}'"
};
}
}
Expand Down

0 comments on commit 424007d

Please sign in to comment.