Skip to content

Commit 26ac91d

Browse files
committed
+ [feature] support unity asmdef
1 parent cb354ec commit 26ac91d

21 files changed

+365
-220
lines changed

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

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

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
78+
#if UNITY_ANDROID || UNITY_STANDALONE_WIN
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public partial class DelegateBridge : DelegateBridgeBase
127127
{
128128
internal static DelegateBridge[] DelegateBridgeList = new DelegateBridge[0];
129129

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

132132
public DelegateBridge(int reference, LuaEnv luaenv) : base(reference, luaenv)
133133
{

Assets/XLua/Src/Editor/Hotfix.cs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,20 @@ public class Hotfix
278278

279279
private List<MethodDefinition> bridgeIndexByKey = null;
280280

281-
private bool isTheSameAssembly = false;
281+
private bool isInjectAndGenTheSameAssembly = false;
282282

283283
private int delegateId = 0;
284284

285-
public void Init(AssemblyDefinition injectAssembly, AssemblyDefinition xluaAssembly, IEnumerable<string> searchDirectorys, Dictionary<string, int> hotfixCfg)
285+
public void Init(AssemblyDefinition injectAssembly, AssemblyDefinition xluaAssembly, AssemblyDefinition genAssembly, IEnumerable<string> searchDirectorys, Dictionary<string, int> hotfixCfg)
286286
{
287-
isTheSameAssembly = injectAssembly == xluaAssembly;
287+
isInjectAndGenTheSameAssembly = injectAssembly == genAssembly;
288288
this.injectAssembly = injectAssembly;
289289
this.hotfixCfg = hotfixCfg;
290290
var injectModule = injectAssembly.MainModule;
291291
objType = injectModule.TypeSystem.Object;
292292

293293
var delegateBridgeTypeDef = xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.DelegateBridge");
294+
var delegateBridgeTypeWrapDef = genAssembly.MainModule.Types.SingleOrDefault(t => t.FullName == "XLua.DelegateBridge_Wrap");
294295
delegateBridgeType = injectModule.TryImport(delegateBridgeTypeDef);
295296
delegateBridgeGetter = injectModule.TryImport(xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixDelegateBridge")
296297
.Methods.Single(m => m.Name == "Get"));
@@ -306,7 +307,7 @@ public void Init(AssemblyDefinition injectAssembly, AssemblyDefinition xluaAssem
306307
inParams = injectModule.TryImport(delegateBridgeTypeDef.Methods.Single(m => m.Name == "InParams"));
307308
outParam = injectModule.TryImport(delegateBridgeTypeDef.Methods.Single(m => m.Name == "OutParam"));
308309

309-
hotfixBridgesDef = (from method in delegateBridgeTypeDef.Methods
310+
hotfixBridgesDef = (from method in (delegateBridgeTypeWrapDef ?? delegateBridgeTypeDef).Methods
310311
where method.Name.StartsWith("__Gen_Delegate_Imp")
311312
select method).ToList();
312313
hotfixBridgeToDelegate = new Dictionary<MethodDefinition, MethodDefinition>();
@@ -317,18 +318,9 @@ where method.Name.StartsWith("__Gen_Delegate_Imp")
317318
bridgeIndexByKey = new List<MethodDefinition>();
318319

319320
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-
}
325321
Action<string> addSearchDirectory = (string dir) =>
326322
{
327323
resolverOfInjectAssembly.AddSearchDirectory(dir);
328-
if (!isTheSameAssembly)
329-
{
330-
resolverOfXluaAssembly.AddSearchDirectory(dir);
331-
}
332324
};
333325
addSearchDirectory("./Library/ScriptAssemblies/");
334326
foreach (var path in
@@ -499,7 +491,7 @@ bool findHotfixDelegate(MethodDefinition method, out MethodReference invoke, Hot
499491
{
500492
bool ignoreValueType = hotfixType.HasFlag(HotfixFlagInTool.ValueTypeBoxing);
501493

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

504496
bool isAdaptByDelegate = !isIntKey && hotfixType.HasFlag(HotfixFlagInTool.AdaptByDelegate);
505497

@@ -553,7 +545,7 @@ bool findHotfixDelegate(MethodDefinition method, out MethodReference invoke, Hot
553545
{
554546
continue;
555547
}
556-
invoke = (isTheSameAssembly && !isAdaptByDelegate) ? hotfixBridgeDef : getDelegateInvokeFor(method, hotfixBridgeDef, ignoreValueType);
548+
invoke = (isInjectAndGenTheSameAssembly && !isAdaptByDelegate) ? hotfixBridgeDef : getDelegateInvokeFor(method, hotfixBridgeDef, ignoreValueType);
557549
return true;
558550
}
559551
}
@@ -692,7 +684,7 @@ public bool InjectType(TypeReference hotfixAttributeType, TypeDefinition type)
692684
{
693685
return true;
694686
}
695-
CustomAttribute hotfixAttr = type.CustomAttributes.FirstOrDefault(ca => ca.AttributeType == hotfixAttributeType);
687+
CustomAttribute hotfixAttr = type.CustomAttributes.FirstOrDefault(ca => isSameType(ca.AttributeType, hotfixAttributeType));
696688
HotfixFlagInTool hotfixType;
697689
if (hotfixAttr != null)
698690
{
@@ -853,10 +845,11 @@ static void writeAssembly(AssemblyDefinition assembly, string assemblyPath)
853845
#endif
854846
}
855847

856-
public static void HotfixInject(string injectAssemblyPath, string xluaAssemblyPath, IEnumerable<string> searchDirectorys, string idMapFilePath, Dictionary<string, int> hotfixConfig)
848+
public static void HotfixInject(string injectAssemblyPath, string xluaAssemblyPath, string genAssemblyPath, IEnumerable<string> searchDirectorys, string idMapFilePath, Dictionary<string, int> hotfixConfig)
857849
{
858850
AssemblyDefinition injectAssembly = null;
859851
AssemblyDefinition xluaAssembly = null;
852+
AssemblyDefinition genAssembly = null;
860853
try
861854
{
862855
injectAssembly = readAssembly(injectAssemblyPath);
@@ -872,9 +865,11 @@ public static void HotfixInject(string injectAssemblyPath, string xluaAssemblyPa
872865

873866
xluaAssembly = (injectAssemblyPath == xluaAssemblyPath || injectAssembly.MainModule.FullyQualifiedName == xluaAssemblyPath) ?
874867
injectAssembly : readAssembly(xluaAssemblyPath);
868+
genAssembly = (injectAssemblyPath == genAssemblyPath || injectAssembly.MainModule.FullyQualifiedName == genAssemblyPath) ?
869+
injectAssembly : readAssembly(genAssemblyPath);
875870

876871
Hotfix hotfix = new Hotfix();
877-
hotfix.Init(injectAssembly, xluaAssembly, searchDirectorys, hotfixConfig);
872+
hotfix.Init(injectAssembly, xluaAssembly, genAssembly, searchDirectorys, hotfixConfig);
878873

879874
//var hotfixDelegateAttributeType = assembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixDelegateAttribute");
880875
var hotfixAttributeType = xluaAssembly.MainModule.Types.Single(t => t.FullName == "XLua.HotfixAttribute");
@@ -1203,7 +1198,7 @@ bool injectMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
12031198

12041199
FieldReference fieldReference = null;
12051200
VariableDefinition injection = null;
1206-
bool isIntKey = hotfixType.HasFlag(HotfixFlagInTool.IntKey) && !type.HasGenericParameters && isTheSameAssembly;
1201+
bool isIntKey = hotfixType.HasFlag(HotfixFlagInTool.IntKey) && !type.HasGenericParameters && isInjectAndGenTheSameAssembly;
12071202
//isIntKey = !type.HasGenericParameters;
12081203

12091204
if (!isIntKey)
@@ -1345,11 +1340,6 @@ bool injectMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
13451340

13461341
bool injectGenericMethod(MethodDefinition method, HotfixFlagInTool hotfixType)
13471342
{
1348-
//如果注入的是xlua所在之外的Assembly的话,不支持该方式
1349-
if (!isTheSameAssembly)
1350-
{
1351-
return true;
1352-
}
13531343
var type = method.DeclaringType;
13541344

13551345
bool isFinalize = (method.Name == "Finalize" && method.IsSpecialName);
@@ -1639,6 +1629,12 @@ public static void HotfixInject(string assemblyDir)
16391629
return;
16401630
}
16411631

1632+
if (!DelegateBridge.Gen_Flag)
1633+
{
1634+
UnityEngine.Debug.LogError("You can't inject without genenerate code, try XLua->Generate Code");
1635+
return;
1636+
}
1637+
16421638
if (EditorApplication.isCompiling)
16431639
{
16441640
UnityEngine.Debug.LogError("You can't inject before the compilation is done");
@@ -1691,19 +1687,21 @@ public static void HotfixInject(string assemblyDir)
16911687
}
16921688
}
16931689

1694-
#if UNITY_2019_1_OR_NEWER
1695-
List<string> args = new List<string>() { assembly_csharp_path, assembly_csharp_path, id_map_file_path, hotfix_cfg_in_editor };
1696-
#else
1697-
List<string> args = new List<string>() { assembly_csharp_path, typeof(LuaEnv).Module.FullyQualifiedName, id_map_file_path, hotfix_cfg_in_editor };
1698-
#endif
1690+
var genCodeAssemblyPath = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
1691+
select assembly.GetType("XLua.DelegateBridge_Wrap")).FirstOrDefault(x => x != null).Module.FullyQualifiedName;
1692+
1693+
List< string> args = new List<string>() { assembly_csharp_path,
1694+
typeof(LuaEnv).Module.FullyQualifiedName,
1695+
genCodeAssemblyPath,
1696+
id_map_file_path, hotfix_cfg_in_editor };
16991697

17001698
foreach (var path in
1701-
(from asm in AppDomain.CurrentDomain.GetAssemblies() select asm.ManifestModule.FullyQualifiedName)
1699+
(from asm in AppDomain.CurrentDomain.GetAssemblies() select System.IO.Path.GetDirectoryName(asm.ManifestModule.FullyQualifiedName))
17021700
.Distinct())
17031701
{
17041702
try
17051703
{
1706-
args.Add(System.IO.Path.GetDirectoryName(path));
1704+
args.Add(path);
17071705
}
17081706
catch (Exception)
17091707
{
@@ -1723,8 +1721,8 @@ public static void HotfixInject(string assemblyDir)
17231721
if (injectAssemblyPaths.Count > 1)
17241722
{
17251723
var injectAssemblyFileName = Path.GetFileName(injectAssemblyPath);
1726-
args[2] = CSObjectWrapEditor.GeneratorConfig.common_path + "Resources/hotfix_id_map_" + injectAssemblyFileName.Substring(0, injectAssemblyFileName.Length - 4) + ".lua.txt";
1727-
idMapFileNames.Add(args[2]);
1724+
args[3] = CSObjectWrapEditor.GeneratorConfig.common_path + "Resources/hotfix_id_map_" + injectAssemblyFileName.Substring(0, injectAssemblyFileName.Length - 4) + ".lua.txt";
1725+
idMapFileNames.Add(args[3]);
17281726
}
17291727
Process hotfix_injection = new Process();
17301728
hotfix_injection.StartInfo.FileName = mono_path;

0 commit comments

Comments
 (0)