private IBinarySerializable SerializeAndDeserializeObject(IBinarySerializable obj, Type objType)
        {
            dynamic buffer;

            using (var stream = new MemoryStream())
            {
                var binaryWriter = new BinaryWriter(stream);

                obj.Write(binaryWriter);

                buffer = stream.ToArray();
            }

            var returnObj = Activator.CreateInstance(objType) as IBinarySerializable;

            using (var readStream = new MemoryStream(buffer))
            {
                var binaryReader = new BinaryReader(readStream);
                returnObj.Read(binaryReader);
            }

            return(returnObj);
        }
        /// <summary>
        /// Serialize a IBinarySerializable object.
        /// </summary>
        /// <param name="buffer">The buffer where to serialize the data.</param>
        /// <param name="start">The start index in the buffer where to serialize the data.</param>
        /// <param name="value">The object to serialize.</param>
        /// <returns>The number of serialized bytes.</returns>
        public uint ToBytes(ref Buffer buffer, uint start, IBinarySerializable value)
        {
            uint written;

            CheckSerializationParameters(buffer, start);

            if (value != null)
            {
                Type type = value.GetType();

                CheckDefaultConstructor(type);

                written = ToBytes(ref buffer, start, type);

                written += value.ToBytes(this, ref buffer, start + written);
            }
            else
            {
                written = ToBytes(ref buffer, start, (Type)null);
            }

            return(written);
        }
        /// <summary>
        /// Reads the object.
        /// </summary>
        /// <returns></returns>
        public IBinarySerializable ReadObject()
        {
            int classId = ReadClassId();

            if (classId == 0)
            {
                return(null);
            }

            IBinarySerializable obj = null;

            switch (classId)
            {
            case 0:
                break;

            default:
                throw new Exception(string.Format(Strings.ErrorBinarySerializerUnknownObjectType, classId));
            }
            obj.Deserialize(this);

            return(obj);
        }
Beispiel #4
0
        //Отправка сообщения в открытый поток
        public void SendMessage(IBinarySerializable message)
        {
            try
            {
                if (Client.Connected)
                {
                    message.SerializeBytes(Stream);
                }
            }

            catch (IOException e)
            {
                if (e.Message == "Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.")
                {
                    Console.WriteLine($"[System] (SendMessage -> {Name}) {e.Message}");
                }
            }

            catch (Exception e)
            {
                Console.WriteLine($"[System] Some serious problems at SendMessage(IBinarySerializable message): {e.Message}");
            }
        }
Beispiel #5
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 public void Write(IBinarySerializable value)
 {
     if (value == null)
         WriteClassId(0);
     else
         value.Serialize(this);
 }
Beispiel #6
0
 public static bool hasSBCompound(this IBinarySerializable bs)
 {
     return(compounds.ContainsKey(bs.GetType()));
 }
Beispiel #7
0
 public static void Write(IBinarySerializable binaryObject, string file)
 {
     File.WriteAllBytes(file, Write(binaryObject));
 }
Beispiel #8
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="encoding"></param>
 public NativeBinarySerializable(Encoding encoding)
 {
     this.encoding           = encoding;
     this.binarySerializable = new BinarySerializable();
 }
        private IBinarySerializable SerializeAndDeserializeObject(IBinarySerializable obj, Type objType)
        {
            dynamic buffer;

            using (var stream = new MemoryStream())
            {
                var binaryWriter = new BinaryWriter(stream);

                obj.Write(binaryWriter);

                buffer = stream.ToArray();
            }

            var returnObj = Activator.CreateInstance(objType) as IBinarySerializable;

            using (var readStream = new MemoryStream(buffer))
            {
                var binaryReader = new BinaryReader(readStream);
                returnObj.Read(binaryReader);
            }

            return returnObj;
        }
Beispiel #10
0
 // Custom
 public void Write(IBinarySerializable data)
 {
     data.Serialize(this);
 }
Beispiel #11
0
 public BigEndianDataBuilder Add(IBinarySerializable x)
 {
     _writer.Write(x);
     return(this);
 }
 public static void Write(this BinaryWriter writer, IBinarySerializable value)
 {
     value.Serialize(writer);
 }
 public static void ReadInto(this BinaryReader reader, IBinarySerializable value)
 {
     value.Deserialize(reader);
 }
 public void Write(IBinarySerializable serializable)
 {
     serializable.Serialize(this);
 }
Beispiel #15
0
        /// <summary>
        ///   Reads and returns an object of the specified type from the current stream.
        /// </summary>
        /// <param name="type">Type of the object to read.</param>
        /// <returns>Object of the specified type read from the current stream.</returns>
        public object Deserialize(Type type)
        {
            // Check for primitive type.
            if (type.IsPrimitive())
            {
                return(this.DeserializePrimitive(type));
            }

            // Check for string.
            if (type == typeof(string))
            {
                return(this.reader.ReadString());
            }

            // Check for derived types.
            if (type == typeof(ValueWithType))
            {
                return(this.DeserializeValueWithType());
            }

            // Check for array.
            if (type.IsArray)
            {
                return(this.DeserializeArray());
            }

            // Check for list.
            if (typeof(IList).IsAssignableFrom(type))
            {
                return(this.DeserializeList());
            }

            // Check for dictionary.
            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                return(this.DeserializeDictionary());
            }

            // Check for enum.
            if (type.IsEnum())
            {
                return(Enum.Parse(type, this.reader.ReadString()));
            }

            // Check for custom implementation.
            if (typeof(IBinarySerializable).IsAssignableFrom(type))
            {
                IBinarySerializable binarySerializable = (IBinarySerializable)Activator.CreateInstance(type);
                binarySerializable.Deserialize(this);
                return(binarySerializable);
            }

            // Deserialize with reflection.
            try
            {
                return(this.DeserializeReflection(type));
            }
            catch (Exception e)
            {
                throw new SerializationException(string.Format("Unsupported type: {0}", type.Name), e);
            }
        }
Beispiel #16
0
        private void Serialize(object o, Type type)
        {
            // Check for primitive types.
            if (type.IsPrimitive())
            {
                this.SerializePrimitive(o);
                return;
            }

            // Check for string.
            if (type == typeof(string))
            {
                string s = (string)o;
                this.writer.Write(string.IsNullOrEmpty((string)o) ? string.Empty : s);
                return;
            }

            // Check for derived types.
            if (type == typeof(ValueWithType))
            {
                this.SerializeValueWithType((ValueWithType)o);
                return;
            }

            // Check for list.
            if (typeof(IList).IsAssignableFrom(type))
            {
                this.SerializeList((IList)o);
                return;
            }

            // Check for dictionary.
            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                this.SerializeDictionary((IDictionary)o);
                return;
            }

            // Check for enum.
            if (type.IsEnum())
            {
                this.writer.Write(o.ToString());
                return;
            }

            // Check for custom implementation.
            if (typeof(IBinarySerializable).IsAssignableFrom(type))
            {
                IBinarySerializable binarySerializable = (IBinarySerializable)o;
                binarySerializable.Serialize(this);
                return;
            }

            // Serialize with reflection.
            try
            {
                this.SerializeReflection(o);
            }
            catch (Exception e)
            {
                throw new SerializationException(string.Format("Unsupported type: {0}", type.Name), e);
            }
        }
Beispiel #17
0
 public static void Write(this BinaryWriter writer, IBinarySerializable obj)
 {
     obj.Serialize(writer);
 }
 public void Write(IBinarySerializable item)
 {
     item.Serialize(this);
 }
 public static bool FileExists(this IBinarySerializable item, string filePath)
 {
     return(File.Exists(filePath) || Directory.Exists(filePath));
 }
 public static void Save(this IBinarySerializable @this, Stream stream, bool leaveOpen = true)
 {
     using (var writer = new ObjectBinaryWriter(stream, leaveOpen, Endianness.Little))
         @this.Write(writer);
 }
 public static void DeleteFile(this IBinarySerializable item, string filePath)
 {
     File.Delete(filePath);
 }
Beispiel #22
0
 public SerializationContext(IBinarySerializable input, Action <Binary.Buffer> callback) : base(callback)
 {
     this.input = input;
 }
Beispiel #23
0
 /**
  *@brief read a class that implements the sulphut.editor.IBinarySerializable interface
  *@param[in] obj (sulphut.editor.IBinarySerializable) object to be filled with data from the buffer
  */
 public void ReadSerializable(IBinarySerializable obj)
 {
     obj.Read(this);
 }
Beispiel #24
0
 /// <summary>
 /// Writes an item implementing the IBinarySerializable interface to a binary writer. Included for symmetry with standard Writer.Write(X) calls.
 /// </summary>
 /// <param name="Writer">Writer to serialize to</param>
 /// <param name="Item">The item to write</param>
 public static void Write(this BinaryWriter Writer, IBinarySerializable Item)
 {
     Item.Write(Writer);
 }
 protected static void CallDefaultConstructor(Type type, out IBinarySerializable value)
 {
     value = (IBinarySerializable)CheckDefaultConstructor(type).Invoke(emptyParameters);
 }