internal void ParseSound(WzBinaryReader reader) { reader.BaseStream.Position++; int soundDataLen = reader.ReadCompressedInt(); reader.ReadCompressedInt(); mp3bytes = reader.ReadBytes(soundDataLen); }
internal WzPngProperty(WzBinaryReader reader, bool parseNow) { // Read compressed bytes width = reader.ReadCompressedInt(); height = reader.ReadCompressedInt(); format = reader.ReadCompressedInt(); format2 = reader.ReadByte(); reader.BaseStream.Position += 4; offs = reader.BaseStream.Position; int len = reader.ReadInt32() - 1; reader.BaseStream.Position += 1; if (len > 0) { if (parseNow) { if (wzReader == null) { wzReader = reader; } compressedBytes = wzReader.ReadBytes(len); ParsePng(); } else reader.BaseStream.Position += len; } wzReader = reader; }
/// <summary> /// Creates a WzDirectory /// </summary> /// <param name="reader">The BinaryReader that is currently reading the wz file</param> /// <param name="blockStart">The start of the data block</param> /// <param name="parentname">The name of the directory</param> /// <param name="wzFile">The parent Wz File</param> internal WzDirectory(WzBinaryReader reader, string dirName, uint verHash, byte[] WzIv, WzFile wzFile) { this.reader = reader; this.name = dirName; this.hash = verHash; this.WzIv = WzIv; this.wzFile = wzFile; }
public override void Dispose() { name = null; reader = null; if (properties != null) { foreach (IWzImageProperty prop in properties) prop.Dispose(); properties.Clear(); properties = null; } }
/// <summary> /// Disposes the obejct /// </summary> public override void Dispose() { name = null; reader = null; foreach (WzImage img in images) img.Dispose(); foreach (WzDirectory dir in subDirs) dir.Dispose(); images.Clear(); subDirs.Clear(); images = null; subDirs = null; }
internal static IWzImageProperty[] ParsePropertyList(uint offset, WzBinaryReader reader, IWzObject parent, WzImage parentImg) { List<IWzImageProperty> properties = new List<IWzImageProperty>(); int entryCount = reader.ReadCompressedInt(); for (int i = 0; i < entryCount; i++) { string name = reader.ReadStringBlock(offset); byte ptype = reader.ReadByte(); switch (ptype) { case 0: properties.Add(new WzNullProperty(name, i) { Parent = parent, ParentImage = parentImg }); break; case 0x0B: case 2: properties.Add(new WzUnsignedShortProperty(name, reader.ReadUInt16()) { Parent = parent, ParentImage = parentImg }); break; case 3: properties.Add(new WzCompressedIntProperty(name, reader.ReadCompressedInt()) { Parent = parent, ParentImage = parentImg }); break; case 4: byte type = reader.ReadByte(); if (type == 0x80) properties.Add(new WzByteFloatProperty(name, reader.ReadSingle()) { Parent = parent, ParentImage = parentImg }); else if (type == 0) properties.Add(new WzByteFloatProperty(name, 0f) { Parent = parent, ParentImage = parentImg }); break; case 5: properties.Add(new WzDoubleProperty(name, reader.ReadDouble()) { Parent = parent, ParentImage = parentImg }); break; case 8: properties.Add(new WzStringProperty(name, reader.ReadStringBlock(offset)) { Parent = parent }); break; case 9: int eob = (int)(reader.ReadUInt32() + reader.BaseStream.Position); WzExtendedProperty exProp = new WzExtendedProperty(offset, eob, name); exProp.Parent = parent; exProp.ParentImage = parentImg; exProp.ParseExtendedProperty(reader); properties.Add(exProp); if (reader.BaseStream.Position != eob) reader.BaseStream.Position = eob; break; default: { Console.WriteLine("Unknown type: {0} | {1}", ptype, name); break; } } } return properties.ToArray(); }
/// <summary> /// BPS of the mp3 file /// </summary> //public byte BPS { get { return bps; } set { bps = value; } } /// <summary> /// Creates a WzSoundProperty with the specified name /// </summary> /// <param name="name">The name of the property</param> /// <param name="reader">The wz reader</param> /// <param name="parseNow">Indicating whether to parse the property now</param> public WzSoundProperty(string name, WzBinaryReader reader, bool parseNow) { this.name = name; wzReader = reader; reader.BaseStream.Position++; offs = reader.BaseStream.Position; //note - soundDataLen does NOT include the length of the header. int soundDataLen = reader.ReadCompressedInt(); len_ms = reader.ReadCompressedInt(); header = reader.ReadBytes(soundHeaderMask.Length); ParseHeader(); if (parseNow) mp3bytes = reader.ReadBytes(soundDataLen); else reader.BaseStream.Position += soundDataLen; }
/// <summary> /// Parses the wz list file /// </summary> public void ParseWzFile() { //WzTools.CreateWzKey(WzMapleVersion.GMS);//what? WzBinaryReader wzParser = new WzBinaryReader(new MemoryStream(wzFileBytes), WzIv); while (wzParser.PeekChar() != -1) { int Len = wzParser.ReadInt32(); char[] List = new char[Len]; for (int i = 0; i < Len; i++) List[i] = (char)wzParser.ReadInt16(); wzParser.ReadUInt16(); string Decrypted = wzParser.DecryptString(List); if (wzParser.PeekChar() == -1) if (Decrypted[Decrypted.Length - 1] == '/') Decrypted = Decrypted.TrimEnd("/".ToCharArray()) + "g"; // Last char should always be a g (.img) listEntries.Add(Decrypted); } wzParser.Close(); }
/// <summary> /// Parses a wz list file on the disk /// </summary> /// <param name="filePath">Path to the wz file</param> public static List<string> ParseListFile(string filePath, byte[] WzIv) { List<string> listEntries = new List<string>(); byte[] wzFileBytes = File.ReadAllBytes(filePath); WzBinaryReader wzParser = new WzBinaryReader(new MemoryStream(wzFileBytes), WzIv); while (wzParser.PeekChar() != -1) { int len = wzParser.ReadInt32(); char[] strChrs = new char[len]; for (int i = 0; i < len; i++) strChrs[i] = (char)wzParser.ReadInt16(); wzParser.ReadUInt16(); //encrypted null string decryptedStr = wzParser.DecryptString(strChrs); listEntries.Add(decryptedStr); } wzParser.Close(); int lastIndex= listEntries.Count - 1; string lastEntry = listEntries[lastIndex]; listEntries[lastIndex] = lastEntry.Substring(0, lastEntry.Length - 1) + "g"; return listEntries; }
public WzImage WzImageFromIMGBytes(byte[] bytes, WzMapleVersion version, string name, bool freeResources) { byte[] iv = WzTool.GetIvByMapleVersion(version); MemoryStream stream = new MemoryStream(bytes); WzBinaryReader wzReader = new WzBinaryReader(stream, iv); WzImage img = new WzImage(name, wzReader); img.BlockSize = bytes.Length; img.Checksum = 0; foreach (byte b in bytes) img.Checksum += b; img.Offset = 0; if (freeResources) { img.ParseImage(true); img.Changed = true; wzReader.Close(); } return img; }
internal WzImage(string name, WzBinaryReader reader) { this.name = name; this.reader = reader; this.blockStart = (int)reader.BaseStream.Position; }
public WzImage(string name, Stream dataStream, WzMapleVersion mapleVersion) { this.name = name; this.reader = new WzBinaryReader(dataStream, WzTool.GetIvByMapleVersion(mapleVersion)); }
internal static List<IWzImageProperty> ParsePropertyList(uint offset, WzBinaryReader reader, IWzObject parent, WzImage parentImg) { int entryCount = reader.ReadCompressedInt(); List<IWzImageProperty> properties = new List<IWzImageProperty>(entryCount); for (int i = 0; i < entryCount; i++) { string name = reader.ReadStringBlock(offset); switch (reader.ReadByte()) { case 0: properties.Add(new WzNullProperty(name) { Parent = parent/*, ParentImage = parentImg*/ }); break; case 0x0B: case 2: properties.Add(new WzUnsignedShortProperty(name, reader.ReadUInt16()) { Parent = parent/*, ParentImage = parentImg*/ }); break; case 3: properties.Add(new WzCompressedIntProperty(name, reader.ReadCompressedInt()) { Parent = parent/*, ParentImage = parentImg*/ }); break; case 4: byte type = reader.ReadByte(); if (type == 0x80) properties.Add(new WzByteFloatProperty(name, reader.ReadSingle()) { Parent = parent/*, ParentImage = parentImg*/ }); else if (type == 0) properties.Add(new WzByteFloatProperty(name, 0f) { Parent = parent/*, ParentImage = parentImg*/ }); break; case 5: properties.Add(new WzDoubleProperty(name, reader.ReadDouble()) { Parent = parent/*, ParentImage = parentImg*/ }); break; case 8: properties.Add(new WzStringProperty(name, reader.ReadStringBlock(offset)) { Parent = parent }); break; case 9: int eob = (int)(reader.ReadUInt32() + reader.BaseStream.Position); IWzImageProperty exProp = ParseExtendedProp(reader, offset, eob, name, parent, parentImg); properties.Add(exProp); if (reader.BaseStream.Position != eob) reader.BaseStream.Position = eob; break; default: throw new Exception("Unknown property type at ParsePropertyList"); } } return properties; }
internal static IExtended ParseExtendedProp(WzBinaryReader reader, uint offset, int endOfBlock, string name, IWzObject parent, WzImage imgParent) { switch (reader.ReadByte()) { case 0x1B: return ExtractMore(reader, offset, endOfBlock, name, reader.ReadStringAtOffset(offset + reader.ReadInt32()), parent, imgParent); case 0x73: return ExtractMore(reader, offset, endOfBlock, name, "", parent, imgParent); default: throw new System.Exception("Invlid byte read at ParseExtendedProp"); } }
internal static IExtended ExtractMore(WzBinaryReader reader, uint offset, int eob, string name, string iname, IWzObject parent, WzImage imgParent) { if (iname == "") iname = reader.ReadString(); switch (iname) { case "Property": WzSubProperty subProp = new WzSubProperty(name) { Parent = parent }; reader.BaseStream.Position += 2; subProp.AddProperties(IWzImageProperty.ParsePropertyList(offset, reader, subProp, imgParent)); return subProp; case "Canvas": WzCanvasProperty canvasProp = new WzCanvasProperty(name) { Parent = parent }; reader.BaseStream.Position++; if (reader.ReadByte() == 1) { reader.BaseStream.Position += 2; canvasProp.AddProperties(IWzImageProperty.ParsePropertyList(offset, reader, canvasProp, imgParent)); } canvasProp.PngProperty = new WzPngProperty(reader, imgParent.parseEverything) { Parent = canvasProp }; return canvasProp; case "Shape2D#Vector2D": WzVectorProperty vecProp = new WzVectorProperty(name) { Parent = parent }; vecProp.X = new WzCompressedIntProperty("X", reader.ReadCompressedInt()) { Parent = vecProp }; vecProp.Y = new WzCompressedIntProperty("Y", reader.ReadCompressedInt()) { Parent = vecProp }; return vecProp; case "Shape2D#Convex2D": WzConvexProperty convexProp = new WzConvexProperty(name) { Parent = parent }; int convexEntryCount = reader.ReadCompressedInt(); convexProp.WzProperties.Capacity = convexEntryCount; //performance thing for (int i = 0; i < convexEntryCount; i++) { convexProp.AddProperty(ParseExtendedProp(reader, offset, 0, name, convexProp, imgParent)); } return convexProp; case "Sound_DX8": WzSoundProperty soundProp = new WzSoundProperty(name, reader, imgParent.parseEverything) { Parent = parent }; return soundProp; case "UOL": reader.BaseStream.Position++; switch (reader.ReadByte()) { case 0: return new WzUOLProperty(name, reader.ReadString()) { Parent = parent }; case 1: return new WzUOLProperty(name, reader.ReadStringAtOffset(offset + reader.ReadInt32())) { Parent = parent }; } throw new Exception("Unsupported UOL type"); default: throw new Exception("Unknown iname: " + iname); } }
internal void ParseMainWzDirectory() { if (this.path == null) { Console.WriteLine("[Error] Path is null"); return; } WzBinaryReader reader = new WzBinaryReader(File.Open(this.path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), WzIv); this.Header = new WzHeader(); this.Header.Ident = reader.ReadString(4); this.Header.FSize = reader.ReadUInt64(); this.Header.FStart = reader.ReadUInt32(); this.Header.Copyright = reader.ReadNullTerminatedString(); reader.ReadBytes((int)(Header.FStart - reader.BaseStream.Position)); reader.Header = this.Header; this.version = reader.ReadInt16(); if (fileVersion == -1) { for (int j = 0; j < short.MaxValue; j++) { this.fileVersion = (short)j; this.versionHash = GetVersionHash(version, fileVersion); if (this.versionHash != 0) { reader.Hash = this.versionHash; long position = reader.BaseStream.Position; WzDirectory testDirectory = null; try { testDirectory = new WzDirectory(reader, this.name, this.versionHash, this.WzIv); testDirectory.ParseDirectory(); } catch { reader.BaseStream.Position = position; continue; } WzImage testImage = testDirectory.GetChildImages()[0]; try { reader.BaseStream.Position = testImage.Offset; byte checkByte = reader.ReadByte(); reader.BaseStream.Position = position; testDirectory.Dispose(); switch (checkByte) { case 0x73: case 0x1b: { WzDirectory directory = new WzDirectory(reader, this.name, this.versionHash, this.WzIv); directory.ParseDirectory(); this.wzDir = directory; return; } } reader.BaseStream.Position = position; } catch { reader.BaseStream.Position = position; } } } throw new Exception("Error with game version hash : The specified game version is incorrect and WzLib was unable to determine the version itself"); } else { this.versionHash = GetVersionHash(version, fileVersion); reader.Hash = this.versionHash; WzDirectory directory = new WzDirectory(reader, this.name, this.versionHash, this.WzIv); directory.ParseDirectory(); this.wzDir = directory; } }
public WzImage WzImageFromIMGFile(string inPath, byte[] iv, string name) { FileStream stream = File.OpenRead(inPath); WzBinaryReader wzReader = new WzBinaryReader(stream, iv); WzImage img = new WzImage(name, wzReader); img.BlockSize = (int)stream.Length; img.Checksum = 0; byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); stream.Position = 0; foreach (byte b in bytes) img.Checksum += b; img.Offset = 0; if (freeResources) { img.ParseImage(true); img.Changed = true; wzReader.Close(); } return img; }
/// <summary> /// Parses the extended property /// </summary> /// <param name="reader">The current BinaryReader that's reading the wz file</param> internal void ParseExtendedProperty(WzBinaryReader reader) { this.reader = reader; DumpBlock(endOfBlock, name); }
/// <summary> /// Dispose the object /// </summary> public override void Dispose() { name = null; extendedProperty.Dispose(); reader = null; }
internal WzPngProperty(WzBinaryReader reader) { // Read compressed bytes width = reader.ReadCompressedInt(); height = reader.ReadCompressedInt(); format = reader.ReadCompressedInt(); format2 = reader.ReadByte(); reader.BaseStream.Position += 4; int len = reader.ReadInt32() - 1; reader.BaseStream.Position += 1; if (len > 0) compressedBytes = reader.ReadBytes(len); }