private static void GetProperty(Assembly a, ClassDescription result, ScriptableClass sc) { result.Propertys = new List<PropertyFieldDescription>(); foreach (ScriptableProperty sp in sc.Property) { // PropertyInfo pi = result.Class.GetProperty(sp.Name,sp.GetArgs(result)); if (null != pi) { PropertyFieldDescription pfd = new PropertyFieldDescription(); pfd.Pi = pi; pfd.IsStatic = false; pfd.RawType = pi.PropertyType; pfd.TypeName = GeneratorHelper.GetTypeName(pfd.RawType); pfd.Name = pi.Name; pfd.Type = GetMessageFieldType(pi.PropertyType); pfd.IsItemProperty = string.Equals(sp.Name, "Item"); result.Propertys.Add(pfd); if (pi.GetGetMethod() != null && pi.GetGetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetSetMethod() != null && pi.GetSetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetGetMethod() != null) { pfd.GetMethod = new MethodDescription(); GetMethod(pfd.GetMethod,pi.GetGetMethod(), sp.Get,pi.Name); } if (pi.GetSetMethod() != null) { pfd.SetMethod = new MethodDescription(); GetMethod(pfd.SetMethod, pi.GetSetMethod(), sp.Set, pi.Name); } } else { Console.WriteLine(string.Format("property {0} not found", sp.Name)); } } }
private static void GenerateGetSetMethod(ClassDescription cd, PropertyFieldDescription pfd, Lua4NetSerializer serializer) { #region get if (pfd.GetMethod != null) { serializer.NewLine(); string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", pfd.GetMethod.NickName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { string classOrInstance = pfd.IsStatic ? cd.ClassName : "Instance"; MethodFieldDescription mfd = pfd.GetMethod.Output; if (!pfd.IsItemProperty) { switch (pfd.GetMethod.Output.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{1}.{0}?1:0);", pfd.Name, classOrInstance)); break; } case NumberType.Enum: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,(int){1}.{0});", pfd.Name, classOrInstance)); break; } case NumberType.Numeric: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{1}.{0});", pfd.Name, classOrInstance)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("LuaApi.lua_pushstring(l,{1}.{0});", pfd.Name, classOrInstance)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine(string.Format("int id = LuaManager.Instance.PushStackObject({1}.{0});", pfd.Name, classOrInstance)); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); break; } } } else { MethodFieldDescription input = pfd.GetMethod.InputArgs[0]; switch (input.Type) { case MessageFieldType.NumberType: { serializer.NewLine("int index = (int)LuaApi.lua_tonumber(l,3);"); break; } case MessageFieldType.StringType: { serializer.NewLine("string index = LuaApi.lua_tostring(l,3);"); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine("not supported...."); break; } } switch (pfd.GetMethod.Output.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{0}[index]?1:0);", classOrInstance)); break; } case NumberType.Enum: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,(int){0}[index]);", classOrInstance)); break; } case NumberType.Numeric: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{0}[index]);",classOrInstance)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("LuaApi.lua_pushstring(l,{0}[index]);", classOrInstance)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine(string.Format("int id = LuaManager.Instance.PushStackObject({0}[index]);", classOrInstance)); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); break; } } } serializer.NewLine("return 1;"); }); } #endregion #region set if (pfd.SetMethod != null) { string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", pfd.SetMethod.NickName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { string classOrInstance = pfd.IsStatic ? cd.ClassName : "Instance"; foreach (MethodFieldDescription mfd in pfd.SetMethod.InputArgs) { switch (mfd.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("{0}.{1} = LuaApi.lua_tonumber(l,3)!=0;", classOrInstance, pfd.Name)); break; } case NumberType.Enum: case NumberType.Numeric: { serializer.NewLine(string.Format("{0}.{1} = ({2})LuaApi.lua_tonumber(l,3);", classOrInstance, pfd.Name, pfd.TypeName)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("{0}.{1} = LuaApi.lua_tostring(l,3);", classOrInstance, pfd.Name)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine("int id = (int)LuaApi.lua_tonumber(l,3);"); serializer.NewLine(string.Format("{0}.{1} = LuaManager.Instance.GetObjectT<{2}>(id);", classOrInstance, pfd.Name, pfd.TypeName)); break; } } } serializer.NewLine("return 0;"); }); } #endregion }
private static void GetProperty(Assembly a, ClassDescription result, Type type) { result.Propertys = new List<PropertyFieldDescription>(); foreach (PropertyInfo pi in type.GetProperties()) { ScriptableAttribute attr = GetAttribute(pi); if (null != attr) { PropertyFieldDescription pfd = new PropertyFieldDescription(); pfd.Pi = pi; pfd.IsStatic = false; pfd.RawType = pi.PropertyType; pfd.TypeName = GeneratorHelper.GetTypeName(pfd.RawType); pfd.Name = pi.Name; pfd.Type = GetMessageFieldType(pi.PropertyType); pfd.IsItemProperty = string.Equals(attr.Name, "Item"); result.Propertys.Add(pfd); if (pi.GetGetMethod() != null && pi.GetGetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetSetMethod() != null && pi.GetSetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetGetMethod() != null) { pfd.GetMethod = new MethodDescription(); string getName = string.IsNullOrEmpty(attr.Name) ? "get_" + pi.Name : "get_" + attr.Name; GetMethod(pfd.GetMethod, pi.GetGetMethod(), getName, pi.Name); } if (pi.GetSetMethod() != null) { pfd.SetMethod = new MethodDescription(); string setName = string.IsNullOrEmpty(attr.Name) ? "set_" + pi.Name : "set_" + attr.Name; GetMethod(pfd.SetMethod, pi.GetSetMethod(), setName, pi.Name); } } } }