This commit is contained in:
@@ -34,17 +34,22 @@ namespace ShrinkModFramework
|
||||
private static bool _resolveRegistered;
|
||||
private static int _lastWarnedResidentCount;
|
||||
|
||||
public static IReadOnlyList<Assembly> LoadExternalAssemblies(ShrinkModFrameworkSettings settings, bool verboseLogging)
|
||||
public static IReadOnlyList<Assembly> LoadExternalAssemblies(
|
||||
ShrinkModFrameworkSettings settings,
|
||||
bool verboseLogging,
|
||||
IEnumerable<string> authorizedDllPaths = null)
|
||||
{
|
||||
return ScanExternalAssemblyRevisions(settings, verboseLogging)
|
||||
return ScanExternalAssemblyRevisions(settings, verboseLogging, authorizedDllPaths)
|
||||
.Select(revision => revision.Assembly)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
internal static IReadOnlyList<ExternalAssemblyRevision> ScanExternalAssemblyRevisions(
|
||||
ShrinkModFrameworkSettings settings, bool verboseLogging)
|
||||
ShrinkModFrameworkSettings settings,
|
||||
bool verboseLogging,
|
||||
IEnumerable<string> authorizedDllPaths = null)
|
||||
{
|
||||
if (settings != null && !settings.enableExternalDllMods)
|
||||
if (settings == null || !settings.enableExternalDllMods)
|
||||
{
|
||||
CurrentAssemblyRevisions.Clear();
|
||||
KnownAssemblyFiles.Clear();
|
||||
@@ -52,27 +57,23 @@ namespace ShrinkModFramework
|
||||
}
|
||||
|
||||
#if ENABLE_IL2CPP && !UNITY_EDITOR
|
||||
Debug.LogWarning("[ShrinkModFramework] IL2CPP 运行时不支持外部 DLL 热加载,已跳过外部模组扫描。");
|
||||
Debug.LogWarning("[ShrinkModFramework] IL2CPP 运行时不支持外部 DLL 加载,已跳过授权模组。");
|
||||
return Array.Empty<ExternalAssemblyRevision>();
|
||||
#else
|
||||
var modsDirectory = GetExternalModsDirectory(settings);
|
||||
if (settings == null || settings.autoCreateExternalModsDirectory)
|
||||
Directory.CreateDirectory(modsDirectory);
|
||||
|
||||
RegisterAssemblyResolve();
|
||||
|
||||
var dllPaths = Directory.GetFiles(modsDirectory, "*.dll", SearchOption.AllDirectories)
|
||||
.Select(Path.GetFullPath)
|
||||
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
var presentPaths = new HashSet<string>(dllPaths, StringComparer.OrdinalIgnoreCase);
|
||||
var dllPaths = NormalizeAuthorizedPaths(authorizedDllPaths);
|
||||
var presentPaths = new HashSet<string>(
|
||||
dllPaths.Where(File.Exists),
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var dllPath in dllPaths
|
||||
KnownAssemblyFiles.Clear();
|
||||
foreach (var dllPath in presentPaths)
|
||||
KnownAssemblyFiles[Path.GetFileNameWithoutExtension(dllPath)] = dllPath;
|
||||
|
||||
foreach (var dllPath in presentPaths
|
||||
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
var assemblyName = Path.GetFileNameWithoutExtension(dllPath);
|
||||
KnownAssemblyFiles[assemblyName] = dllPath;
|
||||
|
||||
try
|
||||
{
|
||||
var bytes = File.ReadAllBytes(dllPath);
|
||||
@@ -131,6 +132,41 @@ namespace ShrinkModFramework
|
||||
#endif
|
||||
}
|
||||
|
||||
private static string[] NormalizeAuthorizedPaths(IEnumerable<string> authorizedDllPaths)
|
||||
{
|
||||
if (authorizedDllPaths == null)
|
||||
return Array.Empty<string>();
|
||||
|
||||
var normalized = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var rawPath in authorizedDllPaths)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(rawPath))
|
||||
continue;
|
||||
|
||||
string fullPath;
|
||||
try
|
||||
{
|
||||
fullPath = Path.GetFullPath(rawPath.Trim());
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new ArgumentException($"授权 DLL 路径无效:{rawPath}",
|
||||
nameof(authorizedDllPaths), exception);
|
||||
}
|
||||
|
||||
if (!string.Equals(Path.GetExtension(fullPath), ".dll",
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new ArgumentException($"授权路径不是 DLL:{fullPath}",
|
||||
nameof(authorizedDllPaths));
|
||||
}
|
||||
|
||||
normalized.Add(fullPath);
|
||||
}
|
||||
|
||||
return normalized.OrderBy(path => path, StringComparer.OrdinalIgnoreCase).ToArray();
|
||||
}
|
||||
|
||||
internal static bool IsExternalAssembly(Assembly assembly) =>
|
||||
assembly != null && ExternalAssemblyHistory.ContainsKey(assembly);
|
||||
|
||||
@@ -268,13 +304,38 @@ namespace ShrinkModFramework
|
||||
if (!KnownAssemblyFiles.TryGetValue(requestedName, out var path) || !File.Exists(path))
|
||||
return null;
|
||||
|
||||
try
|
||||
lock (ResolveLock)
|
||||
{
|
||||
return Assembly.Load(File.ReadAllBytes(path));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
try
|
||||
{
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
var revision = ComputeSha256(bytes);
|
||||
if (CurrentAssemblyRevisions.TryGetValue(path, out var current) &&
|
||||
string.Equals(current.Revision, revision, StringComparison.Ordinal))
|
||||
{
|
||||
return current.Assembly;
|
||||
}
|
||||
|
||||
var pdbPath = Path.ChangeExtension(path, ".pdb");
|
||||
var pdbBytes = File.Exists(pdbPath) ? File.ReadAllBytes(pdbPath) : null;
|
||||
var assembly = pdbBytes != null
|
||||
? Assembly.Load(bytes, pdbBytes)
|
||||
: Assembly.Load(bytes);
|
||||
var loadedRevision = new ExternalAssemblyRevision
|
||||
{
|
||||
Path = path,
|
||||
Revision = revision,
|
||||
Assembly = assembly,
|
||||
LoadedBytes = bytes.LongLength + (pdbBytes?.LongLength ?? 0L)
|
||||
};
|
||||
CurrentAssemblyRevisions[path] = loadedRevision;
|
||||
ExternalAssemblyHistory[assembly] = loadedRevision;
|
||||
return assembly;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user