Beispiel #1
0
 /// <summary>
 /// Writes this object to the given output path.
 /// </summary>
 /// <param name="path">Output path</param>
 public static void WriteToFile(this BinaryRWObject obj, String path)
 {
     using (BinaryWriter writer = new BinaryWriter(new FileStream(path, FileMode.OpenOrCreate)))
     {
         writer.Write(obj);
     }
 }
Beispiel #2
0
 /// <summary>
 /// Reads this object from the given input path.
 /// </summary>
 /// <param name="path">Input path</param>
 public static void ReadFromFile(this BinaryRWObject obj, String path, BXDIO.ProgressReporter progress = null)
 {
     using (BinaryReader reader = new BinaryReader(new FileStream(path, FileMode.Open)))
     {
         System.Threading.Thread progressThread = null;
         if (progress != null)
         {
             // Not the most informative, but it is something.
             progressThread = new System.Threading.Thread(() =>
             {
                 while (true)
                 {
                     progress(reader.BaseStream.Position, reader.BaseStream.Length);
                     System.Threading.Thread.Sleep(10);
                 }
             });
             progressThread.Start();
         }
         obj.ReadBinaryData(reader);
         if (progressThread != null)
         {
             progressThread.Abort();
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// Deserializes the object from this stream
 /// </summary>
 /// <param name="reader">Input stream</param>
 /// <param name="readInternal">Optional delegate to create the object</param>
 public static T ReadRWObject <T>(this BinaryReader reader, BinaryRWObjectExtensions.ReadObjectFully readInternal = null)
 {
     if (readInternal == null)
     {
         // Try to find a constructor...
         System.Reflection.ConstructorInfo ctr = typeof(T).GetConstructor(new Type[0]);
         if (ctr == null)
         {
             throw new IOException("Can't read " + typeof(T).Name + " directly!\n");
         }
         else
         {
             readInternal = (BinaryReader rdr) =>
             {
                 BinaryRWObject ro = (BinaryRWObject)ctr.Invoke(new object[0]);
                 ro.ReadBinaryData(rdr);
                 return(ro);
             };
         }
     }
     return((T)readInternal(reader));
 }
Beispiel #4
0
 /// <summary>
 /// Serializes the object into this stream
 /// </summary>
 /// <param name="writer">Output stream</param>
 public static void Write(this BinaryWriter writer, BinaryRWObject obj)
 {
     obj.WriteBinaryData(writer);
 }
Beispiel #5
0
    public static T[] ReadArray <T>(this BinaryReader reader, BinaryRWObjectExtensions.ReadObjectFully readInternal = null)
    {
        if (typeof(BinaryRWObject).IsAssignableFrom(typeof(T)))
        {
            if (readInternal == null)
            {
                // Try to find a constructor...
                ConstructorInfo ctr = typeof(T).GetConstructor(new Type[0]);
                if (ctr == null)
                {
                    throw new IOException("Can't read array of RWObjects directly!\n");
                }

                readInternal = (BinaryReader rdr) =>
                {
                    BinaryRWObject ro = (BinaryRWObject)ctr.Invoke(new object[0]);
                    ro.ReadBinaryData(rdr);
                    return(ro);
                };
            }

            T[] arr = new T[reader.ReadInt32()];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = (T)readInternal(reader);
            }

            return(arr);
        }
        else if (typeof(T).IsPrimitive)
        {
            int    len  = reader.ReadInt32();
            int    size = Buffer.ByteLength(new T[1]);
            byte[] res  = new byte[len * size];

            int count = reader.Read(res, 0, res.Length);
            while (count != res.Length)
            {
                int read = reader.Read(res, count, res.Length - count);
                count += read;
                if (read < 0)
                {
                    throw new IOException("Failed to read enough bytes to fill array!\n");
                }
            }

            T[] dest = new T[len];
            Buffer.BlockCopy(res, 0, dest, 0, res.Length);
            return(dest);
        }
        else
        {
            MethodInfo tMeth = typeof(BinaryReader).GetMethod("Read" + typeof(T).Name);
            if (tMeth == null)
            {
                throw new IOException("Unsupported read object.");
            }

            T[] arr = new T[reader.ReadInt32()];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = (T)tMeth.Invoke(reader, new object[0]);
            }

            return(arr);
        }
    }