public void WriteStruct(Type type, ref object Input) { FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) { if (HasAttribute(field, Const.IGNORE)) { break; } dynamic Value = field.GetValue(Input); switch (field.FieldType.ToString()) { case Const.STRING: if (HasAttribute(field, Const.CSTRING) && HasAttribute(field, Const.PSTRING)) { throw new Exception("You can't use CString and PString Attribute into the same field."); } if (HasAttribute(field, Const.CSTRING)) { Write(Value, StringStyle.CString); break; } if (HasAttribute(field, Const.UCSTRING)) { Write(Value, StringStyle.UCString); break; } if (HasAttribute(field, Const.PSTRING)) { Write(Value, StringStyle.PString); break; } throw new Exception("String Attribute Not Specified."); default: if (HasAttribute(field, Const.STRUCT)) { WriteStruct(field.FieldType, ref Value); } else { if (field.FieldType.BaseType.ToString() == Const.DELEGATE) { FieldInvoke Invoker = ((FieldInvoke)Value); if (Invoker == null) { break; } Input = Invoker.Invoke(BaseStream, false, Input); field.SetValue(Input, Invoker); } else { Write(Value); } } break; } } }
internal void WriteField(dynamic Value, string Type, FieldInfo field, ref object Instance) { switch (Type) { case Const.STRING: if (HasAttribute(field, Const.CSTRING)) { Write(Value, StringStyle.CString); break; } if (HasAttribute(field, Const.UCSTRING)) { Write(Value, StringStyle.UCString); break; } if (HasAttribute(field, Const.PSTRING)) { Write(field, Value, StringStyle.PString); break; } if (HasAttribute(field, Const.FSTRING)) { byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")]; Encoding.GetBytes((string)Value).CopyTo(Buffer, 0); Write(Buffer); break; } throw new Exception("String Attribute Not Specified."); default: if (HasAttribute(field, Const.STRUCT)) { WriteStruct(System.Type.GetType(Type), ref Value); } else { if (field.FieldType.BaseType.ToString() == Const.DELEGATE) { FieldInvoke Invoker = ((FieldInvoke)Value); if (Invoker == null) { break; } Instance = Invoker.Invoke(BaseStream, false, Instance); field.SetValue(Instance, Invoker); } else if (BigEndian) { Write(Tools.Reverse(Value)); } else { Write(Value); } } break; } }
public void ReadStruct(Type type, ref object Result) { // Result = Activator.CreateInstance(type); FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) { if (Tools.HasAttribute(field, Const.IGNORE)) { break; } object Value = null; switch (field.FieldType.ToString()) { case Const.INT8: Value = base.ReadByte(); break; case Const.UINT8: Value = base.ReadSByte(); break; case Const.INT32: Value = base.ReadInt32(); break; case Const.UINT32: Value = base.ReadUInt32(); break; case Const.DOUBLE: Value = base.ReadDouble(); break; case Const.FLOAT: Value = base.ReadSingle(); break; case Const.INT64: Value = base.ReadInt64(); break; case Const.UINT64: Value = base.ReadUInt64(); break; case Const.STRING: if (Tools.HasAttribute(field, Const.CSTRING) && Tools.HasAttribute(field, Const.PSTRING)) { throw new Exception("You can't use CString and PString Attribute into the same field."); } if (Tools.HasAttribute(field, Const.CSTRING)) { Value = ReadString(StringStyle.CString); break; } if (Tools.HasAttribute(field, Const.UCSTRING)) { Value = ReadString(StringStyle.UCString); break; } if (Tools.HasAttribute(field, Const.PSTRING)) { Value = ReadString(StringStyle.PString); break; } throw new Exception("String Attribute Not Specified."); default: if (Tools.HasAttribute(field, Const.STRUCT)) { Value = Activator.CreateInstance(field.FieldType); ReadStruct(field.FieldType, ref Value); } else { if (field.FieldType.BaseType.ToString() == Const.DELEGATE) { FieldInvoke Invoker = (FieldInvoke)field.GetValue(Result); Value = Invoker; if (Invoker == null) { break; } Result = Invoker.Invoke(BaseStream, true, Result); break; } throw new Exception("Unk Struct Field: " + field.FieldType.ToString()); } break; } field.SetValue(Result, Value); } }
private void ReadStruct(Type type, ref object Instance) { FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int i = 0; i < fields.Length; i++) { FieldInfo field = fields[i]; if (Tools.HasAttribute(field, Const.IGNORE)) { continue; } dynamic Value = null; bool IsNumber = true; switch (field.FieldType.ToString()) { case Const.INT8: Value = base.ReadSByte(); break; case Const.INT16: Value = base.ReadInt16(); break; case Const.UINT16: Value = base.ReadUInt16(); break; case Const.UINT8: Value = base.ReadByte(); break; case Const.INT32: Value = base.ReadInt32(); break; case Const.UINT32: Value = base.ReadUInt32(); break; case Const.DOUBLE: Value = base.ReadDouble(); break; case Const.FLOAT: Value = base.ReadSingle(); break; case Const.INT64: Value = base.ReadInt64(); break; case Const.UINT64: Value = base.ReadUInt64(); break; case Const.STRING: IsNumber = false; if (Tools.HasAttribute(field, Const.CSTRING) && Tools.HasAttribute(field, Const.PSTRING)) { throw new Exception("You can't use CString and PString Attribute into the same field."); } if (Tools.HasAttribute(field, Const.CSTRING)) { Value = ReadString(StringStyle.CString); break; } if (Tools.HasAttribute(field, Const.UCSTRING)) { Value = ReadString(StringStyle.UCString); break; } if (Tools.HasAttribute(field, Const.PSTRING)) { Value = ReadString(StringStyle.PString, field); break; } if (Tools.HasAttribute(field, Const.FSTRING)) { byte[] Bffr = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")]; if (Read(Bffr, 0, Bffr.Length) != Bffr.Length) { throw new Exception("Failed to Read a String"); } Value = Encoding.GetString(Bffr); break; } throw new Exception("String Attribute Not Specified."); case Const.BYTEARR: byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.ArrSize, "Length")]; if (Read(Buffer, 0, Buffer.Length) != Buffer.Length) { throw new Exception("Failed to Read"); } Value = Buffer; break; default: IsNumber = false; if (Tools.HasAttribute(field, Const.STRUCT)) { Value = Activator.CreateInstance(field.FieldType); ReadStruct(field.FieldType, ref Value); } else { if (field.FieldType.BaseType.ToString() == Const.DELEGATE) { FieldInvoke Invoker = (FieldInvoke)field.GetValue(Instance); Value = Invoker; if (Invoker == null) { break; } Instance = Invoker.Invoke(BaseStream, true, Instance); break; } throw new Exception("Unk Struct Field: " + field.FieldType.ToString()); } break; } if (IsNumber && BigEndian) { Value = Tools.Reverse(Value); } field.SetValue(Instance, Value); } }
private void WriteStruct(Type type, ref object Instance) { FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) { if (HasAttribute(field, Const.IGNORE)) { break; } dynamic Value = field.GetValue(Instance); switch (field.FieldType.ToString()) { case Const.STRING: if (HasAttribute(field, Const.CSTRING)) { Write(Value, StringStyle.CString); break; } if (HasAttribute(field, Const.UCSTRING)) { Write(Value, StringStyle.UCString); break; } if (HasAttribute(field, Const.PSTRING)) { Write(field, Value, StringStyle.PString); break; } if (HasAttribute(field, Const.FSTRING)) { byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")]; Encoding.GetBytes((string)Value).CopyTo(Buffer, 0); Write(Buffer); break; } throw new Exception("String Attribute Not Specified."); case Const.BYTEARR: if (HasAttribute(field, Const.ArrSize)) { long BufferLen = Tools.GetAttributePropertyValue(field, Const.ArrSize, "Length"); if (Value.Length > BufferLen) { throw new Exception("Wrong Array Buffer Length"); } byte[] Arr = new byte[BufferLen]; Value.CopyTo(Arr, 0); Write(Arr); } else { throw new Exception("The Array don't have the length specified"); } break; default: if (HasAttribute(field, Const.STRUCT)) { WriteStruct(field.FieldType, ref Value); } else { if (field.FieldType.BaseType.ToString() == Const.DELEGATE) { FieldInvoke Invoker = ((FieldInvoke)Value); if (Invoker == null) { break; } Instance = Invoker.Invoke(BaseStream, false, Instance); field.SetValue(Instance, Invoker); } else if (BigEndian) { Write(Tools.Reverse(Value)); } else { Write(Value); } } break; } } }
internal dynamic ReadField(string Type, FieldInfo field, ref object Instance) { bool IsNumber = true; dynamic Value = null; switch (Type) { case Const.INT8: Value = base.ReadSByte(); break; case Const.INT16: Value = base.ReadInt16(); break; case Const.CHAR: Value = base.ReadChar(); break; case Const.UINT16: Value = base.ReadUInt16(); break; case Const.UINT8: Value = base.ReadByte(); break; case Const.INT32: Value = base.ReadInt32(); break; case Const.UINT32: Value = base.ReadUInt32(); break; case Const.DOUBLE: Value = base.ReadDouble(); break; case Const.FLOAT: Value = base.ReadSingle(); break; case Const.INT64: Value = base.ReadInt64(); break; case Const.UINT64: Value = base.ReadUInt64(); break; case Const.STRING: IsNumber = false; if (Tools.HasAttribute(field, Const.CSTRING) && Tools.HasAttribute(field, Const.PSTRING)) { throw new Exception("You can't use CString and PString Attribute into the same field."); } if (Tools.HasAttribute(field, Const.CSTRING)) { Value = ReadString(StringStyle.CString); break; } if (Tools.HasAttribute(field, Const.UCSTRING)) { Value = ReadString(StringStyle.UCString); break; } if (Tools.HasAttribute(field, Const.PSTRING)) { Value = ReadString(StringStyle.PString, field); break; } if (Tools.HasAttribute(field, Const.FSTRING)) { byte[] Bffr = new byte[Tools.GetAttributePropertyValue(field, Const.FSTRING, "Length")]; if (Read(Bffr, 0, Bffr.Length) != Bffr.Length) { throw new Exception("Failed to Read a String"); } bool TrimEnd = Tools.GetAttributePropertyValue(field, Const.FSTRING, "TrimNull"); Value = TrimEnd ? Encoding.GetString(Bffr).TrimEnd('\x0') : Encoding.GetString(Bffr); break; } throw new Exception("String Attribute Not Specified."); default: IsNumber = false; if (Tools.HasAttribute(field, Const.STRUCT)) { Type FieldType = System.Type.GetType(Type); Value = Activator.CreateInstance(FieldType); ReadStruct(FieldType, ref Value); } else { if (field.FieldType.BaseType.ToString() == Const.DELEGATE) { FieldInvoke Invoker = (FieldInvoke)field.GetValue(Instance); Value = Invoker; if (Invoker == null) { break; } Instance = Invoker.Invoke(BaseStream, true, Instance); break; } throw new Exception("Unk Struct Field: " + field.FieldType.ToString()); } break; } if (IsNumber && BigEndian) { Value = Tools.Reverse(Value); } return(Value); }
internal void ReadStruct(Type type, ref object Instance) { FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int i = 0; i < fields.Length; i++) { FieldInfo field = fields[i]; if (Tools.HasAttribute(field, Const.IGNORE)) { continue; } string FType = field.FieldType.ToString(); dynamic Value = null; if (!FType.EndsWith("[]")) { Value = ReadField(FType, field, ref Instance); } else { if (Tools.HasAttribute(field, Const.FARRAY)) { byte[] Buffer = new byte[Tools.GetAttributePropertyValue(field, Const.FARRAY, "Length")]; if (Read(Buffer, 0, Buffer.Length) != Buffer.Length) { throw new Exception("Failed to Read"); } Value = Buffer; } else if (Tools.HasAttribute(field, Const.PARRAY)) { FType = FType.Substring(0, FType.Length - 2); string PType = Tools.GetAttributePropertyValue(field, Const.PARRAY, "PrefixType"); dynamic Count = ReadField(PType, field, ref Instance); object[] Content = new object[Count]; for (long x = 0; x < Count; x++) { Content[x] = ReadField(FType, field, ref Instance); } Value = Array.CreateInstance(Type.GetType(FType), Count); Content.CopyTo(Value, 0); } else { throw new Exception("Bad Struct Configuration"); } } field.SetValue(Instance, Value); if (Tools.HasAttribute(field, Const.POSTPROCESS)) { var PropertyName = (string)Tools.GetAttributePropertyValue(field, Const.POSTPROCESS, "PropertyName"); var Property = field.DeclaringType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); FieldInvoke Invoker = ((FieldInvoke)Property.GetValue(Instance)); if (Invoker != null) { Instance = Invoker.Invoke(BaseStream, true, BigEndian, Instance); } } } }