static void AddSpaceNameToTree(ToLuaTree<string> tree, ToLuaNode<string> parent, string space) { if (space == null || space == string.Empty) { return; } string[] ns = space.Split(new char[] { '.' }); for (int j = 0; j < ns.Length; j++) { List<ToLuaNode<string>> nodes = tree.Find((_t) => { return _t == ns[j]; }, j); if (nodes.Count == 0) { ToLuaNode<string> node = new ToLuaNode<string>(); node.value = ns[j]; parent.childs.Add(node); node.parent = parent; node.layer = j; parent = node; } else { bool flag = false; int index = 0; for (int i = 0; i < nodes.Count; i++) { int count = j; int size = j; ToLuaNode<string> nodecopy = nodes[i]; while (nodecopy.parent != null) { nodecopy = nodecopy.parent; if (nodecopy.value != null && nodecopy.value == ns[--count]) { size--; } } if (size == 0) { index = i; flag = true; break; } } if (!flag) { ToLuaNode<string> nnode = new ToLuaNode<string>(); nnode.value = ns[j]; nnode.layer = j; nnode.parent = parent; parent.childs.Add(nnode); parent = nnode; } else { parent = nodes[index]; } } } }
static ToLuaTree<string> InitTree() { ToLuaTree<string> tree = new ToLuaTree<string>(); ToLuaNode<string> root = tree.GetRoot(); BindType[] list = GenBindTypes(CustomSettings.customTypeList); for (int i = 0; i < list.Length; i++) { string space = list[i].nameSpace; if (space == null || space == string.Empty) { continue; } string[] ns = space.Split(new char[] { '.' }); ToLuaNode<string> parent = root; for (int j = 0; j < ns.Length; j++) { ToLuaNode<string> node = tree.Find((_t) => { return _t == ns[j];}); if (node == null) { node = new ToLuaNode<string>(); node.value = ns[j]; parent.childs.Add(node); node.parent = parent; parent = node; } else { parent = node; } } } return tree; }
static ToLuaTree<string> InitTree() { ToLuaTree<string> tree = new ToLuaTree<string>(); ToLuaNode<string> root = tree.GetRoot(); BindType[] list = GenBindTypes(CustomSettings.customTypeList); for (int i = 0; i < list.Length; i++) { string space = list[i].nameSpace; AddSpaceNameToTree(tree, root, space); } DelegateType[] dts = CustomSettings.customDelegateList; for (int i = 0; i < dts.Length; i++) { string space = dts[i].type.Namespace; AddSpaceNameToTree(tree, root, space); } return tree; }
static void AddSpaceNameToTree(ToLuaTree <string> tree, ToLuaNode <string> parent, string space) { if (space == null || space == string.Empty) { return; } string[] ns = space.Split(new char[] { '.' }); for (int j = 0; j < ns.Length; j++) { List <ToLuaNode <string> > nodes = tree.Find((_t) => { return(_t == ns[j]); }, j); if (nodes.Count == 0) { ToLuaNode <string> node = new ToLuaNode <string>(); node.value = ns[j]; parent.childs.Add(node); node.parent = parent; node.layer = j; parent = node; } else { bool flag = false; int index = 0; for (int i = 0; i < nodes.Count; i++) { int count = j; int size = j; ToLuaNode <string> nodecopy = nodes[i]; while (nodecopy.parent != null) { nodecopy = nodecopy.parent; if (nodecopy.value != null && nodecopy.value == ns[--count]) { size--; } } if (size == 0) { index = i; flag = true; break; } } if (!flag) { ToLuaNode <string> nnode = new ToLuaNode <string>(); nnode.value = ns[j]; nnode.layer = j; nnode.parent = parent; parent.childs.Add(nnode); parent = nnode; } else { parent = nodes[index]; } } } }
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(); foreach (Type t in set) { if (null == list.Find((p) => { return(p.type == t); })) { DelegateType dt = new DelegateType(t); AddSpaceNameToTree(tree, root, dt.type.Namespace); 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); }); 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); }); 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 !"); }
static void AddSpaceNameToTree(ToLuaTree<string> tree, ToLuaNode<string> root, string space) { if (space == null || space == string.Empty) { return; } string[] ns = space.Split(new char[] { '.' }); ToLuaNode<string> parent = root; for (int j = 0; j < ns.Length; j++) { //pos变量 ToLuaNode<string> node = tree.Find((_t) => { return _t == ns[j]; }, j); if (node == null) { node = new ToLuaNode<string>(); node.value = ns[j]; parent.childs.Add(node); node.parent = parent; //加入pos跟root里的pos比较,只有位置相同才是统一命名空间节点 node.pos = j; parent = node; } else { parent = node; } } }
static void GenLuaBinder() { if (!beAutoGen && EditorApplication.isCompiling) { EditorUtility.DisplayDialog("警告", "请等待编辑器完成编译在执行此功能", "确定"); return; } allTypes.Clear(); ToLuaTree <string> tree = InitTree(); StringBuilder sb = new StringBuilder(); 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--); } } }; 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}"); 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(); }