public ushort GetBasePropertyCount() { if (Type.BaseType != typeof(object)) { var baseClass = SirenMachine.FindClass(Type.BaseType); if (baseClass != null) { return((ushort)(baseClass.GetBasePropertyCount() + (ushort)baseClass.Fields.Count)); } } return(0); }
public virtual void OnField(string name, ushort id, object obj, bool withHeader = true) { if (withHeader) { OnFieldBegin(name, id, SirenMachine.GetTypeId(obj.GetType())); OnValue(obj); OnFieldEnd(); } else { OnValue(obj); } }
public virtual T OnField <T>(string name, ushort id, bool withHeader = true) { if (withHeader) { ushort outId; SirenTypeId outDataType; int r = OnFieldBegin(name, id, SirenMachine.GetTypeId(typeof(T)), out outId, out outDataType); if (r == 0) { var obj = OnValue(typeof(T)); OnFieldEnd(); return((T)obj); } return(default(T)); } else { return((T)OnValue(typeof(T))); } }
public bool Initialize() { if (Type.IsEnum) { return(true); } //get methods SerializeMethodInfo = Type.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Instance); DeserializeMethodInfo = Type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Instance); RegisterMethodInfo = Type.GetMethod("Register", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); if (RegisterMethodInfo != null && !Type.ContainsGenericParameters) { RegisterMethodInfo.Invoke(null, null); } //get properties uint index = 0; ushort id = GetBasePropertyCount(); var properties = Type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); foreach (var propertyInfo in properties) { var attrs = propertyInfo.GetCustomAttributes(typeof(SirenFieldAttribute), false); if (attrs.Length > 0) { SirenFieldAttribute propertyAttribute = attrs[0] as SirenFieldAttribute; if (propertyAttribute != null) { SirenField property = new SirenField(this, propertyInfo, propertyAttribute, index++, id++); Fields.Add(property); FieldIdDict.Add(property.Id, property); FieldNameDict.Add(property.Name, property); } } } if (Fields.Count == 0) { if (Type.BaseType == typeof(object) || Type.BaseType.IsValueType) { return(false); } } if (!Type.IsValueType && Type.BaseType != typeof(object) && !Type.BaseType.IsValueType) { BaseType = SirenMachine.GetClass(Type.BaseType); if (BaseType != null) { //add base properties foreach (var sirenProperty in BaseType.FieldIdDict) { FieldIdDict.Add(sirenProperty.Key, sirenProperty.Value); } foreach (var sirenProperty in BaseType.FieldNameDict) { FieldNameDict.Add(sirenProperty.Key, sirenProperty.Value); } } } return(true); }
public SirenField(SirenCustomClass parentSirenClass, PropertyInfo info, SirenFieldAttribute attribute, uint index, ushort id) { ParentType = parentSirenClass; Info = info; Name = Info.Name; Id = id; Attribute = attribute; Index = index; FieldType = SirenPropertyFieldType.Struct; Type = SirenMachine.GetType(info.PropertyType); DataType = SirenMachine.GetTypeId(Type.Type); if (Type.Type.IsGenericType) { if (Type.Name.StartsWith("List")) { FieldType = SirenPropertyFieldType.List; var valueType = Type.Type.GetGenericArguments()[0]; ValueType = SirenMachine.GetType(valueType); } else if (Type.Name.StartsWith("Dictionary")) { FieldType = SirenPropertyFieldType.Dictionary; var keyType = Type.Type.GetGenericArguments()[0]; var valueType = Type.Type.GetGenericArguments()[1]; KeyType = SirenMachine.GetType(keyType); ValueType = SirenMachine.GetType(valueType); } else { throw new Exception("Invalid siren field"); } DefaultValueString = String.Empty; } else { if (Type.Type.IsPrimitive || Type.Type == typeof(string) || Type.Type == typeof(byte[])) { if (Type.Type == typeof(string)) { FieldType = SirenPropertyFieldType.String; } else if (Type.Type == typeof(byte[])) { FieldType = SirenPropertyFieldType.Blob; } else { FieldType = SirenPropertyFieldType.Value; } } else { FieldType = SirenPropertyFieldType.Struct; ValueType = SirenMachine.GetType(Type.Type); } DefaultValueString = String.Empty; if (Type.Type.IsValueType) { if (Attribute.DefaultValue != null) { if (Type.Type.IsEnum) { DefaultValueString = String.Format("({0}){1}", Type.Name, Convert.ToUInt32(Attribute.DefaultValue)); } else { DefaultValueString = Attribute.DefaultValue.ToString(); } } //else //{ // if (Type.Type == typeof(bool)) // { // DefaultValueString = "false"; // } // else if (Type.Type == typeof(float)) // { // DefaultValueString = "0.f"; // } // else if (Type.Type == typeof(double)) // { // DefaultValueString = "0.0"; // } // else // { // if (Type.Type.IsEnum) // { // DefaultValueString = String.Format("({0})0", Type.Name); // } // else // { // DefaultValueString = "0"; // } // } //} } } }