public bool WriteObject(StreamDataParserTask task, object value)
        {
            var strType = this.GetStringType(task.Attributes);

            var lengthType = LengthType.UInt32;
            var lengthAttr = task.Attributes.FirstOrDefault(a => a is StreamDataLengthAttribute) as StreamDataLengthAttribute;

            if (lengthAttr != null)
            {
                lengthType = lengthAttr.Type;
            }

            switch (strType)
            {
            case StringType.CString:
                task.Stream.WriteCString((string)value, this.GetStringEncoding(task.Attributes));
                return(true);

            default:
                task.Stream.WriteString(
                    (string)value,
                    this.GetStringEncoding(task.Attributes),
                    lengthType);
                return(true);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Write data as described by task
        /// </summary>
        /// <param name="task">How to serialize object</param>
        /// <param name="value">Object to serialize</param>
        /// <returns></returns>
        private static bool WriteParserData(StreamDataParserTask task, object value)
        {
            IStreamDataParser parser = GetParser(task.StreamType);

            if (typeof(IStreamDataFinalizer).IsAssignableFrom(task.DataType))
            {
                var finalizer = (IStreamDataFinalizer)value;
                finalizer.OnSerialize();
            }

            dynamic tmpVal;

            // Cast value, if necessary.
            if (task.StreamType != task.DataType)
            {
                tmpVal = Convert.ChangeType(value, task.StreamType);
            }
            else
            {
                tmpVal = value;
            }

            var result = parser.WriteObject(task, tmpVal);

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Retrieve data as described by task
        /// </summary>
        /// <param name="task">How to retrieve value</param>
        /// <param name="value">Value</param>
        /// <returns></returns>
        private static bool GetParserData(StreamDataParserTask task, out dynamic value)
        {
            IStreamDataParser parser = GetParser(task.StreamType);

            dynamic tmpVal;
            bool    result;

            result = parser.GetObject(task, out tmpVal);

            // Cast value, if necessary.
            if (task.StreamType != task.DataType)
            {
                value = Convert.ChangeType(tmpVal, task.DataType);
            }
            else
            {
                value = tmpVal;
            }

            if (typeof(IStreamDataFinalizer).IsAssignableFrom(task.DataType))
            {
                var finalizer = (IStreamDataFinalizer)value;
                finalizer.OnDeserialize();
            }

            return(result);
        }
        public bool WriteObject(StreamDataParserTask task, object value)
        {
            var ver = value as Version;

            task.Stream.WriteInt32(ver.Major);
            task.Stream.WriteInt32(ver.Minor);
            task.Stream.WriteInt32(ver.Build);
            task.Stream.WriteInt32(ver.Revision);

            return(true);
        }
Beispiel #5
0
 public bool WriteObject(StreamDataParserTask task, object value)
 {
     if (task.StreamType == typeof(DateTime))
     {
         var val = Misc.Unixtime((DateTime)value);
         task.Stream.WriteInt64(val);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #6
0
 public bool GetObject(StreamDataParserTask task, out object value)
 {
     if (task.StreamType == typeof(DateTime))
     {
         var val = task.Stream.ReadInt64();
         value = Misc.Unixtime(val);
         return(true);
     }
     else
     {
         value = null;
         return(false);
     }
 }
        public bool GetObject(StreamDataParserTask task, out object value)
        {
            if (task.StreamType == typeof(MD5Checksum))
            {
                // 128 bits
                value = new MD5Checksum(task.Stream.ReadBytes(16));
                return true;
            }

            if (task.StreamType == typeof(SHA1Checksum))
            {
                // 160 bits
                value = new SHA1Checksum(task.Stream.ReadBytes(20));
                return true;
            }

            if (task.StreamType == typeof(SHA256Checksum))
            {
                // 256 bits
                value = new SHA256Checksum(task.Stream.ReadBytes(32));
                return true;
            }

            if (task.StreamType == typeof(SHA384Checksum))
            {
                // 384 bits
                value = new SHA256Checksum(task.Stream.ReadBytes(48));
                return true;
            }

            if (task.StreamType == typeof(SHA512Checksum))
            {
                // 512 bits
                value = new SHA512Checksum(task.Stream.ReadBytes(64));
                return true;
            }

            value = null;
            return false;
        }
        public bool GetObject(StreamDataParserTask task, out dynamic value)
        {
            var strType    = this.GetStringType(task.Attributes);
            var lengthType = LengthType.UInt32;
            var lengthAttr = task.Attributes.FirstOrDefault(a => a is StreamDataLengthAttribute) as StreamDataLengthAttribute;

            if (lengthAttr != null)
            {
                lengthType = lengthAttr.Type;
            }

            switch (strType)
            {
            case StringType.CString:
                value = task.Stream.ReadCString(this.GetStringEncoding(task.Attributes));
                return(true);

            default:
                value = task.Stream.ReadString(lengthType, this.GetStringEncoding(task.Attributes));
                return(true);
            }
        }
        public bool GetObject(StreamDataParserTask task, out object value)
        {
            var a = task.Stream.ReadInt32();
            var b = task.Stream.ReadInt32();
            var c = task.Stream.ReadInt32();
            var d = task.Stream.ReadInt32();


            if (c == -1)
            {
                value = new Version(a, b);
            }
            else if (d == -1)
            {
                value = new Version(a, b, c);
            }
            else
            {
                value = new Version(a, b, c, d);
            }
            return(true);
        }
 public bool WriteObject(StreamDataParserTask task, object value)
 {
     var cs = value as Checksum;
     task.Stream.WriteBytes(cs.ToArray());
     return true;
 }
 public bool WriteObject(StreamDataParserTask task, object value)
 {
     return(WriteValue(task.StreamType, task.Stream, value));
 }
 public bool GetObject(StreamDataParserTask task, out object value)
 {
     return(GetValue(task.StreamType, task.Stream, out value));
 }
Beispiel #13
0
        /// <summary>
        /// Write a given object to stream
        /// </summary>
        /// <param name="obj">Object to write</param>
        /// <param name="ms">Stream to write to</param>
        public static void Serialize(object obj, SuperStream ms)
        {
            if (typeof(IStreamDataFinalizer).IsAssignableFrom(obj.GetType()))
            {
                IStreamDataFinalizer fin = (IStreamDataFinalizer)obj;
                fin.OnSerialize();
            }
            var properties = StreamDataInfo.GetProperties(obj.GetType());

            // Parse spell arguments
            foreach (var pi in properties)
            {
                dynamic value             = pi.PropertyInfo.SafeGetValue(obj, null);
                StreamDataParserTask task = new StreamDataParserTask(ms, pi.ReadType, pi.DataType, pi.Attributes);

                if (pi.IsCollection)
                {
                    // Find actual length of collection.
                    ulong length;
                    if (pi.IsArray)
                    {
                        length = (ulong)value.Length;
                    }
                    else if (pi.IsList)
                    {
                        length = (ulong)value.Count;
                    }
                    else
                    {
                        throw new Exception("Property is collection, but not array nor list.");
                    }

                    // Write length, and return written length. (Entries= will override length of collection if set)
                    length = pi.WriteContentLength(ms, length);

                    dynamic enumerable = pi.PropertyInfo.SafeGetValue(obj, null);
                    ulong   count      = 0;
                    foreach (var entry in enumerable)
                    {
                        // Make sure we do not write more entries than we've declared
                        count++;
                        if (count > length)
                        {
                            throw new Exception("Collection contains more items than ");
                        }

                        // Write enry.
                        if (!WriteParserData(task, entry))
                        {
                            throw new Exception();
                        }
                    }
                    continue;
                }
                else
                {
                    if (!WriteParserData(task, value))
                    {
                        throw new Exception();
                    }
                }
            }
        }
Beispiel #14
0
        /// <summary>
        /// Populates an existing object with data from stream.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ms"></param>
        /// <returns></returns>
        public static object Populate(object obj, SuperStream ms)
        {
            var     properties        = StreamDataInfo.GetProperties(obj.GetType());
            bool    gracefulStopAtEOF = obj.GetType().GetAttribute <StreamDataGracefulEofAttribute>() != null;
            dynamic value;

            // Parse spell arguments
            foreach (var pi in properties)
            {
                if (gracefulStopAtEOF && ms.EOF)
                {
                    break;
                }
                StreamDataParserTask task = new StreamDataParserTask(ms, pi.ReadType, pi.DataType, pi.Attributes);

                if (pi.IsCollection)
                {
                    var entries = pi.ReadContentLength(ms);

                    if (pi.IsArray)
                    {
                        var arr = new ArrayList((int)Math.Min(int.MaxValue, entries));
                        for (ulong i = 0; i < entries; i++)
                        {
                            if (!GetParserData(task, out value))
                            {
                                throw new Exception();
                            }
                            arr.Add(value);
                        }

                        var arr2 = arr.ToArray(pi.ReadType);
                        pi.PropertyInfo.SafeSetValue(obj, arr2, null);
                        continue;
                    }
                    else if (pi.IsList)
                    {
                        dynamic list = Activator.CreateInstance(
                            typeof(List <>).MakeGenericType(pi.DataType),
                            (int)Math.Min(int.MaxValue, entries));

                        for (ulong i = 0; i < entries; i++)
                        {
                            if (!GetParserData(task, out value))
                            {
                                throw new Exception();
                            }
                            list.Add(value);
                        }
                        pi.PropertyInfo.SafeSetValue(obj, (object)list, null);
                    }
                }
                else
                {
                    if (!GetParserData(task, out value))
                    {
                        throw new Exception();
                    }
                    pi.PropertyInfo.SafeSetValue(obj, (object)value, null);
                    continue;
                }
            }
            if (typeof(IStreamDataFinalizer).IsAssignableFrom(obj.GetType()))
            {
                IStreamDataFinalizer fin = (IStreamDataFinalizer)obj;
                fin.OnDeserialize();
            }
            return(obj);
        }