Skip to content

Commit ad3733c

Browse files
Merge pull request #1068 from ps5mh/master
Revert #1030 related commits
2 parents d860056 + d919198 commit ad3733c

30 files changed

+224
-392
lines changed

Assets/XLua/Examples/13_BuildFromCLI/Editor/BuildFromCLI.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static void Build()
3232
private static void build(string outputDir, string packageName)
3333
{
3434
Debug.Log("构建开始:输出目录 " + outputDir);
35+
DelegateBridge.Gen_Flag = true;
3536
Generator.ClearAll();
3637
Generator.GenAll();
3738

Assets/XLua/Examples/ExampleGenConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static class ExampleGenConfig
7575
new List<string>(){"UnityEngine.CanvasRenderer", "onRequestRebuild"},
7676
new List<string>(){"UnityEngine.Light", "areaSize"},
7777
new List<string>(){"UnityEngine.Light", "lightmapBakeType"},
78-
#if UNITY_ANDROID || UNITY_STANDALONE_WIN
78+
#if UNITY_ANDROID
7979
new List<string>(){"UnityEngine.Light", "SetLightDirty"},
8080
new List<string>(){"UnityEngine.Light", "shadowRadius"},
8181
new List<string>(){"UnityEngine.Light", "shadowAngle"},

Assets/XLua/Src/DelegateBridge.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ public abstract class DelegateBridgeBase : LuaBase
3232

3333
protected int errorFuncRef;
3434

35-
protected RealStatePtr rawL;
36-
3735
public DelegateBridgeBase(int reference, LuaEnv luaenv) : base(reference, luaenv)
3836
{
3937
errorFuncRef = luaenv.errorFuncRef;
40-
rawL = luaenv.rawL;
4138
}
4239

4340
public bool TryGetDelegate(Type key, out Delegate value)
@@ -130,7 +127,7 @@ public partial class DelegateBridge : DelegateBridgeBase
130127
{
131128
internal static DelegateBridge[] DelegateBridgeList = new DelegateBridge[0];
132129

133-
public static bool Gen_Flag { get { return InternalGlobals.Gen_Flag; } }
130+
public static bool Gen_Flag = false;
134131

135132
public DelegateBridge(int reference, LuaEnv luaenv) : base(reference, luaenv)
136133
{

Assets/XLua/Src/Editor/Hotfix.cs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ public class Hotfix
260260
{
261261
private TypeReference objType = null;
262262
private TypeReference delegateBridgeType = null;
263-
private TypeReference delegateBridgeWrapType = null;
264263
private AssemblyDefinition injectAssembly = null;
265264

266265
private MethodReference delegateBridgeGetter = null;
@@ -279,22 +278,20 @@ public class Hotfix
279278

280279
private List<MethodDefinition> bridgeIndexByKey = null;
281280

282-
private bool isInjectAndGenTheSameAssembly = false;
281+
private bool isTheSameAssembly = false;
283282

284283
private int delegateId = 0;
285284

286-
public void Init(AssemblyDefinition injectAssembly, AssemblyDefinition xluaAssembly, AssemblyDefinition genAssembly, IEnumerable<string> searchDirectorys, Dictionary<string, int> hotfixCfg)
285+
public void Init(AssemblyDefinition injectAssembly, AssemblyDefinition xluaAssembly, IEnumerable<string> searchDirectorys, Dictionary<string, int> hotfixCfg)
287286
{
288-
isInjectAndGenTheSameAssembly = injectAssembly == genAssembly;
287+
isTheSameAssembly = injectAssembly == xluaAssembly;
289288
this.injectAssembly = injectAssembly;
290289
this.hotfixCfg = hotfixCfg;
291290
var injectModule = injectAssembly.MainModule;
292291
objType = injectModule.TypeSystem.Object;
293292

294293
var delegateBridgeTypeDef = xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.DelegateBridge");
295-
var delegateBridgeTypeWrapDef = genAssembly.MainModule.Types.SingleOrDefault(t => t.FullName == "XLua.DelegateBridge_Wrap");
296294
delegateBridgeType = injectModule.TryImport(delegateBridgeTypeDef);
297-
delegateBridgeWrapType = injectModule.TryImport(delegateBridgeTypeWrapDef);
298295
delegateBridgeGetter = injectModule.TryImport(xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixDelegateBridge")
299296
.Methods.Single(m => m.Name == "Get"));
300297
hotfixFlagGetter = injectModule.TryImport(xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixDelegateBridge")
@@ -309,7 +306,7 @@ public void Init(AssemblyDefinition injectAssembly, AssemblyDefinition xluaAssem
309306
inParams = injectModule.TryImport(delegateBridgeTypeDef.Methods.Single(m => m.Name == "InParams"));
310307
outParam = injectModule.TryImport(delegateBridgeTypeDef.Methods.Single(m => m.Name == "OutParam"));
311308

312-
hotfixBridgesDef = (from method in (delegateBridgeTypeWrapDef ?? delegateBridgeTypeDef).Methods
309+
hotfixBridgesDef = (from method in delegateBridgeTypeDef.Methods
313310
where method.Name.StartsWith("__Gen_Delegate_Imp")
314311
select method).ToList();
315312
hotfixBridgeToDelegate = new Dictionary<MethodDefinition, MethodDefinition>();
@@ -320,9 +317,18 @@ where method.Name.StartsWith("__Gen_Delegate_Imp")
320317
bridgeIndexByKey = new List<MethodDefinition>();
321318

322319
var resolverOfInjectAssembly = injectAssembly.MainModule.AssemblyResolver as BaseAssemblyResolver;
320+
var resolverOfXluaAssembly = xluaAssembly.MainModule.AssemblyResolver as BaseAssemblyResolver;
321+
if (!isTheSameAssembly)
322+
{
323+
resolverOfXluaAssembly.AddSearchDirectory(Path.GetDirectoryName(injectAssembly.MainModule.FullyQualifiedName));
324+
}
323325
Action<string> addSearchDirectory = (string dir) =>
324326
{
325327
resolverOfInjectAssembly.AddSearchDirectory(dir);
328+
if (!isTheSameAssembly)
329+
{
330+
resolverOfXluaAssembly.AddSearchDirectory(dir);
331+
}
326332
};
327333
addSearchDirectory("./Library/ScriptAssemblies/");
328334
foreach (var path in
@@ -493,7 +499,7 @@ bool findHotfixDelegate(MethodDefinition method, out MethodReference invoke, Hot
493499
{
494500
bool ignoreValueType = hotfixType.HasFlag(HotfixFlagInTool.ValueTypeBoxing);
495501

496-
bool isIntKey = hotfixType.HasFlag(HotfixFlagInTool.IntKey) && !method.DeclaringType.HasGenericParameters && isInjectAndGenTheSameAssembly;
502+
bool isIntKey = hotfixType.HasFlag(HotfixFlagInTool.IntKey) && !method.DeclaringType.HasGenericParameters && isTheSameAssembly;
497503

498504
bool isAdaptByDelegate = !isIntKey && hotfixType.HasFlag(HotfixFlagInTool.AdaptByDelegate);
499505

@@ -547,7 +553,7 @@ bool findHotfixDelegate(MethodDefinition method, out MethodReference invoke, Hot
547553
{
548554
continue;
549555
}
550-
invoke = (isInjectAndGenTheSameAssembly && !isAdaptByDelegate) ? hotfixBridgeDef : getDelegateInvokeFor(method, hotfixBridgeDef, ignoreValueType);
556+
invoke = (isTheSameAssembly && !isAdaptByDelegate) ? hotfixBridgeDef : getDelegateInvokeFor(method, hotfixBridgeDef, ignoreValueType);
551557
return true;
552558
}
553559
}
@@ -692,7 +698,7 @@ public bool InjectType(TypeReference hotfixAttributeType, TypeDefinition type)
692698
{
693699
return true;
694700
}
695-
CustomAttribute hotfixAttr = type.CustomAttributes.FirstOrDefault(ca => isSameType(ca.AttributeType, hotfixAttributeType));
701+
CustomAttribute hotfixAttr = type.CustomAttributes.FirstOrDefault(ca => ca.AttributeType == hotfixAttributeType);
696702
HotfixFlagInTool hotfixType;
697703
if (hotfixAttr != null)
698704
{
@@ -853,11 +859,10 @@ static void writeAssembly(AssemblyDefinition assembly, string assemblyPath)
853859
#endif
854860
}
855861

856-
public static void HotfixInject(string injectAssemblyPath, string xluaAssemblyPath, string genAssemblyPath, IEnumerable<string> searchDirectorys, string idMapFilePath, Dictionary<string, int> hotfixConfig)
862+
public static void HotfixInject(string injectAssemblyPath, string xluaAssemblyPath, IEnumerable<string> searchDirectorys, string idMapFilePath, Dictionary<string, int> hotfixConfig)
857863
{
858864
AssemblyDefinition injectAssembly = null;
859865
AssemblyDefinition xluaAssembly = null;
860-
AssemblyDefinition genAssembly = null;
861866
try
862867
{
863868
injectAssembly = readAssembly(injectAssemblyPath);
@@ -873,11 +878,9 @@ public static void HotfixInject(string injectAssemblyPath, string xluaAssemblyPa
873878

874879
xluaAssembly = (injectAssemblyPath == xluaAssemblyPath || injectAssembly.MainModule.FullyQualifiedName == xluaAssemblyPath) ?
875880
injectAssembly : readAssembly(xluaAssemblyPath);
876-
genAssembly = (injectAssemblyPath == genAssemblyPath || injectAssembly.MainModule.FullyQualifiedName == genAssemblyPath) ?
877-
injectAssembly : readAssembly(genAssemblyPath);
878881

879882
Hotfix hotfix = new Hotfix();
880-
hotfix.Init(injectAssembly, xluaAssembly, genAssembly, searchDirectorys, hotfixConfig);
883+
hotfix.Init(injectAssembly, xluaAssembly, searchDirectorys, hotfixConfig);
881884

882885
//var hotfixDelegateAttributeType = assembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixDelegateAttribute");
883886
var hotfixAttributeType = xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixAttribute");
@@ -1224,7 +1227,7 @@ bool injectMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
12241227

12251228
FieldReference fieldReference = null;
12261229
VariableDefinition injection = null;
1227-
bool isIntKey = hotfixType.HasFlag(HotfixFlagInTool.IntKey) && !type.HasGenericParameters && isInjectAndGenTheSameAssembly;
1230+
bool isIntKey = hotfixType.HasFlag(HotfixFlagInTool.IntKey) && !type.HasGenericParameters && isTheSameAssembly;
12281231
//isIntKey = !type.HasGenericParameters;
12291232

12301233
if (!isIntKey)
@@ -1282,7 +1285,6 @@ bool injectMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
12821285
{
12831286
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Ldc_I4, bridgeIndexByKey.Count));
12841287
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Call, delegateBridgeGetter));
1285-
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Castclass, delegateBridgeWrapType));
12861288
}
12871289
else
12881290
{
@@ -1368,7 +1370,7 @@ bool injectMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
13681370
bool injectGenericMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
13691371
{
13701372
//如果注入的是xlua所在之外的Assembly的话,不支持该方式
1371-
if (!isInjectAndGenTheSameAssembly)
1373+
if (!isTheSameAssembly)
13721374
{
13731375
return true;
13741376
}
@@ -1425,7 +1427,6 @@ bool injectGenericMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
14251427
processor.InsertBefore(insertPoint, jmpInstruction);
14261428
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Ldc_I4, bridgeIndexByKey.Count));
14271429
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Call, delegateBridgeGetter));
1428-
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Castclass, delegateBridgeWrapType));
14291430
processor.InsertBefore(insertPoint, processor.Create(OpCodes.Stloc, injection));
14301431
}
14311432
else
@@ -1662,12 +1663,6 @@ public static void HotfixInject(string assemblyDir)
16621663
return;
16631664
}
16641665

1665-
if (!DelegateBridge.Gen_Flag)
1666-
{
1667-
UnityEngine.Debug.LogError("You can't inject without genenerate code, try XLua->Generate Code");
1668-
return;
1669-
}
1670-
16711666
if (EditorApplication.isCompiling)
16721667
{
16731668
UnityEngine.Debug.LogError("You can't inject before the compilation is done");
@@ -1720,22 +1715,19 @@ public static void HotfixInject(string assemblyDir)
17201715
}
17211716
}
17221717

1723-
var genCodeAssemblyPathInProject = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
1724-
select assembly.GetType("XLua.DelegateBridge_Wrap")).FirstOrDefault(x => x != null).Module.FullyQualifiedName;
1725-
var genCodeAssemblyFileName = Path.GetFileName(genCodeAssemblyPathInProject);
1726-
var genCodeAssemblyPath = Path.Combine(assemblyDir, genCodeAssemblyFileName);
1727-
List< string> args = new List<string>() { assembly_csharp_path,
1728-
typeof(LuaEnv).Module.FullyQualifiedName,
1729-
genCodeAssemblyPath,
1730-
id_map_file_path, hotfix_cfg_in_editor };
1718+
#if UNITY_2019_1_OR_NEWER
1719+
List<string> args = new List<string>() { assembly_csharp_path, assembly_csharp_path, id_map_file_path, hotfix_cfg_in_editor };
1720+
#else
1721+
List<string> args = new List<string>() { assembly_csharp_path, typeof(LuaEnv).Module.FullyQualifiedName, id_map_file_path, hotfix_cfg_in_editor };
1722+
#endif
17311723

17321724
foreach (var path in
1733-
(from asm in AppDomain.CurrentDomain.GetAssemblies() select System.IO.Path.GetDirectoryName(asm.ManifestModule.FullyQualifiedName))
1725+
(from asm in AppDomain.CurrentDomain.GetAssemblies() select asm.ManifestModule.FullyQualifiedName)
17341726
.Distinct())
17351727
{
17361728
try
17371729
{
1738-
args.Add(path);
1730+
args.Add(System.IO.Path.GetDirectoryName(path));
17391731
}
17401732
catch (Exception)
17411733
{
@@ -1755,8 +1747,8 @@ public static void HotfixInject(string assemblyDir)
17551747
if (injectAssemblyPaths.Count > 1)
17561748
{
17571749
var injectAssemblyFileName = Path.GetFileName(injectAssemblyPath);
1758-
args[3] = CSObjectWrapEditor.GeneratorConfig.common_path + "Resources/hotfix_id_map_" + injectAssemblyFileName.Substring(0, injectAssemblyFileName.Length - 4) + ".lua.txt";
1759-
idMapFileNames.Add(args[3]);
1750+
args[2] = CSObjectWrapEditor.GeneratorConfig.common_path + "Resources/hotfix_id_map_" + injectAssemblyFileName.Substring(0, injectAssemblyFileName.Length - 4) + ".lua.txt";
1751+
idMapFileNames.Add(args[2]);
17601752
}
17611753
Process hotfix_injection = new Process();
17621754
hotfix_injection.StartInfo.FileName = mono_path;

0 commit comments

Comments
 (0)