/// <summary> /// Saves a wz file to the disk, AKA repacking. /// </summary> /// <param name="path">Path to the output wz file</param> public void SaveToDisk(string path) { CreateVersionHash(); wzDir.SetHash(versionHash); string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP"; File.Create(tempFile).Close(); wzDir.GenerateDataFile(tempFile); WzTool.StringCache.Clear(); uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + 2)); WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv); wzWriter.Hash = (uint)versionHash; Header.FSize = totalLen - Header.FStart; for (int i = 0; i < 4; i++) wzWriter.Write((byte)Header.Ident[i]); wzWriter.Write((long)Header.FSize); wzWriter.Write(Header.FStart); wzWriter.WriteNullTerminatedString(Header.Copyright); long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position; if (extraHeaderLength > 0) { wzWriter.Write(new byte[(int)extraHeaderLength]); } wzWriter.Write(version); wzWriter.Header = Header; wzDir.SaveDirectory(wzWriter); wzWriter.StringCache.Clear(); FileStream fs = File.OpenRead(tempFile); wzDir.SaveImages(wzWriter, fs); fs.Close(); File.Delete(tempFile); wzWriter.StringCache.Clear(); wzWriter.Close(); }
internal void CompressPng(Bitmap bmp) { //Console.WriteLine("a"); why was that here anyway... byte[] buf = new byte[bmp.Width * bmp.Height * 8]; format = 2; format2 = 0; width = bmp.Width; height = bmp.Height; int curPos = 0; for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) { Color curPixel = bmp.GetPixel(j, i); buf[curPos] = curPixel.B; buf[curPos + 1] = curPixel.G; buf[curPos + 2] = curPixel.R; buf[curPos + 3] = curPixel.A; curPos += 4; } compressedBytes = Compress(buf); if (listWzUsed) { MemoryStream memStream = new MemoryStream(); WzBinaryWriter writer = new WzBinaryWriter(memStream, WzTool.GetIvByMapleVersion(WzMapleVersion.GMS)); writer.Write(2); for (int i = 0; i < 2; i++) { writer.Write((byte)(compressedBytes[i] ^ writer.WzKey[i])); } writer.Write(compressedBytes.Length - 2); for (int i = 2; i < compressedBytes.Length; i++) writer.Write((byte)(compressedBytes[i] ^ writer.WzKey[i - 2])); compressedBytes = memStream.GetBuffer(); writer.Close(); } }
/// <summary> /// Saves a wz file to the disk, AKA repacking. /// </summary> /// <param name="path">Path to the output wz file</param> /// <param name="progressBar">Progressbar, optional. (Set to null if not in use)</param> public void SaveToDisk(string path, ProgressBar progressBar) { WzIv = WzTool.GetIvByMapleVersion(mapleVersion); CreateVersionHash(); wzDir.SetHash(versionHash); if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //30% string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP"; File.Create(tempFile).Close(); wzDir.GenerateDataFile(tempFile); WzTool.StringCache.Clear(); if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //40% uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + 2)); WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv); wzWriter.Hash = (uint)versionHash; Header.FSize = totalLen - Header.FStart; for (int i = 0; i < 4; i++) wzWriter.Write((byte)Header.Ident[i]); if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //50% wzWriter.Write((long)Header.FSize); wzWriter.Write(Header.FStart); wzWriter.WriteNullTerminatedString(Header.Copyright); if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //60% long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position; if (extraHeaderLength > 0) { wzWriter.Write(new byte[(int)extraHeaderLength]); } if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //70% wzWriter.Write(version); wzWriter.Header = Header; wzDir.SaveDirectory(wzWriter); wzWriter.StringCache.Clear(); FileStream fs = File.OpenRead(tempFile); wzDir.SaveImages(wzWriter, fs); fs.Close(); if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //80% File.Delete(tempFile); wzWriter.StringCache.Clear(); wzWriter.Close(); if (progressBar != null) { progressBar.PerformStep(); progressBar.Refresh(); } //90% GC.Collect(); GC.WaitForPendingFinalizers(); }
internal WzImage ParseXMLWzImg(XmlElement imgElement) { string name = imgElement.GetAttribute("name"); WzImage result = new WzImage(name); foreach (XmlElement subelement in imgElement) result.WzProperties.Add(ParsePropertyFromXMLElement(subelement)); result.Changed = true; if (this.useMemorySaving) { string path = Path.GetTempFileName(); WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), iv); result.SaveImage(wzWriter); wzWriter.Close(); result.Dispose(); result = imgDeserializer.WzImageFromIMGFile(path, iv, name); } return result; }
private void serializeImageInternal(WzImage img, string outPath) { FileStream stream = File.Create(outPath); WzBinaryWriter wzWriter = new WzBinaryWriter(stream, ((WzDirectory)img.parent).WzIv); img.SaveImage(wzWriter); wzWriter.Close(); }
private byte[] serializeImageInternal(WzImage img) { MemoryStream stream = new MemoryStream(); WzBinaryWriter wzWriter = new WzBinaryWriter(stream, ((WzDirectory)img.parent).WzIv); img.SaveImage(wzWriter); byte[] result = stream.ToArray(); wzWriter.Close(); return result; }
internal int GenerateDataFile(string fileName) { size = 0; int entryCount = subDirs.Count + images.Count; if (entryCount == 0) { offsetSize = 1; return (size = 0); } size = WzTool.GetCompressedIntLength(entryCount); offsetSize = WzTool.GetCompressedIntLength(entryCount); WzBinaryWriter imgWriter = null; MemoryStream memStream = null; FileStream fileWrite = new FileStream(fileName, FileMode.Append, FileAccess.Write); for (int i = 0; i < images.Count; i++) { memStream = new MemoryStream(); imgWriter = new WzBinaryWriter(memStream, this.WzIv); images[i].SaveImage(imgWriter); images[i].checksum = 0; foreach (byte b in memStream.ToArray()) { images[i].checksum += b; } images[i].tempFileStart = fileWrite.Position; fileWrite.Write(memStream.ToArray(), 0, (int)memStream.Length); images[i].tempFileEnd = fileWrite.Position; memStream.Dispose(); images[i].UnparseImage(); int nameLen = WzTool.GetWzObjectValueLength(images[i].name, 4); size += nameLen; int imgLen = images[i].size; size += WzTool.GetCompressedIntLength(imgLen); size += imgLen; size += WzTool.GetCompressedIntLength(images[i].Checksum); size += 4; offsetSize += nameLen; offsetSize += WzTool.GetCompressedIntLength(imgLen); offsetSize += WzTool.GetCompressedIntLength(images[i].Checksum); offsetSize += 4; imgWriter.Close(); } fileWrite.Close(); for (int i = 0; i < subDirs.Count; i++) { int nameLen = WzTool.GetWzObjectValueLength(subDirs[i].name, 3); size += nameLen; size += subDirs[i].GenerateDataFile(fileName); size += WzTool.GetCompressedIntLength(subDirs[i].size); size += WzTool.GetCompressedIntLength(subDirs[i].checksum); size += 4; offsetSize += nameLen; offsetSize += WzTool.GetCompressedIntLength(subDirs[i].size); offsetSize += WzTool.GetCompressedIntLength(subDirs[i].checksum); offsetSize += 4; } return size; }