static void GenApi(ToLuaMenu.BindType bt) { ToLuaExport.Clear(); ToLuaExport.className = bt.name; ToLuaExport.type = bt.type; ToLuaExport.isStaticClass = bt.IsStatic; ToLuaExport.baseType = bt.baseType; ToLuaExport.wrapClassName = bt.wrapName; ToLuaExport.libClassName = bt.libName; if (bt.type.IsInterface && bt.type != typeof(System.Collections.IEnumerator)) { return; } //如果是枚举 if (bt.type.IsEnum) { GenEnumApi(bt); } //如果是类 else { ToLuaExport.InitMethods(); ToLuaExport.InitPropertyList(); ToLuaExport.InitCtorList(); GenClassApi(bt); } }
public static void GenerateEmmyTypeFiles() { for (int i = 0; i < CustomSettings.customTypeList.Length; i++) { ToLuaMenu.BindType bindType = CustomSettings.customTypeList[i]; RecordTypeAndBaseType(bindType.type); if (bindType.extendList.Count > 0) { for (var j = 0; j < bindType.extendList.Count; j++) { RecordTypeAndBaseType(bindType.extendList[j]); } } } #if ToLuaVersion for (int i = 0; i < ToLuaFacility.toluaBaseTypes.Count; i++) { RecordType(ToLuaFacility.toluaBaseTypes[i]); } #endif HandleExtensionMethods(); //如果没有使用DOTween,屏蔽这一句 UnRecordType(typeof(ShortcutExtensions)); GenerateTypeDefines(); GenerateExportTypeGlobalVariable(); AssetDatabase.Refresh(); }
//在这里添加你要导出注册到lua的类型列表 public static BindType[] GetCustomTypeList() { var list = new BindType[] { _GT(typeof(Test)) }; return(list); }
static void AutoAddBaseType(ToLuaMenu.BindType bt, bool beDropBaseType) { Type t = bt.baseType; if (t == null) { return; } if (t.IsInterface) { Debugger.LogWarning("{0} has a base type {1} is Interface, use SetBaseType to jump it", bt.name, t.FullName); bt.baseType = t.BaseType; } else if (ToLuaMenu.dropType.IndexOf(t) >= 0) { Debugger.LogWarning("{0} has a base type {1} is a drop type", bt.name, t.FullName); bt.baseType = t.BaseType; } else if (!beDropBaseType || ToLuaMenu.baseType.IndexOf(t) < 0) { int index = allTypes.FindIndex((iter) => { return(iter.type == t); }); if (index < 0) { #if JUMP_NODEFINED_ABSTRACT if (t.IsAbstract && !t.IsSealed) { Debugger.LogWarning("not defined bindtype for {0}, it is abstract class, jump it, child class is {1}", t.FullName, bt.name); bt.baseType = t.BaseType; } else { Debugger.LogWarning("not defined bindtype for {0}, autogen it, child class is {1}", t.FullName, bt.name); bt = new BindType(t); allTypes.Add(bt); } #else Debugger.LogWarning("not defined bindtype for {0}, autogen it, child class is {1}", t.FullName, bt.name); bt = new ToLuaMenu.BindType(t); allTypes.Add(bt); #endif } else { return; } } else { return; } AutoAddBaseType(bt, beDropBaseType); }
static void GenType(Type t, bool custom, string path, ToLuaMenu.BindType bindType = null) { if (!CheckType(t, custom)) { return; } //TODO System.MulticastDelegate var sb = new StringBuilder(); string className = bindType != null ? bindType.libName : t.Name; if (!CheckType(t.BaseType, custom)) { sb.AppendFormat("---@class {0}\n", t.Name); } else { sb.AppendFormat("---@class {0} : {1}\n", t.Name, t.BaseType.Name); } GenTypeField(t, sb); // sb.AppendFormat("local _{0}={{ }}\n", t.Name); sb.AppendFormat("{0}={{ }}\n", t.Name); GenTypeMehod(t, sb); // if (string.IsNullOrEmpty(t.Namespace)) // { // sb.AppendFormat("{0} = _{1}", t.Name, t.Name); // } // else // { // sb.AppendFormat("{0}.{1} = _{2}", t.Namespace, t.Name, t.Name); // } if (className != t.Name) { sb.AppendFormat("\n{0} = {1}", className, t.Name); } if (t.Name.Equals("Type")) { return; } // sb.Replace("bool", "boolean"); sb.Replace("UIGeometry", "userdata UIGeometry"); sb.Replace("BetterList", "userdata BetterList"); File.WriteAllText(path + t.Name + ".lua", sb.ToString(), Encoding.UTF8); string contents = string.Format(@"require('EmmyLuaAPI/CSharpAPI/{0}')", t.Name); path = path.Replace("CSharpAPI/", "") + "EmmyLuaAPI.lua"; File.AppendAllLines(path, new [] { contents }, Encoding.UTF8); }
static void GenEnumApi(ToLuaMenu.BindType bt) { //创建api类 var api = GetClassApi(bt.name); foreach (var f in ToLuaExport.type.GetFields(BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static)) { if (ToLuaExport.IsObsolete(f)) { continue; } api.AddValue(f.Name, bt.name); } api.AddMethod("IntToEnum"); }
public static void GenType(Type t, bool custom, string path, ToLuaMenu.BindType bindType = null) { if (!CheckType(t)) { return; } var sb = new StringBuilder(); string tname = t.Name; tname = tname.Replace("`2", "").Replace("`1", ""); string filename = tname; if (t.Namespace != null) { filename = t.Namespace + "." + tname; if (!dic.ContainsKey(t.Namespace)) { dic.Add(t.Namespace, new List <string> ()); } dic[t.Namespace].Add(tname); } string className = bindType != null ? bindType.libName : tname; if (!CheckType(t.BaseType)) { sb.AppendFormat("---@class {0}\n", tname); } else { sb.AppendFormat("---@class {0} : {1}\n", tname, t.BaseType.Name.Replace("`2", "").Replace("`1", "")); } GenTypeField(t, sb); sb.AppendLine("local " + tname + " = {}"); GenTypeMehod(t, sb); filename = filename.Replace(".", "_"); File.WriteAllText(path + filename + ".lua", sb.ToString(), Encoding.UTF8); }
private void ExportType(BindType bindType) { var type = bindType.type; if (!NeedExportType(type)) { return; } ToLuaExport.Clear(); ToLuaExport.className = bindType.name; ToLuaExport.type = bindType.type; ToLuaExport.isStaticClass = bindType.IsStatic; ToLuaExport.baseType = bindType.baseType; ToLuaExport.wrapClassName = bindType.wrapName; ToLuaExport.libClassName = bindType.libName; ToLuaExport.extendList = bindType.extendList; ExportType(type); ToLuaExport.Clear(); AddType(type.BaseType); }
static void GenClassApi(ToLuaMenu.BindType bt) { Type type = bt.type; if (type.IsGenericType)//泛型类被tolua处理成全局库了,如果都加成api自动提示的时候反而不方便 { return; } //计算lua中的全局名和继承者 string name = bt.name; string inherits = null; if (bt.baseType != null && !bt.baseType.IsGenericType) { inherits = ToLuaExport.GetBaseTypeStr(bt.baseType); } //创建api类 var api = GetClassApi(name, inherits); string returns = null, valueType = null; //注册成员函数,参考的是ToLuaExport.GenRegisterFuncItems for (int i = 0; i < ToLuaExport.methods.Count; i++) { MethodInfo m = ToLuaExport.methods[i].Method as MethodInfo; int count = 1; string methodName = ToLuaExport.GetMethodName(m); if (ToLuaExport.nameCounter.TryGetValue(methodName, out count)) { ToLuaExport.nameCounter[methodName] = count + 1; continue; } ToLuaExport.nameCounter[methodName] = 1; if (m.IsGenericMethod || methodName == "set_Item" || methodName == "get_Item" || methodName.StartsWith("op_")) { continue; } //获取返回值信息 GetReturnTypeStr(m.ReturnType, ref returns, ref valueType); //获取参数信息 ParameterInfo[] paramInfos = m.GetParameters(); s_paramName.Clear(); s_paramName.Append('('); for (int j = 0; j < paramInfos.Length; ++j) { s_paramName.Append(paramInfos[j].Name); if (j != paramInfos.Length - 1) { s_paramName.Append(','); } } s_paramName.Append(')'); string param = s_paramName.ToString(); if (m.IsStatic) { api.AddFunction(methodName, param, returns, valueType); } else { api.AddMethod(methodName, param, returns, valueType); } } //注册成员变量和属性 for (int i = 0; i < ToLuaExport.fields.Length; i++) { GetReturnTypeStr(ToLuaExport.fields[i].FieldType, ref returns, ref valueType); api.AddValue(ToLuaExport.fields[i].Name, valueType); } for (int i = 0; i < ToLuaExport.props.Length; i++) { GetReturnTypeStr(ToLuaExport.props[i].PropertyType, ref returns, ref valueType); api.AddValue(ToLuaExport.props[i].Name, valueType); } //注册操作符,暂时不需要 //注册构造函数,暂时不需要 //注册索引器,暂时不需要 }
public static void GenAutoComplete() { s_api = new LuaApi(); s_apiIdx.Clear(); s_apiTypeIdx.Clear(); //收集要生成的类 List <ToLuaMenu.BindType> btList = new List <ToLuaMenu.BindType>(); allTypes.Clear(); ToLuaExport.allTypes.Clear(); ToLuaExport.allTypes.AddRange(ToLuaMenu.baseType); ToLuaExport.allTypes.AddRange(CustomSettings.staticClassTypes); for (int i = 0; i < ToLuaExport.allTypes.Count; i++) { btList.Add(new ToLuaMenu.BindType(ToLuaExport.allTypes[i])); } foreach (var bt in CustomSettings.customTypeList) { if (ToLuaExport.allTypes.Contains(bt.type)) { continue; } ToLuaExport.allTypes.Add(bt.type); btList.Add(bt); } GenBindTypes(btList.ToArray(), false); foreach (var bt in allTypes)//做最后的检查,进一步排除一些类 { if (bt.type.IsInterface && bt.type != typeof(System.Collections.IEnumerator)) { continue; } s_apiTypeIdx[bt.type] = bt; } //一些类需要手动加 { ToLuaMenu.BindType bt = new ToLuaMenu.BindType(typeof(Array)); s_apiTypeIdx[bt.type] = bt; GetClassApi("System.Collections.IEnumerable").AddMethod("GetEnumerator", "()", "System.Collections.IEnumerator", "System.Collections.IEnumerator"); } //生成信息 foreach (var bt in s_apiTypeIdx.Values) { GenApi(bt); } //信息转lua类文件 ToLuaExport.allTypes.Clear(); string s; if (!LuaUtil.TryToLua(s_api.childs, out s)) { return; } s = "return " + s; string path = LuaConst.zbsDir; path = path.Replace("lualibs/mobdebug", "api/lua/gameApiGen.lua"); System.IO.File.WriteAllText(path, s, System.Text.Encoding.UTF8); Debug.Log("生成自动提示文件成功:" + path); }
public static void Bsn_All() { Debug.Log("NBsnEditor.Menu Bsn_All()"); AssetDatabase.Refresh(); ToLuaMenu.beAutoGen = true; ToLuaMenu.ClearLuaWraps(); ToLuaMenu.ClearBaseTypeLuaWrap(); AssetDatabase.Refresh(); var staticClassTypes_back = CustomSettings.staticClassTypes; var staticClassTypes_new = new List <Type>(); staticClassTypes_new.AddRange(staticClassTypes_back); // staticClassTypes_new.AddRange(NBsn.Config.m_reg2LuaStaticType); HashSet <Type> set = new HashSet <Type>(); var customTypeList_back = CustomSettings.customTypeList; List <Type> reg2LuaType = new List <Type>(); // reg2LuaType.AddRange(NBsn.Config.m_reg2LuaType); GetUnityEngine(ref reg2LuaType); GetCustom(ref reg2LuaType); GetUGUI(ref reg2LuaType); for (int i = 0; i < reg2LuaType.Count; i++) { set.Add(reg2LuaType[i]); } for (int i = 0; i < customTypeList_back.Length; i++) { set.Add(reg2LuaType[i]); } foreach (var item in staticClassTypes_new) { set.Add(item); } foreach (var item in CustomSettings.dynamicList) { set.Add(item); } foreach (var item in ms_reg2LuaType) { set.Add(item); } foreach (var item in ToLuaMenu.dropType) { set.Remove(item); } foreach (var item in ToLuaMenu.baseType) { set.Remove(item); } var temp = new List <BindType>(); foreach (var t in set) { if (t.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length > 0) // 废弃类型 { continue; } if (t.GetCustomAttributes(typeof(NBsn.NotReg2LuaAttribute), false).Length > 0) // 不需要注册的类型 { continue; } temp.Add(new BindType(t)); } var customTypeList_new = new BindType[temp.Count]; temp.CopyTo(customTypeList_new); CustomSettings.customTypeList = customTypeList_new; CustomSettings.staticClassTypes = staticClassTypes_new; ToLuaMenu.GenBaseTypeLuaWrap(); ToLuaMenu.GenLuaDelegates(); ToLuaMenu.GenerateClassWraps(); ToLuaMenu.GenLuaBinder(); ToLuaMenu.beAutoGen = false; CustomSettings.customTypeList = customTypeList_back; CustomSettings.staticClassTypes = staticClassTypes_back; }