static int get_Define(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); FieldDescriptor obj = (FieldDescriptor)o; google.protobuf.FieldDescriptorProto ret = obj.Define; ToLua.PushObject(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index Define on a nil value" : e.Message)); } }
// because of protobuf-net we're limited to parsing simple types at run-time as we can't parse the protobuf, but options shouldn't be too complex private void BuildExtension(string key, FieldDescriptorProto[] fields) { TypeBuilder extension = moduleBuilder.DefineType(key + "Ext", TypeAttributes.Class); Type pcType = typeof(ProtoContractAttribute); ConstructorInfo pcCtor = pcType.GetConstructor(Type.EmptyTypes); CustomAttributeBuilder pcBuilder = new CustomAttributeBuilder(pcCtor,Type.EmptyTypes); extension.SetCustomAttribute(pcBuilder); foreach (var field in fields) { DataFormat format; bool buildEnumProxy; Type fieldType = ImageFile.LookupBasicType(field.type, out format, out buildEnumProxy); FieldBuilder fbuilder = extension.DefineField(field.name, fieldType, FieldAttributes.Public); object defaultValue; if (String.IsNullOrEmpty(field.default_value)) { defaultValue = Activator.CreateInstance(fieldType); } else { try { defaultValue = Convert.ChangeType(field.default_value, fieldType); } catch (FormatException) { Console.WriteLine("Constructor for extension had bad format: {0}", key); return; } } if (buildEnumProxy) { Type epType = typeof(EnumProxyAttribute); ConstructorInfo epCtor = epType.GetConstructor(new Type[] { typeof(string) }); CustomAttributeBuilder epBuilder = new CustomAttributeBuilder(epCtor, new object[] { field.type_name }); fbuilder.SetCustomAttribute(epBuilder); } Type dvType = typeof(DefaultValueAttribute); ConstructorInfo dvCtor = dvType.GetConstructor(new Type[] { typeof(object) }); CustomAttributeBuilder dvBuilder = new CustomAttributeBuilder(dvCtor, new object[] { defaultValue }); fbuilder.SetCustomAttribute(dvBuilder); Type pmType = typeof(ProtoMemberAttribute); ConstructorInfo pmCtor = pmType.GetConstructor(new Type[] { typeof(int) }); CustomAttributeBuilder pmBuilder = new CustomAttributeBuilder(pmCtor, new object[] { field.number }, new PropertyInfo[] { pmType.GetProperty("Name"), pmType.GetProperty("DataFormat") }, new object[] { "(" + field.name + ")", format } ); fbuilder.SetCustomAttribute(pmBuilder); } Type extensionType = extension.CreateType(); if (!this.protobufExtensions.ContainsKey(key)) { this.protobufExtensions[key] = new List<Type>(); } this.protobufExtensions[key].Add(extensionType); }
private string BuildDescriptorDeclaration(FieldDescriptorProto field) { PushDescriptorName(field); string type = ResolveType(field); Dictionary<string, string> options = new Dictionary<string, string>(); if (!String.IsNullOrEmpty(field.default_value)) { string default_value = field.default_value; if (field.type == FieldDescriptorProto.Type.TYPE_STRING) default_value = String.Format("\"{0}\"", default_value); options.Add("default", default_value); } else if (field.type == FieldDescriptorProto.Type.TYPE_ENUM && field.label != FieldDescriptorProto.Label.LABEL_REPEATED) { options.Add("default", ResolveOrDeferEnumDefaultValue(type)); } Dictionary<string, string> fieldOptions = DumpOptions(field.options); foreach (var pair in fieldOptions) { options[pair.Key] = pair.Value; } string parameters = String.Empty; if (options.Count > 0) { parameters = " [" + String.Join(", ", options.Select(kvp => String.Format("{0} = {1}", kvp.Key, kvp.Value))) + "]"; } PopDescriptorName(); return GetLabel(field.label) + " " + type + " " + field.name + " = " + field.number + parameters + ";"; }
private String ResolveType(FieldDescriptorProto field) { if (field.type == FieldDescriptorProto.Type.TYPE_MESSAGE) { return field.type_name; } else if (field.type == FieldDescriptorProto.Type.TYPE_ENUM) { return field.type_name; } return GetType(field.type); }
public static Type LookupBasicType(FieldDescriptorProto.Type type, out DataFormat format, out bool buildEnumProxy) { buildEnumProxy = false; format = DataFormat.Default; switch (type) { default: return null; case FieldDescriptorProto.Type.TYPE_INT32: return typeof(Int32); case FieldDescriptorProto.Type.TYPE_INT64: return typeof(Int64); case FieldDescriptorProto.Type.TYPE_SINT32: return typeof(Int32); case FieldDescriptorProto.Type.TYPE_SINT64: return typeof(Int64); case FieldDescriptorProto.Type.TYPE_UINT32: return typeof(UInt32); case FieldDescriptorProto.Type.TYPE_UINT64: return typeof(UInt64); case FieldDescriptorProto.Type.TYPE_STRING: return typeof(String); case FieldDescriptorProto.Type.TYPE_BOOL: return typeof(Boolean); case FieldDescriptorProto.Type.TYPE_BYTES: return typeof(byte[]); case FieldDescriptorProto.Type.TYPE_DOUBLE: return typeof(Double); case FieldDescriptorProto.Type.TYPE_FLOAT: return typeof(float); case FieldDescriptorProto.Type.TYPE_MESSAGE: return typeof(string); case FieldDescriptorProto.Type.TYPE_FIXED32: { format = DataFormat.FixedSize; return typeof(Int32); } case FieldDescriptorProto.Type.TYPE_FIXED64: { format = DataFormat.FixedSize; return typeof(Int64); } case FieldDescriptorProto.Type.TYPE_SFIXED32: { format = DataFormat.FixedSize; return typeof(Int32); } case FieldDescriptorProto.Type.TYPE_SFIXED64: { format = DataFormat.FixedSize; return typeof(Int64); } case FieldDescriptorProto.Type.TYPE_ENUM: { buildEnumProxy = true; return typeof(Int32); } } }
private static String GetType(FieldDescriptorProto.Type type) { switch (type) { default: return type.ToString(); case FieldDescriptorProto.Type.TYPE_INT32: return "int32"; case FieldDescriptorProto.Type.TYPE_INT64: return "int64"; case FieldDescriptorProto.Type.TYPE_SINT32: return "sint32"; case FieldDescriptorProto.Type.TYPE_SINT64: return "sint64"; case FieldDescriptorProto.Type.TYPE_UINT32: return "uint32"; case FieldDescriptorProto.Type.TYPE_UINT64: return "uint64"; case FieldDescriptorProto.Type.TYPE_STRING: return "string"; case FieldDescriptorProto.Type.TYPE_BOOL: return "bool"; case FieldDescriptorProto.Type.TYPE_BYTES: return "bytes"; case FieldDescriptorProto.Type.TYPE_DOUBLE: return "double"; case FieldDescriptorProto.Type.TYPE_ENUM: return "enum"; case FieldDescriptorProto.Type.TYPE_FLOAT: return "float"; case FieldDescriptorProto.Type.TYPE_GROUP: return "GROUP"; case FieldDescriptorProto.Type.TYPE_MESSAGE: return "message"; case FieldDescriptorProto.Type.TYPE_FIXED32: return "fixed32"; case FieldDescriptorProto.Type.TYPE_FIXED64: return "fixed64"; case FieldDescriptorProto.Type.TYPE_SFIXED32: return "sfixed32"; case FieldDescriptorProto.Type.TYPE_SFIXED64: return "sfixed64"; } }
private static String GetLabel(FieldDescriptorProto.Label label) { switch (label) { default: case FieldDescriptorProto.Label.LABEL_OPTIONAL: return "optional"; case FieldDescriptorProto.Label.LABEL_REQUIRED: return "required"; case FieldDescriptorProto.Label.LABEL_REPEATED: return "repeated"; } }
private void PushDescriptorName(FieldDescriptorProto field) { messageNameStack.Push(field.name); }
private string BuildDescriptorDeclaration(FieldDescriptorProto field, bool emitFieldLabel = true) { PushDescriptorName(field); string type = ResolveType(field); Dictionary<string, string> options = new Dictionary<string, string>(); if (!String.IsNullOrEmpty(field.default_value)) { string default_value = field.default_value; if (field.type == FieldDescriptorProto.Type.TYPE_STRING) default_value = String.Format("\"{0}\"", default_value); options.Add("default", default_value); } else if (field.type == FieldDescriptorProto.Type.TYPE_ENUM && field.label != FieldDescriptorProto.Label.LABEL_REPEATED) { options.Add("default", ResolveOrDeferEnumDefaultValue(type)); } Dictionary<string, string> fieldOptions = DumpOptions(field.options); foreach (var pair in fieldOptions) { options[pair.Key] = pair.Value; } string parameters = String.Empty; if (options.Count > 0) { parameters = " [" + String.Join(", ", options.Select(kvp => String.Format("{0} = {1}", kvp.Key, kvp.Value))) + "]"; } PopDescriptorName(); var descriptorDeclarationBuilder = new StringBuilder(); if (emitFieldLabel) { descriptorDeclarationBuilder.Append(GetLabel(field.label)); descriptorDeclarationBuilder.Append(" "); } descriptorDeclarationBuilder.AppendFormat("{0} {1} = {2}{3};", type, field.name, field.number, parameters); return descriptorDeclarationBuilder.ToString(); }
static void GenFieldParse(google.protobuf.FieldDescriptorProto field, StringBuilder builder) { }