Expanded ModDllLoader implementation

This commit is contained in:
Chomp
2025-02-09 11:33:28 +00:00
parent 6c9ee63dc8
commit dec3956167
2 changed files with 44 additions and 15 deletions
+2 -2
View File
@@ -6,14 +6,14 @@ namespace Core.Models.Spt.Mod;
public class SptMod
{
[JsonPropertyName("PackageJson")]
public PackageJsonData PackageJson
public PackageJsonData? PackageJson
{
get;
set;
}
[JsonPropertyName("Assembly")]
public Assembly Assembly
public Assembly? Assembly
{
get;
set;
+42 -13
View File
@@ -23,10 +23,9 @@ public class ModDllLoader
// load both
// if either is missing Throw Warning and skip
var modDirectories = GetSortedModDirectories();
// TODO: Handle package.json 'Dependencies' property
var modDirectories = Directory.GetDirectories(ModPath);
// Load mods found in dir
foreach (var modDirectory in modDirectories)
{
try
@@ -39,15 +38,46 @@ public class ModDllLoader
}
}
ValidateModDependencies(mods);
// Sort by mods LoadBefore/LoadAfter collections
SortMods(mods);
return mods;
}
private static string[] GetSortedModDirectories()
/// <summary>
/// Ensure all mods have their dependencies
/// </summary>
/// <param name="mods">Mods to check dependencies of</param>
private static void ValidateModDependencies(List<SptMod> mods)
{
// TODO: add system to read from package.json and order mod paths by their LoadBefore/LoadAfter properties
return Directory.GetDirectories(ModPath);
foreach (var sptMod in mods)
{
if (sptMod.PackageJson?.Dependencies?.Count > 0)
{
// Has deps, validate they exist
foreach (var dependency in sptMod.PackageJson.Dependencies
.Where(dependency => !mods.Exists(x => x.PackageJson.Name.ToLower() == dependency.Key)))
{
// TODO: also check version passes semver check
throw new Exception($"Mod: {sptMod.PackageJson.Name} is unable to load as it cannot find another mod it needs: {dependency.Key} version: {dependency.Value}");
}
}
}
}
private static void SortMods(List<SptMod> mods)
{
//TODO: implement
Console.WriteLine($"NOT IMPLEMENTED: SortMods");
}
/// <summary>
/// Check the provided directory path for a dll and .json file, load into memory
/// </summary>
/// <param name="path">Directory path that contains mod files</param>
/// <returns>SptMod</returns>
private static SptMod LoadMod(string path)
{
var result = new SptMod();
@@ -59,10 +89,9 @@ public class ModDllLoader
{
packCount++;
// deal with package.json
var jjson = File.ReadAllText(file.FullName);
var json = JsonSerializer.Deserialize<PackageJsonData>(jjson);
result.PackageJson = json;
// Handle package.json
var rawJson = File.ReadAllText(file.FullName);
result.PackageJson = JsonSerializer.Deserialize<PackageJsonData>(rawJson);
if (packCount > 1)
{
throw new Exception($"More than one package.json file found in path: {path}");
@@ -76,14 +105,14 @@ public class ModDllLoader
result.Assembly = Assembly.LoadFile(Path.GetFullPath(file.FullName));
if (asmCount > 1)
{
throw new Exception($"More than one Assembly found in {path}");
throw new Exception($"More than one Assembly found in: {path}");
}
}
}
if (asmCount == 0 && packCount == 0)
{
throw new Exception($"No Assembly or package.json found in {Path.GetFullPath(path)}");
throw new Exception($"No Assembly or package.json found in: {Path.GetFullPath(path)}");
}
if (packCount == 0)
@@ -98,7 +127,7 @@ public class ModDllLoader
if (result.Assembly is not null && result.PackageJson is not null)
{
Console.WriteLine($"Loaded {result.PackageJson.Name} mod by {result.PackageJson.Author}");
Console.WriteLine($"Loaded: {result.PackageJson.Name} Version: {result.PackageJson.Version} by: {result.PackageJson.Author}");
}
return result;