/// <summary> 將文字寫入指定的<see cref="WzFileStream"/>中 </summary> public static void Write(WzFileStream stream, string str) { int len = str.Length; bool uni = false; foreach (char ch in str) { if ((int)ch > 0xFF) { uni = true; break; } } byte[] chars = (uni ? Encoding.Unicode : Encoding.ASCII).GetBytes(str); Process(chars, len, uni); if ((uni && len <= 127) || (!uni && len <= 128)) { stream.Write1((sbyte)(uni ? len : -len)); } else { stream.Write1u((byte)(uni ? 0x7F : 0x80)); stream.Write4(len); } stream.Write(chars, true); }
/// <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); }
private byte[] ResolveData(WzFileStream zs) { byte unk = zs.Read1u(); byte cmf = zs.Read1u(); byte flg = zs.Read1u(); zs.Skip(-3); if (CanvasZlibTool.CheckDeflate(unk, cmf, flg)) { return(zs.Read(this.DataSize)); } else { using (MemoryStream ms = new MemoryStream()) { ms.WriteByte(zs.Read1u()); for (int i = 1; i < this.DataSize;) { int blocksize = zs.Read4(); ms.Write(zs.Read(blocksize, true), 0, blocksize); i += blocksize + 4; } return(ms.ToArray()); } } }
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); }
/// <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); }
/// <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); }
/// <summary> 將腳本資料寫入指定資料流 </summary> public bool Write(WzFileStream stream) { byte[] data = Encoding.UTF8.GetBytes(this.Script); stream.Write1u(1); stream.Write4(data.Length, true); stream.Write(data, data.Length, true); return(true); }
/// <summary> 使用指定的加密金鑰,將加密過的<see cref="WzImage"/>資料寫入<see cref="WzFile"/>中 </summary> /// <param name="name"> <see cref="WzFile"/>的名子 </param> /// <param name="key"> 加密金鑰 </param> public WzFile ToWzFile(string name, WzKeyType key = WzKeyType.None) { WzFile file = new WzFile(name, key); WzFileStream stream = new WzFileStream(file.Stream, file.KeyType); this.Write(stream); file.Size = (int)stream.Length; stream.Dispose(false); return(file); }
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; }
internal override bool Write(WzFileStream stream) { stream.Write2u(0); stream.Write4(this.mList.Count, true); for (int i = 0; i < this.mList.Count; ++i) { this.WriteVariant(this.mList[i], stream); } return(true); }
/// <summary> 從<see cref="WzFile"/>中讀取資料並建立<see cref="WzLua"/>實體 </summary> public static WzLua FromWzFile(WzFile file) { WzLua lua = new WzLua(); WzFileStream fs = new WzFileStream(file.Stream, WzKeyType.K); fs.BaseOffset = file.Offset; lua.Name = file.Name; lua.Read(fs); return(lua); }
internal override bool Write(WzFileStream stream) { int nSize = this.Vertices.Count; stream.Write4(nSize, true); for (int i = 0; i < nSize; ++i) { stream.StringPool.Write(this.Vertices[i].ClassName, 0x73, 0x1B); this.Vertices[i].Write(stream); } return(true); }
internal override bool Read(WzFileStream stream) { int nSize = stream.Read4(true); for (int i = 0; i < nSize; ++i) { WzVector2D vec = WzSerialize.FromClassName(stream.StringPool.Read()) as WzVector2D; vec.Read(stream); this.Vertices.Add(vec); } return(true); }
internal override bool Write(WzFileStream stream) { stream.Write1u(this.Unknow1_Byte); stream.Write4(this.mSoundData.Length, true); stream.Write4(this.Duration, true); stream.Write1u(this.Unknow3_Byte); this.MediaType.Write(stream); stream.Write(this.mSoundData); return(true); }
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); }
/// <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); }
internal override bool Read(WzFileStream stream) { this.mList.Clear(); this.Unknow1_UShort = stream.Read2u(); //0 int count = stream.Read4(true); for (int i = 0; i < count; ++i) { this.Add(this.ReadVariant(stream)); } return(true); }
internal void Read(WzFileStream stream) { this.majortype = new Guid(stream.Read(16)); //MEDIATYPE_Stream this.subtype = new Guid(stream.Read(16)); //MEDIASUBTYPE_WAVE this.Unknow1_Byte = stream.Read1u(); this.Unknow2_Byte = stream.Read1u(); this.formattype = new Guid(stream.Read(16)); //WMFORMAT_WaveFormatEx if (this.formattype == SoundDX8Constants.WMFORMAT_WaveFormatEx) { this.cbFormat = (uint)stream.Read4(true); this.pbFormat = stream.Read((int)this.cbFormat); } }
internal void Write(WzFileStream stream) { stream.Write(this.majortype.ToByteArray()); stream.Write(this.subtype.ToByteArray()); stream.Write1u(this.Unknow1_Byte); stream.Write1u(this.Unknow2_Byte); stream.Write(this.formattype.ToByteArray()); if (this.formattype == SoundDX8Constants.WMFORMAT_WaveFormatEx) { stream.Write4((int)this.cbFormat, true); stream.Write(this.pbFormat); } }
/// <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); }
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); }
/// <summary> 從指定的<see cref="WzFileStream"/>中讀取文字 </summary> public static string Read(WzFileStream stream) { int len = stream.Read1(); bool uni = len > 0; int flag = (uni ? len : ~len); if (len == 0) { return(""); } len = flag == 0x7F ? stream.Read4() : Math.Abs(len); byte[] str = stream.Read(len * (uni ? 2 : 1), true); Process(str, len, uni); return((uni ? Encoding.Unicode : Encoding.ASCII).GetString(str)); }
internal override bool Write(WzFileStream stream) { bool hasprop = this.CanvasProperty.Count > 0; stream.Write1u(this.Unknow1_Byte); stream.WriteBool(hasprop); if (hasprop) { this.CanvasProperty.Write(stream); } stream.Write4(this.Width, true); stream.Write4(this.Height, true); stream.Write4((int)this.Format, true); stream.Write1u(this.Scale); stream.Write4(this.Unknow2_Int); stream.Write4(this.mCanvasData.Length); stream.Write(this.mCanvasData); return(true); }
/// <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); }
/// <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; }
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); }
internal abstract bool Write(WzFileStream stream);
internal abstract bool Read(WzFileStream stream);
internal override void Write(WzFileStream fs) { ValidateValue(); fs.Write2u(this.Value); }
internal override void Read(WzFileStream fs) { this.Value = fs.Read2u(); ValidateValue(); }