diff options
author | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-08-02 22:03:16 +0200 |
---|---|---|
committer | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-08-02 22:03:16 +0200 |
commit | f85f83df7f9d69a76671fa9b1396932f34736e55 (patch) | |
tree | 69a93ef27c21656cdde67d7ad62e27ad5468ebb5 /converter/source | |
parent | Ravines: implemented proper caching - another 10 % in generator speedup :) (diff) | |
download | cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.tar cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.tar.gz cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.tar.bz2 cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.tar.lz cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.tar.xz cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.tar.zst cuberite-f85f83df7f9d69a76671fa9b1396932f34736e55.zip |
Diffstat (limited to '')
-rw-r--r-- | converter/source/MemoryLeak.h | 18 | ||||
-rw-r--r-- | converter/source/cDeNotch.cpp | 242 | ||||
-rw-r--r-- | converter/source/cDeNotch.h | 264 | ||||
-rw-r--r-- | converter/source/cMakeDir.cpp | 24 | ||||
-rw-r--r-- | converter/source/cMakeDir.h | 7 | ||||
-rw-r--r-- | converter/source/cNBTData.cpp | 945 | ||||
-rw-r--r-- | converter/source/cNBTData.h | 198 | ||||
-rw-r--r-- | converter/source/cQuicksort.cpp | 67 | ||||
-rw-r--r-- | converter/source/cQuicksort.h | 16 | ||||
-rw-r--r-- | converter/source/cTimer.cpp | 9 | ||||
-rw-r--r-- | converter/source/cTimer.h | 13 | ||||
-rw-r--r-- | converter/source/main.cpp | 75 | ||||
-rw-r--r-- | converter/source/printdir.c | 57 | ||||
-rw-r--r-- | converter/source/wdirent.h | 372 |
14 files changed, 0 insertions, 2307 deletions
diff --git a/converter/source/MemoryLeak.h b/converter/source/MemoryLeak.h deleted file mode 100644 index 2d62f2a4b..000000000 --- a/converter/source/MemoryLeak.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#ifdef _WIN32 - -#ifdef _DEBUG - -#define _CRTDBG_MAP_ALLOC -#include <stdlib.h> -#include <crtdbg.h> - -#ifndef DEBUG_NEW -#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) -#define new DEBUG_NEW -#endif - -#endif - -#endif diff --git a/converter/source/cDeNotch.cpp b/converter/source/cDeNotch.cpp deleted file mode 100644 index 75d4ffa9d..000000000 --- a/converter/source/cDeNotch.cpp +++ /dev/null @@ -1,242 +0,0 @@ -#include "MemoryLeak.h" - -#include "cDeNotch.h" -#include <iostream> -#include <fstream> -#include <cstring> -#include <stdio.h> -#include <ctype.h> -#include "zlib.h" -#include "cNBTData.h" -#include "cTimer.h" -#include "cQuicksort.h" -#include "cDeNotch.h" -#include "cMakeDir.h" - -#ifdef _WIN32 -#include "wdirent.h" -#else -#include <dirent.h> -#endif - -#ifndef _WIN32 -#define sprintf_s(target, size, ... ) printf(target, __VA_ARGS__ ) -#endif - -inline bool fOpenFile( FILE*& a_hFile, const char* a_FileName, const char* a_Mode ) -{ -#ifdef _WIN32 - return fopen_s(&a_hFile, a_FileName, a_Mode ) == 0; -#else - return (a_hFile = fopen(a_FileName, a_Mode )) != 0; -#endif -} - -cDeNotch::cDeNotch ( ) -{ - -} - -int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput ) -{ - std::string OutDir = "world/"; - - FILE* f = 0; - FILE* wf = 0; - - std::string SourceFileName = mcrSource; - std::string OutFileName = OutDir + pakOutput; - - cMakeDir::MakeDir( OutDir.c_str() ); - if( !fOpenFile(wf, OutFileName.c_str(), "wb") ) - { - printf("Could not open/create file %s\n", OutFileName.c_str() ); - } - - printf ("Now Converting %s to %s\n", SourceFileName.c_str(), OutFileName.c_str() ); - if( fOpenFile(f, SourceFileName.c_str(), "rb") ) - { - char* t_FakeHeader = new char[1*1024*1024]; //1MB Temp FakeHeader array - int t_FakeHeaderSz = -1; //Size of data in array - - char* 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 " << SourceFileName; fclose(f); return false; } - if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { std::cout << "ERROR ks93 READING FROM FILE " << SourceFileName; fclose(f); return false; } - if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { std::cout << "ERROR 2s5f READING FROM FILE " << SourceFileName; 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 " << SourceFileName; 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 locations in the file. - } - 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 " << SourceFileName; 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 largest 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 " << SourceFileName; fclose(f); return false; } - if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { std::cout << "ERROR 2y51 READING FROM FILE " << SourceFileName; fclose(f); return false; } - if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { std::cout << "ERROR 3424 READING FROM FILE " << SourceFileName; fclose(f); return false; } - if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR sd22 READING FROM FILE " << SourceFileName; 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 " << SourceFileName; 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 " << SourceFileName; 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"); - //printf("Chunk [%i, %i]\n", xPos, 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: insert 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; - - 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 " << SourceFileName; fclose(f); return false; } - frloc ++; - } - - - delete NBTData; - } - } //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 deleted file mode 100644 index 7701718a6..000000000 --- a/converter/source/cDeNotch.h +++ /dev/null @@ -1,264 +0,0 @@ -#pragma once - -#include <string> - -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/cMakeDir.cpp b/converter/source/cMakeDir.cpp deleted file mode 100644 index 579d39359..000000000 --- a/converter/source/cMakeDir.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "cMakeDir.h" - -#ifndef _WIN32 -//#include <cstring> // If something is missing, uncomment some of these! -//#include <cstdlib> -//#include <stdio.h> -#include <sys/stat.h> // for mkdir -//#include <sys/types.h> -#else -#include <Windows.h> -#endif - -void cMakeDir::MakeDir( const char* a_Directory ) -{ -#ifdef _WIN32 - SECURITY_ATTRIBUTES Attrib; - Attrib.nLength = sizeof(SECURITY_ATTRIBUTES); - Attrib.lpSecurityDescriptor = NULL; - Attrib.bInheritHandle = false; - ::CreateDirectory(a_Directory, &Attrib); -#else - mkdir(a_Directory, S_IRWXU | S_IRWXG | S_IRWXO); -#endif -}
\ No newline at end of file diff --git a/converter/source/cMakeDir.h b/converter/source/cMakeDir.h deleted file mode 100644 index 240b76507..000000000 --- a/converter/source/cMakeDir.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -class cMakeDir -{ -public: - static void MakeDir( const char* a_Directory ); -};
\ No newline at end of file diff --git a/converter/source/cNBTData.cpp b/converter/source/cNBTData.cpp deleted file mode 100644 index cdd88a0c0..000000000 --- a/converter/source/cNBTData.cpp +++ /dev/null @@ -1,945 +0,0 @@ -#include "MemoryLeak.h" - -#include "cNBTData.h" -#include <string> // memcpy -#include <stdio.h> -#include "zlib.h" -#include <assert.h> -#include <iostream> - -#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_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_BufferSize = a_BufferSize; - m_Buffer = new char[m_BufferSize]; // Make a copy of the buffer - memcpy( m_Buffer, a_Buffer, m_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(); - } - - - delete [] m_Buffer; - m_Buffer = 0; m_BufferSize = 0; -} - -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 = 0; - if( Length > 0 ) - { - ByteArray = new char[ Length ]; - 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(); // Close ALL the compounds!! - - m_CurrentCompound->Clear(); // This recursively clears all compounds - - 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(); - - for( ByteArrayMap::iterator itr = m_ByteArrays.begin(); itr != m_ByteArrays.end(); itr++ ) - { - if( itr->second == 0 ) continue; - delete [] itr->second; - itr->second = 0; - } - m_ByteArrays.clear(); - - // Don't really have to do this, but meh - m_Bytes.clear(); - m_Shorts.clear(); - m_Integers.clear(); - m_Longs.clear(); - m_Doubles.clear(); - m_Floats.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 deleted file mode 100644 index 7d80aacb9..000000000 --- a/converter/source/cNBTData.h +++ /dev/null @@ -1,198 +0,0 @@ -#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 = 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<std::string, char> ByteMap; - typedef std::map<std::string, short> ShortMap; - typedef std::map<std::string, int> IntegerMap; - typedef std::map<std::string, long long> LongMap; - typedef std::map<std::string, double> DoubleMap; - typedef std::map<std::string, float> FloatMap; - typedef std::map<std::string, std::string> StringMap; - typedef std::map<std::string, char*> ByteArrayMap; - typedef std::map<std::string, cNBTCompound*> CompoundMap; - typedef std::map<std::string, cNBTList*> 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<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 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 deleted file mode 100644 index 8a00805eb..000000000 --- a/converter/source/cQuicksort.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "cQuicksort.h" -#include <ctype.h> - - - -// 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 deleted file mode 100644 index b9327bd7d..000000000 --- a/converter/source/cQuicksort.h +++ /dev/null @@ -1,16 +0,0 @@ -#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 deleted file mode 100644 index 6e6314d10..000000000 --- a/converter/source/cTimer.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#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 deleted file mode 100644 index 44523f523..000000000 --- a/converter/source/cTimer.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include <time.h> - -class cTimer -{ -public: - cTimer() - {} - - double diffclock(clock_t, clock_t); - -}; diff --git a/converter/source/main.cpp b/converter/source/main.cpp deleted file mode 100644 index 5df378cd6..000000000 --- a/converter/source/main.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "MemoryLeak.h" - -#include <iostream> -#include "cNBTData.h" -#include "cTimer.h" -#include "cQuicksort.h" -#include "cDeNotch.h" -#ifdef _WIN32 -#include "wdirent.h" -#else -#include <dirent.h> -#endif - -int main () -{ - -#ifdef _DEBUG - _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); -#endif - - 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("./")) - { - while(entry = readdir(dp)) - { - entrys = entry->d_name; - found = entrys.find(".mcr"); - if ( (found!=std::string::npos) ) - { - 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; - -#ifdef _DEBUG - _CrtDumpMemoryLeaks(); -#endif - -#ifdef _WIN32 - system("PAUSE"); -#endif - - return 0; -}; diff --git a/converter/source/printdir.c b/converter/source/printdir.c deleted file mode 100644 index d163be380..000000000 --- a/converter/source/printdir.c +++ /dev/null @@ -1,57 +0,0 @@ -#if 0 - -/* list contents of a directory */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include "wdirent.h" - -int main (int argc, char *argv[]) { - int i; - - /* check command line arguments */ - if (argc <= 1) { - fprintf (stderr, "usage: printdir directory\n"); - return EXIT_FAILURE; - } - - /* print contents of directories listed in command line */ - i = 1; - while (i < argc) { - DIR *dir; - struct dirent *ent; - - /* open directory stream */ - dir = opendir (argv[i]); - if (dir != NULL) { - - /* print all the files and directories within directory */ - while ((ent = readdir (dir)) != NULL) { - switch (ent->d_type) { - case DT_REG: - printf ("%*.*s\n", ent->d_namlen, ent->d_namlen, ent->d_name); - break; - - case DT_DIR: - printf ("%s (dir)\n", ent->d_name); - break; - - default: - printf ("%s:\n", ent->d_name); - } - } - - closedir (dir); - } else { - /* could not open directory */ - perror (""); - return EXIT_FAILURE; - } - i++; - } - return EXIT_SUCCESS; -} - - -#endif
\ No newline at end of file diff --git a/converter/source/wdirent.h b/converter/source/wdirent.h deleted file mode 100644 index 445d040dc..000000000 --- a/converter/source/wdirent.h +++ /dev/null @@ -1,372 +0,0 @@ -/***************************************************************************** - * dirent.h - dirent API for Microsoft Visual Studio - * - * Copyright (C) 2006 Toni Ronkko - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * ``Software''), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * Mar 15, 2011, Toni Ronkko - * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0. - * - * Aug 11, 2010, Toni Ronkko - * Added d_type and d_namlen fields to dirent structure. The former is - * especially useful for determining whether directory entry represents a - * file or a directory. For more information, see - * http://www.delorie.com/gnu/docs/glibc/libc_270.html - * - * Aug 11, 2010, Toni Ronkko - * Improved conformance to the standards. For example, errno is now set - * properly on failure and assert() is never used. Thanks to Peter Brockam - * for suggestions. - * - * Aug 11, 2010, Toni Ronkko - * Fixed a bug in rewinddir(): when using relative directory names, change - * of working directory no longer causes rewinddir() to fail. - * - * Dec 15, 2009, John Cunningham - * Added rewinddir member function - * - * Jan 18, 2008, Toni Ronkko - * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string - * between multi-byte and unicode representations. This makes the - * code simpler and also allows the code to be compiled under MingW. Thanks - * to Azriel Fasten for the suggestion. - * - * Mar 4, 2007, Toni Ronkko - * Bug fix: due to the strncpy_s() function this file only compiled in - * Visual Studio 2005. Using the new string functions only when the - * compiler version allows. - * - * Nov 2, 2006, Toni Ronkko - * Major update: removed support for Watcom C, MS-DOS and Turbo C to - * simplify the file, updated the code to compile cleanly on Visual - * Studio 2005 with both unicode and multi-byte character strings, - * removed rewinddir() as it had a bug. - * - * Aug 20, 2006, Toni Ronkko - * Removed all remarks about MSVC 1.0, which is antiqued now. Simplified - * comments by removing SGML tags. - * - * May 14 2002, Toni Ronkko - * Embedded the function definitions directly to the header so that no - * source modules need to be included in the Visual Studio project. Removed - * all the dependencies to other projects so that this very header can be - * used independently. - * - * May 28 1998, Toni Ronkko - * First version. - *****************************************************************************/ -#ifndef DIRENT_H -#define DIRENT_H - -#define WIN32_LEAN_AND_MEAN -#include <windows.h> -#include <string.h> -#include <stdlib.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <errno.h> - -/* Entries missing from MSVC 6.0 */ -#if !defined(FILE_ATTRIBUTE_DEVICE) -# define FILE_ATTRIBUTE_DEVICE 0x40 -#endif - -/* File type and permission flags for stat() */ -#if defined(_MSC_VER) && !defined(S_IREAD) -# define S_IFMT _S_IFMT /* file type mask */ -# define S_IFDIR _S_IFDIR /* directory */ -# define S_IFCHR _S_IFCHR /* character device */ -# define S_IFFIFO _S_IFFIFO /* pipe */ -# define S_IFREG _S_IFREG /* regular file */ -# define S_IREAD _S_IREAD /* read permission */ -# define S_IWRITE _S_IWRITE /* write permission */ -# define S_IEXEC _S_IEXEC /* execute permission */ -#endif -#define S_IFBLK 0 /* block device */ -#define S_IFLNK 0 /* link */ -#define S_IFSOCK 0 /* socket */ - -#if defined(_MSC_VER) -# define S_IRUSR S_IREAD /* read, user */ -# define S_IWUSR S_IWRITE /* write, user */ -# define S_IXUSR 0 /* execute, user */ -# define S_IRGRP 0 /* read, group */ -# define S_IWGRP 0 /* write, group */ -# define S_IXGRP 0 /* execute, group */ -# define S_IROTH 0 /* read, others */ -# define S_IWOTH 0 /* write, others */ -# define S_IXOTH 0 /* execute, others */ -#endif - -/* Indicates that d_type field is available in dirent structure */ -#define _DIRENT_HAVE_D_TYPE - -/* File type flags for d_type */ -#define DT_UNKNOWN 0 -#define DT_REG S_IFREG -#define DT_DIR S_IFDIR -#define DT_FIFO S_IFFIFO -#define DT_SOCK S_IFSOCK -#define DT_CHR S_IFCHR -#define DT_BLK S_IFBLK - -/* Macros for converting between st_mode and d_type */ -#define IFTODT(mode) ((mode) & S_IFMT) -#define DTTOIF(type) (type) - -/* - * File type macros. Note that block devices, sockets and links cannot be - * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are - * only defined for compatibility. These macros should always return false - * on Windows. - */ -#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO) -#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) -#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) -#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) -#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) -#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) -#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) - -#ifdef __cplusplus -extern "C" { -#endif - - -typedef struct dirent -{ - char d_name[MAX_PATH + 1]; /* File name */ - size_t d_namlen; /* Length of name without \0 */ - int d_type; /* File type */ -} dirent; - - -typedef struct DIR -{ - dirent curentry; /* Current directory entry */ - WIN32_FIND_DATAA find_data; /* Private file data */ - int cached; /* True if data is valid */ - HANDLE search_handle; /* Win32 search handle */ - char patt[MAX_PATH + 3]; /* Initial directory name */ -} DIR; - - -/* Forward declarations */ -static DIR *opendir(const char *dirname); -static struct dirent *readdir(DIR *dirp); -static int closedir(DIR *dirp); -static void rewinddir(DIR* dirp); - - -/* Use the new safe string functions introduced in Visual Studio 2005 */ -#if defined(_MSC_VER) && _MSC_VER >= 1400 -# define DIRENT_STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE) -#else -# define DIRENT_STRNCPY(dest,src,size) strncpy((dest),(src),(size)) -#endif - -/* Set errno variable */ -#if defined(_MSC_VER) -#define DIRENT_SET_ERRNO(x) _set_errno (x) -#else -#define DIRENT_SET_ERRNO(x) (errno = (x)) -#endif - - -/***************************************************************************** - * Open directory stream DIRNAME for read and return a pointer to the - * internal working area that is used to retrieve individual directory - * entries. - */ -static DIR *opendir(const char *dirname) -{ - DIR *dirp; - - /* ensure that the resulting search pattern will be a valid file name */ - if (dirname == NULL) { - DIRENT_SET_ERRNO (ENOENT); - return NULL; - } - if (strlen (dirname) + 3 >= MAX_PATH) { - DIRENT_SET_ERRNO (ENAMETOOLONG); - return NULL; - } - - /* construct new DIR structure */ - dirp = (DIR*) malloc (sizeof (struct DIR)); - if (dirp != NULL) { - int error; - - /* - * Convert relative directory name to an absolute one. This - * allows rewinddir() to function correctly when the current working - * directory is changed between opendir() and rewinddir(). - */ - if (GetFullPathNameA (dirname, MAX_PATH, dirp->patt, NULL)) { - char *p; - - /* append the search pattern "\\*\0" to the directory name */ - p = strchr (dirp->patt, '\0'); - if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') { - *p++ = '\\'; - } - *p++ = '*'; - *p = '\0'; - - /* open directory stream and retrieve the first entry */ - dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data); - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - /* a directory entry is now waiting in memory */ - dirp->cached = 1; - error = 0; - } else { - /* search pattern is not a directory name? */ - DIRENT_SET_ERRNO (ENOENT); - error = 1; - } - } else { - /* buffer too small */ - DIRENT_SET_ERRNO (ENOMEM); - error = 1; - } - - if (error) { - free (dirp); - dirp = NULL; - } - } - - return dirp; -} - - -/***************************************************************************** - * Read a directory entry, and return a pointer to a dirent structure - * containing the name of the entry in d_name field. Individual directory - * entries returned by this very function include regular files, - * sub-directories, pseudo-directories "." and "..", but also volume labels, - * hidden files and system files may be returned. - */ -static struct dirent *readdir(DIR *dirp) -{ - DWORD attr; - if (dirp == NULL) { - /* directory stream did not open */ - DIRENT_SET_ERRNO (EBADF); - return NULL; - } - - /* get next directory entry */ - if (dirp->cached != 0) { - /* a valid directory entry already in memory */ - dirp->cached = 0; - } else { - /* get the next directory entry from stream */ - if (dirp->search_handle == INVALID_HANDLE_VALUE) { - return NULL; - } - if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) { - /* the very last entry has been processed or an error occured */ - FindClose (dirp->search_handle); - dirp->search_handle = INVALID_HANDLE_VALUE; - return NULL; - } - } - - /* copy as a multibyte character string */ - DIRENT_STRNCPY ( dirp->curentry.d_name, - dirp->find_data.cFileName, - sizeof(dirp->curentry.d_name) ); - dirp->curentry.d_name[MAX_PATH] = '\0'; - - /* compute the length of name */ - dirp->curentry.d_namlen = strlen (dirp->curentry.d_name); - - /* determine file type */ - attr = dirp->find_data.dwFileAttributes; - if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) { - dirp->curentry.d_type = DT_CHR; - } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { - dirp->curentry.d_type = DT_DIR; - } else { - dirp->curentry.d_type = DT_REG; - } - return &dirp->curentry; -} - - -/***************************************************************************** - * Close directory stream opened by opendir() function. Close of the - * directory stream invalidates the DIR structure as well as any previously - * read directory entry. - */ -static int closedir(DIR *dirp) -{ - if (dirp == NULL) { - /* invalid directory stream */ - DIRENT_SET_ERRNO (EBADF); - return -1; - } - - /* release search handle */ - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - FindClose (dirp->search_handle); - dirp->search_handle = INVALID_HANDLE_VALUE; - } - - /* release directory structure */ - free (dirp); - return 0; -} - - -/***************************************************************************** - * Resets the position of the directory stream to which dirp refers to the - * beginning of the directory. It also causes the directory stream to refer - * to the current state of the corresponding directory, as a call to opendir() - * would have done. If dirp does not refer to a directory stream, the effect - * is undefined. - */ -static void rewinddir(DIR* dirp) -{ - if (dirp != NULL) { - /* release search handle */ - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - FindClose (dirp->search_handle); - } - - /* open new search handle and retrieve the first entry */ - dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data); - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - /* a directory entry is now waiting in memory */ - dirp->cached = 1; - } else { - /* failed to re-open directory: no directory entry in memory */ - dirp->cached = 0; - } - } -} - - -#ifdef __cplusplus -} -#endif -#endif /*DIRENT_H*/ |