public SaveResult Save(string filename = "") { SaveResult result = SaveResult.Success; if (!string.IsNullOrEmpty(filename)) { FileInfo = new FileInfo(filename); } try { var ms = new MemoryStream(); _sarc.Save(ms, true); using (var bw = new BinaryWriterX(FileInfo.Create())) { bw.Write(BitConverter.GetBytes((int)ms.Length).Reverse().ToArray()); bw.Write(ZLib.Compress(ms.ToArray())); } } catch { result = SaveResult.Failure; } return(result); }
public void Write(Stream input, uint offset) { using (var bw = new BinaryWriterX(input, true)) { bw.BaseStream.Position = offset; if (State == ArchiveFileState.Archived) { base.FileData.CopyTo(bw.BaseStream); bw.WriteAlignment(0x10, (byte)new Random().Next()); } else { if (entry.decompSize == 0) { entry.offset = offset; entry.compSize = (uint)base.FileData.Length; base.FileData.CopyTo(bw.BaseStream); bw.WriteAlignment(0x10, (byte)new Random().Next()); } else { entry.offset = offset; entry.decompSize = (uint)base.FileData.Length; var comp = ZLib.Compress(base.FileData); entry.compSize = (uint)comp.Length; bw.Write(comp); bw.WriteAlignment(0x10, (byte)new Random().Next()); } } } }
private ServerPacket MakeCreatePacket(WorldObject obj) { BinWriter w = new BinWriter(); w.Write(0); w.Write((uint)0); w.Write(2); obj.AddCreateObject(w, false, true); int count = 1; if (obj.ObjectType == OBJECTTYPE.PLAYER) { PlayerObject plr = obj as PlayerObject; count += plr.Inventory.AddCreateInventory(w, false); } w.Set(0, count); ServerPacket pkg = new ServerPacket(SMSG.COMPRESSED_UPDATE_OBJECT); byte[] compressed = ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length); pkg.Write((int)w.BaseStream.Length); pkg.Write(compressed); pkg.Finish(); return(pkg); }
public void Write(Stream output, long offset, ByteOrder byteOrder) { using (var bw = new BinaryWriterX(output, true, byteOrder)) { Metadata.Offset = (int)offset; if (State == ArchiveFileState.Archived) { base.FileData.CopyTo(bw.BaseStream); } else { if (CompressionLevel != CompressionLevel.NoCompression) { var bytes = ZLib.Compress(FileData, CompressionLevel, true); bw.Write(bytes); Metadata.CompressedSize = bytes.Length; } else { FileData.CopyTo(bw.BaseStream); Metadata.CompressedSize = (int)FileData.Length; } Metadata.UncompressedSize = System == Platform.CTR ? (int)(FileData.Length & 0x00FFFFFF) : (int)(FileData.Length << 3); } } }
internal void CreatePlayerObject(bool isClient) { try { // at enter world DebugLogger.Logger.Log("I have a feeling... that this is whats causing it to not work."); BinWriter w = new BinWriter(); w.Write((byte)1); w.Write((uint)0); if (isClient == false) { w.Write((byte)2); Console.WriteLine("DEBUG: 2"); } else { w.Write((byte)3); Console.WriteLine("DEBUG: 3"); } m_player.AddCreateObject(w, true, true); BinWriter pkg = new BinWriter(); pkg.Write((int)w.BaseStream.Length); pkg.Write(ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length)); Send(SMSG.COMPRESSED_UPDATE_OBJECT, pkg); m_player.updateTime(); } catch (Exception exp) { DebugLogger.Logger.Log("", exp); } }
public uint Write(Stream stream) { using (var bw = new BinaryWriterX(stream, true)) { if (State == ArchiveFileState.Archived) { base.FileData.Position = 0; base.FileData.CopyTo(bw.BaseStream); return((uint)base.FileData.Length); } else { if (compressed) { var comp = ZLib.Compress(FileData); bw.Write(comp.Length); bw.Write((comp.Length + 0xf) & ~0xf); bw.WritePadding(0x18); bw.Write(comp); bw.WriteAlignment(); return((uint)((comp.Length + 0xf) & ~0xf) + 0x20); } else { bw.Write((uint)FileData.Length); bw.Write((uint)((FileData.Length + 0xf) & ~0xf)); bw.WritePadding(0x18); FileData.CopyTo(bw.BaseStream); bw.WriteAlignment(); return((uint)((FileData.Length + 0xf) & ~0xf) + 0x20); } } } }
static void OnItemVendor(int msgID, BinReader data) { uint charID = data.ReadUInt32(); uint itemId = data.ReadUInt32(); int price = data.ReadInt32(); WorldClient client = WorldServer.GetClientByCharacterID(charID); if (client == null) { return; } if (client.Player.Money < price) { Chat.System(client, "You do not have the required funds for this item"); return; } DBItem newitem = (DBItem)DBManager.GetDBObject(typeof(DBItem), itemId); if (newitem == null) { Chat.System(client, "Item not found"); return; } newitem.Template = (DBItemTemplate)DBManager.GetDBObject(typeof(DBItemTemplate), newitem.TemplateID); if (newitem.Template == null) { Chat.System(client, "Item Template not found"); return; } Console.WriteLine("Slot:" + newitem.OwnerSlot); if (newitem.OwnerSlot == 0) { newitem.OwnerSlot = client.Player.Inventory.GetOpenBackpackSlot(); } DBManager.SaveDBObject(newitem); ItemObject NewObj = (ItemObject)client.Player.Inventory.CreateItem(newitem); // client.Player.MapTile.Map.Enter(NewObj); BinWriter w = new BinWriter(); w.Write(1); w.Write((byte)0); NewObj.AddCreateObject(w, false, true); ServerPacket pkg = new ServerPacket(SMSG.COMPRESSED_UPDATE_OBJECT); byte[] compressed = ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length); pkg.Write((int)w.BaseStream.Length); pkg.Write(compressed); pkg.Finish(); client.Player.MapTile.SendSurrounding(pkg); client.Player.Money = client.Player.Money - price; client.Player.UpdateData(); // Chat.System (client, "Buy Item working on Worldserver side"); }
internal override void SaveData(BinaryWriter writer) { var position = writer.BaseStream.Position; WriteString(writer, Name); uint realSize; var compressed = false; byte[] data; using (var stream = new MemoryStream()) { var dataWriter = new BinaryWriter(stream); foreach (var subRecord in this.SubRecords) { subRecord.SaveData(dataWriter); } realSize = (uint)stream.Length; if (Properties.Settings.Default.UseDefaultRecordCompression) { compressed = ((this.Flags1 & 0x00040000) != 0) || (Properties.Settings.Default.EnableAutoCompress && Compressor.CompressRecord(Name)) || (Properties.Settings.Default.EnableCompressionLimit && (realSize >= Properties.Settings.Default.CompressionLimit)); } data = stream.ToArray(); if (compressed) { data = ZLib.Compress(data); } } var dataSize = (uint)data.Length; var flags = this.Flags1 & ~0x00040000U; if (compressed) { dataSize += 4; flags |= 0x00040000; Debug.WriteLineIf( this.dataSize != dataSize, string.Format( "COMPRESSED RECORD [NAME={0} AT POSITION={1}] SIZE DIFFERS FROM ORIGINAL: ORIGINAL={2} ACTUAL={3}, RAW RECORD SIZE={4}", Name, position, this.dataSize, dataSize, realSize)); } writer.Write(dataSize); // Size of compressed section + length writer.Write(flags); writer.Write(this.FormID); writer.Write(this.Flags2); writer.Write(this.Flags3); if (compressed) { writer.Write(realSize); } writer.Write(data, 0, data.Length); }
// Used for protobuf json strings. public static byte[] Compress <T>(T data, string prefix) { var jsonData = Encoding.UTF8.GetBytes($"{prefix}:{CreateString(data)}\0"); var jsonDataLength = BitConverter.GetBytes(jsonData.Length); var uncompressedAdler = BitConverter.GetBytes(Adler32.Calculate(jsonData)).Reverse().ToArray(); return(jsonDataLength.Combine(ZLib.Compress(jsonData), uncompressedAdler)); }
public void Write(Stream input) { using (var bw = new BinaryWriterX(input, true, ByteOrder.BigEndian)) if (State == ArchiveFileState.Archived) { bw.Write(decompSize); base.FileData.CopyTo(bw.BaseStream); } else { bw.Write((uint)base.FileData.Length); bw.Write(ZLib.Compress(base.FileData)); } }
internal void CreatePlayerObject() { BinWriter w = new BinWriter(); w.Write(0); m_player.AddCreateObject(w, true, true); w.Set(0, m_player.Inventory.AddCreateInventory(w, true) + 1); BinWriter pkg = new BinWriter(); pkg.Write((int)w.BaseStream.Length); pkg.Write(ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length)); Send(SMSG.COMPRESSED_UPDATE_OBJECT, pkg); }
public Tuple <uint, uint> Write(Stream input, uint compOffset, uint decompOffset) { using (var bw = new BinaryWriterX(input, true)) { if (base.FileData.Length > 0) { if (Entry.entry.compOffset != 0) { Entry.entry.compOffset = compOffset; } Entry.entry.decompOffset = decompOffset; } if (State == ArchiveFileState.Archived) { base.FileData.CopyTo(bw.BaseStream); bw.WriteAlignment(); } else { if (base.FileData.Length > 0) { byte[] comp; if (Entry.entry.compSize != 0 && Entry.entry.compSize != Entry.entry.decompSize) { comp = ZLib.Compress(base.FileData); } else { comp = new BinaryReaderX(base.FileData, true).ReadAllBytes(); } bw.Write(comp); bw.WriteAlignment(); if (Entry.entry.compSize != 0) { Entry.entry.compSize = (uint)comp.Length; } Entry.entry.decompSize = (uint)base.FileData.Length; } } return(new Tuple <uint, uint>( (Entry.entry.compOffset == 0) ? Entry.entry.decompOffset + Entry.entry.decompSize : Entry.entry.compOffset + Entry.entry.compSize, decompOffset + Entry.entry.decompSize)); } }
/// <summary> /// Write the metadata using the supplied writer.. /// </summary> /// <param name="metaData">The metadata to write</param> /// <param name="writer">The writer to write the serialized meta with.</param> private int SerializeMetaData(BookMetaData metaData, BinaryWriter writer) { XmlSerializer serializer = new XmlSerializer(typeof(BookMetaData)); MemoryStream uncompressedStream = new MemoryStream(); StreamWriter swriter = new StreamWriter(uncompressedStream, Encoding.Unicode); swriter.NewLine = "\n"; serializer.Serialize(swriter, metaData); writer.Flush(); // File.WriteAllBytes("C:\\MetaData.xml", uncompressedStream.GetBuffer()); int size = ZLib.Compress(uncompressedStream.GetBuffer(), (int)uncompressedStream.Position, writer); return(size); }
void HandleRequestAccountData(RequestAccountData request) { if (request.DataType > AccountDataTypes.Max) return; AccountData adata = GetAccountData(request.DataType); if (adata.Data == null) return; UpdateAccountData data = new UpdateAccountData(); data.Player = GetPlayer() ? GetPlayer().GetGUID() : ObjectGuid.Empty; data.Time = (uint)adata.Time; data.Size = (uint)adata.Data.Length; data.DataType = request.DataType; data.CompressedData = new ByteBuffer(ZLib.Compress(Encoding.UTF8.GetBytes(adata.Data))); SendPacket(data); }
public void SendConfig(int type) { string config = string.Empty; switch (type) { case 0: config = m_character.UIConfig0; break; case 1: config = m_character.UIConfig1; break; case 2: config = m_character.UIConfig2; break; case 3: config = m_character.UIConfig3; break; case 4: config = m_character.UIConfig4; break; default: return; } BinWriter w = LoginClient.NewPacket(SMSG.UI_CONFIG); w.Write(type); if (config == string.Empty) { w.Write(0); Send(w); return; } byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(config); w.Write(data.Length); w.Write(ZLib.Compress(data)); Send(w); }
public byte[] Convert(byte[] data, StreamFormatFlags eInputFormat, StreamFormatFlags eOutputFormat, ObjectType eObjectType) { if (eInputFormat == eOutputFormat) { return(data); } if ((eInputFormat & StreamFormatFlags.Encrypted) == StreamFormatFlags.Encrypted) { throw new InvalidDataException("Can't handle encrypted streams"); } if ((eOutputFormat & StreamFormatFlags.Encrypted) == StreamFormatFlags.Encrypted) { throw new InvalidDataException("Can't handle encrypted streams"); } if (eOutputFormat == StreamFormatFlags.None) { return(UnscrambleAndUncompress(data, eInputFormat, eObjectType)); } else { byte[] compressed = data; if ((eInputFormat & StreamFormatFlags.Compressed) == StreamFormatFlags.Compressed) { MemoryStream compStream = new MemoryStream(); ZLib.Compress(data, data.Length, compStream); compStream.Flush(); compressed = compStream.GetBuffer(); } byte[] scrambled = compressed; if ((eInputFormat & StreamFormatFlags.Scrambled) == StreamFormatFlags.Scrambled) { XorData(scrambled, eObjectType); } return(scrambled); } }
internal void CreatePlayerObject() { try { // at enter world BinWriter w = new BinWriter(); w.Write(0); w.Write((byte)0); //A9 Fix by Phaze w.Set(0, m_player.Inventory.AddCreateInventory(w, true) + 1); m_player.AddCreateObject(w, true, true); BinWriter pkg = new BinWriter(); pkg.Write((int)w.BaseStream.Length); pkg.Write(ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length)); Send(SMSG.COMPRESSED_UPDATE_OBJECT, pkg); m_player.updateTime(); } catch (Exception exp) { DebugLogger.Log("", exp); } }
public void Save(string filename = "") { if (!string.IsNullOrEmpty(filename)) { FileInfo = new FileInfo(filename); } // Save As... if (!string.IsNullOrEmpty(filename)) { var ms = new MemoryStream(); _sarc.Save(ms, true); _sarc.Close(); using (var bw = new BinaryWriterX(FileInfo.Create())) { bw.Write(BitConverter.GetBytes((int)ms.Length).Reverse().ToArray()); ms.Position = 0; bw.Write(ZLib.Compress(ms)); } } else { // Create the temp file var ms = new MemoryStream(); _sarc.Save(ms, true); _sarc.Close(); using (var bw = new BinaryWriterX(File.Create(FileInfo.FullName + ".tmp"))) { bw.Write(BitConverter.GetBytes((int)ms.Length).Reverse().ToArray()); ms.Position = 0; bw.Write(ZLib.Compress(ms)); } // Delete the original FileInfo.Delete(); // Rename the temporary file File.Move(FileInfo.FullName + ".tmp", FileInfo.FullName); } // Reload the new file to make sure everything is in order Load(FileInfo.FullName); }
public void Save(string filename, bool isZLibCompressed = false) { using (var bw = new BinaryWriterX(File.Create(filename))) { bw.BaseStream.Position = 0x18 + (BankTOC.SectionCount * 0x8); // Save Sections foreach (var section in Sections) { var startOffset = bw.BaseStream.Position; bw.BaseStream.Position += 0x8; switch (section.Magic) { case "PROP": Prop.Save(bw.BaseStream); break; case "BINF": Binf.Save(bw.BaseStream); break; case "GRP ": Grp.Save(bw.BaseStream); break; case "DTON": Dton.Save(bw.BaseStream); break; case "TONE": Tone.PackSize = (int)Files[0].FileSize; Tone.Save(bw.BaseStream); break; case "JUNK": Junk.Save(bw.BaseStream); break; case "PACK": Files[0].FileData.CopyTo(bw.BaseStream); //for (int i = 0; i < Tone.ToneCount; i++) //{ //} break; } section.SectionSize = (int)(bw.BaseStream.Position - (startOffset + 0x8)); bw.BaseStream.Position = startOffset; bw.WriteStruct(section); bw.BaseStream.Position += section.SectionSize; } // FIleSize Header.FileSize = (int)bw.BaseStream.Position - 0x8; bw.BaseStream.Position = 0; bw.WriteStruct(Header); // BankTOC bw.WriteStruct(BankTOC); // Sections foreach (var section in Sections) { bw.WriteStruct(section); } } if (isZLibCompressed) { FileStream origFile = File.OpenRead(filename); byte[] decomp = new byte[(int)origFile.Length]; origFile.Read(decomp, 0, (int)origFile.Length); byte[] comp = ZLib.Compress(new MemoryStream(decomp)); origFile.Close(); File.Delete(filename); File.OpenWrite(filename).Write(comp, 0, comp.Length); } }
//internal override void SaveData(BinaryWriter writer) internal override void SaveData(SnipStreamWrapper snipStreamWrapper) { bool hasRecordUnknown = false; //var position = writer.BaseStream.Position; var position = snipStreamWrapper.SnipStream.Position; //WriteString(writer, Name); snipStreamWrapper.WriteStringInFileStream(Name); var compressed = false; byte[] data; //using (var stream = new MemoryStream()) //{ //var dataWriter = new BinaryWriter(stream); snipStreamWrapper.ResetBufferSizeAndPosition(); if (this.FormID == 496431) { compressed = false; } foreach (var subRecord in this.SubRecords) { //subRecord.SaveData(dataWriter); if (subRecord.Name == "????") { hasRecordUnknown = true; } subRecord.SaveData(snipStreamWrapper); //write data into buffer } uint realSize = snipStreamWrapper.OutputBufferLength; if (hasRecordUnknown) { realSize = 0; } //data = stream.ToArray(); //data = stream.ToArray(); if (Properties.Settings.Default.UseDefaultRecordCompression) { compressed = ((this.Flags1 & 0x00040000) != 0) || (Properties.Settings.Default.EnableAutoCompress && Compressor.CompressRecord(Name)) || (Properties.Settings.Default.EnableCompressionLimit && (realSize >= Properties.Settings.Default.CompressionLimit)); //if (compressed) // data = ZLib.Compress(stream, CompressLevel.Best); //data = ZLib.Compress(data); } // ajouter dans zlib un option pour dire oui compresser ou dans le record une variable pour dire que c'est compressé if (Properties.Settings.Default.UsePluginRecordCompression) { compressed = (this.Flags1 & 0x00040000) != 0; } if (compressed & compressLevel != CompressLevel.None) { ZLib.Compress(snipStreamWrapper, compressLevel); data = new byte[ZLibWrapper.OutputBufferLength]; ZLibWrapper.CopyOutputBufferIntoByteArray(data); } else { data = new byte[snipStreamWrapper.OutputBufferLength]; if (snipStreamWrapper.OutputBufferLength > 0) { snipStreamWrapper.CopyOutputBufferToData(ref data); } //data = stream.ToArray(); //data = stream.ToArray(); } //} // using (var stream = new MemoryStream()) var dataSize = (uint)data.Length; var flags = this.Flags1 & ~0x00040000U; if (compressed) { dataSize += 4; flags |= 0x00040000; Debug.WriteLineIf( this.dataSize != dataSize, string.Format("COMPRESSED RECORD [NAME={0} AT POSITION={1}] SIZE DIFFERS FROM ORIGINAL: ORIGINAL={2} ACTUAL={3}, RAW RECORD SIZE={4}", Name, position, this.dataSize, dataSize, realSize)); } snipStreamWrapper.WriteUInt32(dataSize); //writer.Write(dataSize); // Size of compressed section + length snipStreamWrapper.WriteUInt32(flags); //writer.Write(flags); snipStreamWrapper.WriteUInt32(FormID); //writer.Write(this.FormID); snipStreamWrapper.WriteUInt32(Flags2); //writer.Write(this.Flags2); snipStreamWrapper.WriteUInt32(Flags3); //writer.Write(this.Flags3); if (compressed) { snipStreamWrapper.WriteUInt32(realSize); //writer.Write(realSize); } if (dataSize > 0) { snipStreamWrapper.WriteBytesArrayInFileStream(data); //writer.Write(data, 0, data.Length); } }
public static byte[] Compress(this byte[] self) { return(ZLib.Compress(self, ZLib.CompressionLevels.BEST_COMPRESSION)); }
public void Save(string filename, bool isZLibCompressed = false) { using (BinaryWriterX bw = new BinaryWriterX(File.OpenWrite(filename))) { //Header bw.WriteASCII("NUS3"); bw.BaseStream.Position += 4; //BankToc bw.WriteASCII("BANKTOC "); bw.Write(0x3c); bw.Write(0x7); //BankTocEntries int propSize = 13 + prop.projectNameSize + prop.padding.Length + 5 + prop.dateSize + prop.padding2.Length; int binfSize = 9 + binf.nameSize + binf.padding.Length + 4; int tmp = 0; for (int i = 0; i < tone.toneCount; i++) { tmp += Files[i].Entry.metaSize; } int toneSize = 4 + tone.toneCount * 0x8 + tmp; int packSize = 0; for (int i = 0; i < tone.toneCount; i++) { packSize += (int)Files[i].FileData.Length; } string[] banktocList = new string[7] { "PROP", "BINF", "GRP ", "DTON", "TONE", "JUNK", "PACK" }; for (int i = 0; i < 7; i++) { bw.WriteASCII(banktocList[i]); switch (banktocList[i]) { case "PROP": bw.Write(propSize); break; case "BINF": bw.Write(binfSize); break; case "GRP ": bw.Write(grp.Length); break; case "DTON": bw.Write(dton.Length); break; case "TONE": bw.Write(toneSize); break; case "JUNK": bw.Write(junk.Length); break; case "PACK": bw.Write(packSize); break; } } //PROP bw.WriteASCII("PROP"); bw.Write(propSize); bw.Write(prop.unk1); bw.Write(prop.unk2); bw.Write(prop.unk3); bw.Write(prop.projectNameSize); bw.WriteASCII(prop.projectName); bw.Write(prop.padding); bw.Write(prop.unk4); bw.Write(prop.dateSize); bw.WriteASCII(prop.date); bw.Write(prop.padding2); //BINF bw.WriteASCII("BINF"); bw.Write(binfSize); bw.Write(binf.unk1); bw.Write(binf.unk2); bw.Write(binf.nameSize); bw.WriteASCII(binf.name); bw.Write(binf.padding); bw.Write(binf.ID); //GRP bw.WriteASCII("GRP "); bw.Write(grp.Length); bw.Write(grp); //DTON bw.WriteASCII("DTON"); bw.Write(dton.Length); bw.Write(dton); //TONE bw.WriteASCII("TONE"); bw.Write(toneSize); bw.Write(tone.toneCount); for (int i = 0; i < tone.toneCount; i++) { bw.Write(Files[i].Entry.offset - banktocEntries[4].offset - 8); bw.Write(Files[i].Entry.metaSize); } int packOffset = 0; for (int i = 0; i < tone.toneCount; i++) { if (Files[i].Entry.metaSize > 0xc) { bw.Write(0); bw.Write((short)-1); bw.Write(Files[i].Entry.unk1); bw.Write(Files[i].Entry.nameSize); bw.WriteASCII(Files[i].Entry.name); bw.Write((byte)0); bw.Write(Files[i].Entry.padding); if (Files[i].Entry.zero0 == 0) { bw.Write(0); } bw.Write(8); bw.Write(packOffset); bw.Write((int)Files[i].FileData.Length); packOffset += (int)Files[i].FileData.Length; bw.Write(Files[i].Entry.rest); } else { bw.Write(0); bw.Write(0xff); bw.Write(1); } } //JUNK bw.WriteASCII("JUNK"); bw.Write(junk.Length); bw.Write(junk); //PACK for (int i = 0; i < tone.toneCount; i++) { bw.Write(new BinaryReaderX(Files[i].FileData).ReadBytes((int)Files[i].FileData.Length)); } //update fileSize in NUS3 Header bw.BaseStream.Position = 4; bw.Write((int)bw.BaseStream.Length); } if (isZLibCompressed) { FileStream origFile = File.OpenRead(filename); byte[] decomp = new byte[(int)origFile.Length]; origFile.Read(decomp, 0, (int)origFile.Length); byte[] comp = ZLib.Compress(decomp); origFile.Close(); File.Delete(filename); File.OpenWrite(filename).Write(comp, 0, comp.Length); } }
/// <summary> /// /// </summary> /// <param name="obj">Object to create for the players on this maptile</param> /// <param name="objCreationPkg">creation packet of obj</param> /// <returns>Returns true if there was any players to send objCreationPkg to</returns> internal bool CreateObject(WorldObject obj, ServerPacket objCreationPkg) { if (obj.ObjectType == OBJECTTYPE.PLAYER) { // we might want to change this incase there would be alot // of objects on a single maptile cause it could possibly // overgo the packet size limit (which is 0xFFFF or less, not sure) // and only send one at a time or X at a time. Hopefully we // won't have to since it's compressed... bool ret = false; BinWriter w = new BinWriter(); w.Write(0); int objcount = 0; for (int i = 0; i < (int)OBJECTTYPE.PLAYER; i++) { if (m_objects[i].Count > 0) { objcount += m_objects[i].Count; foreach (WorldObject anObject in m_objects[i].Values) { anObject.AddCreateObject(w, false, true); } } } if (m_objects[(int)OBJECTTYPE.PLAYER].Count > 0) { ret = true; objcount += m_objects[(int)OBJECTTYPE.PLAYER].Count; foreach (PlayerObject plr in m_objects[(int)OBJECTTYPE.PLAYER].Values) { objCreationPkg.AddDestination(plr.CharacterID); plr.AddCreateObject(w, false, true); objcount += plr.Inventory.AddCreateInventory(w, false); } } for (int i = ((int)OBJECTTYPE.PLAYER) + 1; i < (int)OBJECTTYPE.MAX; i++) { if (m_objects[i].Count > 0) { objcount += m_objects[i].Count; foreach (WorldObject anObject in m_objects[i].Values) { anObject.AddCreateObject(w, false, true); } } } if (objcount > 0) { w.Set(0, objcount); ServerPacket pkg = new ServerPacket(SMSG.COMPRESSED_UPDATE_OBJECT); byte[] compressed = ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length); pkg.Write((int)w.BaseStream.Length); pkg.Write(compressed); pkg.Finish(); pkg.AddDestination((obj as PlayerObject).CharacterID); WorldServer.Send(pkg); } return(ret); } else { if (m_objects[(int)OBJECTTYPE.PLAYER].Count > 0) { foreach (PlayerObject plr in m_objects[(int)OBJECTTYPE.PLAYER].Values) { objCreationPkg.AddDestination(plr.CharacterID); } return(true); } return(false); } }
public static void Compress(object sender, EventArgs e) { var tsi = sender as ToolStripMenuItem; if (!Shared.PrepareFiles("Open a decompressed " + tsi?.Tag + "file...", "Save your compressed file...", ".decomp", out var openFile, out var saveFile, true)) { return; } try { using (openFile) using (var outFs = new BinaryWriterX(saveFile)) switch (tsi?.Tag) { case Compression.L5LZ10: outFs.Write(Level5.Compress(openFile, Level5.Method.LZ10)); break; case Compression.L5Huff4: outFs.Write(Level5.Compress(openFile, Level5.Method.Huffman4Bit)); break; case Compression.L5Huff8: outFs.Write(Level5.Compress(openFile, Level5.Method.Huffman8Bit)); break; case Compression.L5RLE: outFs.Write(Level5.Compress(openFile, Level5.Method.RLE)); break; case Compression.NLZ10: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ10)); break; case Compression.NLZ11: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ11)); break; case Compression.NLZ60: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ60)); break; case Compression.NHuff4: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.Huff4)); break; case Compression.NHuff8: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.Huff8)); break; case Compression.NRLE: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.RLE)); break; case Compression.LZ77: outFs.Write(LZ77.Compress(openFile)); break; case Compression.RevLZ77: outFs.Write(RevLZ77.Compress(openFile)); break; case Compression.LZOvl: outFs.Write(LZOvl.Compress(openFile)); break; case Compression.LZ4: outFs.Write(Kontract.Compression.LZ4.Compress(openFile)); break; case Compression.MIO0LE: outFs.Write(MIO0.Compress(openFile, ByteOrder.LittleEndian)); break; case Compression.MIO0BE: outFs.Write(MIO0.Compress(openFile, ByteOrder.BigEndian)); break; case Compression.Yay0LE: outFs.Write(Yay0.Compress(openFile, ByteOrder.LittleEndian)); break; case Compression.Yay0BE: outFs.Write(Yay0.Compress(openFile, ByteOrder.BigEndian)); break; case Compression.Yaz0LE: outFs.Write(Yaz0.Compress(openFile, ByteOrder.LittleEndian)); break; case Compression.Yaz0BE: outFs.Write(Yaz0.Compress(openFile, ByteOrder.BigEndian)); break; case Compression.GZip: outFs.Write(GZip.Compress(openFile)); break; case Compression.ZLib: outFs.Write(ZLib.Compress(openFile)); break; case Compression.PSVSpikeChun: outFs.Write(PSVSpikeChun.Compress(openFile)); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show($"Successfully compressed {Path.GetFileName(openFile.Name)}.", tsi.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static void Compress(object sender, EventArgs e) { var tsi = sender as ToolStripMenuItem; if (!PrepareFiles("Open a decompressed " + tsi.Tag.ToString() + "file...", "Save your compressed file...", ".comp", out FileStream openFile, out FileStream saveFile)) { return; } try { using (openFile) using (var outFs = new BinaryWriterX(saveFile)) switch (tsi.Tag) { /*case Compression.L5LZSS: * outFs.Write(Level5.Compress(openFile, 1)); * break; * case Compression.L5Huff4: * outFs.Write(Level5.Compress(openFile, 2)); * break; * case Compression.L5Huff8: * outFs.Write(Level5.Compress(openFile, 3)); * break; * case Compression.L5RLE: * outFs.Write(Level5.Compress(openFile, 4)); * break;*/ case Compression.GZip: outFs.Write(GZip.Compress(openFile)); break; case Compression.Huff4: outFs.Write(Huffman.Compress(openFile, 4)); break; case Compression.Huff8: outFs.Write(Huffman.Compress(openFile, 8)); break; case Compression.LZ10: outFs.Write(LZ10.Compress(openFile)); break; case Compression.LZ11: outFs.Write(LZ11.Compress(openFile)); break; /*case Compression.LZSS: * outFs.Write(LZSS.Compress(openFile)); * break;*/ case Compression.RevLZ77: outFs.Write(RevLZ77.Compress(openFile)); break; case Compression.RLE: outFs.Write(RLE.Compress(openFile)); break; case Compression.ZLib: outFs.Write(ZLib.Compress(openFile)); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } MessageBox.Show($"Successfully compressed {Path.GetFileName(openFile.Name)}.", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }