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; } } }
//加入pos跟root里的pos比较,只有位置相同才是统一命名空间节点 ToLuaNode <T> FindParent(List <ToLuaNode <T> > root, Predicate <T> match, int pos) { if (root == null) { return(null); } for (int i = 0; i < root.Count; i++) { //加入pos跟root里的pos比较,只有位置相同才是统一命名空间节点 if (match(root[i].value) && root[i].pos == pos) { return(root[i]); } ToLuaNode <T> node = FindParent(root[i].childs, match, pos); if (node != null) { return(node); } } return(null); }
static string GetSpaceNameFromTree(ToLuaNode <string> node) { string name = node.value; while (node.parent != null && node.parent.value != null) { node = node.parent; name = node.value + "." + name; } return(name); }
private ToLuaTree <string> InitTree(List <BindType> list, List <DelegateType> delegateList) { ToLuaTree <string> tree = new ToLuaTree <string>(); ToLuaNode <string> root = tree.GetRoot(); for (int i = 0; i < list.Count; i++) { string space = list[i].nameSpace; AddSpaceNameToTree(tree, root, space); } for (int i = 0; i < delegateList.Count; i++) { string space = delegateList[i].type.Namespace; AddSpaceNameToTree(tree, root, space); } 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; 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); }
ToLuaNode <T> FindParent(List <ToLuaNode <T> > root, Predicate <T> match) { if (root == null) { return(null); } for (int i = 0; i < root.Count; i++) { if (match(root[i].value)) { return(root[i]); } ToLuaNode <T> node = FindParent(root[i].childs, match); if (node != null) { return(node); } } return(null); }
static ToLuaTree <string> InitTree() { ToLuaTree <string> tree = new ToLuaTree <string>(); ToLuaNode <string> root = tree.GetRoot(); BindType[] list = GenBindTypes(WrapList.customTypeList); for (int i = 0; i < list.Length; i++) { string space = list[i].nameSpace; AddSpaceNameToTree(tree, root, space); } DelegateType[] dts = WrapList.customDelegateList; string str = null; for (int i = 0; i < dts.Length; i++) { string space = ToLuaExport.GetNameSpace(dts[i].type, out str); 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 string GetSpaceNameFromTree(ToLuaNode<string> node) { string name = node.value; while (node.parent != null && node.parent.value != null) { node = node.parent; name = node.value + "." + name; } return name; }
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; }
/*public void BreadthFirstTraversal(Action<ToLuaNode<T>> action) * { * List<ToLuaNode<T>> root = _root.childs; * Queue<ToLuaNode<T>> queue = new Queue<ToLuaNode<T>>(); * * for (int i = 0; i < root.Count; i++) * { * queue.Enqueue(root[i]); * } * * while (queue.Count > 0) * { * ToLuaNode<T> node = queue.Dequeue(); * action(node); * * if (node.childs != null) * { * for (int i = 0; i < node.childs.Count; i++) * { * queue.Enqueue(node.childs[i]); * } * } * } * }*/ public void DepthFirstTraversal(Action <ToLuaNode <T> > begin, Action <ToLuaNode <T> > end, ToLuaNode <T> node) { begin(node); for (int i = 0; i < node.childs.Count; i++) { DepthFirstTraversal(begin, end, node.childs[i]); } end(node); }
public ToLuaTree() { _root = new ToLuaNode <T>(); }
public ToLuaTree() { _root = new ToLuaNode <T>(); _list = new List <ToLuaNode <T> >(); }
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> 形式获取 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 !"); }
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 !"); }
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; } } }