XLuaGenConfig.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Reflection;
  4. using System.Linq;
  5. // using XLua;
  6. //配置的详细介绍请看Doc下《XLua的配置.doc》
  7. public static class ExampleConfig
  8. {
  9. /***************如果你全lua编程,可以参考这份自动化配置***************/
  10. //--------------begin 纯lua编程配置参考----------------------------
  11. static List<string> exclude = new List<string> {
  12. "HideInInspector", "ExecuteInEditMode",
  13. "AddComponentMenu", "ContextMenu",
  14. "RequireComponent", "DisallowMultipleComponent",
  15. "SerializeField", "AssemblyIsEditorAssembly",
  16. "Attribute", "Types",
  17. "UnitySurrogateSelector", "TrackedReference",
  18. "TypeInferenceRules", "FFTWindow",
  19. "RPC", "Network", "MasterServer",
  20. "BitStream", "HostData",
  21. "ConnectionTesterStatus", "GUI", "EventType",
  22. "EventModifiers", "FontStyle", "TextAlignment",
  23. "TextEditor", "TextEditorDblClickSnapping",
  24. "TextGenerator", "TextClipping", "Gizmos",
  25. "ADBannerView", "ADInterstitialAd",
  26. "Android", "Tizen", "jvalue",
  27. "iPhone", "iOS", "Windows", "CalendarIdentifier",
  28. "CalendarUnit", "CalendarUnit",
  29. "ClusterInput", "FullScreenMovieControlMode",
  30. "FullScreenMovieScalingMode", "Handheld",
  31. "LocalNotification", "NotificationServices",
  32. "RemoteNotificationType", "RemoteNotification",
  33. "SamsungTV", "TextureCompressionQuality",
  34. "TouchScreenKeyboardType", "TouchScreenKeyboard",
  35. "MovieTexture", "UnityEngineInternal",
  36. "Terrain", "Tree", "SplatPrototype",
  37. "DetailPrototype", "DetailRenderMode",
  38. "MeshSubsetCombineUtility", "AOT", "Social", "Enumerator",
  39. "SendMouseEvents", "Cursor", "Flash", "ActionScript",
  40. "OnRequestRebuild", "Ping",
  41. "ShaderVariantCollection", "SimpleJson.Reflection",
  42. "CoroutineTween", "GraphicRebuildTracker",
  43. "Advertisements", "UnityEditor", "WSA",
  44. "EventProvider", "Apple",
  45. "ClusterInput", "Motion",
  46. "UnityEngine.UI.ReflectionMethodsCache", "NativeLeakDetection",
  47. "NativeLeakDetectionMode", "WWWAudioExtensions", "UnityEngine.Experimental",
  48. "UnityEngine.ClusterSerialization", "UnityEngine.LightingSettings",
  49. "InputManagerEntry", "InputRegistering"
  50. };
  51. static bool isExcluded(Type type)
  52. {
  53. var fullName = type.FullName;
  54. // Add For FullName == null; Modify On 2021/11/18 By dkpyihong.
  55. if (string.IsNullOrEmpty(fullName))
  56. return false;
  57. for (int i = 0; i < exclude.Count; i++)
  58. {
  59. if (fullName.Contains(exclude[i]))
  60. {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. // 批量导出LuaCallCSharp
  67. [XLua.LuaCallCSharp]
  68. public static IEnumerable<Type> LuaCallCSharp
  69. {
  70. get
  71. {
  72. List<string> namespaces = new List<string>() // 在这里添加名字空间
  73. {
  74. "UnityEngine",
  75. "UnityEngine.UI",
  76. "UnityEngine.Video",
  77. "UnityEngine.Networking",
  78. "UnityEngine.SceneManagement",
  79. "UnityEngine.AI",
  80. "DG",
  81. "DG.Tweening",
  82. "ZLog",
  83. "Cinemachine",
  84. };
  85. var unityTypes = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
  86. where !(assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder)
  87. from type in assembly.GetExportedTypes()
  88. where type.Namespace != null && namespaces.Contains(type.Namespace) && !isExcluded(type)
  89. && type.BaseType != typeof(MulticastDelegate) && !type.IsInterface && !type.IsEnum
  90. select type);
  91. string[] customAssemblys = new string[] {
  92. "Assembly-CSharp",
  93. };
  94. var customTypes = (from assembly in customAssemblys.Select(s => Assembly.Load(s))
  95. from type in assembly.GetExportedTypes()
  96. where type.Namespace == null || !type.Namespace.StartsWith("XLua")
  97. && type.BaseType != typeof(MulticastDelegate) && !type.IsInterface && !type.IsEnum
  98. select type);
  99. return unityTypes.Concat(customTypes);
  100. }
  101. }
  102. // 手动增加部分CSharpCallLua
  103. [XLua.CSharpCallLua]
  104. public static List<Type> CustomCSharpCallLua = new List<Type>() {
  105. typeof(Action),
  106. typeof(Func<double, double, double>),
  107. typeof(Action<string>),
  108. typeof(Action<double>),
  109. typeof(UnityEngine.Events.UnityAction),
  110. typeof(System.Collections.IEnumerator),
  111. typeof(Cinemachine.CinemachineTrackedDolly),
  112. };
  113. //自动把LuaCallCSharp涉及到的delegate加到CSharpCallLua列表,后续可以直接用lua函数做callback
  114. [XLua.CSharpCallLua]
  115. public static List<Type> CSharpCallLua
  116. {
  117. get
  118. {
  119. var lua_call_csharp = LuaCallCSharp;
  120. var delegate_types = new List<Type>();
  121. var flag = BindingFlags.Public | BindingFlags.Instance
  122. | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.DeclaredOnly;
  123. foreach (var field in (from type in lua_call_csharp select type).SelectMany(type => type.GetFields(flag)))
  124. {
  125. if (typeof(Delegate).IsAssignableFrom(field.FieldType) && !isExcluded(field.FieldType))
  126. {
  127. delegate_types.Add(field.FieldType);
  128. }
  129. }
  130. foreach (var method in (from type in lua_call_csharp select type).SelectMany(type => type.GetMethods(flag)))
  131. {
  132. if (typeof(Delegate).IsAssignableFrom(method.ReturnType) && !isExcluded(method.ReturnType))
  133. {
  134. delegate_types.Add(method.ReturnType);
  135. }
  136. foreach (var param in method.GetParameters())
  137. {
  138. var paramType = param.ParameterType.IsByRef ? param.ParameterType.GetElementType() : param.ParameterType;
  139. if (typeof(Delegate).IsAssignableFrom(paramType) && !isExcluded(paramType))
  140. {
  141. delegate_types.Add(paramType);
  142. }
  143. }
  144. }
  145. return delegate_types.Where(t => t.BaseType == typeof(MulticastDelegate) && !hasGenericParameter(t) && !delegateHasEditorRef(t)).Distinct().ToList();
  146. }
  147. }
  148. //--------------end 纯lua编程配置参考----------------------------
  149. /***************热补丁可以参考这份自动化配置***************/
  150. //[Hotfix]
  151. //static IEnumerable<Type> HotfixInject
  152. //{
  153. // get
  154. // {
  155. // return (from type in Assembly.Load("Assembly-CSharp").GetTypes()
  156. // where type.Namespace == null || !type.Namespace.StartsWith("XLua")
  157. // select type);
  158. // }
  159. //}
  160. //--------------begin 热补丁自动化配置-------------------------
  161. static bool hasGenericParameter(Type type)
  162. {
  163. if (type.IsGenericTypeDefinition) return true;
  164. if (type.IsGenericParameter) return true;
  165. if (type.IsByRef || type.IsArray)
  166. {
  167. return hasGenericParameter(type.GetElementType());
  168. }
  169. if (type.IsGenericType)
  170. {
  171. foreach (var typeArg in type.GetGenericArguments())
  172. {
  173. if (hasGenericParameter(typeArg))
  174. {
  175. return true;
  176. }
  177. }
  178. }
  179. return false;
  180. }
  181. static bool typeHasEditorRef(Type type)
  182. {
  183. if (type.Namespace != null && (type.Namespace == "UnityEditor" || type.Namespace.StartsWith("UnityEditor.")))
  184. {
  185. return true;
  186. }
  187. if (type.IsNested)
  188. {
  189. return typeHasEditorRef(type.DeclaringType);
  190. }
  191. if (type.IsByRef || type.IsArray)
  192. {
  193. return typeHasEditorRef(type.GetElementType());
  194. }
  195. if (type.IsGenericType)
  196. {
  197. foreach (var typeArg in type.GetGenericArguments())
  198. {
  199. if (typeArg.IsGenericParameter) {
  200. //skip unsigned type parameter
  201. continue;
  202. }
  203. if (typeHasEditorRef(typeArg))
  204. {
  205. return true;
  206. }
  207. }
  208. }
  209. return false;
  210. }
  211. static bool delegateHasEditorRef(Type delegateType)
  212. {
  213. if (typeHasEditorRef(delegateType)) return true;
  214. var method = delegateType.GetMethod("Invoke");
  215. if (method == null)
  216. {
  217. return false;
  218. }
  219. if (typeHasEditorRef(method.ReturnType)) return true;
  220. return method.GetParameters().Any(pinfo => typeHasEditorRef(pinfo.ParameterType));
  221. }
  222. // 配置某Assembly下所有涉及到的delegate到CSharpCallLua下,Hotfix下拿不准那些delegate需要适配到lua function可以这么配置
  223. //[CSharpCallLua]
  224. //static IEnumerable<Type> AllDelegate
  225. //{
  226. // get
  227. // {
  228. // BindingFlags flag = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  229. // List<Type> allTypes = new List<Type>();
  230. // var allAssemblys = new Assembly[]
  231. // {
  232. // Assembly.Load("Assembly-CSharp")
  233. // };
  234. // foreach (var t in (from assembly in allAssemblys from type in assembly.GetTypes() select type))
  235. // {
  236. // var p = t;
  237. // while (p != null)
  238. // {
  239. // allTypes.Add(p);
  240. // p = p.BaseType;
  241. // }
  242. // }
  243. // allTypes = allTypes.Distinct().ToList();
  244. // var allMethods = from type in allTypes
  245. // from method in type.GetMethods(flag)
  246. // select method;
  247. // var returnTypes = from method in allMethods
  248. // select method.ReturnType;
  249. // var paramTypes = allMethods.SelectMany(m => m.GetParameters()).Select(pinfo => pinfo.ParameterType.IsByRef ? pinfo.ParameterType.GetElementType() : pinfo.ParameterType);
  250. // var fieldTypes = from type in allTypes
  251. // from field in type.GetFields(flag)
  252. // select field.FieldType;
  253. // return (returnTypes.Concat(paramTypes).Concat(fieldTypes)).Where(t => t.BaseType == typeof(MulticastDelegate) && !hasGenericParameter(t) && !delegateHasEditorRef(t)).Distinct();
  254. // }
  255. //}
  256. //--------------end 热补丁自动化配置-------------------------
  257. //黑名单
  258. [XLua.BlackList]
  259. public static List<List<string>> BlackList = new List<List<string>>() {
  260. new List<string>(){"System.Xml.XmlNodeList", "ItemOf"},
  261. new List<string>(){"UnityEngine.WWW", "movie"},
  262. #if UNITY_WEBGL
  263. new List<string>(){"UnityEngine.WWW", "threadPriority"},
  264. #endif
  265. new List<string>(){"UnityEngine.Texture2D", "alphaIsTransparency"},
  266. new List<string>(){"UnityEngine.Security", "GetChainOfTrustValue"},
  267. new List<string>(){"UnityEngine.CanvasRenderer", "onRequestRebuild"},
  268. new List<string>(){"UnityEngine.Light", "areaSize"},
  269. new List<string>(){"UnityEngine.Light", "lightmapBakeType"},
  270. new List<string>(){"UnityEngine.WWW", "MovieTexture"},
  271. new List<string>(){"UnityEngine.WWW", "GetMovieTexture"},
  272. new List<string>(){"UnityEngine.AnimatorOverrideController", "PerformOverrideClipListCleanup"},
  273. #if !UNITY_WEBPLAYER
  274. new List<string>(){"UnityEngine.Application", "ExternalEval"},
  275. #endif
  276. new List<string>(){"UnityEngine.GameObject", "networkView"}, //4.6.2 not support
  277. new List<string>(){"UnityEngine.Component", "networkView"}, //4.6.2 not support
  278. new List<string>(){"System.IO.FileInfo", "GetAccessControl", "System.Security.AccessControl.AccessControlSections"},
  279. new List<string>(){"System.IO.FileInfo", "SetAccessControl", "System.Security.AccessControl.FileSecurity"},
  280. new List<string>(){"System.IO.DirectoryInfo", "GetAccessControl", "System.Security.AccessControl.AccessControlSections"},
  281. new List<string>(){"System.IO.DirectoryInfo", "SetAccessControl", "System.Security.AccessControl.DirectorySecurity"},
  282. new List<string>(){"System.IO.DirectoryInfo", "CreateSubdirectory", "System.String", "System.Security.AccessControl.DirectorySecurity"},
  283. new List<string>(){"System.IO.DirectoryInfo", "Create", "System.Security.AccessControl.DirectorySecurity"},
  284. new List<string>(){"UnityEngine.MonoBehaviour", "runInEditMode"},
  285. // for build on windows x86_64
  286. // modify on 2021/11/18 by dkpyihong;
  287. new List<string>(){"UnityEngine.AnimatorControllerParameter", "name"},
  288. new List<string>(){"UnityEngine.AudioSettings", "GetSpatializerPluginNames"},
  289. new List<string>(){"UnityEngine.AudioSettings", "SetSpatializerPluginName" , "System.String"},
  290. new List<string>(){"UnityEngine.Caching", "SetNoBackupFlag","UnityEngine.CachedAssetBundle"},
  291. new List<string>(){"UnityEngine.Caching", "SetNoBackupFlag", "System.String","UnityEngine.Hash128"},
  292. new List<string>(){"UnityEngine.Caching", "ResetNoBackupFlag","UnityEngine.CachedAssetBundle"},
  293. new List<string>(){"UnityEngine.Caching", "ResetNoBackupFlag", "System.String","UnityEngine.Hash128"},
  294. new List<string>(){"UnityEngine.DrivenRectTransformTracker", "StopRecordingUndo"},
  295. new List<string>(){"UnityEngine.DrivenRectTransformTracker", "StartRecordingUndo"},
  296. new List<string>(){"UnityEngine.Input", "IsJoystickPreconfigured","System.String"},
  297. new List<string>(){"UnityEngine.LightProbeGroup", "dering"},
  298. new List<string>(){"UnityEngine.LightProbeGroup", "probePositions"},
  299. new List<string>(){"UnityEngine.Light", "SetLightDirty"},
  300. new List<string>(){"UnityEngine.Light", "shadowRadius"},
  301. new List<string>(){"UnityEngine.Light", "shadowAngle"},
  302. new List<string>(){"UnityEngine.MeshRenderer", "scaleInLightmap"},
  303. new List<string>(){"UnityEngine.MeshRenderer", "receiveGI"},
  304. new List<string>(){"UnityEngine.MeshRenderer", "stitchLightmapSeams"},
  305. new List<string>(){"UnityEngine.ParticleSystemForceField", "FindAll"},
  306. new List<string>(){"UnityEngine.ParticleSystemRenderer", "supportsMeshInstancing"},
  307. new List<string>(){"UnityEngine.QualitySettings", "streamingMipmapsRenderersPerFrame"},
  308. new List<string>(){"UnityEngine.Texture", "imageContentsHash"},
  309. new List<string>(){"UnityEngine.UI.DefaultControls", "factory"},
  310. new List<string>(){"UnityEngine.UI.Graphic", "OnRebuildRequested"},
  311. new List<string>(){"UnityEngine.UI.Text", "OnRebuildRequested"},
  312. };
  313. #if UNITY_2018_1_OR_NEWER
  314. [XLua.BlackList]
  315. public static Func<MemberInfo, bool> MethodFilter = (memberInfo) =>
  316. {
  317. if (memberInfo.DeclaringType.IsGenericType && memberInfo.DeclaringType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
  318. {
  319. if (memberInfo.MemberType == MemberTypes.Constructor)
  320. {
  321. ConstructorInfo constructorInfo = memberInfo as ConstructorInfo;
  322. var parameterInfos = constructorInfo.GetParameters();
  323. if (parameterInfos.Length > 0)
  324. {
  325. if (typeof(System.Collections.IEnumerable).IsAssignableFrom(parameterInfos[0].ParameterType))
  326. {
  327. return true;
  328. }
  329. }
  330. }
  331. else if (memberInfo.MemberType == MemberTypes.Method)
  332. {
  333. var methodInfo = memberInfo as MethodInfo;
  334. if (methodInfo.Name == "TryAdd" || methodInfo.Name == "Remove" && methodInfo.GetParameters().Length == 2)
  335. {
  336. return true;
  337. }
  338. }
  339. }
  340. return false;
  341. };
  342. #endif
  343. }