public static string ToMakeClasses(Type t, string[] props, MethodData[] methods, MethodData[] constructors, ExMethodData[] extmethods, string macros, string folder, List<string>[] errors ) { InitOp_FuncNames(); ParamTypeData T = new ParamTypeData(t); string str = string.Format("-------- generating Lua Interface for {0} ----------", T.csTypeName); Debug.Log(str); funcNames.Clear(); string className = string.Format("Lua{0}",T.csTypeName); string filename = string.Format("{0}/{1}.cs", folder, className); FileStream fs = new FileStream(filename, FileMode.Create); StreamWriter n = new StreamWriter(fs); if (string.IsNullOrEmpty(macros) == false) n.WriteLine(macros); writeHead(n, t); Dictionary<string,bool> ignoreProps = new Dictionary<string,bool>(); bool ignored = false; bool generatAllProps = false; bool ignore = false; for (int i = 0; props != null && i < props.Length; ++i) { string propname = props[i]; if( String.IsNullOrEmpty(propname)) continue; if (propname == "*") { if (!ignore) { generatAllProps = true; Debug.Log("generating all properties & fields ..."); } continue; } if (propname == "|") { ignore = true; continue; } if (ignore) { ignoreProps[propname] = true; continue; } if (generatAllProps) { continue; } string s = string.Format("generating prop:{0}", props[i]); Debug.Log(s); PropertyInfo info = t.GetProperty(props[i]); if (info == null) { FieldInfo finfo = t.GetField(props[i]); if (finfo == null) { string strerr = string.Format("can't find property:{0}.{1}", T.csTypeName, props[i]); errors[1].Add(strerr); continue; } if (IsDelegate(finfo.FieldType)) continue; bool isStatic = finfo.IsStatic; writeReadProp(n, t, finfo.FieldType, finfo.Name,isStatic, finfo.FieldType.IsEnum); if(finfo.IsLiteral == false) writeWriteProp(n, t, finfo.FieldType, finfo.Name, isStatic, finfo.FieldType.IsEnum); continue; } MethodInfo getM = info.GetGetMethod(); MethodInfo setM = info.GetSetMethod(); bool isStaticProp = getM != null ? getM.IsStatic : setM.IsStatic; if (info.CanRead) writeReadProp(n, t, info.PropertyType, info.Name, isStaticProp, info.PropertyType.IsEnum); if (info.CanWrite) writeWriteProp(n, t, info.PropertyType, info.Name, isStaticProp, info.PropertyType.IsEnum); } if (generatAllProps) { Debug.Log("auto props as follows:"); Dictionary<string, bool> haveProps = new Dictionary<string, bool>(); PropertyInfo[] infos = t.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (PropertyInfo info in infos) { if(ignoreProps.TryGetValue(info.Name,out ignored)) continue; if (IsDelegate(info.PropertyType)) continue; MethodInfo getM = info.GetGetMethod(); MethodInfo setM = info.GetSetMethod(); bool isStaticProp = getM != null ? getM.IsStatic : setM.IsStatic; haveProps[info.Name] = true; Debug.Log(info.Name); if (info.CanRead) writeReadProp(n, t, info.PropertyType, info.Name, isStaticProp, info.PropertyType.IsEnum); if (info.CanWrite) writeWriteProp(n, t, info.PropertyType, info.Name, isStaticProp, info.PropertyType.IsEnum); } Debug.Log("-----------"); FieldInfo[] finfos = t.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (FieldInfo finfo in finfos) { if (haveProps.TryGetValue(finfo.Name, out ignored)) continue; if(ignoreProps.TryGetValue(finfo.Name,out ignored)) continue; if (IsDelegate(finfo.FieldType)) continue; Debug.Log(finfo.Name); bool isStatic = finfo.IsStatic; writeReadProp(n, t, finfo.FieldType, finfo.Name, isStatic, finfo.FieldType.IsEnum); if( finfo.IsLiteral == false) writeWriteProp(n, t, finfo.FieldType, finfo.Name, isStatic, finfo.FieldType.IsEnum); } } Dictionary<string, bool> ignoreMethods = new Dictionary<string, bool>(); ignoreMethods["Equals"] = true; ignoreMethods["GetHashCode"] = true; ignoreMethods["GetType"] = true; ignoreMethods["ReferenceEquals"] = true; ignoreMethods["ToString"] = true; ignoreMethods["OnDrawGizmos"] = true; bool generatAllMethods = false; ignore = false; for (int i = 0; methods != null && i < methods.Length; ++i) { MethodData data = methods[i]; if (data.name == "*") { if (!ignore) { Debug.Log("generating all method ..."); generatAllMethods = true; } continue; } if (data.name == "|") { ignore = true; continue; } if (ignore) { ignoreMethods[data.name] = true; continue; } if (generatAllMethods) { continue; } string s = string.Format("generating method:{0}", string.IsNullOrEmpty(data.alias) ? data.name : data.alias); Debug.Log(s); MethodInfo info = null; if (methods[i].tlist != null) { info = t.GetMethod(data.name, data.tlist); } else { info = t.GetMethod(data.name); } if (info == null) { string strerr = string.Format("can't find function:{0}.{1}", T.csTypeName,data.name); errors[1].Add(strerr); continue; } writeLine(n, "//---------------------------------"); writeMethod(n, info, t,data.alias,errors); } if (generatAllMethods) { Dictionary<string, int> generatedMethods = new Dictionary<string, int>(); MethodInfo[] infos = t.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (MethodInfo info in infos) { if (ignoreMethods.TryGetValue(info.Name, out ignored)) continue; if (info.Name.IndexOf("get_") >= 0) continue; if (info.Name.IndexOf("set_") >= 0) continue; string s = string.Format("generating method:{0}", info.Name); Debug.Log(s); writeLine(n, "//---------------------------------"); int count; if (generatedMethods.TryGetValue(info.Name, out count) == false) count = 0; ++count; generatedMethods[info.Name] = count; writeMethod(n, info, t, count == 1 ? "" : info.Name + "_"+count, errors); } } for (int i = 0; constructors != null && i < constructors.Length; ++i) { MethodData data = constructors[i]; Debug.Log("generating constructor"); ConstructorInfo info = default(ConstructorInfo); if (data.tlist != null) info = t.GetConstructor(data.tlist); else { ConstructorInfo[] infos = t.GetConstructors(); if (infos != null && infos.Length > 0) info = infos[0]; } if (info == null) { string strerr = string.Format("can't find Constructor:{0}.{1}", T.csTypeName,info.Name); errors[1].Add(strerr); continue; } writeConstructor(n, info, t,data.alias); } if (t.IsSubclassOf(typeof(UnityEngine.Object))) writeIsNilFunc(n, T); writeFuncsArray(n,t,extmethods); writeTail(n, t); if (string.IsNullOrEmpty(macros) == false) n.WriteLine("#endif"); n.Close(); fs.Close(); return className; }
public LuaObjectInterfaceInfo(Type _t, string[] _props=null, MethodData[] _methods=null, MethodData[] _constructors=null, ExMethodData[] _extmethods = null, bool _enabled = true, string _macros="") { t = _t; props = _props; methods = _methods; constructors = _constructors; enabled = _enabled; extmethods = _extmethods; macros = _macros; }
static void writeFuncsArray(StreamWriter n,Type t,ExMethodData[] extmethods) { ParamTypeData T = new ParamTypeData(t); string str; string tableName = T.csTypeName; n.WriteLine("public static void Register(IntPtr L)"); n.WriteLine("{"); writeLine(n, "LuaDLL.lua_newtable(L);"); List<string> metafuncs = new List<string>(); foreach( string each in funcNames) { if (each.StartsWith("__")) { metafuncs.Add(each); metafuncs.Add(each); continue; } n.WriteLine("LuaDLL.lua_pushstdcallcfunction(L,new LuaCSFunction({0}) );", each); n.WriteLine("LuaDLL.lua_setfield(L, -2, \"{0}\");", each); } //write unbind n.WriteLine("LuaDLL.lua_pushstdcallcfunction(L,new LuaCSFunction(LuaStatic.removeGameObjectFrameCache) );"); n.WriteLine("LuaDLL.lua_setfield(L, -2, \"unbind\");"); if (extmethods != null) { foreach (ExMethodData extmethod in extmethods) { if (extmethod.luaName.StartsWith("__")) { metafuncs.Add(extmethod.luaName); metafuncs.Add(extmethod.csName); continue; } n.WriteLine("LuaDLL.lua_pushstdcallcfunction(L,new LuaCSFunction({0}) );", extmethod.csName); n.WriteLine("LuaDLL.lua_setfield(L, -2, \"{0}\");", extmethod.luaName); } } //gc metafuncs.Add("__gc"); metafuncs.Add("LuaStatic.GameObjectGC"); writeLine(n, "LuaDLL.lua_pushvalue(L,-1);"); writeLine(n, "LuaDLL.lua_setglobal(L,\"{0}\");","mt" + tableName); //mt writeLine(n, "LuaDLL.lua_newtable(L);"); writeLine(n, "LuaDLL.lua_pushvalue(L,-2);"); writeLine(n, "LuaDLL.lua_setfield(L,-2,\"__index\");"); writeLine(n, "LuaDLL.lua_setglobal(L,\"{0}\");", "mtp"+tableName); //{ __index = mt } writeLine(n, "LuaDLL.lua_pop(L,1);"); n.WriteLine("}"); n.WriteLine("public static void SetMtLink(IntPtr L)"); n.WriteLine("{"); str = string.Format("LuaDLL.lua_getglobal(L,\"{0}\");", "mt"+tableName); //mt n.WriteLine(str); string parentName = (T.csTypeName == "Object" || t.BaseType.FullName == "System.Object") ? "" : t.BaseType.Name; //__index if(string.IsNullOrEmpty(parentName) == false) { n.WriteLine("LuaDLL.lua_getglobal(L,\"{0}\");","mtp"+parentName); //mt,mtp writeLine(n, "LuaDLL.lua_setmetatable(L,-2);");//mt } writeLine(n, "LuaDLL.lua_newtable(L);"); //mt,cs writeLine(n, "LuaDLL.lua_pushstring(L,\"{0}\");", tableName); //mt,cs,tablename writeLine(n, "LuaDLL.lua_setfield(L,-2,\"name\");"); //mt,cs //metafuncs { for (int i = 0; i < metafuncs.Count; i += 2) { n.WriteLine("LuaDLL.lua_pushstdcallcfunction(L,new LuaCSFunction({0}) );", metafuncs[i + 1]); n.WriteLine("LuaDLL.lua_setfield(L, -2, \"{0}\");", metafuncs[i]); } } writeLine(n, "LuaDLL.lua_newtable(L);"); //mt,cs,cst n.WriteLine("LuaDLL.lua_getglobal(L,\"createcstable\");"); //mt,cs,cst,f n.WriteLine("LuaDLL.lua_pushvalue(L,-4);"); //mt,cs,cst,f,mt n.WriteLine("LuaDLL.lua_pushvalue(L,-4);"); //mt,cs,cst,f,mt,cs n.WriteLine("LuaDLL.lua_pushvalue(L,-4);"); //mt,cs,cst,f,mt,cs,cst n.WriteLine("LuaDLL.lua_pcall(L,3,0,0);"); //mt,cs,cst writeLine(n, "LuaDLL.lua_setmetatable(L,-2);"); //mt,cs writeLine(n, "LuaDLL.lua_setglobal(L,\"{0}\");", tableName);//mt n.WriteLine("LuaDLL.lua_pop(L,1);"); n.WriteLine("}"); n.WriteLine(""); }