XLuaGenConfig.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. };
  50. static bool isExcluded(Type type)
  51. {
  52. var fullName = type.FullName;
  53. // Add For FullName == null; Modify On 2021/11/18 By dkpyihong.
  54. if (string.IsNullOrEmpty(fullName))
  55. return false;
  56. for (int i = 0; i < exclude.Count; i++)
  57. {
  58. if (fullName.Contains(exclude[i]))
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. // 批量导出LuaCallCSharp
  66. [XLua.LuaCallCSharp]
  67. public static IEnumerable<Type> LuaCallCSharp
  68. {
  69. get
  70. {
  71. List<string> namespaces = new List<string>() // 在这里添加名字空间
  72. {
  73. "UnityEngine",
  74. "UnityEngine.UI",
  75. "DG",
  76. "DG.Tweening",
  77. "ZLog",
  78. };
  79. var unityTypes = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
  80. where !(assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder)
  81. from type in assembly.GetExportedTypes()
  82. where type.Namespace != null && namespaces.Contains(type.Namespace) && !isExcluded(type)
  83. && type.BaseType != typeof(MulticastDelegate) && !type.IsInterface && !type.IsEnum
  84. select type);
  85. string[] customAssemblys = new string[] {
  86. "Assembly-CSharp",
  87. };
  88. var customTypes = (from assembly in customAssemblys.Select(s => Assembly.Load(s))
  89. from type in assembly.GetExportedTypes()
  90. where type.Namespace == null || !type.Namespace.StartsWith("XLua")
  91. && type.BaseType != typeof(MulticastDelegate) && !type.IsInterface && !type.IsEnum
  92. select type);
  93. return unityTypes.Concat(customTypes);
  94. }
  95. }
  96. // 手动增加部分CSharpCallLua
  97. [XLua.CSharpCallLua]
  98. public static List<Type> CustomCSharpCallLua = new List<Type>() {
  99. typeof(Action),
  100. typeof(Func<double, double, double>),
  101. typeof(Action<string>),
  102. typeof(Action<double>),
  103. typeof(UnityEngine.Events.UnityAction),
  104. typeof(System.Collections.IEnumerator),
  105. };
  106. //自动把LuaCallCSharp涉及到的delegate加到CSharpCallLua列表,后续可以直接用lua函数做callback
  107. [XLua.CSharpCallLua]
  108. public static List<Type> CSharpCallLua
  109. {
  110. get
  111. {
  112. var lua_call_csharp = LuaCallCSharp;
  113. var delegate_types = new List<Type>();
  114. var flag = BindingFlags.Public | BindingFlags.Instance
  115. | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.DeclaredOnly;
  116. foreach (var field in (from type in lua_call_csharp select type).SelectMany(type => type.GetFields(flag)))
  117. {
  118. if (typeof(Delegate).IsAssignableFrom(field.FieldType) && !isExcluded(field.FieldType))
  119. {
  120. delegate_types.Add(field.FieldType);
  121. }
  122. }
  123. foreach (var method in (from type in lua_call_csharp select type).SelectMany(type => type.GetMethods(flag)))
  124. {
  125. if (typeof(Delegate).IsAssignableFrom(method.ReturnType) && !isExcluded(method.ReturnType))
  126. {
  127. delegate_types.Add(method.ReturnType);
  128. }
  129. foreach (var param in method.GetParameters())
  130. {
  131. var paramType = param.ParameterType.IsByRef ? param.ParameterType.GetElementType() : param.ParameterType;
  132. if (typeof(Delegate).IsAssignableFrom(paramType) && !isExcluded(paramType))
  133. {
  134. delegate_types.Add(paramType);
  135. }
  136. }
  137. }
  138. return delegate_types.Where(t => t.BaseType == typeof(MulticastDelegate) && !hasGenericParameter(t) && !delegateHasEditorRef(t)).Distinct().ToList();
  139. }
  140. }
  141. //--------------end 纯lua编程配置参考----------------------------
  142. /***************热补丁可以参考这份自动化配置***************/
  143. //[Hotfix]
  144. //static IEnumerable<Type> HotfixInject
  145. //{
  146. // get
  147. // {
  148. // return (from type in Assembly.Load("Assembly-CSharp").GetTypes()
  149. // where type.Namespace == null || !type.Namespace.StartsWith("XLua")
  150. // select type);
  151. // }
  152. //}
  153. //--------------begin 热补丁自动化配置-------------------------
  154. static bool hasGenericParameter(Type type)
  155. {
  156. if (type.IsGenericTypeDefinition) return true;
  157. if (type.IsGenericParameter) return true;
  158. if (type.IsByRef || type.IsArray)
  159. {
  160. return hasGenericParameter(type.GetElementType());
  161. }
  162. if (type.IsGenericType)
  163. {
  164. foreach (var typeArg in type.GetGenericArguments())
  165. {
  166. if (hasGenericParameter(typeArg))
  167. {
  168. return true;
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174. static bool typeHasEditorRef(Type type)
  175. {
  176. if (type.Namespace != null && (type.Namespace == "UnityEditor" || type.Namespace.StartsWith("UnityEditor.")))
  177. {
  178. return true;
  179. }
  180. if (type.IsNested)
  181. {
  182. return typeHasEditorRef(type.DeclaringType);
  183. }
  184. if (type.IsByRef || type.IsArray)
  185. {
  186. return typeHasEditorRef(type.GetElementType());
  187. }
  188. if (type.IsGenericType)
  189. {
  190. foreach (var typeArg in type.GetGenericArguments())
  191. {
  192. if (typeArg.IsGenericParameter) {
  193. //skip unsigned type parameter
  194. continue;
  195. }
  196. if (typeHasEditorRef(typeArg))
  197. {
  198. return true;
  199. }
  200. }
  201. }
  202. return false;
  203. }
  204. static bool delegateHasEditorRef(Type delegateType)
  205. {
  206. if (typeHasEditorRef(delegateType)) return true;
  207. var method = delegateType.GetMethod("Invoke");
  208. if (method == null)
  209. {
  210. return false;
  211. }
  212. if (typeHasEditorRef(method.ReturnType)) return true;
  213. return method.GetParameters().Any(pinfo => typeHasEditorRef(pinfo.ParameterType));
  214. }
  215. // 配置某Assembly下所有涉及到的delegate到CSharpCallLua下,Hotfix下拿不准那些delegate需要适配到lua function可以这么配置
  216. //[CSharpCallLua]
  217. //static IEnumerable<Type> AllDelegate
  218. //{
  219. // get
  220. // {
  221. // BindingFlags flag = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  222. // List<Type> allTypes = new List<Type>();
  223. // var allAssemblys = new Assembly[]
  224. // {
  225. // Assembly.Load("Assembly-CSharp")
  226. // };
  227. // foreach (var t in (from assembly in allAssemblys from type in assembly.GetTypes() select type))
  228. // {
  229. // var p = t;
  230. // while (p != null)
  231. // {
  232. // allTypes.Add(p);
  233. // p = p.BaseType;
  234. // }
  235. // }
  236. // allTypes = allTypes.Distinct().ToList();
  237. // var allMethods = from type in allTypes
  238. // from method in type.GetMethods(flag)
  239. // select method;
  240. // var returnTypes = from method in allMethods
  241. // select method.ReturnType;
  242. // var paramTypes = allMethods.SelectMany(m => m.GetParameters()).Select(pinfo => pinfo.ParameterType.IsByRef ? pinfo.ParameterType.GetElementType() : pinfo.ParameterType);
  243. // var fieldTypes = from type in allTypes
  244. // from field in type.GetFields(flag)
  245. // select field.FieldType;
  246. // return (returnTypes.Concat(paramTypes).Concat(fieldTypes)).Where(t => t.BaseType == typeof(MulticastDelegate) && !hasGenericParameter(t) && !delegateHasEditorRef(t)).Distinct();
  247. // }
  248. //}
  249. //--------------end 热补丁自动化配置-------------------------
  250. //黑名单
  251. [XLua.BlackList]
  252. public static List<List<string>> BlackList = new List<List<string>>() {
  253. new List<string>(){"System.Xml.XmlNodeList", "ItemOf"},
  254. new List<string>(){"UnityEngine.WWW", "movie"},
  255. #if UNITY_WEBGL
  256. new List<string>(){"UnityEngine.WWW", "threadPriority"},
  257. #endif
  258. new List<string>(){"UnityEngine.Texture2D", "alphaIsTransparency"},
  259. new List<string>(){"UnityEngine.Security", "GetChainOfTrustValue"},
  260. new List<string>(){"UnityEngine.CanvasRenderer", "onRequestRebuild"},
  261. new List<string>(){"UnityEngine.Light", "areaSize"},
  262. new List<string>(){"UnityEngine.Light", "lightmapBakeType"},
  263. new List<string>(){"UnityEngine.WWW", "MovieTexture"},
  264. new List<string>(){"UnityEngine.WWW", "GetMovieTexture"},
  265. new List<string>(){"UnityEngine.AnimatorOverrideController", "PerformOverrideClipListCleanup"},
  266. #if !UNITY_WEBPLAYER
  267. new List<string>(){"UnityEngine.Application", "ExternalEval"},
  268. #endif
  269. new List<string>(){"UnityEngine.GameObject", "networkView"}, //4.6.2 not support
  270. new List<string>(){"UnityEngine.Component", "networkView"}, //4.6.2 not support
  271. new List<string>(){"System.IO.FileInfo", "GetAccessControl", "System.Security.AccessControl.AccessControlSections"},
  272. new List<string>(){"System.IO.FileInfo", "SetAccessControl", "System.Security.AccessControl.FileSecurity"},
  273. new List<string>(){"System.IO.DirectoryInfo", "GetAccessControl", "System.Security.AccessControl.AccessControlSections"},
  274. new List<string>(){"System.IO.DirectoryInfo", "SetAccessControl", "System.Security.AccessControl.DirectorySecurity"},
  275. new List<string>(){"System.IO.DirectoryInfo", "CreateSubdirectory", "System.String", "System.Security.AccessControl.DirectorySecurity"},
  276. new List<string>(){"System.IO.DirectoryInfo", "Create", "System.Security.AccessControl.DirectorySecurity"},
  277. new List<string>(){"UnityEngine.MonoBehaviour", "runInEditMode"},
  278. // for build on windows x86_64
  279. // modify on 2021/11/18 by dkpyihong;
  280. new List<string>(){"UnityEngine.AnimatorControllerParameter", "name"},
  281. new List<string>(){"UnityEngine.AudioSettings", "GetSpatializerPluginNames"},
  282. new List<string>(){"UnityEngine.AudioSettings", "SetSpatializerPluginName" , "System.String"},
  283. new List<string>(){"UnityEngine.Caching", "SetNoBackupFlag","UnityEngine.CachedAssetBundle"},
  284. new List<string>(){"UnityEngine.Caching", "SetNoBackupFlag", "System.String","UnityEngine.Hash128"},
  285. new List<string>(){"UnityEngine.Caching", "ResetNoBackupFlag","UnityEngine.CachedAssetBundle"},
  286. new List<string>(){"UnityEngine.Caching", "ResetNoBackupFlag", "System.String","UnityEngine.Hash128"},
  287. new List<string>(){"UnityEngine.DrivenRectTransformTracker", "StopRecordingUndo"},
  288. new List<string>(){"UnityEngine.DrivenRectTransformTracker", "StartRecordingUndo"},
  289. new List<string>(){"UnityEngine.Input", "IsJoystickPreconfigured","System.String"},
  290. new List<string>(){"UnityEngine.LightProbeGroup", "dering"},
  291. new List<string>(){"UnityEngine.LightProbeGroup", "probePositions"},
  292. new List<string>(){"UnityEngine.Light", "SetLightDirty"},
  293. new List<string>(){"UnityEngine.Light", "shadowRadius"},
  294. new List<string>(){"UnityEngine.Light", "shadowAngle"},
  295. new List<string>(){"UnityEngine.MeshRenderer", "scaleInLightmap"},
  296. new List<string>(){"UnityEngine.MeshRenderer", "receiveGI"},
  297. new List<string>(){"UnityEngine.MeshRenderer", "stitchLightmapSeams"},
  298. new List<string>(){"UnityEngine.ParticleSystemForceField", "FindAll"},
  299. new List<string>(){"UnityEngine.ParticleSystemRenderer", "supportsMeshInstancing"},
  300. new List<string>(){"UnityEngine.QualitySettings", "streamingMipmapsRenderersPerFrame"},
  301. new List<string>(){"UnityEngine.Texture", "imageContentsHash"},
  302. new List<string>(){"UnityEngine.UI.DefaultControls", "factory"},
  303. new List<string>(){"UnityEngine.UI.Graphic", "OnRebuildRequested"},
  304. new List<string>(){"UnityEngine.UI.Text", "OnRebuildRequested"},
  305. };
  306. #if UNITY_2018_1_OR_NEWER
  307. [XLua.BlackList]
  308. public static Func<MemberInfo, bool> MethodFilter = (memberInfo) =>
  309. {
  310. if (memberInfo.DeclaringType.IsGenericType && memberInfo.DeclaringType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
  311. {
  312. if (memberInfo.MemberType == MemberTypes.Constructor)
  313. {
  314. ConstructorInfo constructorInfo = memberInfo as ConstructorInfo;
  315. var parameterInfos = constructorInfo.GetParameters();
  316. if (parameterInfos.Length > 0)
  317. {
  318. if (typeof(System.Collections.IEnumerable).IsAssignableFrom(parameterInfos[0].ParameterType))
  319. {
  320. return true;
  321. }
  322. }
  323. }
  324. else if (memberInfo.MemberType == MemberTypes.Method)
  325. {
  326. var methodInfo = memberInfo as MethodInfo;
  327. if (methodInfo.Name == "TryAdd" || methodInfo.Name == "Remove" && methodInfo.GetParameters().Length == 2)
  328. {
  329. return true;
  330. }
  331. }
  332. }
  333. return false;
  334. };
  335. #endif
  336. }