框架更新

This commit is contained in:
Bob.Song
2025-10-29 17:59:43 +08:00
parent fc18c8626a
commit a2cb248512
429 changed files with 7173 additions and 38748 deletions

View File

@@ -1,30 +1,27 @@
using System.Reflection;
using System.Runtime.Loader;
using System.Runtime.Loader;
using Fantasy.Generated;
namespace NB
namespace Fantasy
{
public static class AssemblyHelper
{
private const string HotfixDll = "Hotfix";
private static AssemblyLoadContext? _assemblyLoadContext = null;
public static System.Reflection.Assembly[] Assemblies
public static void Initialize()
{
get
{
var assemblies = new System.Reflection.Assembly[2];
assemblies[0] = LoadEntityAssembly();
assemblies[1] = LoadHotfixAssembly();
return assemblies;
}
LoadEntityAssembly();
LoadHotfixAssembly();
}
private static System.Reflection.Assembly LoadEntityAssembly()
private static void LoadEntityAssembly()
{
return typeof(AssemblyHelper).Assembly;
// .NET 运行时采用延迟加载机制,如果代码中不使用程序集的类型,程序集不会被加载
// 执行一下,触发运行时强制加载从而自动注册到框架中
Entity_AssemblyMarker.EnsureLoaded();
}
private static System.Reflection.Assembly LoadHotfixAssembly()
public static System.Reflection.Assembly LoadHotfixAssembly()
{
if (_assemblyLoadContext != null)
{
@@ -33,9 +30,20 @@ namespace NB
}
_assemblyLoadContext = new AssemblyLoadContext(HotfixDll, true);
var dllBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, $"{HotfixDll}.dll"));
var pdbBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, $"{HotfixDll}.pdb"));
return _assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
var dllBytes = File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, $"{HotfixDll}.dll"));
var pdbBytes = File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, $"{HotfixDll}.pdb"));
var assembly = _assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
// 强制触发 ModuleInitializer 执行
// AssemblyLoadContext.LoadFromStream 只加载程序集到内存,不会自动触发 ModuleInitializer
// 必须访问程序集中的类型才能触发初始化,这里通过反射调用生成的 AssemblyMarker
// 注意此方法仅用于热重载场景JITNative AOT 不支持动态加载
var markerType = assembly.GetType("Fantasy.Generated.Hotfix_AssemblyMarker");
if (markerType != null)
{
var method = markerType.GetMethod("EnsureLoaded");
method?.Invoke(null, null);
}
return assembly;
}
}
}