static void GenLuaBinder() { if (!beAutoGen && EditorApplication.isCompiling) { EditorUtility.DisplayDialog("警告", "请等待编辑器完成编译再执行此功能", "确定"); return; } allTypes.Clear(); ToLuaTree <string> tree = InitTree(); StringBuilder sb = new StringBuilder(); List <DelegateType> dtList = new List <DelegateType>(); List <DelegateType> list = new List <DelegateType>(); list.AddRange(CustomSettings.customDelegateList); HashSet <Type> set = GetCustomTypeDelegates(); List <BindType> backupList = new List <BindType>(); backupList.AddRange(allTypes); ToLuaNode <string> root = tree.GetRoot(); string libname = null; foreach (Type t in set) { if (null == list.Find((p) => { return(p.type == t); })) { DelegateType dt = new DelegateType(t); AddSpaceNameToTree(tree, root, ToLuaExport.GetNameSpace(t, out libname)); list.Add(dt); } } sb.AppendLineEx("//this source code was auto-generated by tolua#, do not modify it"); sb.AppendLineEx("using System;"); sb.AppendLineEx("using UnityEngine;"); sb.AppendLineEx("using LuaInterface;"); sb.AppendLineEx(); sb.AppendLineEx("public static class LuaBinder"); sb.AppendLineEx("{"); sb.AppendLineEx("\tpublic static void Bind(LuaState L)"); sb.AppendLineEx("\t{"); sb.AppendLineEx("\t\tfloat t = Time.realtimeSinceStartup;"); sb.AppendLineEx("\t\tL.BeginModule(null);"); GenRegisterInfo(null, sb, list, dtList); Action <ToLuaNode <string> > begin = (node) => { if (node.value == null) { return; } sb.AppendFormat("\t\tL.BeginModule(\"{0}\");\r\n", node.value); string space = GetSpaceNameFromTree(node); GenRegisterInfo(space, sb, list, dtList); }; Action <ToLuaNode <string> > end = (node) => { if (node.value != null) { sb.AppendLineEx("\t\tL.EndModule();"); } }; tree.DepthFirstTraversal(begin, end, tree.GetRoot()); sb.AppendLineEx("\t\tL.EndModule();"); if (CustomSettings.dynamicList.Count > 0) { sb.AppendLineEx("\t\tL.BeginPreLoad();"); for (int i = 0; i < CustomSettings.dynamicList.Count; i++) { Type t1 = CustomSettings.dynamicList[i]; BindType bt = backupList.Find((p) => { return(p.type == t1); }); if (bt != null) { sb.AppendFormat("\t\tL.AddPreLoad(\"{0}\", LuaOpen_{1}, typeof({0}));\r\n", bt.name, bt.wrapName); } } sb.AppendLineEx("\t\tL.EndPreLoad();"); } sb.AppendLineEx("\t\tDebugger.Log(\"Register lua type cost time: {0}\", Time.realtimeSinceStartup - t);"); sb.AppendLineEx("\t}"); for (int i = 0; i < dtList.Count; i++) { ToLuaExport.GenEventFunction(dtList[i].type, sb); } if (CustomSettings.dynamicList.Count > 0) { for (int i = 0; i < CustomSettings.dynamicList.Count; i++) { Type t = CustomSettings.dynamicList[i]; BindType bt = backupList.Find((p) => { return(p.type == t); }); if (bt != null) { GenPreLoadFunction(bt, sb); } } } sb.AppendLineEx("}\r\n"); allTypes.Clear(); string file = CustomSettings.saveDir + "LuaBinder.cs"; using (StreamWriter textWriter = new StreamWriter(file, false, Encoding.UTF8)) { textWriter.Write(sb.ToString()); textWriter.Flush(); textWriter.Close(); } AssetDatabase.Refresh(); Debugger.Log("Generate LuaBinder over !"); }
public void GenLuaBinder() { StringBuilder sb = new StringBuilder(); List <DelegateType> dtList = new List <DelegateType>(); List <BindType> allTypes = GetAllWrappedCustomType(); List <DelegateType> delegateList = GetAllBuildedDelegate(); ToLuaTree <string> tree = InitTree(allTypes, delegateList); List <BindType> preloadList = GetAllPreloadType(); List <BindType> backupList = new List <BindType>(); backupList.AddRange(allTypes); sb.AppendLine("//this source code was auto-generated by tolua#, do not modify it"); sb.AppendLine("using System;"); sb.AppendLine("using UnityEngine;"); sb.AppendLine("using LaoHan.Infrastruture.ulua;"); sb.AppendLine(); sb.AppendLine("public static class LuaBinder"); sb.AppendLine("{"); sb.AppendLine("\tpublic static void Bind(LuaState L)"); sb.AppendLine("\t{"); sb.AppendLine("\t\tfloat t = Time.realtimeSinceStartup;"); sb.AppendLine("\t\tL.BeginModule(null);"); for (int i = 0; i < allTypes.Count; i++) { BindType dt = preloadList.Find((p) => { return(allTypes[i].type == p.type); }); if (dt == null && allTypes[i].nameSpace == null) { string str = "\t\t" + allTypes[i].wrapName + ".Register(L);\r\n"; sb.Append(str); allTypes.RemoveAt(i--); } } Action <ToLuaNode <string> > begin = (node) => { if (node.value == null) { return; } sb.AppendFormat("\t\tL.BeginModule(\"{0}\");\r\n", node.value); string space = GetSpaceNameFromTree(node); for (int i = 0; i < allTypes.Count; i++) { BindType dt = preloadList.Find((p) => { return(allTypes[i].type == p.type); }); if (dt == null && allTypes[i].nameSpace == space) { string str = "\t\t" + allTypes[i].wrapName + ".Register(L);\r\n"; sb.Append(str); allTypes.RemoveAt(i--); } } string funcName = null; for (int i = 0; i < delegateList.Count; i++) { DelegateType dt = delegateList[i]; Type type = dt.type; string typeSpace = ToLuaExport.GetNameSpace(type, out funcName); if (typeSpace == space) { funcName = ToLuaExport.ConvertToLibSign(funcName); string abr = dt.abr; abr = abr == null ? funcName : abr; sb.AppendFormat("\t\tL.RegFunction(\"{0}\", {1});\r\n", abr, dt.name); dtList.Add(dt); } } }; Action <ToLuaNode <string> > end = (node) => { if (node.value != null) { sb.AppendLine("\t\tL.EndModule();"); } }; tree.DepthFirstTraversal(begin, end, tree.GetRoot()); sb.AppendLine("\t\tL.EndModule();"); if (preloadList.Count > 0) { sb.AppendLine("\t\tL.BeginPreLoad();"); for (int i = 0; i < preloadList.Count; i++) { BindType t1 = preloadList[i]; BindType bt = backupList.Find((p) => { return(p.type == t1.type); }); sb.AppendFormat("\t\tL.AddPreLoad(\"{0}\", LuaOpen_{1}, typeof({0}));\r\n", bt.className, bt.wrapName); } sb.AppendLine("\t\tL.EndPreLoad();"); } //sb.AppendLine("\t\tDebugger.Log(\"Register lua type cost time: {0}\", Time.realtimeSinceStartup - t);"); sb.AppendLine("\t}"); for (int i = 0; i < dtList.Count; i++) { ToLuaExport.GenEventFunction(dtList[i].type, sb); } if (preloadList.Count > 0) { for (int i = 0; i < preloadList.Count; i++) { BindType t = preloadList[i]; BindType bt = backupList.Find((p) => { return(p.type == t.type); }); GenPreLoadFunction(bt, sb); } } sb.AppendLine("}\r\n"); allTypes.Clear(); using (StreamWriter textWriter = new StreamWriter(luaBinderFilePath, false, Encoding.UTF8)) { textWriter.Write(sb.ToString()); textWriter.Flush(); textWriter.Close(); } lhDebug.Log("Generate LuaBinder over !"); }
static void GenLuaBinder() { if (!beAutoGen && EditorApplication.isCompiling) { EditorUtility.DisplayDialog("警告", "请等待编辑器完成编译在执行此功能", "确定"); return; } allTypes.Clear(); ToLuaTree <string> tree = InitTree(); StringBuilder sb = new StringBuilder(); List <DelegateType> dtList = new List <DelegateType>(); sb.AppendLineEx("using System;"); sb.AppendLineEx("using UnityEngine;"); sb.AppendLineEx("using LuaInterface;"); sb.AppendLineEx(); sb.AppendLineEx("public static class LuaBinder"); sb.AppendLineEx("{"); sb.AppendLineEx("\tpublic static void Bind(LuaState L)"); sb.AppendLineEx("\t{"); sb.AppendLineEx("\t\tfloat t = Time.realtimeSinceStartup;"); sb.AppendLineEx("\t\tL.BeginModule(null);"); //if (File.Exists(CustomSettings.saveDir + "DelegateFactoryWrap.cs")) //{ // sb.AppendLineEx("\t\tDelegateFactoryWrap.Register(L);"); //} for (int i = 0; i < allTypes.Count; i++) { if (allTypes[i].nameSpace == null) { string str = "\t\t" + allTypes[i].wrapName + "Wrap.Register(L);\r\n"; sb.Append(str); allTypes.RemoveAt(i--); } } Action <ToLuaNode <string> > begin = (node) => { if (node.value == null) { return; } sb.AppendFormat("\t\tL.BeginModule(\"{0}\");\r\n", node.value); string space = GetSpaceNameFromTree(node); for (int i = 0; i < allTypes.Count; i++) { if (allTypes[i].nameSpace == space) { string str = "\t\t" + allTypes[i].wrapName + "Wrap.Register(L);\r\n"; sb.Append(str); allTypes.RemoveAt(i--); } } string funcName = null; for (int i = 0; i < CustomSettings.customDelegateList.Length; i++) { DelegateType dt = CustomSettings.customDelegateList[i]; Type type = CustomSettings.customDelegateList[i].type; if (type.Namespace == space) { ToLuaExport.GetNameSpace(type, out funcName); funcName = ToLuaExport.ConvertToLibSign(funcName); string abr = dt.abr; abr = abr == null ? funcName : abr; sb.AppendFormat("\t\tL.RegFunction(\"{0}\", {1});\r\n", abr, dt.name); dtList.Add(dt); } } }; Action <ToLuaNode <string> > end = (node) => { if (node.value != null) { sb.AppendLineEx("\t\tL.EndModule();"); } }; tree.DepthFirstTraversal(begin, end, tree.GetRoot()); sb.AppendLineEx("\t\tL.EndModule();"); sb.AppendLineEx("\t\tDebugger.Log(\"Register lua type cost time: {0}\", Time.realtimeSinceStartup - t);"); sb.AppendLineEx("\t}"); for (int i = 0; i < dtList.Count; i++) { ToLuaExport.GenEventFunction(dtList[i].type, sb); } sb.AppendLineEx("}\r\n"); allTypes.Clear(); string file = CustomSettings.saveDir + "LuaBinder.cs"; using (StreamWriter textWriter = new StreamWriter(file, false, Encoding.UTF8)) { textWriter.Write(sb.ToString()); textWriter.Flush(); textWriter.Close(); } AssetDatabase.Refresh(); }
static void GenLuaBinder() { if (!beAutoGen && EditorApplication.isCompiling) { EditorUtility.DisplayDialog("警告", "请等待编辑器完成编译再执行此功能", "确定"); return; } allTypes.Clear(); ToLuaTree <string> tree = InitTree(); StringBuilder sb = new StringBuilder(); List <DelegateType> dtList = new List <DelegateType>(); List <DelegateType> list = new List <DelegateType>(); list.AddRange(CustomSettings.customDelegateList); // 以 HashSet<Type> 形式获取 CustomSettings.customTypeList 中所有委托类型元素(字段、属性、方法参数)的类型 HashSet <Type> set = GetCustomTypeDelegates(); // 以 List<BindType> 形式获取自身 allTypes 所有元素 List <BindType> backupList = new List <BindType>(); backupList.AddRange(allTypes); ToLuaNode <string> root = tree.GetRoot(); string libname = null; // 补充 List<DelegateType> list 中没有的 DelegateType foreach (Type t in set) { if (null == list.Find((p) => { return(p.type == t); })) { DelegateType dt = new DelegateType(t); AddSpaceNameToTree(tree, root, ToLuaExport.GetNameSpace(t, out libname)); list.Add(dt); } } // 拼接文本 sb.AppendLineEx("//this source code was auto-generated by tolua#, do not modify it"); sb.AppendLineEx("using System;"); sb.AppendLineEx("using UnityEngine;"); sb.AppendLineEx("using LuaInterface;"); sb.AppendLineEx(); sb.AppendLineEx("public static class LuaBinder"); sb.AppendLineEx("{"); sb.AppendLineEx("\tpublic static void Bind(LuaState L)"); sb.AppendLineEx("\t{"); sb.AppendLineEx("\t\tfloat t = Time.realtimeSinceStartup;"); sb.AppendLineEx("\t\tL.BeginModule(null);"); // 生成注册信息字符串并对 list、dtList 两个 list 做相应更新 GenRegisterInfo(null, sb, list, dtList); // 为 DepthFirstTraversal 方法新建一个将 value 不为空的 ToLuaNode<string> 的 value 组织字符串,并添加到参数 sb 中(于开头处添加字符);再获取参数 node 的完全限定名、根据 t 的 Namespace 更新参数 tree 和 root 的父或子节点信息 Action <ToLuaNode <string> > begin = (node) => { if (node.value == null) { return; } sb.AppendFormat("\t\tL.BeginModule(\"{0}\");\r\n", node.value); string space = GetSpaceNameFromTree(node); GenRegisterInfo(space, sb, list, dtList); }; // 为 DepthFirstTraversal 方法新建一个将 value 不为空的 ToLuaNode<string> 的 value 组织字符串并添加到参数 sb 中(于结尾处添加字符) Action <ToLuaNode <string> > end = (node) => { if (node.value != null) { sb.AppendLineEx("\t\tL.EndModule();"); } }; // 从最底层的子节点开始对参数 node 及其子节点进行遍历,同时使用 begin 和 end 委托对字符串进行修改 tree.DepthFirstTraversal(begin, end, tree.GetRoot()); // 在结尾处添加字符串 sb.AppendLineEx("\t\tL.EndModule();"); // 处理动态类型 list 字符串 if (CustomSettings.dynamicList.Count > 0) { sb.AppendLineEx("\t\tL.BeginPreLoad();"); for (int i = 0; i < CustomSettings.dynamicList.Count; i++) { Type t1 = CustomSettings.dynamicList[i]; BindType bt = backupList.Find((p) => { return(p.type == t1); }); sb.AppendFormat("\t\tL.AddPreLoad(\"{0}\", LuaOpen_{1}, typeof({0}));\r\n", bt.name, bt.wrapName); } sb.AppendLineEx("\t\tL.EndPreLoad();"); } sb.AppendLineEx("\t\tDebugger.Log(\"Register lua type cost time: {0}\", Time.realtimeSinceStartup - t);"); sb.AppendLineEx("\t}"); // 遍历 dtList,为每一个元素生成指定类型的 EventFunction 字符串并添加到参数 StringBuilder sb 中 for (int i = 0; i < dtList.Count; i++) { ToLuaExport.GenEventFunction(dtList[i].type, sb); } // 遍历动态类型 list 并为每一个 backupList 中类型与当前元素相等的 BindType 生成 PreLoadFunction 字符串并添加到参数 StringBuilder sb if (CustomSettings.dynamicList.Count > 0) { for (int i = 0; i < CustomSettings.dynamicList.Count; i++) { Type t = CustomSettings.dynamicList[i]; BindType bt = backupList.Find((p) => { return(p.type == t); }); GenPreLoadFunction(bt, sb); } } // 添加结尾字符串,并清空 allTypes,合成文件储存路径 sb.AppendLineEx("}\r\n"); allTypes.Clear(); string file = CustomSettings.saveDir + "LuaBinder.cs"; // 生成文件、刷新 AssetDatabase 并打印提示 using (StreamWriter textWriter = new StreamWriter(file, false, Encoding.UTF8)) { textWriter.Write(sb.ToString()); textWriter.Flush(); textWriter.Close(); } AssetDatabase.Refresh(); Debugger.Log("Generate LuaBinder over !"); }