Esempio n. 1
0
        /// <summary> </summary>
        internal override void Write(WzFileStream fs)
        {
            fs.Write4u(0); // Size Reserve

            long startBlock = fs.Tell();

            fs.StringPool.Write(this.Value.ClassName, 0x73, 0x1B);
            this.Value.Write(fs);
            long endBlock = fs.Tell();

            fs.Seek(startBlock - 4);
            fs.Write4u((uint)(endBlock - startBlock));
            fs.Seek(endBlock);
        }
Esempio n. 2
0
        /// <summary> 使用外部的的<see cref="WzFileStream"/>儲存資料 </summary>
        public bool Write(WzFileStream zs)
        {
            this.Hash = HashTools.GenerateArchiveVersionHash(this.Version.ToString());

            // header
            zs.Write(new byte[] { (byte)'P', (byte)'K', (byte)'G', (byte)'1' }, 4);
            zs.Write8(0);  //Reserve
            zs.Write4u(0); //Reserve
            zs.WriteString(this.Description);

            // data
            long off = zs.Tell();

            this.DataOffset = (uint)off;
            zs.BaseOffset   = this.DataOffset;

            zs.Write2u((ushort)HashTools.EncryptArchiveVersionHash(this.Hash));
            this.RootDirectory.Update();
            this.RootDirectory.Write(zs);

            long endoff = zs.Tell();

            // rewrite size, offset
            zs.Seek(4);
            zs.Write8(endoff - off);
            zs.Write4u((uint)off);

            // end write
            zs.Flush();

            return(true);
        }
Esempio n. 3
0
 /// <summary> 從指定的資料流中讀取<see cref="WzImage"/>的資料 </summary>
 /// <param name="stream"> 來源資料流 </param>
 public void Read(WzFileStream stream)
 {
     stream.Seek(0, true);
     stream.StringPool.Clear();
     this.Data           = WzSerialize.FromClassName(stream.StringPool.Read());
     this.Data.ImageFile = this;
     this.Data.Read(stream);
 }
Esempio n. 4
0
        internal override void Write(WzFileStream zf)
        {
            // start write
            int count = this.Items.Count;

            long[] itemoff = new long[count];

            this.Offset = (uint)zf.Tell();

            zf.Write4(count, true);

            // write header
            for (int i = 0; i < count; i++)
            {
                WzArchiveItem item = this.Items[i];
                zf.StringPool.DirectoryWrite(item.Name, item.Type, WzArchiveItemType.Reference);
                zf.Write4(item.Size, true);
                zf.Write4(item.Checksum, true);

                itemoff[i] = zf.Tell();
                zf.Write4u(0u); // reserve
            }

            // package items
            for (int i = 0; i < count; i++)
            {
                this.Items[i].Write(zf);
            }

            long endoff = zf.Tell();

            // rewrite offset
            for (int i = 0; i < count; i++)
            {
                zf.Seek(itemoff[i]);
                uint offKey = HashTools.GenerateOffsetKey((uint)zf.Tell(), this.Archive.DataOffset, this.Archive.Hash);
                uint encoff = HashTools.EncryptOffsetHash(this.Items[i].Offset, offKey, this.Archive.DataOffset);
                zf.Write4u(encoff);
            }

            // end write
            zf.Seek(endoff);
        }
Esempio n. 5
0
        internal bool Read(WzFileStream zs)
        {
            this.Items.Clear();
            zs.Seek(this.Offset);

            int count = zs.Read4(true);

            for (int i = 0; i < count; i++)
            {
                WzArchiveItem     item = null;
                WzArchiveItemType itemtype;
                string            itemname = zs.StringPool.DirectoryRead(out itemtype, WzArchiveItemType.Reference);

                if (itemtype == WzArchiveItemType.Directory)
                {
                    item = new WzDirectory(itemname);
                }
                else if (itemtype == WzArchiveItemType.File)
                {
                    item = new WzFile(itemname, zs.KeyType)
                    {
                        Stream = zs.BaseStream
                    };
                }
                else
                {
                    throw new InvalidDataException("Undefined item type : " + (int)itemtype);
                }

                item.Size     = zs.Read4(true);
                item.Checksum = zs.Read4(true);
                uint offKey = HashTools.GenerateOffsetKey((uint)zs.Tell(), this.Archive.DataOffset, this.Archive.Hash);
                item.Offset = HashTools.DecryptOffsetHash(zs.Read4u(), offKey, this.Archive.DataOffset);

                if ((item.Offset + item.Size) > zs.Length)
                {
                    return(false);
                }

                this.Add(item);
            }
            for (int i = 0; i < this.Items.Count; i++)
            {
                if (this.Items[i].Type == WzArchiveItemType.Directory)
                {
                    (this.Items[i] as WzDirectory).Read(zs);
                }
            }

            return(true);
        }
Esempio n. 6
0
        /// <summary> 從指定資料流讀取腳本資料 </summary>
        public bool Read(WzFileStream stream)
        {
            stream.Seek(0, true);
            byte flag = stream.Read1u();

            switch (flag)
            {
            case 1:
                int len = stream.Read4(true);
                this.Script = stream.ReadString(len, Encoding.UTF8, true);
                break;

            default:
                throw new NotSupportedException("Not supported flag : " + flag);
            }
            return(true);
        }
Esempio n. 7
0
        /// <summary> 從<see cref="WzFile"/>中讀取資料並建立<see cref="WzImage"/>實體 </summary>
        /// <param name="file"> </param>
        /// <param name="dynamic"> 是否使用動態讀取。使用動態讀取可以減少圖片和聲音佔用大量記憶體空間,但讀取速度會稍微慢一點 </param>
        public static WzImage FromWzFile(WzFile file, bool dynamic = false)
        {
            if (file == null)
            {
                return(null);
            }

            WzImage      img = new WzImage();
            WzFileStream fs  = new WzFileStream(file.Stream, file.KeyType);

            fs.BaseOffset = file.Offset;
            fs.Seek(file.Offset);
            fs.DynamicRead = dynamic;
            img.Read(fs);

            if (!dynamic)
            {
                fs.Dispose(false);
            }
            return(img);
        }
Esempio n. 8
0
        /// <summary> </summary>
        internal override void Read(WzFileStream fs)
        {
            int  blockSize = fs.Read4();
            long off       = fs.Tell();

            string      classname = fs.StringPool.Read();
            WzSerialize obj       = WzSerialize.FromClassName(classname, this.Name);

            obj.ImageFile = this.Parent.ImageFile;
            obj.Read(fs);

            if (fs.Tell() != (off + blockSize))
            {
                fs.Seek(off + blockSize);
#if DEBUG
                System.Console.WriteLine("沒有解析完全 : {0}", this.GetImagePath());
#endif
            }

            this.Value = obj;
        }