예제 #1
0
파일: WzArchive.cs 프로젝트: stu98832/libwz
        /// <summary> 使用外部的<see cref="WzFileStream"/>讀取資料 </summary>
        public bool Read(WzFileStream zs)
        {
            string identifier = zs.ReadString(4);

            if (!identifier.Equals("PKG1"))
            {
                return(false);
            }

            this.DataSize    = zs.Read8();
            this.DataOffset  = zs.Read4u();
            this.Description = zs.ReadString((int)(this.DataOffset - zs.Tell()));

            zs.BaseOffset = this.DataOffset;

            ushort cryptedHash = zs.Read2u();

            this.RootDirectory.Offset = (uint)zs.Tell();
            for (int j = 0; j < 0xFFFF; j++)
            {
                this.Hash = HashTools.GenerateArchiveVersionHash(j.ToString());
                if (HashTools.EncryptArchiveVersionHash(this.Hash) == cryptedHash)
                {
                    zs.StringPool.Clear();
                    if (this.RootDirectory.Read(zs))
                    {
                        this.Version = j;
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #2
0
파일: WzArchive.cs 프로젝트: stu98832/libwz
        /// <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);
        }
예제 #3
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);
        }
예제 #4
0
파일: WzCanvas.cs 프로젝트: stu98832/libwz
        internal override bool Read(WzFileStream stream)
        {
            this.Unknow1_Byte = stream.Read1u();
            bool hasProperty = stream.ReadBool();

            if (hasProperty)
            {
                this.CanvasProperty.Read(stream);
            }
            this.Width         = stream.Read4(true);
            this.Height        = stream.Read4(true);
            this.Format        = (WzCanvasFormat)stream.Read4(true);
            this.Scale         = stream.Read1u();
            this.Unknow2_Int   = stream.Read4();
            this.DataSize      = stream.Read4();
            this.mCanvasOffset = (uint)stream.Tell();

            if (stream.DynamicRead)
            {
                stream.Skip(this.DataSize);
                this.mStream = stream;
            }
            else
            {
                this.CanvasData = this.ResolveData(stream);
            }

            return(true);
        }
예제 #5
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);
        }
예제 #6
0
파일: WzFile.cs 프로젝트: stu98832/libwz
        internal override void Write(WzFileStream zs)
        {
            uint newoff = (uint)zs.Tell();

            if (this.Stream != null)
            {
                zs.WriteDataFromStream(this.Stream, this.Offset, this.Size);
            }

            this.Offset = newoff;
        }
예제 #7
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;
        }
예제 #8
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);
        }
예제 #9
0
        internal override bool Read(WzFileStream stream)
        {
            this.Unknow1_Byte = stream.Read1u();
            this.DataSize     = stream.Read4(true);
            this.Duration     = stream.Read4(true);
            this.Unknow3_Byte = stream.Read1u();

            WzMediaType wzmt = new WzMediaType();

            wzmt.Read(stream);
            this.MediaType    = wzmt;
            this.mSoundOffset = (uint)stream.Tell();
            if (stream.DynamicRead)
            {
                this.mStream = stream;
                stream.Skip(this.DataSize);
            }
            else
            {
                this.SoundData = stream.Read(this.DataSize);
            }
            return(true);
        }