public static bool ReadPackable(this MemoryStream pMemoryStream, IPackable pValue)
 {
     MemoryStream stream = null;
     if (!pMemoryStream.ReadPrefixed(out stream)) return false;
     if (!pValue.Read(stream)) return false;
     return true;
 }
        public static bool ReadPackable(this MemoryStream pMemoryStream, IPackable pValue)
        {
            MemoryStream stream = null;

            if (!pMemoryStream.ReadPrefixed(out stream))
            {
                return(false);
            }
            if (!pValue.Read(stream))
            {
                return(false);
            }
            return(true);
        }
        public static IPackable Decode(byte[] data)
        {
            if (data.Length < 5 || data.Length > 40)
            {
                throw new ArgumentOutOfRangeException("data");
            }

            var seed = BitConverter.ToUInt32(data, 1).Swap();
            var unobfuscatedBytes = (byte[])data.Clone();

            BogoDecrypt(seed, unobfuscatedBytes, 5, unobfuscatedBytes.Length - 5);

            var fileCheck = BitConverter.ToUInt16(unobfuscatedBytes, 5).Swap();

            unobfuscatedBytes[5] = 0xFF;
            unobfuscatedBytes[6] = 0xFF;

            if (unobfuscatedBytes.Length < 40)
            {
                var start = unobfuscatedBytes.Length;
                Array.Resize(ref unobfuscatedBytes, 40);
                for (int i = start; i < unobfuscatedBytes.Length; i++)
                {
                    unobfuscatedBytes[i] = 0xFF;
                }
            }

            var hash          = CRC32.Hash(unobfuscatedBytes, 0, unobfuscatedBytes.Length);
            var computedCheck = (ushort)(((hash & 0xFFFF0000) >> 16) ^ ((hash & 0x0000FFFF) >> 0));

            if (fileCheck != computedCheck)
            {
                throw new FormatException();
            }

            var reader = new BitReader(unobfuscatedBytes);

            var version = reader.ReadInt32(7);

            if (version != InfoManager.AssetLibraryManager.Version)
            {
                throw new FormatException();
            }

            var isWeapon = reader.ReadBoolean();

            int uniqueId = 0;

            if (version >= 3)
            {
                uniqueId = reader.ReadInt32(32);
            }

            ushort check = reader.ReadUInt16(16);

            if (check != 0xFFFF)
            {
                throw new FormatException();
            }

            int setId = 0;

            if (version >= 2)
            {
                setId = reader.ReadInt32(8);
            }

            var set = InfoManager.AssetLibraryManager.GetSet(setId);

            if (set == null)
            {
                throw new FormatException();
            }

            if (setId != 0)
            {
            }

            IPackable packable = isWeapon == true
                                     ? (IPackable) new TWeapon()
                                     : new TItem();

            packable.UniqueId          = uniqueId;
            packable.AssetLibrarySetId = setId;
            packable.Read(reader);
            return(packable);
        }