XLuaGenConfig.cs 16 KB

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