diff options
author | admin@omencraft.com <admin@omencraft.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-10-29 23:19:06 +0200 |
---|---|---|
committer | admin@omencraft.com <admin@omencraft.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2011-10-29 23:19:06 +0200 |
commit | 625a4accdad2e6acfe1135634cb32ee149415645 (patch) | |
tree | 8c4dbd53b335ef9f5d3af6bc91c70ca92e1be6da /converter | |
parent | Fixed a memory leak (diff) | |
download | cuberite-625a4accdad2e6acfe1135634cb32ee149415645.tar cuberite-625a4accdad2e6acfe1135634cb32ee149415645.tar.gz cuberite-625a4accdad2e6acfe1135634cb32ee149415645.tar.bz2 cuberite-625a4accdad2e6acfe1135634cb32ee149415645.tar.lz cuberite-625a4accdad2e6acfe1135634cb32ee149415645.tar.xz cuberite-625a4accdad2e6acfe1135634cb32ee149415645.tar.zst cuberite-625a4accdad2e6acfe1135634cb32ee149415645.zip |
Diffstat (limited to 'converter')
-rw-r--r-- | converter/cConvert.cpp | 310 | ||||
-rw-r--r-- | converter/cNBTData.cpp | 700 | ||||
-rw-r--r-- | converter/cNBTData.h | 162 | ||||
-rwxr-xr-x | converter/denotch | bin | 0 -> 143870 bytes | |||
-rw-r--r-- | converter/region/r.0.0.mcr | bin | 0 -> 4939776 bytes |
5 files changed, 1172 insertions, 0 deletions
diff --git a/converter/cConvert.cpp b/converter/cConvert.cpp new file mode 100644 index 000000000..788399dcf --- /dev/null +++ b/converter/cConvert.cpp @@ -0,0 +1,310 @@ +// reading a complete binary file +#include <iostream> +#include <fstream> +#include <string> +#include <stdio.h> +#include <string.h> +#include <ctype.h> +#include "zlib.h" +#include <time.h> +#include "cNBTData.cpp" + +void quicksort(int*, int, int); +int partition(int*, int, int, int); +int median3(int*,int,int); +void swap(int &, int &); +double diffclock(clock_t, clock_t); + +using namespace std; + + +int main () { + + char SourceFile[128]; + char OutputFile[128]; + //ifstream file ("region/r.0.0.mcr", ios::in|ios::binary|ios::ate); + + clock_t begin=clock(); //start execution timer + + //need to add code to search through region/ directory and load contents of filenames into array. + //for each file in array do the following: + FILE* f = 0; + FILE* wf = 0; + #ifdef _WIN32 + sprintf_s(SourceFile, 128, "region/%s","r.0.0.mcr"); //replace hard coded file with file array variable + sprintf_s(OutputFile, 128, "world/%s","X0_Z0.pak"); //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 0; } //open new pak file for writing + #else + sprintf(SourceFile, "region/%s","r.0.0.mcr"); //same as above but for linux + sprintf(OutputFile, "world/%s","X0_Z0.pak"); + if( (wf = fopen(OutputFile, "wb" )) != 0 ) {} else { cout << "uhoh!" << endl; return 0; } + #endif + + + if( (f = fopen(SourceFile, "rb" )) != 0 ) { // no error + + //loop through notch's header + int n; + 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; + //string comp_data; + string ucomp_date; + int toffarr[1024]; + //unsigned char *BlockData; + //unsigned char *comp_data; + + //cout << sizeof(byte1) << endl; + //return 0; + 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. + 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 (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. + 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) + //printf("byte1: %x \n", byte1); + //printf("byte2: %x \n", byte2); + //printf("byte3: %x \n", byte3); + //printf("byte4: %x \n", byte4); + + frloc += 5; //moved ahead 5 bytes while reading data. + //cout << compdlength << endl; return 1; + //unsigned char* comp_data = new unsigned char[ compdlength ]; + //cout << "size of comp_data: " << compdlength << endl; + //cout << "size of comp_data2: " << sizeof(comp_data) << endl; + + //fread( comp_data, sizeof(unsigned char), compdlength, f); + //if( fread( &comp_data, sizeof(unsigned char), compdlength, f) != 1 ) { cout << "ERROR 1234 READING FROM FILE " << SourceFile; fclose(f); return false; } //actual compressed chunk data + //cout << "frloc: " << frloc << endl; + + + + char temparr[compdlength]; //can't get fread to read more than one char at a time into a char array... so that's what I'll do. :( At least it works. + int re = 0; + char tempbyte = 0; + while (re < compdlength) { //loop through file and read contents into char array a byte at a time. + if( fread( &tempbyte, sizeof(tempbyte), 1, f) != 1 ) { cout << "ERROR rf22 READING FROM FILE " << SourceFile; fclose(f); return false; } + temparr[re] = tempbyte; + re++; + frloc++; + } + + + + //if( fread( comp_data, compdlength, sizeof(unsigned char), f) != 1 ) { cout << "ERROR 1234 READING FROM FILE " << SourceFile <<endl; fclose(f); return false; } //actual compressed chunk data + //frloc += compdlength; + //cout << "frloc: " << frloc << endl; + //return 1; + //cout << deflateBound(&comp_data,compdlength) << endl; + uLongf DestSize = 98576;// uncompressed chunks should never be larger than this + //cout << "echo1: " << DestSize << endl; + char* BlockData = new char[ DestSize ]; + //return 1; + //cout << "size of comp_data1: " << sizeof(comp_data) << endl; + //int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)comp_data, compdlength ); + int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)temparr, compdlength ); //DestSize will update to the actual uncompressed data size after this opperation. + //cout << "echo2: " << DestSize << endl; + //cout << "echo3: " << errorcode << endl; + //cout << "size of Block data: " << sizeof(BlockData) << endl; + //int errorcode = 1; + 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; + }; + } + + //cout << "1" << endl; + //cout << comp_data << endl; + //return 0; + + + //playing with FakeTruth's NBT parser. (unsuccessfully) + //string BlockDataString(BlockData); + //memcpy (BlockDataString,BlockData,strlen(BlockData)+1); + //BlockDataString = BlockData; + //cNBTCompound* NBTCompound = new cNBTCompound( 0, 0 ); + //cout << cNBTData(BlockData, DestSize)->cNBTCompound << endl; + //cout << BlockDataString << endl; + + + fwrite( BlockData, DestSize, 1, wf ); //write contents of uncompressed block data to file to check to see if it's valid... It is! :D + //fwrite( &temparr, compdlength, sizeof(unsigned char), wf ); + //cin >> n; //just to see screen output + //delete [] comp_data; + //return 0; + delete [] BlockData; + 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 ++; + } + + } + //if (ia == 30) { break; } + } + //return 0; +/* + for( short i = 0; i < 1024 ; ++i ) { + if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { cout << "ERROR READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { cout << "ERROR READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { cout << "ERROR READING FROM FILE " << SourceFile; fclose(f); return false; } + if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { cout << "ERROR READING FROM FILE " << SourceFile; fclose(f); return false; } + + } + //printf("value: %x \n",trash); + + +*/ + for ( short i = 0; i < 1024; i++ ) { + //cout << toffarr[i] << endl; + } + //cin >> n; + + for ( short i = 0; i < 24; i++ ) { + // cout << toffarr[i] << endl; + } + + + fclose(wf); //close file. + fclose(f); //close file. + } + + clock_t end=clock(); + cout << "Time elapsed: " << double(diffclock(end,begin)) << " ms"<< endl; + return 0; + + +} + + +double diffclock(clock_t clock1,clock_t clock2) +{ + double diffticks=clock1-clock2; + double diffms=(diffticks*10)/CLOCKS_PER_SEC; + return diffms; +} + +// Quicksort controller function, it partitions the different pieces of our array. +void quicksort(int *arIntegers, int left, int right) +{ +/* cout << "quicksort ([" << arIntegers[0] << "," + << arIntegers[1] << "," + << arIntegers[2] << "," + << arIntegers[3] << "," + << arIntegers[4] << "," + << arIntegers[5] << "," + << arIntegers[6] << "]," + << left << "," + << right << ")\n"; +*/ + 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 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 partition(int *arIntegers, int left, int right, int pivot) +{ +/* cout << "partition ("<< arIntegers[0] << "," + << arIntegers[1] << "," + << arIntegers[2] << "," + << arIntegers[3] << "," + << arIntegers[4] << "," + << arIntegers[5] << "," + << arIntegers[6] << "]," + << left << "," + << right << ")\n"; +*/ + 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 swap(int &val1, int &val2) +{ + int temp = val1; + val1 = val2; + val2 = temp; +} diff --git a/converter/cNBTData.cpp b/converter/cNBTData.cpp new file mode 100644 index 000000000..9bc25b789 --- /dev/null +++ b/converter/cNBTData.cpp @@ -0,0 +1,700 @@ +#include "cNBTData.h" +#include <string> // memcpy +#include <stdio.h> +#include "zlib.h" +#include <assert.h> + +#ifndef _WIN32 +#include <cstring> +#include <netinet/in.h> +#endif + +#ifdef _WIN32 +#include <WinSock2.h> +#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_String] = &cNBTData::ParseString; + m_ParseFunctions[TAG_List] = &cNBTData::ParseList; + m_ParseFunctions[TAG_Compound] = &cNBTData::ParseCompound; + + + m_Buffer = a_Buffer; + m_BufferSize = a_BufferSize; + m_Index = 0; + + m_CurrentCompound = this; + + m_bDecompressed = false; +} + +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 ); + const int MAXNBTSIZE = 1024 * 2; + + 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"); + } + + if(m_Buffer) + { + delete [] m_Buffer; + m_Buffer = 0; + } + + //printf("Compressed size: %i\n", have ); + + 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 ); + + const int MAXNBTSIZE = 1024 * 2; + + 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 ); + 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"); + 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 ); + } +} + +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("%s COMPOUND (%s)\n", Prefix, a_Name.c_str() ); + else + printf("%s COMPOUND\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( 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( ByteMap::iterator itr = m_Bytes.begin(); itr != m_Bytes.end(); itr++ ) + { + printf("%s BYTE %s (%i)\n", Prefix, itr->first.c_str(), 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(); + +// for(unsigned int i = 0; i < m_BufferSize; i++) +// { +// printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); +// } +} + +void cNBTData::ParseData() +{ + if(!m_bDecompressed) + { + printf("WARNING: ParseData() called while data was not decompressed\n"); + return; + } + + m_Index = 0; +// printf("cNBTData::ParseData()\n"); +// for(unsigned int i = 0; i < m_BufferSize; i++) +// { +// printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] ); +// } + + while( m_Index < m_BufferSize ) + { + 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] ); + unsigned char Tag = m_Buffer[m_Index]; + if( Tag > 0 && m_ParseFunctions[ Tag ] ) + { + m_Index++; + (*this.*m_ParseFunctions[ Tag ])(true); + } + else if( Tag == TAG_End ) + { + 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() ); + + PutCompound( Name ); + OpenCompound( Name ); + while( m_Index < m_BufferSize && m_Buffer[ m_Index ] != TAG_End ) + { + ParseTags(); + } + CloseCompound(); + //printf("CLOSE COMPOUND\n"); +} + +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 ); + +// 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] ); +// } + + 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); + m_Index++; + } + } + CloseList(); + +} + +void cNBTData::ParseByte( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + char Value = ReadByte(); + + PutByte( Name, Value ); + + //printf("BYTE: %s %i\n", Name.c_str(), Value ); +} + +void cNBTData::ParseShort( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + short Value = ReadShort(); + + PutShort( Name, Value ); + + //printf("SHORT: %s %i\n", Name.c_str(), Value ); +} + +void cNBTData::ParseInt( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + int Value = ReadInt(); + + PutInteger( Name, Value ); + + //printf("INT: %s %i\n", Name.c_str(), Value ); +} + +void cNBTData::ParseString( bool a_bNamed ) +{ + std::string Name; + if( a_bNamed ) Name = ReadName(); + std::string String = ReadName(); + + PutString( Name, String ); + + //printf("STRING: %s (%s)\n", Name.c_str(), String.c_str() ); +} + +std::string cNBTData::ReadName() +{ + short Length = ReadShort(); + + 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 ); +} + +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 ); + } +} + +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/cNBTData.h b/converter/cNBTData.h new file mode 100644 index 000000000..cb4918fcb --- /dev/null +++ b/converter/cNBTData.h @@ -0,0 +1,162 @@ +#pragma once + +#include <map> +#include <list> +#include <string> + +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, + TAG_Short, + TAG_Int, + TAG_String = 8, + TAG_List, + TAG_Compound, + 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 PutString( std::string Name, std::string Value ) { m_Strings[Name] = Value; } + 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]; } + std::string GetString( std::string Name ) { return m_Strings[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<std::string, char> ByteMap; + typedef std::map<std::string, short> ShortMap; + typedef std::map<std::string, int> IntegerMap; + typedef std::map<std::string, std::string> StringMap; + typedef std::map<std::string, cNBTCompound*> CompoundMap; + typedef std::map<std::string, cNBTList*> ListMap; + ByteMap m_Bytes; + ShortMap m_Shorts; + IntegerMap m_Integers; + StringMap m_Strings; + 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<void*> 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 PutString( std::string Name, std::string Value ) { m_CurrentCompound->PutString(Name, Value); } + 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); } + std::string GetString( std::string Name ) { return m_CurrentCompound->GetString(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 ParseInt( bool a_bNamed ); + void ParseShort( bool a_bNamed ); + + short ReadShort(); + std::string ReadName(); + char ReadByte(); + int ReadInt(); + + cNBTCompound* m_CurrentCompound; + + char* m_Buffer; + unsigned int m_BufferSize; + unsigned int m_Index; + + void (cNBTData::*m_ParseFunctions[TAG_NumTags])(bool); +}; diff --git a/converter/denotch b/converter/denotch Binary files differnew file mode 100755 index 000000000..363de19b8 --- /dev/null +++ b/converter/denotch diff --git a/converter/region/r.0.0.mcr b/converter/region/r.0.0.mcr Binary files differnew file mode 100644 index 000000000..772a8fc8a --- /dev/null +++ b/converter/region/r.0.0.mcr |