From c10ba99d155ec0f1b0b735d3651d3b78304c6b82 Mon Sep 17 00:00:00 2001 From: "admin@omencraft.com" Date: Fri, 4 Nov 2011 20:13:10 +0000 Subject: fixing svn directory git-svn-id: http://mc-server.googlecode.com/svn/trunk@58 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- converter/source/cDeNotch.cpp | 226 ++++++++++ converter/source/cDeNotch.h | 264 ++++++++++++ converter/source/cNBTData.cpp | 922 ++++++++++++++++++++++++++++++++++++++++ converter/source/cNBTData.h | 198 +++++++++ converter/source/cQuicksort.cpp | 67 +++ converter/source/cQuicksort.h | 16 + converter/source/cTimer.cpp | 9 + converter/source/cTimer.h | 13 + converter/source/main.cpp | 52 +++ 9 files changed, 1767 insertions(+) create mode 100644 converter/source/cDeNotch.cpp create mode 100644 converter/source/cDeNotch.h create mode 100644 converter/source/cNBTData.cpp create mode 100644 converter/source/cNBTData.h create mode 100644 converter/source/cQuicksort.cpp create mode 100644 converter/source/cQuicksort.h create mode 100644 converter/source/cTimer.cpp create mode 100644 converter/source/cTimer.h create mode 100644 converter/source/main.cpp (limited to 'converter') diff --git a/converter/source/cDeNotch.cpp b/converter/source/cDeNotch.cpp new file mode 100644 index 000000000..f76dda5d4 --- /dev/null +++ b/converter/source/cDeNotch.cpp @@ -0,0 +1,226 @@ +#include "cDeNotch.h" +#include +#include +//#include +#include +#include +#include +#include "zlib.h" +#include "cNBTData.h" +#include "cTimer.h" +#include "cQuicksort.h" +#include "cDeNotch.h" +#include + + +//int cDeNotch ( std::string, std::string ); + +//std::string mcrSource; +//std::string pakOutput; + + + +//using namespace std; + + +cDeNotch::cDeNotch ( ) { + +} + +int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput ) { + char SourceFile[128]; + char OutputFile[128]; + + + FILE* f = 0; + FILE* wf = 0; + #ifdef _WIN32 + sprintf_s(SourceFile, 128, "region/%s", mcrSource.c_str() ); //replace hard coded file with file array variable + sprintf_s(OutputFile, 128, "world/%s", pakOutput.c_str() ); //parce x and z from file array variable and place into pak file format + if( fopen_s(&wf, OutputFile, "wb" ) == 0 ) {} else { std::cout << "uhoh!" << std::endl; return false; } //open new pak file for writing + #else + sprintf(SourceFile, "region/%s", mcrSource.c_str() ); //same as above but for linux + sprintf(OutputFile, "world/%s", pakOutput.c_str() ); + if( (wf = fopen(OutputFile, "wb" )) != 0 ) {} else { std::cout << "uhoh!" << std::endl; return false; } + #endif + + + printf ("Now Converting %s to %s\n", mcrSource.c_str(), pakOutput.c_str() ); + if( (f = fopen(SourceFile, "rb" )) != 0 ) { // no error + + char* t_FakeHeader; + t_FakeHeader = new char[1*1024*1024]; //1MB Temp FakeHeader array + int t_FakeHeaderSz = -1; //Size of data in array + + char* t_CompChunk; + t_CompChunk = new char[5*1024*1024]; //5MB Temp Compressed Chunk Data array + int t_CompChunkSz = -1; //Size of data in array + + char PakVersion = 1; + char ChunkVersion = 1; + short NumChunks = 0; + + unsigned char byte1 = 0; + unsigned char byte2 = 0; + unsigned char byte3 = 0; + unsigned char byte4 = 0; + unsigned char byte5 = 0; + unsigned char trash = 0; + unsigned int frloc = 0; + + int toffset = 0; + int compdlength = 0; + + int toffarr[1024]; + + //loop through notch's header + for( short i = 0; i < 1024 ; ++i ) {//loop through first 4096 bytes of data, 4 bytes at a time + //Region files begin with an 8kiB header containing information about which chunks are present in the region file, when they were last updated, and where they can be found. The location in the region file of a chunk at (x, z) (in chunk coordinates) can be found at byte offset 4 * ((x mod 32) + (z mod 32) * 32) in its region file. Its timestamp can be found 4096 bytes later in the file. The remainder of the file consists of data for up to 1024 chunks, interspersed with an arbitrary amount of unused space. + //we are only using the first 4096 bytes. We don't need the timestamps right now. + if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { std::cout << "ERROR 21hs READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { std::cout << "ERROR ks93 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { std::cout << "ERROR 2s5f READING FROM FILE " << SourceFile; fclose(f); return false; }//first three bytes area big-endian representation of the chunk offsets in no particular order. + if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR dhj3 READING FROM FILE " << SourceFile; fclose(f); return false; }//we don't need to use this byte right now. + toffset = 4096 * ((byte1*256*256) + (byte2*256) + byte3);//find the chunk offsets using the first three bytes of each long; + toffarr[i] = toffset;//array of chunk offset locatiosn in the fle. + } + for ( short i = 0; i < 4096; i++ ) {//loop through next 4096 bytes of the header. + //keeping this code here in case we need it later. not using it right now. + if( fread( &trash, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR 2jkd READING FROM FILE " << SourceFile; fclose(f); return false; } + } + frloc = 8192; //current location of fread is at 4096+ 4096 since we read through and collected important info from the header. + + cQuicksort Quick; + Quick.quicksort(toffarr, 0, 1023); //sort the array from smallest to larget offset locations so we only have to read through the file once. + + for ( short ia = 0; ia < 1024; ia++ ) {//a region file can hold a maximum of 1024 chunks (32*32) + if (ia < 3500 ) { //only run chunk # 3 + if (toffarr[ia] < 8192) { //offsets of less than 8192 are impossible. 0 means there is no chunk in a particular location. + if (toffarr[ia] > 0) { std::cout << "ERROR 2s31 IN COLLECTED CHUNK OFFSETS " << toffarr[ia]; fclose(f); return false; } //values between 0 and 8192 should be impossible. + //This file does not contain the max 1024 chunks, skip until we get to the first + } else { // found a chunk offset value + //Chunk data begins with a (big-endian) four-byte length field which indicates the exact length of the remaining chunk data in bytes. The following byte indicates the compression scheme used for chunk data, and the remaining (length-1) bytes are the compressed chunk data. + //printf("Working on chunk %i :: %i\n", ia, toffarr[ia]); + if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { std::cout << "ERROR 2t32 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { std::cout << "ERROR 2y51 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { std::cout << "ERROR 3424 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR sd22 READING FROM FILE " << SourceFile; fclose(f); return false; } + compdlength = ((byte1*256*256*256) + (byte2*256*256) + (byte3*256) + byte4 - 0); //length of compressed chunk data + if( fread( &byte5, sizeof(byte5), 1, f) != 1 ) { std::cout << "ERROR 2341 READING FROM FILE " << SourceFile; fclose(f); return false; } //compression type, 1 = GZip (RFC1952) (unused in practice) , 2 = Zlib (RFC1950) + + frloc += 5; //moved ahead 5 bytes while reading data. + + char* compBlockData = new char[compdlength]; + if( fread( compBlockData, compdlength, 1, f) != 1 ) { std::cout << "ERROR rf22 READING FROM FILE " << SourceFile; fclose(f); return false; } + frloc = frloc + compdlength; + + uLongf DestSize = 128576;// uncompressed chunks should never be larger than this + + char* BlockData = new char[ DestSize ]; + + int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)compBlockData, compdlength ); //DestSize will update to the actual uncompressed data size after this opperation. + int testr = (int)DestSize; //testing something, can't remember what. + if( errorcode != Z_OK ){ + printf("ERROR: Decompressing chunk data! %i", errorcode ); + switch( errorcode ) + { + case Z_MEM_ERROR: + printf("Not enough memory"); + break; + case Z_BUF_ERROR: + printf("Not enough room in output buffer"); + break; + case Z_DATA_ERROR: + printf("Input data corrupted or incomplete"); + break; + default: + break; + }; + } + + NumChunks++; + cNBTData* NBTData = new cNBTData(BlockData, (int)DestSize); + NBTData->ParseData(); + //NBTData->PrintData(); + NBTData->OpenCompound(""); + NBTData->OpenCompound("Level"); // You need to open the right compounds before you can access the data in it + + //NBT Data for blocks should look something like this: + //==== STRUCTURED NBT DATA ==== + // COMPOUND ( ) + // COMPOUND + // COMPOUND (Level) + // LIST (Entities) + // LIST (TileEntities) + // INTEGER LastUpdate (0) + // INTEGER xPos (0) + // INTEGER zPos (0) + // BYTE TerrainPopulated (1) + // BYTE ARRAY BlockLight (length: 16384) + // BYTE ARRAY Blocks (length: 32768) + // BYTE ARRAY Data (length: 16384) + // BYTE ARRAY HeightMap (length: 256) + // BYTE ARRAY SkyLight (length: 16384) + //============================= + + int UncompressedChunkSz = (32768+16384+16384+16384); + char* UncompressedChunk = new char[ UncompressedChunkSz ]; + uLongf CompressedSize = compressBound( UncompressedChunkSz ); + char* CompressedChunk = new char[ CompressedSize ]; + int UnChunkArrLoc = 0; + int xPos = NBTData->GetInteger("xPos"); + int zPos = NBTData->GetInteger("zPos"); + + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &xPos, sizeof(int) );t_FakeHeaderSz += sizeof(int); + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &zPos, sizeof(int) );t_FakeHeaderSz += sizeof(int); + + + //todo: inserert json code and add it to chunk data + + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("Blocks"), 32768 );UnChunkArrLoc += 32768; + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("Data"), 16384 );UnChunkArrLoc += 16384; + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("BlockLight"), 16384 );UnChunkArrLoc += 16384; + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("SkyLight"), 16384 );UnChunkArrLoc += 16384; + + errorcode = compress2( (Bytef*)CompressedChunk, &CompressedSize, (const Bytef*)UncompressedChunk, UncompressedChunkSz, Z_DEFAULT_COMPRESSION); + if( errorcode != Z_OK ) + { + printf("Error compressing data (%i)", errorcode ); + break; + } + + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &CompressedSize, sizeof(int) );t_FakeHeaderSz += sizeof(int); + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &UncompressedChunkSz, sizeof(int) );t_FakeHeaderSz += sizeof(int); + memcpy( t_CompChunk + t_CompChunkSz + 1, CompressedChunk, CompressedSize );t_CompChunkSz += CompressedSize; + + NBTData->CloseCompound();// Close the compounds after you're done + NBTData->CloseCompound(); + + delete [] UncompressedChunk; + delete [] CompressedChunk; + delete [] compBlockData; + delete [] BlockData; + //delete [] NBTData; + + while ( (frloc < toffarr[ia+1]) && (ia<1023) ) { //loop through Notch's junk data until we get to another chunk offset possition to start the loop again + if( fread( &trash, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR 2nkd READING FROM FILE " << SourceFile; fclose(f); return false; } + frloc ++; + } + + } + } //only run chunk # 3 + + } + fwrite( &PakVersion, sizeof(PakVersion), 1, wf ); + fwrite( &ChunkVersion, sizeof(ChunkVersion), 1, wf ); + fwrite( &NumChunks, sizeof(NumChunks), 1, wf ); + fwrite( t_FakeHeader, t_FakeHeaderSz+1, 1, wf ); + fwrite( t_CompChunk, t_CompChunkSz+1, 1, wf ); + delete [] t_FakeHeader; + delete [] t_CompChunk; + + fclose(wf); //close file. + fclose(f); //close file. + } + return true; +}; diff --git a/converter/source/cDeNotch.h b/converter/source/cDeNotch.h new file mode 100644 index 000000000..7701718a6 --- /dev/null +++ b/converter/source/cDeNotch.h @@ -0,0 +1,264 @@ +#pragma once + +#include + +class cDeNotch +{ +public: + + +cDeNotch(); + +int Converter ( std::string, std::string ); + +std::string mcrSource; +std::string pakOutput; + + +}; + +/* +using namespace std; + + +int main () { + + string dir; + DIR* dp; + struct dirent *entry; + int found; + string entrys; + string str2; + string str3; + string filexPos; + string filezPos; + string pak_name; + //string* dir_array; + int dir_num_files = 0; + int ctr = 0; + + if(dp = opendir("region/")){ + while(entry = readdir(dp)){ + entrys = entry->d_name; + found = entrys.find(".mcr"); + if ( (found!=string::npos) && (entry->d_type==8) ) { + str2 = entrys.substr (2,sizeof(entrys)); + filexPos = str2.substr (0,(int)str2.find(".")); + str3 = str2.substr ((int)str2.find(".")+1, sizeof(str2)); + filezPos = str3.substr (0,(int)str3.find(".")); + pak_name = "X" + filexPos + "_Z" + filezPos + ".pak"; + + clock_t begin=clock(); //start execution timer + + cDeNotch ( entrys, pak_name ); + + clock_t end=clock(); + cout << "Time to convert chunk: " << double(diffclock(end,begin)) << " Seconds"<< endl; + } + } + closedir(dp); + } + + return 0; +}; + + + +int cDeNotch ( std::string mcrSource, std::string pakOutput ) { + char SourceFile[128]; + char OutputFile[128]; + + + FILE* f = 0; + FILE* wf = 0; + #ifdef _WIN32 + sprintf_s(SourceFile, 128, "region/%s", mcrSource.c_str() ); //replace hard coded file with file array variable + sprintf_s(OutputFile, 128, "world/%s", pakOutput.c_str() ); //parce x and z from file array variable and place into pak file format + if( fopen_s(&wf, OutputFile, "wb" ) == 0 ) {} else { cout << "uhoh!" << endl; return false; } //open new pak file for writing + #else + sprintf(SourceFile, "region/%s", mcrSource.c_str() ); //same as above but for linux + sprintf(OutputFile, "world/%s", pakOutput.c_str() ); + if( (wf = fopen(OutputFile, "wb" )) != 0 ) {} else { cout << "uhoh!" << endl; return false; } + #endif + + + printf ("Now Converting %s to %s\n", mcrSource.c_str(), pakOutput.c_str() ); + if( (f = fopen(SourceFile, "rb" )) != 0 ) { // no error + + char* t_FakeHeader; + t_FakeHeader = new char[1*1024*1024]; //1MB Temp FakeHeader array + int t_FakeHeaderSz = -1; //Size of data in array + + char* t_CompChunk; + t_CompChunk = new char[5*1024*1024]; //5MB Temp Compressed Chunk Data array + int t_CompChunkSz = -1; //Size of data in array + + char PakVersion = 1; + char ChunkVersion = 1; + short NumChunks = 0; + + unsigned char byte1 = 0; + unsigned char byte2 = 0; + unsigned char byte3 = 0; + unsigned char byte4 = 0; + unsigned char byte5 = 0; + unsigned char trash = 0; + unsigned int frloc = 0; + + int toffset = 0; + int compdlength = 0; + + int toffarr[1024]; + + //loop through notch's header + for( short i = 0; i < 1024 ; ++i ) {//loop through first 4096 bytes of data, 4 bytes at a time + //Region files begin with an 8kiB header containing information about which chunks are present in the region file, when they were last updated, and where they can be found. The location in the region file of a chunk at (x, z) (in chunk coordinates) can be found at byte offset 4 * ((x mod 32) + (z mod 32) * 32) in its region file. Its timestamp can be found 4096 bytes later in the file. The remainder of the file consists of data for up to 1024 chunks, interspersed with an arbitrary amount of unused space. + //we are only using the first 4096 bytes. We don't need the timestamps right now. + if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { cout << "ERROR 21hs READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { cout << "ERROR ks93 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { cout << "ERROR 2s5f READING FROM FILE " << SourceFile; fclose(f); return false; }//first three bytes area big-endian representation of the chunk offsets in no particular order. + if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { cout << "ERROR dhj3 READING FROM FILE " << SourceFile; fclose(f); return false; }//we don't need to use this byte right now. + toffset = 4096 * ((byte1*256*256) + (byte2*256) + byte3);//find the chunk offsets using the first three bytes of each long; + toffarr[i] = toffset;//array of chunk offset locatiosn in the fle. + } + for ( short i = 0; i < 4096; i++ ) {//loop through next 4096 bytes of the header. + //keeping this code here in case we need it later. not using it right now. + if( fread( &trash, sizeof(byte4), 1, f) != 1 ) { cout << "ERROR 2jkd READING FROM FILE " << SourceFile; fclose(f); return false; } + } + frloc = 8192; //current location of fread is at 4096+ 4096 since we read through and collected important info from the header. + + cQuicksort Quick; + Quick.quicksort(toffarr, 0, 1023); //sort the array from smallest to larget offset locations so we only have to read through the file once. + + for ( short ia = 0; ia < 1024; ia++ ) {//a region file can hold a maximum of 1024 chunks (32*32) + if (ia < 3500 ) { //only run chunk # 3 + if (toffarr[ia] < 8192) { //offsets of less than 8192 are impossible. 0 means there is no chunk in a particular location. + if (toffarr[ia] > 0) { cout << "ERROR 2s31 IN COLLECTED CHUNK OFFSETS " << toffarr[ia]; fclose(f); return false; } //values between 0 and 8192 should be impossible. + //This file does not contain the max 1024 chunks, skip until we get to the first + } else { // found a chunk offset value + //Chunk data begins with a (big-endian) four-byte length field which indicates the exact length of the remaining chunk data in bytes. The following byte indicates the compression scheme used for chunk data, and the remaining (length-1) bytes are the compressed chunk data. + //printf("Working on chunk %i :: %i\n", ia, toffarr[ia]); + if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { cout << "ERROR 2t32 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { cout << "ERROR 2y51 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { cout << "ERROR 3424 READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { cout << "ERROR sd22 READING FROM FILE " << SourceFile; fclose(f); return false; } + compdlength = ((byte1*256*256*256) + (byte2*256*256) + (byte3*256) + byte4 - 0); //length of compressed chunk data + if( fread( &byte5, sizeof(byte5), 1, f) != 1 ) { cout << "ERROR 2341 READING FROM FILE " << SourceFile; fclose(f); return false; } //compression type, 1 = GZip (RFC1952) (unused in practice) , 2 = Zlib (RFC1950) + + frloc += 5; //moved ahead 5 bytes while reading data. + + char* compBlockData = new char[compdlength]; + if( fread( compBlockData, compdlength, 1, f) != 1 ) { cout << "ERROR rf22 READING FROM FILE " << SourceFile; fclose(f); return false; } + frloc = frloc + compdlength; + + uLongf DestSize = 128576;// uncompressed chunks should never be larger than this + + char* BlockData = new char[ DestSize ]; + + int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)compBlockData, compdlength ); //DestSize will update to the actual uncompressed data size after this opperation. + int testr = (int)DestSize; //testing something, can't remember what. + if( errorcode != Z_OK ){ + printf("ERROR: Decompressing chunk data! %i", errorcode ); + switch( errorcode ) + { + case Z_MEM_ERROR: + printf("Not enough memory"); + break; + case Z_BUF_ERROR: + printf("Not enough room in output buffer"); + break; + case Z_DATA_ERROR: + printf("Input data corrupted or incomplete"); + break; + default: + break; + }; + } + + NumChunks++; + cNBTData* NBTData = new cNBTData(BlockData, (int)DestSize); + NBTData->ParseData(); + //NBTData->PrintData(); + NBTData->OpenCompound(""); + NBTData->OpenCompound("Level"); // You need to open the right compounds before you can access the data in it + + //NBT Data for blocks should look something like this: + //==== STRUCTURED NBT DATA ==== + // COMPOUND ( ) + // COMPOUND + // COMPOUND (Level) + // LIST (Entities) + // LIST (TileEntities) + // INTEGER LastUpdate (0) + // INTEGER xPos (0) + // INTEGER zPos (0) + // BYTE TerrainPopulated (1) + // BYTE ARRAY BlockLight (length: 16384) + // BYTE ARRAY Blocks (length: 32768) + // BYTE ARRAY Data (length: 16384) + // BYTE ARRAY HeightMap (length: 256) + // BYTE ARRAY SkyLight (length: 16384) + //============================= + + int UncompressedChunkSz = (32768+16384+16384+16384); + char* UncompressedChunk = new char[ UncompressedChunkSz ]; + uLongf CompressedSize = compressBound( UncompressedChunkSz ); + char* CompressedChunk = new char[ CompressedSize ]; + int UnChunkArrLoc = 0; + int xPos = NBTData->GetInteger("xPos"); + int zPos = NBTData->GetInteger("zPos"); + + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &xPos, sizeof(int) );t_FakeHeaderSz += sizeof(int); + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &zPos, sizeof(int) );t_FakeHeaderSz += sizeof(int); + + + //todo: inserert json code and add it to chunk data + + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("Blocks"), 32768 );UnChunkArrLoc += 32768; + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("Data"), 16384 );UnChunkArrLoc += 16384; + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("BlockLight"), 16384 );UnChunkArrLoc += 16384; + memcpy( UncompressedChunk + UnChunkArrLoc, NBTData->GetByteArray("SkyLight"), 16384 );UnChunkArrLoc += 16384; + + errorcode = compress2( (Bytef*)CompressedChunk, &CompressedSize, (const Bytef*)UncompressedChunk, UncompressedChunkSz, Z_DEFAULT_COMPRESSION); + if( errorcode != Z_OK ) + { + printf("Error compressing data (%i)", errorcode ); + break; + } + + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &CompressedSize, sizeof(int) );t_FakeHeaderSz += sizeof(int); + memcpy( t_FakeHeader + t_FakeHeaderSz + 1, &UncompressedChunkSz, sizeof(int) );t_FakeHeaderSz += sizeof(int); + memcpy( t_CompChunk + t_CompChunkSz + 1, CompressedChunk, CompressedSize );t_CompChunkSz += CompressedSize; + + NBTData->CloseCompound();// Close the compounds after you're done + NBTData->CloseCompound(); + + delete [] UncompressedChunk; + delete [] CompressedChunk; + delete [] compBlockData; + delete [] BlockData; + //delete [] NBTData; + + while ( (frloc < toffarr[ia+1]) && (ia<1023) ) { //loop through Notch's junk data until we get to another chunk offset possition to start the loop again + if( fread( &trash, sizeof(byte4), 1, f) != 1 ) { cout << "ERROR 2nkd READING FROM FILE " << SourceFile; fclose(f); return false; } + frloc ++; + } + + } + } //only run chunk # 3 + + } + fwrite( &PakVersion, sizeof(PakVersion), 1, wf ); + fwrite( &ChunkVersion, sizeof(ChunkVersion), 1, wf ); + fwrite( &NumChunks, sizeof(NumChunks), 1, wf ); + fwrite( t_FakeHeader, t_FakeHeaderSz+1, 1, wf ); + fwrite( t_CompChunk, t_CompChunkSz+1, 1, wf ); + delete [] t_FakeHeader; + delete [] t_CompChunk; + + fclose(wf); //close file. + fclose(f); //close file. + } + return true; +}; +*/ diff --git a/converter/source/cNBTData.cpp b/converter/source/cNBTData.cpp new file mode 100644 index 000000000..556960732 --- /dev/null +++ b/converter/source/cNBTData.cpp @@ -0,0 +1,922 @@ +#include "cNBTData.h" +#include // memcpy +#include +#include "zlib.h" +#include +#include + +#ifndef _WIN32 +#include +#include +#endif + +#ifdef _WIN32 +#include +#endif + +cNBTData::~cNBTData() +{ + // TODO: Delete all compounds and stuff in it + Clear(); +} + +cNBTData::cNBTData( char* a_Buffer, unsigned int a_BufferSize ) + : cNBTCompound( 0 ) +{ + m_NumUnnamedElements = 0; + for(int i = 0; i < TAG_NumTags; i++) + { + m_ParseFunctions[i] = 0; + } + m_ParseFunctions[TAG_Byte] = &cNBTData::ParseByte; + m_ParseFunctions[TAG_Short] = &cNBTData::ParseShort; + m_ParseFunctions[TAG_Int] = &cNBTData::ParseInt; + m_ParseFunctions[TAG_Long] = &cNBTData::ParseLong; + m_ParseFunctions[TAG_Double] = &cNBTData::ParseDouble; + m_ParseFunctions[TAG_Float] = &cNBTData::ParseFloat; + m_ParseFunctions[TAG_String] = &cNBTData::ParseString; + m_ParseFunctions[TAG_List] = &cNBTData::ParseList; + m_ParseFunctions[TAG_Compound] = &cNBTData::ParseCompound; + m_ParseFunctions[TAG_ByteArray] = &cNBTData::ParseByteArray; + + + m_Buffer = a_Buffer; + m_BufferSize = a_BufferSize; + m_Index = 0; + + tm = false; //tm to true will print more information for test mode + if (m_BufferSize == 82659) { +// tm = true; + } + + m_CurrentCompound = this; + + m_bDecompressed = true; +} + +bool cNBTData::OpenCompound( std::string a_Name ) +{ + cNBTCompound* Compound = GetCompound( a_Name ); + if( Compound ) + { + m_CurrentCompound = Compound; + return true; + } + printf("WARNING: Could not open NBT Compound %s\n", a_Name.c_str() ); + return false; +} + +bool cNBTData::CloseCompound() +{ + if( m_CurrentCompound->GetParentCompound() ) + { + m_CurrentCompound = m_CurrentCompound->GetParentCompound(); + return true; + } + printf("WARNING: Could not close NBT Compound, already at root!\n" ); + return false; +} + +bool cNBTCompound::OpenList( std::string a_Name ) +{ + if( GetList( a_Name ) ) + { + m_CurrentList = GetList( a_Name ); + return true; + } + printf("WARNING: Could not open NBT List %s\n", a_Name.c_str() ); + return false; +} + +bool cNBTCompound::CloseList() +{ + if( m_CurrentList ) + { + m_CurrentList = m_CurrentList->GetParentList(); + return true; + } + printf("WARNING: Could not close NBT List, no list open!\n" ); + return false; +} + +bool cNBTData::OpenList( std::string a_Name ) +{ + return m_CurrentCompound->OpenList( a_Name ); +} + +bool cNBTData::CloseList() +{ + return m_CurrentCompound->CloseList(); +} + +void cNBTData::Compress() +{ + //printf("Before Compress size: %i\n", m_BufferSize );//re + const int MAXNBTSIZE = 1024 * 1024 * 120; + + int ret; + unsigned have; + z_stream strm; + unsigned char* Compressed = new unsigned char[MAXNBTSIZE]; + + /* allocate deflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = m_BufferSize; + strm.avail_out = MAXNBTSIZE; + strm.next_in =(Bytef*)m_Buffer; + strm.next_out = Compressed; + strm.total_in = 0; + strm.total_out = 0; + ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15+MAX_WBITS, 8, Z_DEFAULT_STRATEGY); + if (ret != Z_OK) + { + printf("deflateInit2 returned NOT OK\n"); + return; + } + + + + /* run deflate() on input until output buffer not full, finish + compression if all of source has been read in */ + + ret = deflate(&strm, Z_FULL_FLUSH); /* no bad return value */ + if( ret != Z_OK ) + { + printf("WARNING: deflate returned NOT OK\n"); + } + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + have = strm.total_out; + + assert(strm.avail_in == 0); /* all input will be used */ + + if( ret != Z_STREAM_END ) + { + //printf("WARNING: Compressing didn't go to end of stream\n");//re + } + + if(m_Buffer) + { + delete [] m_Buffer; + m_Buffer = 0; + } + + //printf("Compressed size: %i\n", have );//re + + m_BufferSize = have; + m_Buffer = new char[ m_BufferSize ]; + memcpy( m_Buffer, Compressed, m_BufferSize ); + delete Compressed; + + /* clean up and return */ + deflateEnd(&strm); + m_bDecompressed = false; + return; +} + +bool cNBTData::Decompress() +{ + if( m_bDecompressed ) + { + printf("WARNING: Decompress called, while it has already been decompressed\n"); + return false; + } + if( m_BufferSize == 0 ) + { + printf("WARNING: Decompress called, with m_BufferSize of 0\n"); + return false; + } + + //printf("Before Decompress size: %i\n", m_BufferSize );//re + + const int MAXNBTSIZE = 1024 * 1024 * 120 ; + + int ret; + z_stream strm; + unsigned char* out = new unsigned char[MAXNBTSIZE]; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = Z_NULL; + strm.next_in = Z_NULL; + strm.avail_in = m_BufferSize; + strm.avail_out = Z_NULL; + strm.next_in = (Bytef*)m_Buffer; + strm.next_out = Z_NULL; + strm.avail_out = MAXNBTSIZE; + strm.next_out = out; + strm.total_in = 0; + strm.total_out = 0; + + ret = inflateInit2(&strm, 16+MAX_WBITS); + if (ret != Z_OK) + { + printf("inflateInit2 returned NOT OK\n"); + delete out; + return false; + } + + if( (ret = inflate(&strm, Z_NO_FLUSH)) != Z_STREAM_END) + { + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + printf("ret != Z_STREAM_END\n"); + } + unsigned UncompressedSize = strm.total_out; //MAXNBTSIZE - strm.avail_out; + + m_Buffer = new char[ UncompressedSize ]; + memcpy( m_Buffer, out, UncompressedSize ); + m_BufferSize = UncompressedSize; + + inflateEnd(&strm); + delete [] out; + + if( ret != Z_STREAM_END ) + { + printf("WARNING: NBT Data received was too big! (More than %i bytes)\n", MAXNBTSIZE); + } + + //printf("Decompressed Size: %i\n", UncompressedSize );//re + m_bDecompressed = true; + return (ret == Z_STREAM_END) ? true : false; +} + +void cNBTCompound::AppendShort( std::string & a_Buffer, short a_Value ) +{ + a_Buffer.push_back( (char)((a_Value>>8)&0xff) ); + a_Buffer.push_back( (char)((a_Value)&0xff) ); +} + +void cNBTCompound::AppendInteger( std::string & a_Buffer, int a_Value ) +{ + int NetVal = htonl( a_Value ); + a_Buffer.append( (char*)&NetVal, sizeof( int ) ); +} + +void cNBTCompound::Serialize(std::string & a_Buffer) +{ + //printf("cNBTCompound::Serialize()\n");//re + for( CompoundMap::iterator itr = m_Compounds.begin(); itr != m_Compounds.end(); itr++ ) + { + if( itr->second == 0 ) continue; + a_Buffer.push_back( TAG_Compound ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + itr->second->Serialize( a_Buffer ); + a_Buffer.push_back( TAG_End ); + } + + for( ListMap::iterator itr = m_Lists.begin(); itr != m_Lists.end(); itr++ ) + { + if( itr->second == 0 ) continue; + a_Buffer.push_back( TAG_List ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + a_Buffer.push_back( (char)itr->second->GetType() ); + AppendInteger( a_Buffer, itr->second->GetSize() ); + itr->second->Serialize( a_Buffer ); + } + + for( IntegerMap::iterator itr = m_Integers.begin(); itr != m_Integers.end(); itr++ ) + { + a_Buffer.push_back( TAG_Int ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + AppendInteger( a_Buffer, itr->second ); + } + + for( ShortMap::iterator itr = m_Shorts.begin(); itr != m_Shorts.end(); itr++ ) + { + a_Buffer.push_back( TAG_Short ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + AppendShort( a_Buffer, itr->second ); + } + + for( ByteMap::iterator itr = m_Bytes.begin(); itr != m_Bytes.end(); itr++ ) + { + a_Buffer.push_back( TAG_Byte ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + a_Buffer.push_back( itr->second ); + } + + for( DoubleMap::iterator itr = m_Doubles.begin(); itr != m_Doubles.end(); itr++ ) + { + a_Buffer.push_back( TAG_Double ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + a_Buffer.push_back( itr->second ); + } + + for( FloatMap::iterator itr = m_Floats.begin(); itr != m_Floats.end(); itr++ ) + { + a_Buffer.push_back( TAG_Float ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + a_Buffer.push_back( itr->second ); + } + + for( LongMap::iterator itr = m_Longs.begin(); itr != m_Longs.end(); itr++ ) + { + a_Buffer.push_back( TAG_Long ); + AppendShort( a_Buffer, (short)itr->first.size() ); + if( itr->first.size() > 0 ) + { + a_Buffer.append( itr->first.c_str(), itr->first.size() ); + } + a_Buffer.push_back( itr->second ); + } + + +} + +void cNBTCompound::PrintData( int a_Depth, std::string a_Name ) +{ + char* Prefix = new char[a_Depth*4+1]; + for(int i = 0; i < a_Depth*4; i++) + Prefix[i] = ' '; + Prefix[ a_Depth*4 ] = 0; + + if( a_Name.size() > 0 ) + printf("%sCOMPOUND (%s)\n", Prefix, a_Name.c_str() ); + else + printf("%sCOMPOUND (...)\n", Prefix ); + + delete Prefix; + a_Depth++; + Prefix = new char[a_Depth*4]; + for(int i = 0; i < a_Depth*4; i++) + Prefix[i] = ' '; + Prefix[ a_Depth*4-1 ] = 0; + + for( CompoundMap::iterator itr = m_Compounds.begin(); itr != m_Compounds.end(); itr++ ) + { + if( itr->second == 0 ) continue; + itr->second->PrintData( a_Depth, itr->first ); + } + + for( ListMap::iterator itr = m_Lists.begin(); itr != m_Lists.end(); itr++) + { + if( itr->second == 0 ) continue; + itr->second->PrintData( a_Depth, itr->first ); + } + + for( StringMap::iterator itr = m_Strings.begin(); itr != m_Strings.end(); itr++ ) + { + printf("%s STRING %s (%s)\n", Prefix, itr->first.c_str(), itr->second.c_str() ); + } + + for( IntegerMap::iterator itr = m_Integers.begin(); itr != m_Integers.end(); itr++ ) + { + printf("%s INTEGER %s (%i)\n", Prefix, itr->first.c_str(), itr->second ); + } + + for( ShortMap::iterator itr = m_Shorts.begin(); itr != m_Shorts.end(); itr++ ) + { + printf("%s SHORT %s (%i)\n", Prefix, itr->first.c_str(), itr->second ); + } + + for( FloatMap::iterator itr = m_Floats.begin(); itr != m_Floats.end(); itr++ ) + { + printf("%s FLOAT %s (%f)\n", Prefix, itr->first.c_str(), itr->second ); + } + + for( LongMap::iterator itr = m_Longs.begin(); itr != m_Longs.end(); itr++ ) + { + printf("%s LONG %s (%lli)\n", Prefix, itr->first.c_str(), itr->second ); + } + + for( DoubleMap::iterator itr = m_Doubles.begin(); itr != m_Doubles.end(); itr++ ) + { + printf("%s Double %s (%f)\n", Prefix, itr->first.c_str(), itr->second ); + } + + for( ByteMap::iterator itr = m_Bytes.begin(); itr != m_Bytes.end(); itr++ ) + { + printf("%s BYTE %s (%i)\n", Prefix, itr->first.c_str(), itr->second ); + } + + for( ByteArrayMap::iterator itr = m_ByteArrays.begin(); itr != m_ByteArrays.end(); itr++ ) + { + printf("%s BYTE ARRAY %s (length: %li)\n", Prefix, itr->first.c_str(), sizeof(itr->second) ); + } + + delete Prefix; +} + +void cNBTData::PrintData() +{ + printf("==== STRUCTURED NBT DATA ====\n"); + m_CurrentCompound->PrintData( 0, " " ); + printf("=============================\n"); +} + +void cNBTData::Serialize() +{ + std::string Buffer; + m_CurrentCompound->Serialize( Buffer ); + + if( m_Buffer ) + delete m_Buffer; + m_Buffer = new char[Buffer.size()]; + memcpy( m_Buffer, Buffer.c_str(), Buffer.size() ); + m_BufferSize = Buffer.size(); + + //printf("m_BufferSize1: %i\n", m_BufferSize);//re + + //for(unsigned int i = 0; i < m_BufferSize; i++)//re + //{//re + // printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );//re + //}//re +} + +void cNBTData::ParseData() +{ + if(!m_bDecompressed) + { + printf("WARNING: ParseData() called while data was not decompressed\n"); + return; + } + + m_Index = 0; + //printf("m_BufferSize2: %i\n", m_BufferSize);//re + //printf("cNBTData::ParseData()\n");//re + //for(unsigned int i = 0; i < m_BufferSize; i++)//re + //for(unsigned int i = 0; i < 70; i++)//re + //{//re + // printf("echo%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );//re + //}//re + + while( m_Index < m_BufferSize ) + { + if (tm) { + printf("m_BufferSize3: %i\n", m_BufferSize); + printf("m_Index: %i\n", m_Index); + } + ParseTags(); + } +} + +void cNBTData::ParseTags() +{ + if( m_Index < m_BufferSize ) + { + //printf("ParseTags idx:%02i %02x %3i %c\n", m_Index, (unsigned char)m_Buffer[m_Index], (unsigned char)m_Buffer[m_Index], m_Buffer[m_Index] );//re + ENUM_TAG Tag = (ENUM_TAG)m_Buffer[m_Index]; + if( Tag > 0 && m_ParseFunctions[ Tag ] ) + { + //printf("m_BufferSize4: %i\n", m_BufferSize); + //printf("m_Index1: %i\n\n\n\n", m_Index); + + m_Index++; + if (tm) { + printf("Tag: %i\n", Tag); + } + (*this.*m_ParseFunctions[ Tag ])(true); + } + else if( Tag == TAG_End ) + { + if (tm) { + printf("Tag End\n"); + int n; + std::cin >> n; + } + m_Index++; + } + else + { + printf("UNKNOWN TAG %x\n", m_Buffer[m_Index] ); + for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+10 && i < m_BufferSize; i++) + { + printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); + } + m_Index = m_BufferSize; + return; + } + } +} + +void cNBTData::ParseCompound( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + //printf("OPEN COMPOUND: %s\n", Name.c_str() );//re + + PutCompound( Name ); + OpenCompound( Name ); + while( m_Index < m_BufferSize && m_Buffer[ m_Index ] != TAG_End ) + { + ParseTags(); + } + CloseCompound(); + m_Index++; + //printf("CLOSE COMPOUND\n");//re +} + +void cNBTData::ParseList( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + ENUM_TAG TagType = (ENUM_TAG)ReadByte(); + int Length = ReadInt(); + //printf("LIST: %s Type: %02x Length: %i\n", Name.c_str(), TagType, Length );//re + + //for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+10 && i < m_BufferSize; i++)//re + //{//re + //printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );//re + //}//re + + if (tm) { + printf("List Name, tag, length: %s, %i, %i\n", Name.c_str(), (int)TagType, Length); + } + + PutList( Name, TagType ); + OpenList( Name ); + for(int i = 0; i < Length && m_Index < m_BufferSize; i++) + { + if( m_ParseFunctions[ TagType ] ) + { + (*this.*m_ParseFunctions[ TagType ] )(false); + } + } + if (tm) { + printf("List Done Name, tag, length: %s, %i, %i\n", Name.c_str(), (int)TagType, Length); + } + + CloseList(); + +} + +void cNBTData::ParseByte( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + char Value = ReadByte(); + + PutByte( Name, Value ); + if (tm) { + printf("BYTE: %s %i\n", Name.c_str(), Value );//re + } +} + +void cNBTData::ParseShort( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + short Value = ReadShort(); + + PutShort( Name, Value ); + if (tm) { + printf("SHORT: %s %i\n", Name.c_str(), Value );//re + } +} + +void cNBTData::ParseInt( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + int Value = ReadInt(); + + PutInteger( Name, Value ); + if (tm) { + printf("INT: %s %i\n", Name.c_str(), Value );//re + } +} + +void cNBTData::ParseLong( bool a_bNamed ) +{ + if (tm) { + for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+30 && i < m_BufferSize; i++) { + printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); + } + } + std::string Name; + if( a_bNamed ) Name = ReadName(); + long long Value = ReadLong(); + + //PutInteger( Name, (int)Value ); + PutLong( Name, Value ); + if (tm) { + printf("LONG: %s %lli\n", Name.c_str(), Value );//re + } +} + +void cNBTData::ParseDouble( bool a_bNamed ) +{ + if (tm) { + for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+30 && i < m_BufferSize; i++) { + printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); + } + } + std::string Name; + if( a_bNamed ) Name = ReadName(); + double Value = ReadDouble(); + + //PutInteger( Name, (int)Value ); + PutDouble( Name, Value ); + if (tm) { + printf("Double: %s %f\n", Name.c_str(), Value );//re + } +} + +void cNBTData::ParseFloat( bool a_bNamed ) +{ + if (tm) { + for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+30 && i < m_BufferSize; i++) { + printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); + } + } + std::string Name; + if( a_bNamed ) Name = ReadName(); + float Value = ReadFloat(); + + //PutInteger( Name, (int)Value ); + PutFloat( Name, Value ); + if (tm) { + printf("Float: %s %f\n", Name.c_str(), Value );//re + } +} + +void cNBTData::ParseString( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + std::string String = ReadName(); + + PutString( Name, String ); + if (tm) { + printf("STRING: %s (%s)\n", Name.c_str(), String.c_str() );//re + } +} + +void cNBTData::ParseByteArray( bool a_bNamed ) +{ + + std::string Name; + if( a_bNamed ) Name = ReadName(); + + int Length = ReadInt(); + std::string String; + + char* ByteArray = new char[ Length ]; + if( Length > 0 ) + { + memcpy( ByteArray, &m_Buffer[ m_Index ], Length ); + m_Index += Length; + } + + PutByteArray( Name, ByteArray ); + + if (tm) { + for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+10 && i < m_BufferSize; i++) { + printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); + } + } +} + +std::string cNBTData::ReadName() +{ + //printf("crui1 \n"); + short Length = ReadShort(); + + //printf("crui Length: %i\n", Length); + std::string Name; + if( Length > 0 ) + { + for(int i = 0; i < Length; i++, m_Index++) + { + Name.push_back( m_Buffer[m_Index] ); + } + } + return Name; +} + +char cNBTData::ReadByte() +{ + unsigned char Byte = m_Buffer[ m_Index ]; m_Index++; + return Byte; +} + +short cNBTData::ReadShort() +{ + short Length = 0; + Length |= m_Buffer[ m_Index ] << 8; m_Index++; + Length |= m_Buffer[ m_Index ]; m_Index++; + return Length; +} + +int cNBTData::ReadInt() +{ + int Value = 0; + memcpy( &Value, m_Buffer+m_Index, sizeof(int) ); + m_Index+=sizeof(int); + + return ntohl( Value ); +} + +long long cNBTData::ReadLong() +{ + if (tm) { + printf( "here1 : %i, m_Index: %i\n", (int)sizeof(long long), (int)m_Index ); + } + long long Value = 0; + memcpy( &Value, m_Buffer+m_Index, sizeof(long long) ); + m_Index+=sizeof(long long); + if (tm) { + printf( "here2 : %i, m_Index: %i\n", (int)sizeof(long long), (int)m_Index ); + } + return Value; +} + +double cNBTData::ReadDouble() +{ + double Value = 0; + memcpy( &Value, m_Buffer+m_Index, sizeof(double) ); + m_Index+=sizeof(double); + + return Value; +} + +float cNBTData::ReadFloat() +{ + float Value = 0; + memcpy( &Value, m_Buffer+m_Index, sizeof(float) ); + m_Index+=sizeof(float); + + return Value; +} + +void cNBTCompound::PutList( std::string Name, ENUM_TAG Type ) +{ + m_Lists[Name] = new cNBTList( m_CurrentList, Type ); +} + +void cNBTCompound::PutCompound( std::string Name ) +{ + if( m_CurrentList ) + { + m_CurrentList->AddToList( new cNBTCompound( this ) ); + } + else + { + m_Compounds[Name] = new cNBTCompound( this ); + } +} + +void cNBTCompound::PutFloat( std::string Name, float Value ) +{ + if( m_CurrentList ) + m_CurrentList->AddToList( (void*)((unsigned int*)&Value) ); + else + m_Floats[Name] = Value; +} + +cNBTCompound* cNBTCompound::GetCompound( std::string Name ) +{ + if( m_CurrentList ) + { + if( m_CurrentList->GetType() != TAG_Compound ) + return 0; + + return (cNBTCompound*)m_CurrentList->GetLastElement(); + } + return m_Compounds[Name]; +} + +void cNBTList::PrintData(int a_Depth, std::string a_Name) +{ + char* Prefix = new char[a_Depth*4]; + for(int i = 0; i < a_Depth*4; i++) + Prefix[i] = ' '; + Prefix[ a_Depth*4-1 ] = 0; + + if( a_Name.size() > 0 ) + printf("%s LIST (%s)\n", Prefix, a_Name.c_str() ); + else + printf("%s LIST\n", Prefix ); + + delete [] Prefix; + + for( VoidList::iterator itr = m_List.begin(); itr != m_List.end(); itr++) + { + switch( m_Type ) + { + case cNBTCompound::TAG_Compound: + { + ((cNBTCompound*)*itr)->PrintData(a_Depth+1, "..."); + } + break; + default: + break; + } + } +} + +void cNBTList::Serialize(std::string & a_Buffer) +{ + for( VoidList::iterator itr = m_List.begin(); itr != m_List.end(); itr++ ) + { + switch( m_Type ) + { + case cNBTCompound::TAG_Compound: + { + ((cNBTCompound*)(*itr))->Serialize( a_Buffer ); + a_Buffer.push_back( cNBTCompound::TAG_End ); + } + break; + default: + break; + } + } +} + +void cNBTData::Clear() +{ + while( m_CurrentCompound != this ) CloseCompound(); + m_CurrentCompound->Clear(); + + if( m_Buffer ) + { + delete m_Buffer; + m_Buffer = 0; + } + m_BufferSize = 0; +} + +void cNBTCompound::Clear() +{ + for( CompoundMap::iterator itr = m_Compounds.begin(); itr != m_Compounds.end(); itr++ ) + { + if( itr->second == 0 ) continue; + itr->second->Clear(); + delete itr->second; + itr->second = 0; + } + m_Compounds.clear(); + + for( ListMap::iterator itr = m_Lists.begin(); itr != m_Lists.end(); itr++ ) + { + if( itr->second == 0 ) continue; + itr->second->Clear(); + delete itr->second; + itr->second = 0; + } + m_Lists.clear(); + m_Bytes.clear(); + m_Shorts.clear(); + m_Integers.clear(); + m_Strings.clear(); +} + +void cNBTList::Clear() +{ + for( VoidList::iterator itr = m_List.begin(); itr != m_List.end(); itr++) + { + switch( m_Type ) + { + case cNBTCompound::TAG_Compound: + { + cNBTCompound* Compound = (cNBTCompound*)(*itr); + Compound->Clear(); + delete Compound; + *itr = 0; + } + break; + case cNBTCompound::TAG_List: + { + cNBTList* List = (cNBTList*)(*itr); + List->Clear(); + delete List; + *itr = 0; + } + break; + default: + break; + } + } + m_List.clear(); +} diff --git a/converter/source/cNBTData.h b/converter/source/cNBTData.h new file mode 100644 index 000000000..7d80aacb9 --- /dev/null +++ b/converter/source/cNBTData.h @@ -0,0 +1,198 @@ +#pragma once + +#include +#include +#include + +class cNBTList; +class cNBTData; + +class cNBTCompound +{ +public: + cNBTCompound( cNBTCompound* a_ParentCompound ) : m_ParentCompound( a_ParentCompound ), m_CurrentList(0) { } + virtual ~cNBTCompound() {} +public: +#ifdef _WIN32 + enum ENUM_TAG : unsigned char +#else + enum ENUM_TAG +#endif + { + TAG_End = 0, + TAG_Byte = 1, + TAG_Short = 2, + TAG_Int = 3, + TAG_Long = 4, + TAG_Float = 5, + TAG_Double = 6, + TAG_ByteArray = 7, + TAG_String = 8, + TAG_List = 9, + TAG_Compound = 10, + TAG_NumTags // Not a real tag, but contains number of tags + }; + + void Clear(); + + void PutByte( std::string Name, char Value ) { m_Bytes[Name] = Value; } + void PutShort( std::string Name, short Value ) { m_Shorts[Name] = Value; } + void PutInteger( std::string Name, int Value ) { m_Integers[Name] = Value; } + void PutLong( std::string Name, long long Value ) { m_Longs[Name] = Value; } + void PutDouble( std::string Name, double Value ) { m_Doubles[Name] = Value; } + void PutFloat( std::string Name, float Value ); + void PutString( std::string Name, std::string Value ) { m_Strings[Name] = Value; } + void PutByteArray( std::string Name, char* ByteArray ) { m_ByteArrays[Name] = ByteArray; } + void PutCompound( std::string Name ); + void PutList( std::string Name, ENUM_TAG Type ); + + char GetByte( std::string Name ) { return m_Bytes[Name]; } + short GetShort( std::string Name ) { return m_Shorts[Name]; } + int GetInteger( std::string Name ) { return m_Integers[Name]; } + long long GetLong( std::string Name ) { return m_Longs[Name]; } + double GetDouble( std::string Name ) { return m_Doubles[Name]; } + float GetFloat( std::string Name ) { return m_Floats[Name]; } + std::string GetString( std::string Name ) { return m_Strings[Name]; } + char* GetByteArray( std::string Name ) { return m_ByteArrays[Name]; } + cNBTCompound* GetCompound( std::string Name ); + cNBTList* GetList( std::string Name ) { return m_Lists[Name]; } + + cNBTList* GetCurrentList() { return m_CurrentList; } + cNBTCompound* GetParentCompound() { return m_ParentCompound; } + + bool OpenList( std::string a_Name ); + bool CloseList(); + + void Serialize(std::string & a_Buffer); + + void PrintData( int a_Depth, std::string a_Name ); +private: + void AppendShort( std::string & a_Buffer, short a_Value ); + void AppendInteger( std::string & a_Buffer, int a_Value ); + + cNBTCompound* m_ParentCompound; + cNBTList* m_CurrentList; + + typedef std::map ByteMap; + typedef std::map ShortMap; + typedef std::map IntegerMap; + typedef std::map LongMap; + typedef std::map DoubleMap; + typedef std::map FloatMap; + typedef std::map StringMap; + typedef std::map ByteArrayMap; + typedef std::map CompoundMap; + typedef std::map ListMap; + ByteMap m_Bytes; + ShortMap m_Shorts; + IntegerMap m_Integers; + LongMap m_Longs; + DoubleMap m_Doubles; + FloatMap m_Floats; + StringMap m_Strings; + ByteArrayMap m_ByteArrays; + CompoundMap m_Compounds; + ListMap m_Lists; +}; + +class cNBTList +{ +public: + cNBTList( cNBTList* a_ParentList, cNBTCompound::ENUM_TAG a_Type ) : m_ParentList( a_ParentList ), m_Type( a_Type ) {} + void AddToList( void* a_Item ) { m_List.push_back( a_Item ); } + void* GetLastElement() { return m_List.back(); } + cNBTCompound::ENUM_TAG GetType() { return m_Type; } + cNBTList* GetParentList() { return m_ParentList; } + + unsigned int GetSize() { return m_List.size(); } + + void Serialize(std::string & a_Buffer); + void PrintData(int a_Depth, std::string a_Name); + typedef std::list VoidList; + VoidList GetList() { return m_List; } + + void Clear(); +private: + cNBTList* m_ParentList; + cNBTCompound::ENUM_TAG m_Type; + VoidList m_List; +}; + +class cNBTData : public cNBTCompound +{ +public: + cNBTData( char* a_Buffer, unsigned int a_BufferSize ); + virtual ~cNBTData(); + + void Clear(); + + void PrintData(); + + void ParseData(); + + bool OpenCompound( std::string a_Name ); + bool CloseCompound(); + + bool OpenList( std::string a_Name ); + bool CloseList(); + + void PutByte( std::string Name, char Value ) { m_CurrentCompound->PutByte( Name, Value ); } + void PutShort( std::string Name, short Value ) { m_CurrentCompound->PutShort( Name, Value ); } + void PutInteger( std::string Name, int Value ) { m_CurrentCompound->PutInteger( Name, Value ); } + void PutLong( std::string Name, long long Value ) { m_CurrentCompound->PutLong( Name, Value ); } + void PutDouble( std::string Name, double Value ) { m_CurrentCompound->PutDouble( Name, Value ); } + void PutFloat( std::string Name, float Value ) { m_CurrentCompound->PutFloat( Name, Value ); } + void PutString( std::string Name, std::string Value ) { m_CurrentCompound->PutString( Name, Value ); } + void PutByteArray( std::string Name, char* ByteArray ) { m_CurrentCompound->PutByteArray( Name, ByteArray ); } + void PutCompound( std::string Name ) { m_CurrentCompound->PutCompound( Name ); } + void PutList( std::string Name, ENUM_TAG Type ) { m_CurrentCompound->PutList( Name, Type ); } + + int GetInteger( std::string Name ) { return m_CurrentCompound->GetInteger(Name); } + long long GetLong( std::string Name ) { return m_CurrentCompound->GetLong(Name); } + double GetDouble( std::string Name ) { return m_CurrentCompound->GetDouble(Name); } + float GetFloat( std::string Name ) { return m_CurrentCompound->GetFloat(Name); } + std::string GetString( std::string Name ) { return m_CurrentCompound->GetString(Name); } + char* GetByteArray( std::string Name ) { return m_CurrentCompound->GetByteArray(Name); } + cNBTCompound* GetCompound( std::string Name ) { return m_CurrentCompound->GetCompound(Name); } + cNBTList* GetList( std::string Name ) { return m_CurrentCompound->GetList(Name); } + + char* GetBuffer() { return m_Buffer; } + unsigned int GetBufferSize() { return m_BufferSize; } + + void Compress(); + bool Decompress(); + + void Serialize(); +private: + int m_NumUnnamedElements; + bool m_bDecompressed; + + void ParseTags(); + void ParseCompound( bool a_bNamed ); + void ParseList( bool a_bNamed ); + void ParseString( bool a_bNamed ); + void ParseByte( bool a_bNamed ); + void ParseByteArray( bool a_bNamed ); + void ParseInt( bool a_bNamed ); + void ParseLong( bool a_bNamed ); + void ParseDouble( bool a_bNamed ); + void ParseFloat( bool a_bNamed ); + void ParseShort( bool a_bNamed ); + + short ReadShort(); + std::string ReadName(); + char ReadByte(); + int ReadInt(); + long long ReadLong(); + double ReadDouble(); + float ReadFloat(); + + cNBTCompound* m_CurrentCompound; + + char* m_Buffer; + unsigned int m_BufferSize; + unsigned int m_Index; + bool tm; + + void (cNBTData::*m_ParseFunctions[TAG_NumTags])(bool); +}; diff --git a/converter/source/cQuicksort.cpp b/converter/source/cQuicksort.cpp new file mode 100644 index 000000000..8a00805eb --- /dev/null +++ b/converter/source/cQuicksort.cpp @@ -0,0 +1,67 @@ +#include "cQuicksort.h" +#include + + + +// Quicksort controller function, it partitions the different pieces of our array. +void cQuicksort::quicksort(int *arIntegers, int left, int right) +{ + if (right > left) + { + int pivotIndex = median3(arIntegers,left,right); + int pivotNewIndex = partition(arIntegers, left, right, pivotIndex); + + // Recursive call to quicksort to sort each half. + quicksort(arIntegers, left, pivotNewIndex-1); + quicksort(arIntegers, pivotNewIndex+1, right); + } +} + +int cQuicksort::median3(int *arIntegers,int left,int right) +{ + int center = (left+right)/2; + + if(arIntegers[center] < arIntegers[left]) + swap(arIntegers[left],arIntegers[center]); + if(arIntegers[right] < arIntegers[left]) + swap(arIntegers[left],arIntegers[right]); + if(arIntegers[right] < arIntegers[center]) + swap(arIntegers[center],arIntegers[right]); + + swap(arIntegers[center],arIntegers[right-1]); + + return center; +} + +// This function takes an array (or one half an array) and sorts it. +// It then returns a new pivot index number back to quicksort. +int cQuicksort::partition(int *arIntegers, int left, int right, int pivot) +{ + int pivotValue = arIntegers[pivot]; + + // Swap it out all the way to the end of the array + // So we know where it always is. + swap(arIntegers[pivot], arIntegers[right]); + int storeIndex = left; + + // Move through the array from start to finish comparing each to our + // pivot value (not index, the value that was located at the pivot index) + for (int i = left; i < right; i++) + { + if (arIntegers[i] <= pivotValue) + { + swap(arIntegers[i], arIntegers[storeIndex]); + storeIndex++; + } + } + swap(arIntegers[storeIndex], arIntegers[right]); + return storeIndex; +} + +// Simple swap function for our in place swapping. +void cQuicksort::swap(int &val1, int &val2) +{ + int temp = val1; + val1 = val2; + val2 = temp; +} diff --git a/converter/source/cQuicksort.h b/converter/source/cQuicksort.h new file mode 100644 index 000000000..b9327bd7d --- /dev/null +++ b/converter/source/cQuicksort.h @@ -0,0 +1,16 @@ +#pragma once + + +class cQuicksort { + + +public: + void quicksort(int*, int, int); + + +private: + int partition(int*, int, int, int); + int median3(int*,int,int); + void swap(int &, int &); + +}; diff --git a/converter/source/cTimer.cpp b/converter/source/cTimer.cpp new file mode 100644 index 000000000..6e6314d10 --- /dev/null +++ b/converter/source/cTimer.cpp @@ -0,0 +1,9 @@ +#include "cTimer.h" + +double cTimer::diffclock(clock_t clock1,clock_t clock2) +{ + double diffticks=clock1-clock2; + double diffms=(diffticks*1)/CLOCKS_PER_SEC; + return diffms; +} + diff --git a/converter/source/cTimer.h b/converter/source/cTimer.h new file mode 100644 index 000000000..44523f523 --- /dev/null +++ b/converter/source/cTimer.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +class cTimer +{ +public: + cTimer() + {} + + double diffclock(clock_t, clock_t); + +}; diff --git a/converter/source/main.cpp b/converter/source/main.cpp new file mode 100644 index 000000000..7f415d896 --- /dev/null +++ b/converter/source/main.cpp @@ -0,0 +1,52 @@ +#include +#include "cNBTData.h" +#include "cTimer.h" +#include "cQuicksort.h" +#include "cDeNotch.h" +#include + +int main () { + + cTimer Timer; + clock_t progBegin = clock(); //start main program timer + std::string dir; + DIR* dp; + struct dirent *entry; + int found; + std::string entrys; + std::string str2; + std::string str3; + std::string filexPos; + std::string filezPos; + std::string pak_name; + //string* dir_array; + int dir_num_files = 0; + int ctr = 0; + + if(dp = opendir("region/")){ + while(entry = readdir(dp)){ + entrys = entry->d_name; + found = entrys.find(".mcr"); + if ( (found!=std::string::npos) && (entry->d_type==8) ) { + str2 = entrys.substr (2,sizeof(entrys)); + filexPos = str2.substr (0,(int)str2.find(".")); + str3 = str2.substr ((int)str2.find(".")+1, sizeof(str2)); + filezPos = str3.substr (0,(int)str3.find(".")); + pak_name = "X" + filexPos + "_Z" + filezPos + ".pak"; + + clock_t begin=clock(); //start execution timer + + cDeNotch DeNotch; + DeNotch.Converter ( entrys, pak_name ); + + + clock_t end=clock(); + std::cout << "Time to convert chunk: " << double(Timer.diffclock(end,begin)) << " Seconds"<< std::endl; + } + } + closedir(dp); + } + clock_t progEnd = clock(); //end main program timer + std::cout << "Time to complete converter: " << double(Timer.diffclock(progEnd,progBegin)) << " Seconds"<< std::endl; + return 0; +}; -- cgit v1.2.3