Example #1
0
        public AssetStream(AssetRec asset, UInt32 chunkMaxSize = NetworkConstants.ASSET_STREAM_BUFFER_SIZE) : base()
        {
            _buffer = new byte[NetworkConstants.FILE_STREAM_BUFFER_SIZE];

            assetRec = asset;

            AlignBytes((int)assetRec.size);
        }
Example #2
0
        public bool Open(AssetSpec assetSpec)
        {
            try
            {
                if (AssetLoader.assetsCache.ContainsKey(assetSpec.id))
                {
                    assetRec = AssetLoader.assetsCache[assetSpec.id];
                }
                else
                {
                    using (var dbContext = Database.For <ThePalaceEntities>())
                    {
                        assetRec = dbContext.Assets.AsNoTracking()
                                   .Where(a => a.AssetId == assetSpec.id)
                                   .Where(a => (int)assetSpec.crc == 0 || a.AssetCrc == (int)assetSpec.crc)
                                   .Where(a => (a.Flags & (Int32)ServerAssetFlags.HighResProp) == 0)
                                   .AsEnumerable()
                                   .Select(a => new AssetRec
                        {
                            //type = LegacyAssetTypes.RT_PROP,
                            propSpec = new AssetSpec(a.AssetId, (UInt32)a.AssetCrc),
                            name     = a.Name,
                            flags    = (UInt32)a.Flags,
                            size     = (UInt32)(a.Data?.Length ?? 0),
                            data     = a.Data,
                        })
                                   .FirstOrDefault();
                    }
                }

                if (assetRec != null && assetRec.size > 0)
                {
                    assetRec.nbrBlocks = (UInt16)((assetRec.size / _chunkMaxSize) + ((assetRec.size % _chunkMaxSize) > 0 ? 1 : 0));
                    assetRec.blockSize = (UInt32)((assetRec.size - _bytesRead > _chunkMaxSize) ?
                                                  ((_chunkMaxSize > assetRec.size - _bytesRead) ? assetRec.size - _bytesRead : _chunkMaxSize) :
                                                  (assetRec.size - _bytesRead > 0) ? assetRec.size - _bytesRead : 0);

                    AlignBytes((int)assetRec.size);

                    if (assetRec.size <= 9000)
                    {
                        AssetLoader.assetsCache[assetSpec.id] = assetRec;
                    }

                    _stream = new MemoryStream(assetRec.data);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ex.DebugLog();
            }

            return(false);
        }
        public void Deserialize(Packet packet)
        {
            assetRec = new AssetRec();
            packet.DropBytes(4); //assetRec.type = (LegacyAssetTypes)packet.ReadSInt32();
            assetRec.propSpec    = new AssetSpec(packet);
            assetRec.blockSize   = packet.ReadUInt32();
            assetRec.blockOffset = packet.ReadSInt32();
            assetRec.blockNbr    = packet.ReadUInt16();
            assetRec.nbrBlocks   = packet.ReadUInt16();

            if (assetRec.blockNbr < 1)
            {
                assetRec.flags = packet.ReadUInt32();
                assetRec.size  = packet.ReadUInt32();
                assetRec.name  = packet.ReadPString(32);

                if (assetRec.blockSize > assetRec.size || assetRec.blockSize > NetworkConstants.ASSET_MAX_BLOCK_SIZE || assetRec.blockSize < 0 || assetRec.size < 1 || assetRec.size > NetworkConstants.ASSET_MAX_BLOCK_SIZE)
                {
                    return;
                }

                assetRec.data = packet.getData((int)assetRec.blockSize);
            }
        }
Example #4
0
        public void Read()
        {
            var _fileHeader = new FileHeaderRec();
            var _mapHeader  = new MapHeaderRec();
            //var _types = new List<AssetTypeRec>();
            var _assets = new List <AssetRec>();

            var nameData = new byte[0];
            var data     = new byte[0];
            var read     = 0;

            _fileStream.Seek(0, SeekOrigin.Begin);
            data = new byte[FileHeaderRec.SizeOf];
            read = _fileStream.Read(data, 0, FileHeaderRec.SizeOf);

            if (read == FileHeaderRec.SizeOf)
            {
                using (var tmp = new Packet(data))
                {
                    _fileHeader.dataOffset     = tmp.ReadSInt32();
                    _fileHeader.dataSize       = tmp.ReadSInt32();
                    _fileHeader.assetMapOffset = tmp.ReadSInt32();
                    _fileHeader.assetMapSize   = tmp.ReadSInt32();
                    //tmp.Clear();
                }
                //data = null;
            }
            else
            {
                throw new Exception("Bad Read");
            }

            _fileStream.Seek(_fileHeader.assetMapOffset, SeekOrigin.Begin);
            data = new byte[MapHeaderRec.SizeOf];
            read = _fileStream.Read(data, 0, MapHeaderRec.SizeOf);

            if (read == MapHeaderRec.SizeOf)
            {
                using (var tmp = new Packet(data))
                {
                    _mapHeader.nbrTypes    = tmp.ReadSInt32();
                    _mapHeader.nbrAssets   = tmp.ReadSInt32();
                    _mapHeader.lenNames    = tmp.ReadSInt32();
                    _mapHeader.typesOffset = tmp.ReadSInt32();
                    _mapHeader.recsOffset  = tmp.ReadSInt32();
                    _mapHeader.namesOffset = tmp.ReadSInt32();
                    //tmp.Clear();
                }
                //data = null;
            }
            else
            {
                throw new Exception("Bad Read");
            }

            if (_mapHeader.nbrTypes < 0 || _mapHeader.nbrAssets < 0 || _mapHeader.lenNames < 0)
            {
                throw new Exception("Invalid Map Header");
            }

            #region Asset Types


            _fileStream.Seek(_mapHeader.typesOffset + _fileHeader.assetMapOffset, SeekOrigin.Begin);
            data = new byte[_mapHeader.nbrTypes * AssetTypeRec.SizeOf];
            read = _fileStream.Read(data, 0, _mapHeader.nbrTypes * AssetTypeRec.SizeOf);

            if (read == _mapHeader.nbrTypes * AssetTypeRec.SizeOf)
            {
                // Deprecated
                //using (var tmp = new Packet(data))
                //{
                //    for (int i = 0; i < _mapHeader.nbrTypes; i++)
                //    {
                //        var t = new AssetTypeRec();
                //        t.Type = (LegacyAssetTypes)tmp.ReadUInt32();
                //        t.nbrAssets = tmp.ReadUInt32();
                //        t.firstAsset = tmp.ReadUInt32();

                //        _types.Add(t);
                //    }
                //    //data.Clear();
                //}
                //data = null;
            }
            else
            {
                throw new Exception("Bad Read");
            }

            #endregion

            #region Prop Names

            if (_mapHeader.lenNames > 0)
            {
                _fileStream.Seek(_mapHeader.namesOffset + _fileHeader.assetMapOffset, SeekOrigin.Begin);
                nameData = new byte[_mapHeader.lenNames];
                read     = _fileStream.Read(nameData, 0, _mapHeader.lenNames);

                //if (read != mapHeader.lenNames)
                //{
                //    mapHeader.namesOffset = 0;
                //    mapHeader.lenNames = read;
                //}
            }

            #endregion

            #region Asset Records

            data = new byte[_mapHeader.nbrAssets * AssetRec.SizeOf];
            _fileStream.Seek(_mapHeader.recsOffset + _fileHeader.assetMapOffset, SeekOrigin.Begin);
            read = _fileStream.Read(data, 0, _mapHeader.nbrAssets * AssetRec.SizeOf);

            if (read == _mapHeader.nbrAssets * AssetRec.SizeOf)
            {
                using (var tmp = new Packet(data))
                {
                    for (int i = 0; i < _mapHeader.nbrAssets; i++)
                    {
                        var t = new AssetRec();
                        t.propSpec.id = tmp.ReadSInt32();
                        tmp.DropBytes(4); //rHandle
                        t.blockOffset  = tmp.ReadSInt32();
                        t.blockSize    = tmp.ReadUInt32();
                        t.lastUsed     = tmp.ReadSInt32();
                        t.nameOffset   = tmp.ReadSInt32();
                        t.flags        = tmp.ReadUInt32();
                        t.propSpec.crc = tmp.ReadUInt32();
                        t.name         = nameData.ReadPString(32, t.nameOffset);
                        t.data         = new byte[t.blockSize];

                        _fileStream.Seek(_fileHeader.dataOffset + t.blockOffset, SeekOrigin.Begin);
                        read = _fileStream.Read(t.data, 0, (Int32)t.blockSize);

                        if (read == t.blockSize)
                        {
                            var crc = Cipher.ComputeCrc(t.data, 12, true);
                            if (t.propSpec.crc == crc)
                            {
                                //t.type = LegacyAssetTypes.RT_PROP;
                                _assets.Add(t);
                            }
                        }
                        else
                        {
                            throw new Exception("Bad Read");
                        }
                    }
                    //data.Clear();
                }
                //data = null;
            }
            else
            {
                throw new Exception("Bad Read");
            }

            #endregion

            #region Flush to Database

            using (var dbContext = Database.Database.For <ThePalaceEntities>())
            {
                _assets
                //.Where(m => m.type == LegacyAssetTypes.RT_PROP)
                //.ToList()
                .ForEach(a =>
                {
                    if (!dbContext.Assets.Any(m => m.AssetId == a.propSpec.id))
                    {
                        var asset = new Assets
                        {
                            AssetId  = a.propSpec.id,
                            AssetCrc = (Int32)a.propSpec.crc,
                            Flags    = (Int32)a.flags,
                            Name     = a.name,
                            Data     = a.data,
                        };

                        asset.LastUsed = AssetConstants.epoch.AddSeconds(a.lastUsed);

                        if (asset.LastUsed.CompareTo(AssetConstants.pepoch) < 0)
                        {
                            asset.LastUsed = AssetConstants.pepoch;
                        }

                        dbContext.Assets.Add(asset);
                    }
                });

                if (dbContext.HasUnsavedChanges())
                {
                    dbContext.SaveChanges();
                }
            }

            #endregion
        }