/// <summary> /// Takes a byte array and attempts to deserialize a single object at the given offset. /// The deserialized object will be returned. /// </summary> /// <param name="bytes">Bytes previously serialized by the SerializationCtrl.</param> /// <param name="offset">An index within the array from which deserialization will be attempted.</param> public static object Deserialize(byte[] bytes, int offset) { DeserializationData data = new DeserializationData(); data.bytes = bytes; data.offset = offset; return(Deserialize(data)); }
public override object CreateNewInstance(DeserializationData data, Type type) { int elementTypeID = data.ReadInt4B(); Type elementType = GetTypeBySerializeID(elementTypeID); int arrLength = data.ReadInt4B(); Array arr = Array.CreateInstance(elementType, arrLength); return(arr); }
public override void ReadBytes(DeserializationData data, object obj) { IList list = (IList)obj; int count = data.ReadInt4B(); for (int i = 0; i < count; i++) { object val = SerializationCtrl.Deserialize(data); list.Add(val); } }
public override void ReadBytes(DeserializationData data, object obj) { Array arr = (Array)obj; long[] indices = { 0 }; for (int i = 0; i < arr.Length; i++) { indices[0] = i; object val = SerializationCtrl.Deserialize(data); arr.SetValue(val, indices); } }
public override object CreateNewInstance(DeserializationData data, Type type) { int byteCount = data.ReadInt4B(); byte[] strBytes = new byte[byteCount]; for (int i = 0; i < byteCount; i++) { strBytes[i] = data.bytes[data.offset + i]; } data.offset += byteCount; return(utf8.GetString(strBytes.ToArray())); }
void IUONSerialization.OnDeserialized(DeserializationData data) { if (this.image == null && this.asset != null) { string path = data.GetRawValue("image"); if (string.IsNullOrEmpty(path) == false) { this.image = Utility.GetIcon(this.asset.GetInstanceID()); } } }
public override void ReadBytes(DeserializationData data, object obj) { FieldInfo[] props = obj.GetType().GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var info in props) { object val = SerializationCtrl.Deserialize(data); info.SetValue(obj, val); } }
public override object CreateNewInstance(DeserializationData data, Type type) { byte[] arr = new Byte[sizeof(float)]; for (int i = 0; i < sizeof(float); i++) { arr[i] = data.bytes[data.offset++]; } if (!BitConverter.IsLittleEndian) { Array.Reverse(arr); } return(BitConverter.ToSingle(arr, 0)); }
/// <summary> /// Takes a byte array, attempts to deserializes it and adds each deserialized /// object to the given output List. /// </summary> /// <param name="bytes">Bytes previously serialized by the SerializationCtrl.</param> /// <param name="output">A List to which deserialized objects will be added.</param> public static List <object> Deserialize(byte[] bytes, List <object> output) { DeserializationData data = new DeserializationData(); data.bytes = bytes; data.offset = 0; while (data.offset < bytes.Length) { object obj = Deserialize(data); output.Add(obj); } return(output); }
void IUONSerialization.OnDeserialized(DeserializationData data) { try { string path = this.GetStoreRowPath(); if (File.Exists(path) == true) { byte[] raw = File.ReadAllBytes(path); if (raw != null && raw.Length > 0) { this.rows = Utility.DeserializeField <List <Row> >(raw); } } } catch (Exception ex) { InternalNGDebug.VerboseLogException(ex); } }
public override object CreateNewInstance(DeserializationData data, Type type) { return(data.ReadInt8B()); }
public override object CreateNewInstance(DeserializationData data, Type type) { return(Activator.CreateInstance(type)); }
public DeserializeData Deserialize(byte[] datas, int dataLen) { DeserializeData result = null; if (datas != null) { using (MemoryStream memoryStream = new MemoryStream(datas, 0, dataLen)) using (BinaryReader binaryReader = new BinaryReader(memoryStream)) { byte tID = binaryReader.ReadByte(); int bodyLength = binaryReader.ReadInt32(); if (!littleEnd) { bodyLength = bodyLength.SwapInt32(); } byte gID = binaryReader.ReadByte(); byte uID = binaryReader.ReadByte(); if (Config.Detail_Debug_Log()) { Debug.Log("---------net adapter deserialize msg data->" + dataLen + "^" + (bodyLength + NetUtils.MSG_HEADER_LEN)); } if (dataLen == (bodyLength + NetUtils.MSG_HEADER_LEN)) { result = new DeserializeData(); NetMsg msg = NetMsgPool.GenNetMsg(bodyLength); if (msg == null) { Debugger.LogError("net adapter gen net msg failed->" + bodyLength); return(null); } msg.set_tgu(tID, gID, uID); if (Config.Detail_Debug_Log()) { Debug.LogError("deserialize msg t g u->" + tID + "^" + gID + "^" + uID + "^" + bodyLength); } msg.copy_data(datas, NetUtils.MSG_HEADER_LEN, bodyLength); result.data = msg; if (gID <= NetUtils.SCRIPTTOP_GROUP) { #if JSSCRIPT sb.Remove(0, sb.Length); //sb.Append(protoNumber.ToString()); sb.Append(gID); sb.Append(">"); sb.Append(uID); sb.Append(">"); sb.Append(Convert.ToBase64String(rawBytes)); // util.Log.Log(sb.ToString()); result = new DeserializationData() { Data = sb.ToString(), toScript = true, gID = gID, uID = uID //protoNumber = protoNumber }; #elif LUASCRIPT result.toScript = true; #endif } else { result.toScript = false; } } } } return(result); }
/// <summary> /// Takes DeserializationData which includes a byte array and an offset into the array /// from where to start deserializing and attempts to deserialize a single object. /// The deserialized object or null will be returned. /// /// For more details on the serialization format refer to the documentation of the /// Serialize(SerializationData, object) method. /// /// This method may push errors to the internal error buffer when attempting to deserialize /// objects of unknown type, illegal data or data not generated by the SerializationCtrl. /// By the nature of the serialization process attempting to deserialize data not generated /// by the SerializationCtrl, manually modifying the raw data, providing an illegal offset /// or garbage data will result in undefined behavior. Any random byte array might randomly /// happen to be identical to valid, serialized data and is indistinguishable from actual data. /// For more information on the error handling process refer to the documentation of /// GetErrorBuffer(). /// /// The following return values are possible: /// if the serialized data is illegal /// null is returned and an error is generated /// if the serialized data was unknown to the serializer /// null is returned and an error is generated /// if the serialized data is unknown to the deserializer /// null is returned and an error is generated /// else /// the deserialized object (or null) is returned /// </summary> /// <param name="data">The serialized data used during this deserialization process.</param> public static object Deserialize(DeserializationData data) { ChunkType type = data.ReadChunkType(); // if something went wrong with the serialization process if (type == ChunkType.ILLEGAL) { errBuffer.Add(new ErrorDeserializeIllegalChunk()); return(null); } // the serialized "object" was null if (type == ChunkType.NULL) { return(null); } // the deserializd object was already read before if (type == ChunkType.ID_REF) { short objRefID = data.ReadInt2B(); object refObj; bool success = data.objToID.TryGetValue(objRefID, out refObj); if (success) { return(refObj); } // the serialized data is corrupted errBuffer.Add(new ErrorDeserializeUnknownObjectRef(objRefID)); return(null); } int typeID = data.ReadInt3B(); if (typeID == UNKNOWN_TYPE_ID) { errBuffer.Add(new ErrorDeserializeUnknownType(typeID)); return(null); } TypeHelpTuple typeTuple; bool isKnownType = typeIdMap.TryGetValue(typeID, out typeTuple); // this can happen if the SerializationCtrl on both devices has desynchronized type IDs. if (!isKnownType) { errBuffer.Add(new ErrorDeserializeUnknownType(typeID)); return(null); } Serializer ser = typeTuple.ser; short objID = 0; if (!ser.IsPrimitive()) { objID = data.nextID++; } object obj = ser.CreateNewInstance(data, typeTuple.type); if (!ser.IsPrimitive()) { data.RegisterObject(obj, objID); } ser.ReadBytes(data, obj); return(obj); }
// Deserializes the bytes from data and creates a new instance of the given type public virtual object CreateNewInstance(DeserializationData data, Type type) { return(null); }
public override object CreateNewInstance(DeserializationData data, Type type) { byte i = data.bytes[data.offset++]; return(i != bFalse); }
// Deserializes the bytes from data and modifies the given object accordingly public virtual void ReadBytes(DeserializationData data, object obj) { }