public static dynamic Parse(this ClassField field, ref IDataReader reader) { if (field.is_vector || field.type == "ByteArray") { dynamic[] array = null; if (field.constant_length.HasValue) { array = new dynamic[field.constant_length.Value]; } else { string read_length_method = $"Read{field.write_length_method.Replace("write", "")}"; array = new dynamic[read_length_method._readMethod(ref reader)]; } for (int i = 0; i < array.Length; i++) { array[i] = field._readElement(ref reader); } return(array); } else { return(field._readElement(ref reader)); } }
public static string GetDefaultValueByType(ClassField f) { if (f.Declare.DataType != DataTypes.BuiltIn) { return(""); } switch (f.Declare.Name) { case "Byte": case "UInt16": case "UInt32": case "UInt64": case "SByte": case "Int16": case "Int32": case "Int64": case "Double": case "Single": return("0"); case "Boolean": return("false"); case "String": return(""); default: if (f.Declare.Class != null && f.Declare.Class.IsEnum) { return((f.Declare.Namespace != "" ? f.Declare.Namespace : _pn) + "." + f.Declare.Name + "." + ((Enum)f.Declare.Class).Fields.First().Name); } return(""); } }
public void EmptyClassesTest() { var field = new ClassField(); Assert.AreEqual(string.Format(classTemplate, ""), field.ToString()); }
public static bool IsSimpleType(ClassField f) { if (f.Declare.DataType != DataTypes.BuiltIn) { return(false); } if (f.Declare.Name == "String") { return(false); } if (f.Declare.Class != null && f.Declare.Class.IsEnum) { return(true); } switch (f.Declare.Name) { case "Byte": case "UInt16": case "UInt32": case "UInt64": case "SByte": case "Int16": case "Int32": case "Int64": case "Double": case "Single": case "Boolean": return(true); } return(false); }
public override string ToString(string format, IFormatProvider formatProvider) { var sb = new StringBuilder(); sb.AppendFormat("<{0}:0x{1:x8} {{", Class.Name, SerialID); bool trimEnd = false; foreach (KeyValuePair <string, Dynamic> pair in fields) { ClassField field = klass.GetField(pair.Key); if (field != null && field.Scope != Scope.Public) { continue; } sb.AppendFormat("{0} = {1}, ", pair.Key, pair.Value); trimEnd = true; } if (trimEnd) { sb.Remove(sb.Length - 2, 2); } return(sb.Append("}>").ToString()); }
public static void Parse(ClassField field, dynamic value, BigEndianWriter writer) { if (field.is_vector || field.type == "ByteArray") { if (!field.constant_length.HasValue) { string write_len_method = field.write_length_method.Replace("write", "Write"); if (value is null) { _writeMethod(write_len_method, 0, ref writer); return; } _writeMethod(write_len_method, value.Length, ref writer); } for (int i = 0; i < value.Length; i++) { _writeElement(field, value[i], ref writer); } } else { _writeElement(field, value, ref writer); } }
private static void SetVal(ref ClassField cf, MemberInfo field, Assembly hotCode, object hotInstance, GameObject instance) { object value; var type = (field is PropertyInfo) ? ((PropertyInfo)field).PropertyType : ((FieldInfo)field).FieldType; if (hotInstance == null) { value = type.IsValueType ? Activator.CreateInstance(type) : null; } else { if (field is PropertyInfo) { value = ((PropertyInfo)field).GetValue(hotInstance); } else { value = ((FieldInfo)field).GetValue(hotInstance); } } SetVal(ref cf, type, hotCode, value, instance); }
public void LoadClass(VLTDataClassLoad classLoad, VLTFile vltFile) { _vltFile = vltFile; _classLoad = classLoad; _classHash = classLoad.NameHash; _pointers = vltFile.GetChunk(VLTChunkId.Pointers) as VLTPointers; int offset = _pointers[classLoad.Pointer].OffsetDest; vltFile.RawStream.Seek(offset, SeekOrigin.Begin); BinaryReader br = new BinaryReader(vltFile.RawStream); _classFields = new ClassField[_classLoad.TotalFieldsCount]; for (int i = 0; i < _classLoad.TotalFieldsCount; i++) { ClassField field = new ClassField(); field.Read(br); // HACK: for hash dumping later on HashResolver.Resolve(field.NameHash); _classFields[i] = field; } _data = new ClassData(this); }
private void DumpField(ClassField field) { textWriter.Write(field.Scope.ToString().ToLower()); switch (field.Modifier) { case Modifier.Default: break; case Modifier.StaticFinal: textWriter.Write(" static final"); break; default: textWriter.Write(" {0}", field.Modifier.ToString().ToLower()); break; } textWriter.Write(" {0}", SafeName(field.Name)); if (field.Initializer != null) { textWriter.Write(" = "); field.Initializer.AcceptCompiler(this); } textWriter.WriteLine(";"); }
private void getClassData(ClassDataDesc cdd) { var details = cdd.ClassDetails; ClassDetails cd = details[0]; var fields = cd.GetFields(); ClassField cf = (ClassField)fields[0]; _fieldData[cf.GetName()] = cf.GetValue(); }
public InfoDisabledActionViewModel(string name, string icon, string tooltip) : base("", "", name, icon) { Tooltip = tooltip; ClassField.AddClass("disabled"); if (tooltip != null) { ClassField.AddClass("has-tip"); } }
private object getClassData(ClassDataDesc cdd) { var details = cdd.ClassDetails; ClassDetails cd = details[0]; var fields = cd.GetFields(); ClassField cf = (ClassField)fields[0]; return(cf.GetValue()); }
public void RemoveClassTest() { var field = new ClassField() .AddClass("test") .RemoveClass("test"); Assert.IsFalse(field.hasClass("test")); Assert.AreEqual(string.Format(classTemplate, ""), field.ToString()); }
public void NoDuplicateTest() { var field = new ClassField() .AddClass("test") .AddClass("test"); Assert.IsTrue(field.hasClass("test")); Assert.AreEqual(string.Format(classTemplate, "test"), field.ToString()); }
private static void SetType(ClassField cf, Type type, Assembly hotCode) { if (type == typeof(GameObject)) { cf.fieldType = ClassField.FieldType.GameObject; } else if (type == typeof(Component) || type.IsSubclassOf(typeof(MonoBehaviour)) || IsJBehaviourType(type)) { cf.fieldType = ClassField.FieldType.UnityComponent; } else if (type.IsSubclassOf(typeof(Object))) { if (type == typeof(AudioClip) || type == typeof(Sprite) || type == typeof(TextAsset) || type == typeof(Material)) { cf.fieldType = ClassField.FieldType.HotUpdateResource; } else { cf.fieldType = ClassField.FieldType.UnityComponent; } } else { var numType = new[] { typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(decimal), typeof(double) }; if (numType.Contains(type)) { cf.fieldType = ClassField.FieldType.Number; } else if (type == typeof(string)) { cf.fieldType = ClassField.FieldType.String; } else if (type == typeof(bool)) { cf.fieldType = ClassField.FieldType.Bool; } else if (hotCode.GetTypes().Contains(type)) { cf.fieldType = ClassField.FieldType.UnityComponent; } else { cf.fieldType = ClassField.FieldType.NotSupported; } } }
private StringValue evalName(Env env) { StringValue name = _nameExpr.evalStringValue(env); ClassField entry = _qThis.getClassDef().getField(name); if (entry != null) { name = entry.getCanonicalName(); } return(name); }
public void AsyncSerialization(dynamic xmlObject) { Type type; // Generation of .xml file node names string PilotName = NameGeneration(xmlObject.GetType().ToString()), RootName = PilotName + "s"; FileStream file = new FileStream(path, FileMode.Create); using (var writer = new StreamWriter(file)) { writer.WriteLine(EncryptionLine("<?xml version=\"1.0\"?>")); writer.WriteLine(EncryptionLine(string.Format("<{0}>", RootName))); string RetReat = " ", elementEncrypt = ""; for (int i = 0; i < xmlObject.Count; i++) { elementEncrypt = ""; // Obtaining a list item type type = xmlObject[i].GetType(); // Obtaining the list item's fields FieldInfo[] ClassFields = type.GetFields(); elementEncrypt += string.Format("<{0}", PilotName); foreach (var ClassField in ClassFields) { ShOneXmlAttribute attribut = (ShOneXmlAttribute)ClassField.GetCustomAttribute(typeof(ShOneXmlAttribute), false); if (attribut != null) { if (attribut.PositionAttribute == "Attribute") { elementEncrypt += string.Format(" {0}=\"{1}\"", ClassField.Name, ClassField.GetValue(xmlObject[i])); } } } elementEncrypt += ">"; writer.WriteLine(RetReat + EncryptionLine(elementEncrypt)); RetReat += " "; foreach (var ClassField in ClassFields) { ShOneXmlAttribute attribut = (ShOneXmlAttribute)ClassField.GetCustomAttribute(typeof(ShOneXmlAttribute), false); if (attribut == null || attribut.PositionAttribute == "Element") { elementEncrypt = string.Format("<{0}>{1}</{2}>", ClassField.Name, ClassField.GetValue(xmlObject[i]), ClassField.Name); writer.WriteLine("{0}{1}", RetReat, EncryptionLine(elementEncrypt)); } } RetReat = RetReat.Remove(0, 2); writer.WriteLine(RetReat + EncryptionLine(string.Format("</{0}>", PilotName))); } writer.WriteLine(EncryptionLine(string.Format("</{0}>", RootName))); } state = true; }
public static string GetDefaultValueByType(ClassField f) { if (f.IsArray) { return("null"); // "new List<" + GetTypeKeyword( f ) + ">()"; } else if (f.IsDictionary) { return("null"); // "new Dictionary<" + GetKeyTypeKeyword( f ) + ", " + GetTypeKeyword( f ) + ">()"; } else { switch (f.Type) { case "Byte[]": return("null"); // "new byte[0]"; case "Byte": case "UInt8": case "UInt16": case "UInt32": case "UInt64": case "SByte": case "Int8": case "Int16": case "Int32": case "Int64": case "Double": case "Float": case "Single": return("0"); case "Boolean": case "Bool": return("false"); case "String": return("\"\""); case "DateTime": return("DateTime.MinValue"); default: if (f.TypeClass != null && f.TypeClass.IsEnum) { return((f.TypeNamespace != "" ? f.TypeNamespace : _pn) + "." + f.Type + "." + ((Enum)f.TypeClass).Fields.First().Name); } else { return("null"); // "new " + ( f.TypeNamespace != "" ? f.TypeNamespace : _pn ) + "." + f.Type + "()"; } } } }
public void init() { /// XXX: have this called by QuercusParser after class parsing if (!_isInit) { _isInit = true; ClassField entry = _qThis.getClassDef().getField(_name); if (entry != null) { _name = entry.getCanonicalName(); } } }
public static ProtocolJsonContent FromBytes(ProtocolJsonContent content, NetworkElement field, IDataReader reader) { if (content is null) { content = new ProtocolJsonContent(); } if (field is null) { return(content); } try { if (field.super_serialize) { NetworkElement super = ProtocolManager.Instance.Protocol[ProtocolKeyEnum.MessagesAndTypes, x => x.name == field.super]; content = FromBytes(content, super, reader); } IEnumerable <ClassField> boolWrapper = field.fields.Where(x => x.use_boolean_byte_wrapper).OrderBy(x => x.boolean_byte_wrapper_position); IEnumerable <ClassField> vars = field.fields.Where(x => !boolWrapper.Contains(x)).OrderBy(x => x.position); byte flag = 0; for (byte i = 0; i < boolWrapper.Count(); i++) { ClassField _bool = boolWrapper.ElementAt(i); if (i % 8 == 0) { flag = reader.ReadByte(); } content[_bool.name] = BooleanByteWrapper.GetFlag(flag, i); } foreach (ClassField _var in vars) { content[_var.name] = _var.Parse(ref reader); } } catch (Exception e) { Console.WriteLine($"{e}"); } return(content); }
public static string GetKeyTypeKeyword(this ClassField f) { switch (f.KeyType) { case "Byte": return("byte"); case "Byte[]": return("byte[]"); case "SByte": return("sbyte"); case "UInt16": return("ushort"); case "Int16": return("short"); case "UInt32": return("uint"); case "Int32": return("int"); case "UInt64": return("ulong"); case "Int64": return("long"); case "Double": return("double"); case "Single": return("float"); case "Boolean": return("bool"); case "String": return("string"); case "DateTime": return("DateTime"); default: if (f.KeyTypeClass != null && f.KeyTypeClass.IsEnum) { return((f.KeyTypeNamespace != "" ? f.KeyTypeNamespace : _pn) + "." + f.KeyType); } else { return((f.TypeNamespace != "" ? f.TypeNamespace : _pn) + "." + f.Type); } } }
private void _parse_var_element(ClassField field, dynamic value, BigEndianWriter writer) { if (_is_primitive(field)) { string write_method = field.write_method.Replace("write", "Write"); _write_value(write_method, value, writer); } else { bool is_null = value is null; if (field.write_false_if_null_method != null && field.write_false_if_null_method != "" && is_null) { string check_null_method = field.write_false_if_null_method.Replace("write", "Write"); _write_value(check_null_method, 0, writer); return; } if (is_null) { throw new ArgumentNullException(nameof(value)); } NetworkElement var_type = null; if (field.prefixed_by_type_id) { string write_type_id_method = field.write_type_id_method.Replace("write", "Write"); _write_value(write_type_id_method, value["protocol_id"], writer); var_type = BotofuProtocolManager.Protocol[ProtocolKeyEnum.Types, x => x.protocolID == value["protocol_id"]]; } else { var_type = BotofuProtocolManager.Protocol[ProtocolKeyEnum.Types, x => x.name == field.type]; } MessageDataBufferWriter _writer = new MessageDataBufferWriter(var_type); writer.WriteBytes(_writer.Parse(value)); } }
private static void SetVal(ref ClassField cf, Type type, Assembly hotCode, object value, GameObject instance) { if (type != typeof(Object) || !type.IsSubclassOf(hotCode.GetType("JEngine.Core.JBehaviour"))) { try { if (type == typeof(String)) { value = ""; } cf.value = value.ToString(); } catch { Log.PrintWarning(String.Format(Setting.GetString(SettingString.ClassBindUnableSetFieldValue), instance.name, type.Name, cf.fieldName)); } } }
private static dynamic _readElement(this ClassField field, ref IDataReader reader) { if (IsPrimitiv(field.type)) { string read_method = $"Read{field.write_method.Replace("write", "")}"; return(read_method._readMethod(ref reader)); } else { NetworkElement network_element = null; ProtocolJsonContent content = null; bool is_null = false; if (field.write_false_if_null_method != null && field.write_false_if_null_method != "") { string check_null_method = $"Read{field.write_false_if_null_method.Replace("write", "")}"; is_null = check_null_method._readMethod(ref reader) == 0; } if (is_null) { return(null); } if (!field.prefixed_by_type_id) { network_element = ProtocolManager.Instance.Protocol[ProtocolKeyEnum.Types, x => x.name == field.type]; } else { string read_id_method = $"Read{field.write_type_id_method.Replace("write", "")}"; dynamic protocol_id = read_id_method._readMethod(ref reader); content = new ProtocolJsonContent(); content["protocol_id"] = protocol_id; network_element = ProtocolManager.Instance.Protocol[ProtocolKeyEnum.Types, x => x.protocolID == protocol_id]; } return(FromBytes(content, network_element, reader)); } }
public override int LoadFromDb() { int cpt = 0; string sql = "use [" + Db.Name + "];exec sp_columns " + Name + ";"; using (OdbcDataAdapter dataAdapterParam = new OdbcDataAdapter(sql, Dsn)) { DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; dataAdapterParam.Fill(table); foreach (DataRow rowParam in table.Rows) { cpt++; ClassField fld = new ClassField(rowParam["COLUMN_NAME"].ToString(), rowParam["TYPE_NAME"].ToString(), "", "", "", "", this); ListField.Add(fld); } } return cpt; }
public override int LoadFromDb() { int cpt = 0; string sql = "DESCRIBE " + FullName + ";"; using (OdbcDataAdapter dataAdapterParam = new OdbcDataAdapter(sql, Dsn)) { DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; dataAdapterParam.Fill(table); foreach (DataRow rowParam in table.Rows) { cpt++; ClassField fld = new ClassField(rowParam[0].ToString(), rowParam[1].ToString(), rowParam[2].ToString(), rowParam[3].ToString(), rowParam[4].ToString(), rowParam[5].ToString(), this); ListField.Add(fld); } } return cpt; }
public static void _writeElement(ClassField field, dynamic value, ref BigEndianWriter writer) { if (IsPrimitiv(field.type)) { string write_method = field.write_method.Replace("write", "Write"); _writeMethod(write_method, value, ref writer); } else { NetworkElement var_type = null; bool is_null = value is null; if (is_null && field.write_false_if_null_method != null && field.write_false_if_null_method != "") { string check_null_method = field.write_false_if_null_method.Replace("write", "Write"); _writeMethod(check_null_method, 0, ref writer); return; } if (is_null) { throw new Exception($"{var_type.name} cannot be null"); } if (!field.prefixed_by_type_id) { var_type = ProtocolManager.Instance.Protocol[ProtocolKeyEnum.Types, x => x.name == field.type]; } else { string write_type_id_method = field.write_type_id_method.Replace("write", "Write"); _writeMethod(write_type_id_method, value["protocol_id"], ref writer); var_type = ProtocolManager.Instance.Protocol[ProtocolKeyEnum.Types, x => x.protocolID == value["protocol_id"]]; } writer.WriteBytes(FromContent(value as ProtocolJsonContent, var_type)); } }
private static IEnumerable <Header> GetClassHeaders(ClassField classes) { IList <Header> headers = new List <Header>(); if (classes.Class1) { headers.Add(Header.AllObjects(60, 2)); } if (classes.Class2) { headers.Add(Header.AllObjects(60, 3)); } if (classes.Class3) { headers.Add(Header.AllObjects(60, 4)); } if (classes.Class0) { headers.Add(Header.AllObjects(60, 1)); } return(headers); }
private static string ClassDescription(ClassField classes) { ICollection <string> names = new List <string>(); if (classes.Class1) { names.Add("1"); } if (classes.Class2) { names.Add("2"); } if (classes.Class3) { names.Add("3"); } if (classes.Class0) { names.Add("0"); } return(names.Any() ? String.Format("Class {0}", String.Join("/", names.ToArray())) : "Empty"); }
private void ProcessClassField(XmlElement parent, ClassField field) { XmlElement savedElement = currentElement; XmlElement tmpElement = document.CreateElement("ClassField"); tmpElement.SetAttribute("Name", field.Name); tmpElement.SetAttribute("Scope", field.Scope.ToString()); if (field.Modifier != Modifier.Default) { tmpElement.SetAttribute("Modifier", field.Modifier.ToString()); } if (field.Initializer != null) { currentElement = document.CreateElement("Initializer"); field.Initializer.AcceptCompiler(this); tmpElement.AppendChild(currentElement); } parent.AppendChild(tmpElement); currentElement = savedElement; }
private dynamic _parse_var_element(ClassField field, BigEndianReader reader) { if (_is_primitive(field)) { string read_method = $"Read{field.write_method.Replace("write", "")}"; return(_read_value(read_method, reader)); } else { if (field.write_false_if_null_method != null && field.write_false_if_null_method != "") { string check_null_method = $"Read{field.write_false_if_null_method.Replace("write", "")}"; if (_read_value(check_null_method, reader) == 0) { return(null); } } MessageDataBufferReader _type_reader; if (field.prefixed_by_type_id) { string read_id_method = $"Read{field.write_type_id_method.Replace("write", "")}"; dynamic protocol_id = _read_value(read_id_method, reader); _type_reader = new MessageDataBufferReader(BotofuProtocolManager.Protocol[ProtocolKeyEnum.Types, x => x.protocolID == protocol_id]); _type_reader._network_content["protocol_id"] = protocol_id; } else { _type_reader = new MessageDataBufferReader(BotofuProtocolManager.Protocol[ProtocolKeyEnum.Types, x => x.name == field.type]); } return(_type_reader.Parse(reader)); } }
public static string GetDefaultValueByType( ClassField f ) { if( f.IsArray ) { return "null"; // "new List<" + GetTypeKeyword( f ) + ">()"; } else if( f.IsDictionary ) { return "null"; // "new Dictionary<" + GetKeyTypeKeyword( f ) + ", " + GetTypeKeyword( f ) + ">()"; } else { switch( f.Type ) { case "Byte[]": return "null"; // "new byte[0]"; case "Byte": case "UInt8": case "UInt16": case "UInt32": case "UInt64": case "SByte": case "Int8": case "Int16": case "Int32": case "Int64": case "Double": case "Float": case "Single": return "0"; case "Boolean": case "Bool": return "false"; case "String": return "\"\""; case "DateTime": return "DateTime.MinValue"; default: if( f.TypeClass != null && f.TypeClass.IsEnum ) { return ( f.TypeNamespace != "" ? f.TypeNamespace : _pn ) + "." + f.Type + "." + ( (Enum)f.TypeClass ).Fields.First().Name; } else return "null"; // "new " + ( f.TypeNamespace != "" ? f.TypeNamespace : _pn ) + "." + f.Type + "()"; } } }
public static string GetDefaultValueByType( ClassField f ) { if( f.Declare.DataType != DataTypes.BuiltIn ) { return ""; } switch( f.Declare.Name ) { case "Byte": case "UInt16": case "UInt32": case "UInt64": case "SByte": case "Int16": case "Int32": case "Int64": case "Double": case "Single": return "0"; case "Boolean": return "false"; case "String": return ""; default: if( f.Declare.Class != null && f.Declare.Class.IsEnum ) { return ( f.Declare.Namespace != "" ? f.Declare.Namespace : _pn ) + "." + f.Declare.Name + "." + ( (Enum)f.Declare.Class ).Fields.First().Name; } return ""; } }
public static bool IsSimpleType( ClassField f ) { if( f.Declare.DataType != DataTypes.BuiltIn ) return false; if( f.Declare.Name == "String" ) { return false; } if( f.Declare.Class != null && f.Declare.Class.IsEnum ) return true; switch( f.Declare.Name ) { case "Byte": case "UInt16": case "UInt32": case "UInt64": case "SByte": case "Int16": case "Int32": case "Int64": case "Double": case "Single": case "Boolean": return true; } return false; }
public static Template GetTemplate( Assembly asm ) { var template = new Template(); // 扫枚举 var r_enums = ( from t in asm.GetTypes() where ( t.IsEnum ) select t ).ToList(); ; foreach( var r_enum in r_enums ) { var e = new Enum(); e.IsEnum = true; e.Name = r_enum.Name.ToString(); e.Namespace = r_enum.Namespace ?? ""; foreach( var r_attribute in r_enum.GetCustomAttributes( false ) ) { if( r_attribute is LIB.Desc ) e.Desc = ( (LIB.Desc)r_attribute ).Value; // more class attributes } var ut = r_enum.GetEnumUnderlyingType(); e.Type = ut.Name; switch( ut.Name ) { case "Byte": e.Size = 1; e.Unsigned = true; break; case "SByte": e.Size = 1; e.Unsigned = false; break; case "UInt16": e.Size = 2; e.Unsigned = true; break; case "Int16": e.Size = 2; e.Unsigned = false; break; case "UInt32": e.Size = 4; e.Unsigned = true; break; case "Int32": e.Size = 4; e.Unsigned = false; break; case "UInt64": e.Size = 8; e.Unsigned = true; break; case "Int64": e.Size = 8; e.Unsigned = false; break; } var r_fields = r_enum.GetFields( BindingFlags.Static | BindingFlags.Public ); foreach( var r_field in r_fields ) { var ef = new EnumField(); ef.Enum = e; e.Fields.Add( ef ); foreach( var a in r_field.GetCustomAttributes( false ) ) { if( a is LIB.Desc ) ef.Desc = ( (LIB.Desc)a ).Value; // more class attributes } ef.Name = r_field.Name; ef.Value = r_field.GetValue( null ).ToIntegerString( ut ); } template.Enums.Add( e ); } // 扫类 var r_classes = from t in asm.GetTypes() where ( t.IsClass || t.IsValueType && !t.IsEnum ) && t.Namespace != libNS select t; foreach( var r_class in r_classes ) { var c = new Class(); c.IsEnum = false; c.Name = r_class.Name; c.Namespace = r_class.Namespace ?? ""; var cis = r_class.GetInterfaces(); foreach( var r_attribute in r_class.GetCustomAttributes( false ) ) { if( r_attribute is LIB.Desc ) c.Desc = ( (LIB.Desc)r_attribute ).Value; } template.Classes.Add( c ); } // 自增 type id ushort tid = 0; foreach( var c in template.Classes ) { // 填充自增 TypeID c.TypeID = tid++; } // 继续扫类 foreach( var r_class in r_classes ) { var c = template.Classes.Find( a => a.Name == r_class.Name && a.Namespace == ( r_class.Namespace ?? "" ) ); // 扫继承字段 var r_fields = r_class.GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance ).ToList(); if( r_class.BaseType != typeof( object ) ) { r_fields.InsertRange( 0, r_class.BaseType.GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance ) ); } // 扫字段 foreach( var r_field in r_fields ) { var f = new ClassField { Class = c }; f.Name = r_field.Name; c.Fields.Add( f ); fillDeclare( template, f.Declare, r_field.FieldType ); } // 继续扫字段 for( int fidx = 0; fidx < c.Fields.Count; ++fidx ) { var r_field = r_fields[ fidx ]; var f = c.Fields[ fidx ]; foreach( var r_attribute in r_field.GetCustomAttributes( false ) ) { if( r_attribute is LIB.Desc ) f.Desc = ( (LIB.Desc)r_attribute ).Value; else if( r_attribute is LIB.Default ) f.Default = ( (LIB.Default)r_attribute ).Value; else if( r_attribute is LIB.Get ) f.Get = ( (LIB.Get)r_attribute ).Value; else if( r_attribute is LIB.Get ) f.Get = ( (LIB.Get)r_attribute ).Value; else if( r_attribute is LIB.Limit ) { f.Declare.MinLen = ( (LIB.Limit)r_attribute ).Min; f.Declare.MaxLen = ( (LIB.Limit)r_attribute ).Max; } else if( r_attribute is LIB.Limits ) { fillDeclareLimits( f.Declare, (LIB.Limits)r_attribute ); } else if( r_attribute is LIB.Condation ) { var ps = ( (LIB.Condation)r_attribute ).Value; for( int i=0; i < ps.Length; i += 2 ) { // todo: 检查如果被引用的 fields 位于当前 field 的后方,条件非法 f.Condation.Add( c.Fields.Find( a => a.Name == (string)ps[ i ] ), ps[ i + 1 ] ); } } } } } // 整理命名空间 template.Namespaces = template.Classes.Select( a => a.Namespace ).Concat( template.Enums.Select( a => a.Namespace ) ).Distinct().ToList(); return template; }
public static string projEnum = "__projects"; // 重要:生成过程中通过这个枚举来识别项目分类 public static Template GetTemplate(Assembly asm) { var template = new Template(); // 扫枚举 var r_enums = (from t in asm.GetTypes() where (t.IsEnum) select t).ToList();; foreach (var r_enum in r_enums) { var e = new Enum(); e.IsEnum = true; e.Name = r_enum.Name.ToString(); e.Namespace = r_enum.Namespace ?? ""; foreach (var r_attribute in r_enum.GetCustomAttributes(false)) { if (r_attribute is LIB.Desc) { e.Desc = ((LIB.Desc)r_attribute).Value; } // more class attributes } var ut = r_enum.GetEnumUnderlyingType(); e.Type = ut.Name; switch (ut.Name) { case "Byte": e.Size = 1; e.Unsigned = true; break; case "SByte": e.Size = 1; e.Unsigned = false; break; case "UInt16": e.Size = 2; e.Unsigned = true; break; case "Int16": e.Size = 2; e.Unsigned = false; break; case "UInt32": e.Size = 4; e.Unsigned = true; break; case "Int32": e.Size = 4; e.Unsigned = false; break; case "UInt64": e.Size = 8; e.Unsigned = true; break; case "Int64": e.Size = 8; e.Unsigned = false; break; } var r_fields = r_enum.GetFields(BindingFlags.Static | BindingFlags.Public); foreach (var r_field in r_fields) { var ef = new EnumField(); ef.Enum = e; e.Fields.Add(ef); foreach (var a in r_field.GetCustomAttributes(false)) { if (a is LIB.Desc) { ef.Desc = ((LIB.Desc)a).Value; } // more class attributes } ef.Name = r_field.Name; ef.Value = r_field.GetValue(null).ToIntegerString(ut); } template.Enums.Add(e); } // 扫类 var r_classes = from t in asm.GetTypes() where (t.IsClass || t.IsValueType && !t.IsEnum) && t.Namespace != libNS select t; foreach (var r_class in r_classes) { var c = new Class(); c.IsEnum = false; c.Name = r_class.Name; c.Namespace = r_class.Namespace ?? ""; var cis = r_class.GetInterfaces(); foreach (var r_attribute in r_class.GetCustomAttributes(false)) { if (r_attribute is LIB.Desc) { c.Desc = ((LIB.Desc)r_attribute).Value; } } template.Classes.Add(c); } // 自增 type id ushort tid = 0; foreach (var c in template.Classes) { // 填充自增 TypeID c.TypeID = tid++; } // 继续扫类 foreach (var r_class in r_classes) { var c = template.Classes.Find(a => a.Name == r_class.Name && a.Namespace == (r_class.Namespace ?? "")); // 扫继承字段 var r_fields = r_class.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).ToList(); if (r_class.BaseType != typeof(object)) { r_fields.InsertRange(0, r_class.BaseType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)); } // 扫字段 foreach (var r_field in r_fields) { var f = new ClassField { Class = c }; f.Name = r_field.Name; c.Fields.Add(f); fillDeclare(template, f.Declare, r_field.FieldType); } // 继续扫字段 for (int fidx = 0; fidx < c.Fields.Count; ++fidx) { var r_field = r_fields[fidx]; var f = c.Fields[fidx]; foreach (var r_attribute in r_field.GetCustomAttributes(false)) { if (r_attribute is LIB.Desc) { f.Desc = ((LIB.Desc)r_attribute).Value; } else if (r_attribute is LIB.Default) { f.Default = ((LIB.Default)r_attribute).Value; } else if (r_attribute is LIB.Get) { f.Get = ((LIB.Get)r_attribute).Value; } else if (r_attribute is LIB.Get) { f.Get = ((LIB.Get)r_attribute).Value; } else if (r_attribute is LIB.Limit) { f.Declare.MinLen = ((LIB.Limit)r_attribute).Min; f.Declare.MaxLen = ((LIB.Limit)r_attribute).Max; } else if (r_attribute is LIB.Limits) { fillDeclareLimits(f.Declare, (LIB.Limits)r_attribute); } else if (r_attribute is LIB.Condation) { var ps = ((LIB.Condation)r_attribute).Value; for (int i = 0; i < ps.Length; i += 2) { // todo: 检查如果被引用的 fields 位于当前 field 的后方,条件非法 f.Condation.Add(c.Fields.Find(a => a.Name == (string)ps[i]), ps[i + 1]); } } } } } // 整理命名空间 template.Namespaces = template.Classes.Select(a => a.Namespace).Concat(template.Enums.Select(a => a.Namespace)).Distinct().ToList(); return(template); }