From f4d89bb420526ccb5c4b0db740ca6ecc8f975ce5 Mon Sep 17 00:00:00 2001
From: Alex-Rachel <574809918@qq.com>
Date: Mon, 17 Mar 2025 17:31:42 +0800
Subject: [PATCH] =?UTF-8?q?EmitHelper=E5=8A=A8=E6=80=81=E6=96=B9=E6=B3=95?=
=?UTF-8?q?=E4=B8=8D=E6=94=AF=E6=8C=81IL2CPP?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../GameLogic/Module/UIModule/UIModule.cs | 2 +-
.../MemoryPool/MemoryPool.MemoryCollection.cs | 4 +-
.../TEngine/Runtime/Core/ModuleSystem.cs | 2 +-
.../Runtime/Core/Utility/EmitHelper.cs | 193 ------------------
.../Runtime/Core/Utility/EmitHelper.cs.meta | 3 -
.../ObjectPoolModule/ObjectPoolModule.cs | 2 +-
.../ProcedureModule/ProcedureSetting.cs | 2 +-
.../TEngine/Runtime/Module/RootModule.cs | 6 +-
8 files changed, 9 insertions(+), 205 deletions(-)
delete mode 100644 UnityProject/Assets/TEngine/Runtime/Core/Utility/EmitHelper.cs
delete mode 100644 UnityProject/Assets/TEngine/Runtime/Core/Utility/EmitHelper.cs.meta
diff --git a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Module/UIModule/UIModule.cs b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Module/UIModule/UIModule.cs
index 11d9444b..c2d48931 100644
--- a/UnityProject/Assets/GameScripts/HotFix/GameLogic/Module/UIModule/UIModule.cs
+++ b/UnityProject/Assets/GameScripts/HotFix/GameLogic/Module/UIModule/UIModule.cs
@@ -538,7 +538,7 @@ namespace GameLogic
private UIWindow CreateInstance(Type type)
{
- UIWindow window = EmitHelper.CreateInstance(type) as UIWindow;
+ UIWindow window = Activator.CreateInstance(type) as UIWindow;
WindowAttribute attribute = Attribute.GetCustomAttribute(type, typeof(WindowAttribute)) as WindowAttribute;
if (window == null)
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPool.MemoryCollection.cs b/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPool.MemoryCollection.cs
index 2d222e41..aba4bcf0 100644
--- a/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPool.MemoryCollection.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Core/MemoryPool/MemoryPool.MemoryCollection.cs
@@ -77,7 +77,7 @@ namespace TEngine
}
_addMemoryCount++;
- return (IMemory)EmitHelper.CreateInstance(_memoryType);
+ return (IMemory)Activator.CreateInstance(_memoryType);
}
public void Release(IMemory memory)
@@ -121,7 +121,7 @@ namespace TEngine
_addMemoryCount += count;
while (count-- > 0)
{
- _memories.Enqueue((IMemory)EmitHelper.CreateInstance(_memoryType));
+ _memories.Enqueue((IMemory)Activator.CreateInstance(_memoryType));
}
}
}
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/ModuleSystem.cs b/UnityProject/Assets/TEngine/Runtime/Core/ModuleSystem.cs
index d7eb37e1..401590d4 100644
--- a/UnityProject/Assets/TEngine/Runtime/Core/ModuleSystem.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Core/ModuleSystem.cs
@@ -106,7 +106,7 @@ namespace TEngine
/// 要创建的游戏框架模块。
private static Module CreateModule(Type moduleType)
{
- Module module = (Module)EmitHelper.CreateInstance(moduleType);
+ Module module = (Module)Activator.CreateInstance(moduleType);
if (module == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not create module '{0}'.", moduleType.FullName));
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/Utility/EmitHelper.cs b/UnityProject/Assets/TEngine/Runtime/Core/Utility/EmitHelper.cs
deleted file mode 100644
index bf30026b..00000000
--- a/UnityProject/Assets/TEngine/Runtime/Core/Utility/EmitHelper.cs
+++ /dev/null
@@ -1,193 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using System.Reflection.Emit;
-
-namespace TEngine
-{
- public static class EmitHelper
- {
- private static readonly Dictionary _defaultCache = new Dictionary();
-
- private static readonly Dictionary _argCache = new Dictionary();
-
- public static T CreateInstance() where T : new()
- {
- var type = typeof(T);
-
- if (!_defaultCache.TryGetValue(type, out var factory))
- {
- if (!_defaultCache.TryGetValue(type, out factory))
- {
- factory = CreateFactory();
- _defaultCache.TryAdd(type, factory);
- }
- }
-
- return ((Func)factory)();
- }
-
- public static object CreateInstance(Type type)
- {
- if (!_defaultCache.TryGetValue(type, out var factory))
- {
- lock (_defaultCache)
- {
- if (!_defaultCache.TryGetValue(type, out factory))
- {
- var constructor = type.GetConstructor(
- BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
- null, Type.EmptyTypes, null);
-
- if (constructor == null)
- {
- throw new MissingMethodException($"No parameterless constructor defined for type '{type.FullName}'");
- }
-
- // 创建动态方法(关键优化点)
- var dynamicMethod = new DynamicMethod(
- name: $"CreateInstance_{type.FullName}",
- returnType: typeof(object),
- parameterTypes: Type.EmptyTypes,
- owner: typeof(object),
- skipVisibility: true);
-
- // 生成IL指令(核心逻辑)
- var il = dynamicMethod.GetILGenerator();
-
- // 处理值类型和引用类型的差异
- if (type.IsValueType)
- {
- il.DeclareLocal(type);
- il.Emit(OpCodes.Ldloca_S, 0);
- il.Emit(OpCodes.Initobj, type);
- il.Emit(OpCodes.Ldloc_0);
- il.Emit(OpCodes.Box, type); // 值类型需要装箱
- }
- else
- {
- il.Emit(OpCodes.Newobj, constructor);
- }
-
- il.Emit(OpCodes.Ret);
-
- factory = dynamicMethod.CreateDelegate(typeof(Func