// Get all properties from the type we are analyzing. public static List <IRClassProperty> ExtractClassProperties(TypeDefinition _subjectClass, out List <TypeDefinition> references) { // Will contain all typedefinitions of types referenced by this class. references = new List <TypeDefinition>(); // All properties for the given class definition. List <IRClassProperty> properties = new List <IRClassProperty>(); // Property != field // Properties expose fields by providing a getter/setter. // The properties are public accessible data, and these are the things that map // to protobuffer files. foreach (var property in _subjectClass.Properties) { // Property must have a setter method, otherwise it wouldn't be related to ProtoBuffers schema. if (property.SetMethod == null) { continue; } // Object which the current property references. TypeDefinition refDefinition; // Set of field (proto) options. IRClassProperty.ILPropertyOptions options = new IRClassProperty.ILPropertyOptions(); // Default to invalid field. options.Label = FieldLabel.INVALID; // IR type of the property. PropertyTypeKind propType = InspectorTools.DefaultTypeMapper(property, out refDefinition); // IR object - reference placeholder for the IR Class. IRTypeNode irReference = null; if (propType == PropertyTypeKind.TYPE_REF) { irReference = InspectorTools.ConstructIRType(refDefinition); // Also save the reference typedefinition for the caller to process. references.Add(refDefinition); } // Construct IR property and store. var prop = new IRClassProperty { Name = property.Name, Type = propType, ReferencedType = irReference, Options = options, }; properties.Add(prop); } return(properties); }
public static PropertyTypeKind FixedTypeMapper(IRClassProperty property) { PropertyTypeKind fieldType; switch (property.Type) { case PropertyTypeKind.UINT32: fieldType = PropertyTypeKind.FIXED32; break; case PropertyTypeKind.UINT64: fieldType = PropertyTypeKind.FIXED64; break; default: fieldType = property.Type; break; } return(fieldType); }
// Get all properties from the type we are analyzing. public static List <IRClassProperty> ExtractClassProperties(TypeDefinition _subjectClass, out List <TypeDefinition> references) { // Contains all references (TypeDefinitions) that are referenced by this class. references = new List <TypeDefinition>(); // All properties for the given class. List <IRClassProperty> properties = new List <IRClassProperty>(); // Propertye != field; see SilentOrbitInspector.ExtractClassproperties(..) foreach (var property in _subjectClass.Properties) { // Default to OPTIONAL label. The Google protobuffer compiler is written for // proto3 syntax, which does not allow REQUIRED. // Everything is implicitly OPTIONAL, but OPTIONAL fields who have the default value // will not be written onto the wire. // CARE MUST BE TAKEN to provide all necessary values when using these decompiled proto files. FieldLabel label = FieldLabel.OPTIONAL; // Each schema related property must have a set method, but repeated fields don't have // a set method. We must figure out if the property is lib related or schema related! if (property.SetMethod == null) { // Repeated field have propertyType RepeatedField<X>. if (property.PropertyType.Name.Contains("RepeatedField") == true && property.PropertyType.IsGenericInstance) { // This field must be analyzed. label = FieldLabel.REPEATED; } else { continue; } } // Object which the current property references. TypeDefinition refDefinition; // Field options (directly related to protobuf schema) IRClassProperty.ILPropertyOptions opts = new IRClassProperty.ILPropertyOptions(); // Add label to the property options. opts.Label = label; // Fetch the IR type of the property. - Doesn't actually matter, the Serialize handler will overwrite this. PropertyTypeKind propType = InspectorTools.DefaultTypeMapper(property, out refDefinition); // Construct IR reference placeholder. IRTypeNode irReference = null; if (propType == PropertyTypeKind.TYPE_REF) { irReference = InspectorTools.ConstructIRType(refDefinition); // And save the reference TYPEDEFINITION for the caller to process. references.Add(refDefinition); } // Fetch the fieldNumber for this property. var tag = ExtractFieldNumber(_subjectClass, property.Name); // Add it to the options. opts.PropertyOrder = tag; // Construct the IR property and store it. var prop = new IRClassProperty() { Name = property.Name, Type = propType, ReferencedType = irReference, Options = opts, }; properties.Add(prop); } return(properties); }
public static void SerializeOnCall(CallInfo info, List <string> property_names, List <FieldDefinition> allFields, List <IRClassProperty> properties, List <TypeDefinition> references) { if (!info.Method.Name.StartsWith("Write") || info.Method.Name.Equals("WriteUntil")) { // We are in no relevant method. return; } var fieldNameArg = info.Arguments[2].ToString(); var bracketIdx = fieldNameArg.LastIndexOf("["); var bracketLastIdx = fieldNameArg.LastIndexOf("]"); var fieldNameRef = UInt32.Parse(fieldNameArg.Substring(bracketIdx + 1, bracketLastIdx - bracketIdx - 1)); var fieldName = property_names[(int)fieldNameRef]; var type = info.Method.Name.Substring(5); var packedIdx = type.IndexOf("Packed"); var isPacked = false; if (packedIdx > -1) { Debug.Assert(packedIdx == 0); isPacked = true; type = type.Substring(6); } var specificType = InspectorTools.LiteralTypeMapper(type); var fieldIdx = (int)info.Arguments[1]; var label = FieldLabel.REQUIRED; if (info.Conditions.Any(c => c.Lhs.Contains("get_Count"))) { label = FieldLabel.REPEATED; } else if (info.Conditions.Any(c => c.Lhs.Contains("has"))) { label = FieldLabel.OPTIONAL; } // Construct IR reference placeholder. IRTypeNode irReference = null; if (specificType == PropertyTypeKind.TYPE_REF) { TypeReference fieldReference; if (info.Method.IsGenericInstance) { var method = info.Method as GenericInstanceMethod; fieldReference = method.GenericArguments[0]; } else { // Find out definition from arguments var cleanFieldName = fieldName.ToLower().Replace("_", ""); var fieldRef = allFields.OrderBy(f => f.Name.Length).First(field => field.Name.ToLower().Contains(cleanFieldName)); fieldReference = fieldRef.FieldType; } if (label == FieldLabel.REPEATED && fieldReference.IsGenericInstance) { var genericField = fieldReference as GenericInstanceType; fieldReference = genericField.GenericArguments[0]; } var fieldDefinition = fieldReference.Resolve(); irReference = InspectorTools.ConstructIRType(fieldDefinition); Debug.Assert(!fieldDefinition.FullName.Equals("System.UInt32")); // And save the reference TYPEDEFINITION for the caller to process. references.Add(fieldDefinition); } var newProperty = new IRClassProperty() { Name = fieldName, Type = specificType, ReferencedType = irReference, Options = new IRClassProperty.ILPropertyOptions() { Label = label, PropertyOrder = fieldIdx, IsPacked = isPacked, } }; properties.Add(newProperty); return; }