static private void ReadStrings(ZInputStream Inflater, int count, string[] target) { byte[] byte2 = new byte[2]; for (int i = 0; i < count; ++i) { if (Inflater.Read(byte2) == 0) { throw new Exception("Error: byte = 0"); } int length = ByteConverter.getShort(byte2, 0); byte[] message = new byte[length]; Inflater.Read(message); target[i] = Encoding.UTF8.GetString(message); } }
private byte[] GetContentHelperFunc(FileStream fStream) { var buffer = new byte[FileSize]; //Will contain compressed data fStream.Seek(FileOffset, SeekOrigin.Begin); fStream.Read(buffer, 0, (int)FileSize); try { var mStream = new MemoryStream(buffer); var zinput = new ZInputStream(mStream); var dBuffer = new List <byte>(); //decompressed buffer, arraylist to my knowledge... //This could be optimized in the future by reading a block and adding it to our arraylist.. //which would be much faster, obviously int data; while ((data = zinput.Read()) != -1) { dBuffer.Add((byte)data); } return(dBuffer.ToArray()); } catch { //it's not compressed, just return original content return(buffer); } }
/// <summary> /// Returns the content of the actual file (extracts from raf archive) /// </summary> public byte[] GetContent() { if (inMemory) { throw new Exception("Invalid call to GetContent, this entry is only in memory, and has not been linked to the DAT file yet."); } byte[] buffer = new byte[this.FileSize]; //Will contain compressed data //lock (this.raf) //No longer necessary { //Todo: Since we are no longer modifying the data file content stream, // it would be valid to create the stream more than once. // We could then work WHILE reading. FileStream fStream = new FileStream(archive.DatFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //this.raf.GetDataFileContentStream(); fStream.Seek(this.FileOffset, SeekOrigin.Begin); fStream.Read(buffer, 0, (int)this.FileSize); fStream.Close(); } try { // using (var outputMs = new MemoryStream()) { // // var deflate = new DeflateStream(new MemoryStream(buffer, 2, buffer.Length - 2), CompressionMode.Decompress); // deflate.CopyTo(outputMs); // return outputMs.ToArray(); // } ZInputStream zinput = new ZInputStream(new MemoryStream(buffer)); MemoryStream ms = new MemoryStream(); int data = 0; while ((data = zinput.Read()) != -1) { ms.WriteByte((byte)data); } return(ms.ToArray()); // new DeflateStream() // // //List<byte> dBuffer = new List<byte>(); //decompressed buffer, arraylist to my knowledge... // // MemoryStream ms = new MemoryStream(); // // //This could be optimized in the future by reading a block and adding it to our arraylist.. // //which would be much faster, obviously // int data = 0; // while ((data = zinput.Read()) != -1) // ms.WriteByte((byte)data); // //dBuffer.Add((byte)data); // //ComponentAce.Compression.Libs.zlib.ZStream a = new ComponentAce.Compression.Libs.zlib.ZStream(); // // //File.WriteAllBytes((dumpDir + entry.FileName).Replace("/", "\\"), dBuffer.ToArray()); // return ms.ToArray(); } catch (Exception e) { Console.WriteLine(e); //it's not compressed, just return original content return(buffer); } }
public static void DecompressFile(string inFile, string outFile) { int data; const int stopByte = -1; var outFileStream = new FileStream(outFile, FileMode.Create); var inZStream = new ZInputStream(File.Open(inFile, FileMode.Open, FileAccess.Read)); while (stopByte != (data = inZStream.Read())) { var databyte = (byte)data; outFileStream.WriteByte(databyte); } inZStream.Close(); outFileStream.Close(); }
private static void UncompressFile(string inFile, string outFile) { // Do it slow way because calling CopyStream sometimes causes an inflated exception on LoL comrpessed files int stopByte = -1; System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); ZInputStream inZStream = new ZInputStream(System.IO.File.Open(inFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)); int data; while (stopByte != (data = inZStream.Read())) { byte _dataByte = (byte)data; outFileStream.WriteByte(_dataByte); } inZStream.Close(); outFileStream.Close(); }
public static OSD ZDecompressBytesToOsd(byte[] input) { OSD osd = null; using (MemoryStream msSinkUnCompressed = new MemoryStream()) { using (ZInputStream zOut = new ZInputStream(msSinkUnCompressed)) { zOut.Read(input, 0, input.Length); msSinkUnCompressed.Seek(0L, SeekOrigin.Begin); osd = OSDParser.DeserializeLLSDBinary(msSinkUnCompressed.ToArray()); zOut.Close(); } } return(osd); }
public static void uncompressFile(string inFile, string outFile) { int data = 0; int stopByte = -1; System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); ZInputStream inZStream = new ZInputStream(System.IO.File.Open(inFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)); while (stopByte != (data = inZStream.Read())) { byte _dataByte = (byte)data; outFileStream.WriteByte(_dataByte); } inZStream.Close(); outFileStream.Close(); }
/// <summary> /// Распаковка файла /// </summary> /// <param name="fileName">Путь к файлу</param> /// <param name="state"></param> /// <returns></returns> public string BackProcessExecute(string fileName) { int data = 0; int stopByte = -1; string out_file = TempNameGenerator.GenerateTempNameFromFile(fileName); using (FileStream outFileStream = new FileStream(out_file, FileMode.Create)) { using (ZInputStream inZStream = new ZInputStream(File.Open(fileName, FileMode.Open, FileAccess.Read))) { while (stopByte != (data = inZStream.Read())) { byte _dataByte = (byte)data; outFileStream.WriteByte(_dataByte); } inZStream.Close(); } outFileStream.Close(); } return(out_file); }
public void uncompressFile(string inFile, string outFile) { try { int data = 0; int stopByte = -1; System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create); ZInputStream inZStream = new ZInputStream(System.IO.File.Open(inFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)); while (stopByte != (data = inZStream.Read())) { byte _dataByte = (byte)data; outFileStream.WriteByte(_dataByte); } inZStream.Close(); outFileStream.Close(); } catch { Client.Log("Unable to find a file to uncompress"); } }
public void UncompressFile(string inFile, string outFile) { try { int data; const int stopByte = -1; var outFileStream = new FileStream(outFile, FileMode.Create); var inZStream = new ZInputStream(File.Open(inFile, FileMode.Open, FileAccess.Read)); while (stopByte != (data = inZStream.Read())) { var dataByte = (byte)data; outFileStream.WriteByte(dataByte); } inZStream.Close(); outFileStream.Close(); } catch { Client.Log("Unable to find a file to uncompress"); } }
public void ReadPacket(Stream s) { int slen = 0; byte[] rdata; VarInt viPacketLength = new VarInt(s); PacketLength = VarintBitConverter.ToInt32(viPacketLength.VarIntData); VarInt viPacketID; if (Compression == false) { viPacketID = new VarInt(s); PacketId = VarintBitConverter.ToInt32(viPacketID.VarIntData); DataLength = PacketLength - viPacketID.Length; Data = new byte[DataLength]; if (s.Read(Data, 0, DataLength) == DataLength) { DataRead = true; } else { DataRead = false; } } else { VarInt viDataLength = new VarInt(s); DataLength = VarintBitConverter.ToInt32(viDataLength.VarIntData); if (DataLength == 0) { //No compression viPacketID = new VarInt(s); PacketId = VarintBitConverter.ToInt32(viPacketID.VarIntData); DataLength = PacketLength - viDataLength.Length - viPacketID.Length; Data = new byte[DataLength]; if (s.Read(Data, 0, DataLength) == DataLength) { DataRead = true; } else { DataRead = false; } } else { Data = new byte[DataLength]; slen = PacketLength - viDataLength.Length; rdata = new byte[slen]; if (s.Read(rdata, 0, slen) == slen) { DataRead = true; } else { DataRead = false; } if (DataRead == true) { MemoryStream ms; ms = new MemoryStream(rdata); ZInputStream zlib = new ZInputStream(ms); if (zlib.Read(Data, 0, DataLength) == DataLength) { DataRead = true; } else { DataRead = false; } zlib.Close(); ms = new MemoryStream(Data); viPacketID = new VarInt(ms); PacketId = VarintBitConverter.ToInt32(viPacketID.VarIntData); slen = DataLength - viPacketID.Length; if (ms.Read(Data, 0, slen) == slen) { DataRead = true; } else { DataRead = false; } ms.Close(); } } } }
/// <summary> /// Decompresses the given data stream from its source ZIP or GZIP format. /// </summary> /// <param name="dataBytes"></param> /// <returns></returns> internal static byte[] Inflate(Stream dataStream, CompressionFormat format) { byte[] outputBytes = null; switch (format) { case CompressionFormat.Zlib: try { var zipInputStream = new ZipInputStream(dataStream); if (zipInputStream.CanDecompressEntry) { MemoryStream zipoutStream = new MemoryStream(); zipInputStream.CopyTo(zipoutStream); outputBytes = zipoutStream.ToArray(); } else { dataStream.Seek(0, SeekOrigin.Begin); var inZInputStream = new ZInputStream(dataStream); var outMemoryStream = new MemoryStream(); outputBytes = new byte[inZInputStream.BaseStream.Length]; while (true) { int bytesRead = inZInputStream.Read(outputBytes, 0, outputBytes.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(outputBytes, 0, bytesRead); } } } catch (Exception exc) { CCLog.Log("Error decompressing image data: " + exc.Message); } break; case CompressionFormat.Gzip: try { #if !WINDOWS_PHONE var gzipInputStream = new GZipInputStream(dataStream, CompressionMode.Decompress); #else var gzipInputStream = new GZipInputStream(dataStream); #endif MemoryStream zipoutStream = new MemoryStream(); gzipInputStream.CopyTo(zipoutStream); outputBytes = zipoutStream.ToArray(); } catch (Exception exc) { CCLog.Log("Error decompressing image data: " + exc.Message); } break; } return(outputBytes); }
public void EndElement(object ctx, string elementName) { CCTMXMapInfo pTMXMapInfo = this; byte[] encoded = null; if (elementName == "data" && (pTMXMapInfo.LayerAttribs & (int)CCTMXLayerAttrib.Base64) != 0) { pTMXMapInfo.StoringCharacters = false; CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault(); if ((pTMXMapInfo.LayerAttribs & ((int)(CCTMXLayerAttrib.Gzip) | (int)CCTMXLayerAttrib.Zlib)) != 0) { //gzip compress if ((pTMXMapInfo.LayerAttribs & (int)CCTMXLayerAttrib.Gzip) != 0) { try { GZipStream inGZipStream = new GZipStream(new MemoryStream(pTMXMapInfo.CurrentString)); var outMemoryStream = new MemoryStream(); var buffer = new byte[1024]; while (true) { int bytesRead = inGZipStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(buffer, 0, bytesRead); } encoded = outMemoryStream.ToArray(); } catch (Exception ex) { CCLog.Log("failed to decompress embedded data object in TMX file."); CCLog.Log(ex.ToString()); } } //zlib if ((pTMXMapInfo.LayerAttribs & (int)CCTMXLayerAttrib.Zlib) != 0) { var inZInputStream = new ZInputStream(new MemoryStream(pTMXMapInfo.CurrentString)); var outMemoryStream = new MemoryStream(); var buffer = new byte[1024]; while (true) { int bytesRead = inZInputStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(buffer, 0, bytesRead); } encoded = outMemoryStream.ToArray(); } } else { encoded = pTMXMapInfo.CurrentString; } for (int i = 0; i < layer.Tiles.Length; i++) { int i4 = i * 4; var gid = (uint)( encoded[i4] | encoded[i4 + 1] << 8 | encoded[i4 + 2] << 16 | encoded[i4 + 3] << 24); layer.Tiles[i] = gid; } pTMXMapInfo.CurrentString = null; } else if (elementName == "map") { // The map element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } else if (elementName == "layer") { // The layer element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } else if (elementName == "objectgroup") { // The objectgroup element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } else if (elementName == "object") { // The object element has ended pTMXMapInfo.ParentElement = (int)CCTMXProperty.None; } }
static void Main(string[] args) { FileStream fs = new FileStream("D:\\bloomextract.bls", FileMode.Open, FileAccess.Read); try { ZInputStream zIn = new ZInputStream(fs); int curPosition = 0; Byte[] data = new Byte[4096]; Byte[] bytes = new Byte[0]; while (true) { int size = zIn.Read(data, 0, data.Length); if (size > 0) { curPosition += size; Byte[] tempBytes = new Byte[bytes.LongLength + data.Length]; bytes.CopyTo(tempBytes, 0); data.CopyTo(tempBytes, bytes.LongLength); bytes = tempBytes; } else { break; } } string str = System.Text.Encoding.Default.GetString(bytes); Console.WriteLine(str); Console.WriteLine("ddddddddddd"); bytes = deCompressBytes(bytes); str = System.Text.Encoding.Default.GetString(bytes); Console.WriteLine(str); fs.Close(); fs.Dispose(); //string strDecompress = System.Text.Encoding.UTF8.GetString(bytesDecompress); //Console.WriteLine(strDecompress); //BLS p = new BLS(buffur); //byte[] re = new ShaderBlock(buffur).Serialize(); using (FileStream fs1 = new FileStream("D:\\bloom.bls", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (BinaryWriter bw = new BinaryWriter(fs1)) { bw.Write(bytes); bw.Close(); } } } catch { } finally { if (fs != null) { //关闭资源 fs.Close(); } } Console.WriteLine("Hello World!"); Console.ReadLine(); }
public void endElement(object ctx, string elementName) { CCTMXMapInfo cCTMXMapInfo = this; byte[] currentString = cCTMXMapInfo.CurrentString; if (!(elementName == "data") || (cCTMXMapInfo.LayerAttribs & 2) == 0) { if (elementName == "map") { cCTMXMapInfo.ParentElement = 0; return; } if (elementName == "layer") { cCTMXMapInfo.ParentElement = 0; return; } if (elementName == "objectgroup") { cCTMXMapInfo.ParentElement = 0; return; } if (elementName == "object") { cCTMXMapInfo.ParentElement = 0; } return; } cCTMXMapInfo.StoringCharacters = false; CCTMXLayerInfo cCTMXLayerInfo = cCTMXMapInfo.Layers.LastOrDefault <CCTMXLayerInfo>(); if ((cCTMXMapInfo.LayerAttribs & 12) == 0) { for (int i = 0; i < (int)cCTMXLayerInfo.m_pTiles.Length; i++) { cCTMXLayerInfo.m_pTiles[i] = currentString[i * 4]; } } else { using (MemoryStream memoryStream = new MemoryStream(currentString, false)) { if ((cCTMXMapInfo.LayerAttribs & 4) != 0) { using (GZipInputStream gZipInputStream = new GZipInputStream(memoryStream)) { using (BinaryReader binaryReader = new BinaryReader(gZipInputStream)) { for (int j = 0; j < (int)cCTMXLayerInfo.m_pTiles.Length; j++) { cCTMXLayerInfo.m_pTiles[j] = binaryReader.ReadInt32(); } } } } if ((cCTMXMapInfo.LayerAttribs & 8) != 0) { using (ZInputStream zInputStream = new ZInputStream(memoryStream)) { for (int k = 0; k < (int)cCTMXLayerInfo.m_pTiles.Length; k++) { cCTMXLayerInfo.m_pTiles[k] = zInputStream.Read(); zInputStream.Read(); zInputStream.Read(); zInputStream.Read(); } } } } } cCTMXMapInfo.CurrentString = null; }
public static SCX Load(string srcfile) { SCX scx = new SCX(); FileStream InputStream = File.OpenRead(srcfile); byte[] byte16 = new byte[16]; byte[] byte4 = new byte[4]; byte[] byte2 = new byte[2]; byte[] byte1 = new byte[1]; ///////////////////HEADER////////////////////////////////// InputStream.Read(byte4); //version number scx.version = Encoding.UTF8.GetString(byte4); InputStream.Read(byte4); //Length of header InputStream.Read(byte4); //Can you save in this scenario? InputStream.Read(byte4); //Timestamp of last save InputStream.Read(byte4); //Scenario Instructions scx.briefing_length = 0; byte[] briefing = new byte[scx.briefing_length]; InputStream.Read(briefing); scx.briefing = Encoding.UTF8.GetString(briefing); InputStream.Read(byte4); //Individual victories used InputStream.Read(byte4); //Player count scx.player_count[0] = 0; ///////////////////BODY////////////////////////////////// ZInputStream Inflater = new ZInputStream(InputStream); Inflater.Read(byte4); scx.next_unit_id = 0; Inflater.Read(byte4); scx.version2 = 0; //player names byte[] name = new byte[SCX.NAME_LENGTH]; for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { Inflater.Read(name); scx.players[i].name = "zorksox";//new String(pname).trim(); Console.WriteLine(Encoding.UTF8.GetString(name)); } //player strings for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { Inflater.Read(name); scx.players[i].name_st = 0; } //player config for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { Inflater.Read(name); scx.players[i].boolean = 0; Inflater.Read(name); scx.players[i].machine = 0; Inflater.Read(name); scx.players[i].profile = 0; Inflater.Read(name); scx.players[i].unknown = 0; } //message ???? Inflater.Read(byte4); scx.message_option_0 = 0; Inflater.Read(byte1); scx.message_option_1 = (char)byte1[0]; Inflater.Read(byte4); //file name Inflater.Read(byte2); short fileNameLength = ByteConverter.getShort(byte2, 0); Console.WriteLine(fileNameLength); byte[] fileName = new byte[fileNameLength]; Inflater.Read(fileName); scx.filename = Encoding.UTF8.GetString(fileName); //# message strings //# 0x01 = objective //# 0x02 = hints //# 0x03 = victory //# 0x04 = failure //# 0x05 = history //# 0x06 = scouts for (int i = 0; i < SCX.MESSAGE_COUNT; ++i) { Inflater.Read(byte4); scx.messages_st[i] = 0; } // message scripts ReadStrings(Inflater, SCX.MESSAGE_COUNT, scx.messages); //message cinematics ReadStrings(Inflater, SCX.CINEMATIC_COUNT, scx.cinematics); //message / bitmap scx.bitmap.boolean = 0; Inflater.Read(byte4); scx.bitmap.width = 0; Inflater.Read(byte4); scx.bitmap.height = 0; Inflater.Read(byte4); Inflater.Read(byte2); //scx.bitmap.def = ByteConverter.getShort(byte2, 0); if (scx.bitmap.boolean > 0) { byte[] bitmap = new byte[40 + 1024 + scx.bitmap.width * scx.bitmap.height]; Inflater.Read(bitmap); scx.bitmap.bitmap = bitmap; } else { scx.bitmap.bitmap = new byte[0]; } //behavior names Inflater.Read(new byte[SCX.PLAYER_MAX_16 * (SCX.BEHAVIOR_COUNT - 1) * 2]); // SKIP ALL AOE1 PROPS //for (int i=0; i<3; ++i){ for (int j = 0; j < SCX.PLAYER_MAX_16; ++j) { Inflater.Read(byte2); int length = ByteConverter.getShort(byte2, 0); if (length > 0) { byte[] message = new byte[length]; Inflater.Read(message); scx.players[j].ai = Encoding.UTF8.GetString(message); } } //behavior size & data for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { int[] lengths = new int[SCX.BEHAVIOR_COUNT]; for (int j = 0; j < SCX.BEHAVIOR_COUNT; ++j) { lengths[j] = 0; Inflater.Read(byte4); } for (int j = 0; j < SCX.BEHAVIOR_COUNT; ++j) { byte[] message = new byte[lengths[j]]; Inflater.Read(message); scx.players[i].aic[j] = Encoding.UTF8.GetString(message); } } //behavior type Inflater.Read(byte16); for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { scx.players[i].aitype = (char)byte16[i]; } //separator 1 Inflater.Read(byte4); //player config 2 Inflater.Read(new byte[24 * SCX.PLAYER_MAX_16]); //separator 2 Inflater.Read(byte4); //victory / globals //# 0x01 = conquest //# 0x02 = ruins //# 0x03 = artifacts //# 0x04 = discoveries //# 0x05 = explored //# 0x06 = gold count //# 0x07 = required //# 0x08 = condition //# 0x09 = score //# 0x0A = time limit for (int i = 0; i < SCX.VICTORY_CONDITION_COUNT; ++i) { scx.victories[i] = 0; Inflater.Read(byte4); } //victory / diplomacy / player / stance for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { for (int j = 0; j < SCX.PLAYER_MAX_16; ++j) { scx.players[i].v_diplomacies[j] = 0; Inflater.Read(byte4); } } //victory / individual-victory (12 triggers per players) //(they are unused in AoK/AoC once the new trigger system was introduced) Inflater.Read(new byte[SCX.PLAYER_MAX_16 * 15 * 12 * 4]); //separator 3 Inflater.Read(byte4); //victory / diplomacy / player / allied for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { scx.players[i].alliedvictory = 0; Inflater.Read(byte4); } //disability / techtree int[] disables = { SCX.DISABLED_TECH_COUNT, SCX.DISABLED_UNIT_COUNT, SCX.DISABLED_BUILDING_COUNT }; for (int i = 0; i < 3; ++i) { Inflater.Read(new byte[64]); for (int j = 0; j < SCX.PLAYER_MAX_16; ++j) { for (int k = 0; k < disables[i]; ++k) { Inflater.Read(byte4); switch (i) { case 0: scx.players[j].disabled_techs[k] = 0; break; case 1: scx.players[j].disabled_units[k] = 0; break; case 2: scx.players[j].disabled_buildings[k] = 0; break; } } } } //disability / options for (int i = 0; i < 3; ++i) { Inflater.Read(byte4); scx.disability_options[i] = 0; } //disability / starting age for (int i = 0; i < SCX.PLAYER_MAX_16; ++i) { //Starting age is determined by the first byte. 0 = dark, 1 = feudal, 2=castle, 3=imp, 4 = post-imp. //The other 3 byte appear to no importance other than buffer data. //byte[] newByte4 = new byte[] { (byte)0, (byte)0, (byte)0, (byte)0 }; scx.players[i].startage = 0; Inflater.Read(byte4); } //separator 4 Inflater.Read(byte4); //terrain / view Inflater.Read(byte4); Inflater.Read(byte4); //terrain / type Inflater.Read(byte4); //terrain size scx.terrain.sizex = 0; Inflater.Read(byte4); scx.terrain.sizey = 0; Inflater.Read(byte4); //terrain data @TERRAIN byte[] byte3 = new byte[3]; scx.terrain.InitializeTiles(); for (int i = 0; i < scx.terrain.sizey; ++i) { for (int j = 0; j < scx.terrain.sizex; ++j) { Inflater.Read(byte3); scx.terrain.tiles[j, i] = (char)byte3[0]; scx.terrain.hills[j, i] = (char)byte3[1]; } } //players count 2 // GAIA included Inflater.Read(byte4); scx.player_count[1] = 0; //player sources & config //The original version had a loop here that looped through playercount //and set all the resources. However, playercount was hard-coded to 0, so I removed the loop. //objects: players //The original version had a loop here that looped through playercount //However, playercount was hard-coded to 0, so I removed the loop. //players count 3: Should be 9 scx.player_count[2] = 0; Inflater.Read(byte4); Inflater.Close(); return(scx); }
private void LoadLayerDataBase64(XmlHelper xmlHelper, Layer layer, string dataCompression) { xmlHelper.AdvanceNode(XmlNodeType.Text); string base64Data = xmlHelper.XmlReader.Value; byte[] dataBytes = System.Convert.FromBase64String(base64Data); if (dataCompression == "none") { // do nothing } else if (dataCompression == "gzip") { GZipStream inGZipStream = new GZipStream( new MemoryStream(dataBytes), CompressionMode.Decompress); MemoryStream outMemoryStream = new MemoryStream(); byte[] buffer = new byte[1024]; while (true) { int bytesRead = inGZipStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(buffer, 0, bytesRead); } dataBytes = outMemoryStream.ToArray(); } else if (dataCompression == "zlib") { ZInputStream inZInputStream = new ZInputStream( new MemoryStream(dataBytes)); MemoryStream outMemoryStream = new MemoryStream(); byte[] buffer = new byte[1024]; while (true) { int bytesRead = inZInputStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } outMemoryStream.Write(buffer, 0, bytesRead); } dataBytes = outMemoryStream.ToArray(); } else { throw new Exception("Unknown compression scheme: " + dataCompression); } if (dataBytes.Length % 4 != 0) { throw new Exception("Base64 byte stream size must be a mutliple of 4 bytes"); } Location tileLocation = Location.Origin; for (int byteIndex = 0; byteIndex < dataBytes.Length; byteIndex += 4) { int gid = dataBytes[byteIndex] | dataBytes[byteIndex + 1] << 8 | dataBytes[byteIndex + 2] << 16 | dataBytes[byteIndex + 3] << 24; layer.Tiles[tileLocation] = LoadStaticTile(layer, gid); ++tileLocation.X; if (tileLocation.X >= layer.LayerWidth) { tileLocation.X = 0; ++tileLocation.Y; } } xmlHelper.AdvanceEndElement("data"); }
public void endElement(object ctx, string elementName) { CCTMXMapInfo pTMXMapInfo = this; byte[] buffer = pTMXMapInfo.CurrentString; if (elementName == "data" && (pTMXMapInfo.LayerAttribs & (int)TMXLayerAttrib.TMXLayerAttribBase64) != 0) { pTMXMapInfo.StoringCharacters = false; CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault(); if ((pTMXMapInfo.LayerAttribs & ((int)(TMXLayerAttrib.TMXLayerAttribGzip) | (int)TMXLayerAttrib.TMXLayerAttribZlib)) != 0) { using (MemoryStream ms = new MemoryStream(buffer, false)) { //gzip compress if ((pTMXMapInfo.LayerAttribs & (int)TMXLayerAttrib.TMXLayerAttribGzip) != 0) { using (GZipInputStream inStream = new GZipInputStream(ms)) { using (var br = new BinaryReader(inStream)) { for (int i = 0; i < layer.m_pTiles.Length; i++) { layer.m_pTiles[i] = br.ReadInt32(); } } } } //zlib if ((pTMXMapInfo.LayerAttribs & (int)TMXLayerAttrib.TMXLayerAttribZlib) != 0) { using (ZInputStream inZStream = new ZInputStream(ms)) { for (int i = 0; i < layer.m_pTiles.Length; i++) { layer.m_pTiles[i] = inZStream.Read(); inZStream.Read(); inZStream.Read(); inZStream.Read(); } } } } } else { for (int i = 0; i < layer.m_pTiles.Length; i++) { layer.m_pTiles[i] = buffer[i * 4]; } } pTMXMapInfo.CurrentString = null; } else if (elementName == "map") { // The map element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } else if (elementName == "layer") { // The layer element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } else if (elementName == "objectgroup") { // The objectgroup element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } else if (elementName == "object") { // The object element has ended pTMXMapInfo.ParentElement = (int)TMXProperty.TMXPropertyNone; } }