From dec395616777f7a39dfd4771c648bfa280abb287 Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 9 Feb 2025 11:33:28 +0000 Subject: [PATCH] Expanded `ModDllLoader` implementation --- Libraries/Core/Models/Spt/Mod/SptMod.cs | 4 +- Server/ModDllLoader.cs | 55 +++++++++++++++++++------ 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/Libraries/Core/Models/Spt/Mod/SptMod.cs b/Libraries/Core/Models/Spt/Mod/SptMod.cs index 33873231..ce471de2 100644 --- a/Libraries/Core/Models/Spt/Mod/SptMod.cs +++ b/Libraries/Core/Models/Spt/Mod/SptMod.cs @@ -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; diff --git a/Server/ModDllLoader.cs b/Server/ModDllLoader.cs index a7dd39dc..34c7cd87 100644 --- a/Server/ModDllLoader.cs +++ b/Server/ModDllLoader.cs @@ -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() + /// + /// Ensure all mods have their dependencies + /// + /// Mods to check dependencies of + private static void ValidateModDependencies(List 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 mods) + { + //TODO: implement + Console.WriteLine($"NOT IMPLEMENTED: SortMods"); + } + + /// + /// Check the provided directory path for a dll and .json file, load into memory + /// + /// Directory path that contains mod files + /// SptMod 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(jjson); - result.PackageJson = json; + // Handle package.json + var rawJson = File.ReadAllText(file.FullName); + result.PackageJson = JsonSerializer.Deserialize(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;