Пример #1
0
        public void Read(BinaryReader reader)
        {
            var hasInstance = reader.ReadBoolean();

            if (!hasInstance)
            {
                return;
            }

            PacketVersion = (PacketVersion)reader.ReadByte();

            switch (PacketVersion)
            {
            case PacketVersion.Version1:
                Data = new PacketV1();
                break;

            default:
#if DEBUG
                throw new ArgumentException(string.Format("Invalid Packet Version : {0}", PacketVersion.ToString()));
#else
                return;
#endif
            }

            Data.Read(reader);
        }
Пример #2
0
 public static bool TryDeserialize(IBinarySerializable obj, string fileId, CacheManager.DataType dataType = CacheManager.DataType.CachedData)
 {
     try
     {
         Stopwatch stopwatch = new Stopwatch();
         stopwatch.Start();
         using (IsolatedStorageFile storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
         {
             string filePath = CacheManager.GetFilePath(fileId, dataType, "/");
             if (!storeForApplication.FileExists(filePath))
             {
                 return(false);
             }
             using (IsolatedStorageFileStream storageFileStream = storeForApplication.OpenFile(filePath, FileMode.Open, FileAccess.Read))
             {
                 BinaryReader reader = new BinaryReader((Stream)storageFileStream);
                 obj.Read(reader);
             }
         }
         stopwatch.Stop();
         Logger.Instance.Info("CacheManager.TryDeserialize succeeded for fileId = {0}, in {1} ms.", fileId, stopwatch.ElapsedMilliseconds);
         return(true);
     }
     catch (Exception ex)
     {
         Logger.Instance.Error("CacheManager.TryDeserialize failed.", ex);
     }
     return(false);
 }
        public void ReadObject <TContext>(IBinarySerializable <TContext> obj, TContext context)
        {
            var startOffset = Position;

            obj.Read(this, context);
            var endOffset = Position;

            MaybePopulateSourceInfo(obj, startOffset, endOffset);
        }
        public void ReadObject(IBinarySerializable obj)
        {
            var startOffset = Position;

            obj.Read(this);
            var endOffset = Position;

            if (PopulateBinarySourceInfo && obj is IBinarySourceInfo info)
            {
                info.BinarySourceInfo = new BinarySourceInfo(FilePath, startOffset, endOffset, ( int )(endOffset - startOffset), Endianness);
            }
        }
Пример #5
0
 public static void TryDeserializeFromString(IBinarySerializable obj, string serStr)
 {
     try
     {
         using (MemoryStream memoryStream = new MemoryStream(CacheManager.StringToAscii(serStr)))
         {
             BinaryReader reader = new BinaryReader((Stream)memoryStream);
             obj.Read(reader);
         }
     }
     catch (Exception ex)
     {
         Logger.Instance.Error("TrySerializeToString.TryDeserialize failed.", ex);
     }
 }
Пример #6
0
        public static object Deserialize(System.Type type, BinaryReader reader)
        {
            // Grab the appropriate constructor.

            const BindingFlags constructorFlags = BindingFlags.Instance | BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.NonPublic;
            ConstructorInfo    constructor      = type.GetConstructor(constructorFlags, null, new System.Type[] {}, null);

            if (constructor == null)
            {
                throw new System.ApplicationException("Cannot find constructor for class '" + type.FullName + "'.");
            }

            IBinarySerializable serializable = constructor.Invoke(new object[] {}) as IBinarySerializable;

            if (serializable != null)
            {
                serializable.Read(reader);
            }
            return(serializable);
        }
Пример #7
0
        private void UnpackTypeBinary()
        {
            // Create an instance.

            ClassInfo           info  = new ClassInfo(m_class);
            IBinarySerializable value = info.CreateInstance <IBinarySerializable>();

            // Ask the object to unserialize itself using the IBinarySerializable interface.

            MemoryStream stream = new MemoryStream((byte[])m_value);

            using (BinaryReader reader = new BinaryReader(stream))
            {
                value.Read(reader);
            }

            m_class       = null;
            m_value       = value;
            m_valueFormat = ValueFormat.Raw;
        }
Пример #8
0
        public static void ReadObjectDataForBinarySerializable(IBinarySerializable value, SerializationInfo info)
        {
            const string method = "ReadObjectDataForBinarySerializable";

            if (value == null)
            {
                throw new Exceptions.NullParameterException(typeof(BinarySerializer), method, "value");
            }
            if (info == null)
            {
                throw new Exceptions.NullParameterException(typeof(BinarySerializer), method, "info");
            }

            byte[] buffer = (byte[])info.GetValue(Constants.Serialization.BinarySerializableStreamKey, typeof(byte[]));
            Debug.Assert(buffer != null, "buffer != null");

            using (BinaryReader reader = new BinaryReader(new MemoryStream(buffer)))
            {
                value.Read(reader);
            }
        }
Пример #9
0
        public static async Task <bool> TryDeserializeAsync(IBinarySerializable obj, string fileId, CacheManager.DataType dataType = CacheManager.DataType.CachedData)
        {
            bool result;

            try
            {
                StorageFolder rootDirectory = await ApplicationData.Current.LocalFolder.GetFolderAsync(CacheManager.GetFolderNameForDataType(dataType));

                IsolatedStorageFile var_5 = IsolatedStorageFile.GetUserStoreForApplication();
                try
                {
                    if (!var_5.FileExists(CacheManager.GetFilePath(fileId, dataType, "/")))
                    {
                        result = false;
                        return(result);
                    }
                }
                finally
                {
                    // int num;
                    if (/*num < 0 &&*/ var_5 != null)
                    {
                        var_5.Dispose();
                    }
                }
                Stream arg_143_0 = await rootDirectory.OpenStreamForReadAsync(fileId);

                BinaryReader reader = new BinaryReader(arg_143_0);
                obj.Read(reader);
                arg_143_0.Close();
                result = true;
            }
            catch (Exception var_8_15F)
            {
                Logger.Instance.Error("CacheManager.TryDeserializeAsync failed.", var_8_15F);
                result = false;
            }
            return(result);
        }
Пример #10
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);
 }