Make mod loading non nullable, set certain properties to be read only after init (#506)

* Make mod loading non nullable, set certain properties to be read only after init

- Breaks all mods, will require new nugets

* Make mod assembly list IEnumerable

* Convert checks to IsNullOrEmpty

* Update comment, enforce ModGuid
This commit is contained in:
Jesse
2025-07-23 12:24:55 +02:00
committed by GitHub
parent 95ba8a5b06
commit 839e154adc
4 changed files with 34 additions and 32 deletions
+23 -19
View File
@@ -53,14 +53,12 @@ public class ModDllLoader
/// <returns>SptMod</returns>
private static SptMod LoadMod(string path)
{
var result = new SptMod { Directory = path, Assemblies = [] };
var assemblyCount = 0;
List<Assembly> assemblyList = [];
foreach (var file in new DirectoryInfo(path).GetFiles()) // Only search top level
{
if (string.Equals(file.Extension, ".dll", StringComparison.OrdinalIgnoreCase))
{
assemblyCount++;
result.Assemblies.Add(
assemblyList.Add(
AssemblyLoadContext.Default.LoadFromAssemblyPath(
Path.GetFullPath(file.FullName)
)
@@ -68,30 +66,29 @@ public class ModDllLoader
}
}
if (assemblyCount == 0)
if (assemblyList.Count == 0)
{
throw new Exception($"No Assemblies found in path: {Path.GetFullPath(path)}");
}
result.ModMetadata = LoadModMetadata(result.Assemblies, path);
if (result.ModMetadata == null)
SptMod result = new()
{
throw new Exception(
$"Failed to load mod metadata for: {Path.GetFullPath(path)} \ndid you override `AbstractModMetadata`?"
);
}
Directory = path,
Assemblies = assemblyList,
ModMetadata = LoadModMetadata(assemblyList, path),
};
if (
result.ModMetadata?.Name == null
|| result.ModMetadata?.Author == null
|| result.ModMetadata?.Version == null
|| result.ModMetadata?.Licence == null
|| result.ModMetadata?.SptVersion == null
string.IsNullOrEmpty(result.ModMetadata.ModGuid)
|| string.IsNullOrEmpty(result.ModMetadata.Name)
|| string.IsNullOrEmpty(result.ModMetadata.Author)
|| string.IsNullOrEmpty(result.ModMetadata.Version)
|| string.IsNullOrEmpty(result.ModMetadata.License)
|| string.IsNullOrEmpty(result.ModMetadata.SptVersion)
)
{
throw new Exception(
$"The mod metadata for: {Path.GetFullPath(path)} is missing one of these properties: name, author, licence, version or sptVersion"
$"The mod metadata for: {Path.GetFullPath(path)} is missing one of these properties: ModGuid, Name, Author, License, Version or SptVersion"
);
}
@@ -105,7 +102,7 @@ public class ModDllLoader
/// <param name="path">Path of the mod directory</param>
/// <returns>Mod metadata</returns>
/// <exception cref="Exception">Thrown if duplicate metadata implementations are found</exception>
private static AbstractModMetadata? LoadModMetadata(List<Assembly> assemblies, string path)
private static AbstractModMetadata LoadModMetadata(List<Assembly> assemblies, string path)
{
AbstractModMetadata? result = null;
@@ -131,6 +128,13 @@ public class ModDllLoader
}
}
if (result == null)
{
throw new Exception(
$"Failed to load mod metadata for: {Path.GetFullPath(path)} \ndid you override `AbstractModMetadata`?"
);
}
return result;
}
}