put the timer and quicksort functions into their own files. Made a few changes to the converter. Converter doesn't understand Entity tags and some chunks cause it to segfault for a currently unknown reason.
git-svn-id: http://mc-server.googlecode.com/svn/trunk@28 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
// reading a complete binary file
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
@@ -6,14 +5,9 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "zlib.h"
|
||||
#include <time.h>
|
||||
#include "cNBTData.h"
|
||||
|
||||
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);
|
||||
#include "timer.h"
|
||||
#include "quicksort.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -48,9 +42,9 @@ int main () {
|
||||
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 char byte4 = 0;
|
||||
unsigned char byte5 = 0;
|
||||
unsigned char trash = 0;
|
||||
unsigned int frloc = 0;
|
||||
int toffset = 0;
|
||||
int compdlength = 0;
|
||||
@@ -72,7 +66,7 @@ int main () {
|
||||
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.
|
||||
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; }
|
||||
}
|
||||
@@ -80,65 +74,38 @@ int main () {
|
||||
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 == 31) { ia++; }
|
||||
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\n", 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)
|
||||
//printf("byte1: %x \n", byte1);
|
||||
//printf("byte2: %x \n", byte2);
|
||||
//printf("byte3: %x \n", byte3);
|
||||
//printf("byte4: %x \n", byte4);
|
||||
|
||||
//printf("byte1: %i\n", byte1);
|
||||
//printf("byte2: %i\n", byte2);
|
||||
//printf("byte3: %i\n", byte3);
|
||||
//printf("byte4: %i\n", byte4);
|
||||
//printf("byte5: %i\n", byte5);
|
||||
|
||||
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;
|
||||
|
||||
|
||||
// TODO - delete [] temparr after you're done with it, now it's a memory leak
|
||||
char* temparr = new char[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.
|
||||
if( fread( temparr, compdlength, 1, f) != 1 ) { cout << "ERROR rf22 READING FROM FILE " << SourceFile; fclose(f); return false; }
|
||||
char* compBlockData = new char[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.
|
||||
if( fread( compBlockData, compdlength, 1, f) != 1 ) { cout << "ERROR rf22 READING FROM FILE " << SourceFile; fclose(f); return false; }
|
||||
frloc = frloc + compdlength;
|
||||
/*
|
||||
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 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 );
|
||||
@@ -158,45 +125,49 @@ int main () {
|
||||
};
|
||||
}
|
||||
|
||||
//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;
|
||||
|
||||
|
||||
//testing of nbtparser.
|
||||
cNBTData* NBTData = new cNBTData(BlockData, (testr));
|
||||
//NBTData->m_bDecompressed = true;
|
||||
NBTData->ParseData();
|
||||
NBTData->PrintData();
|
||||
//NBTData->PrintData();
|
||||
NBTData->OpenCompound("");
|
||||
NBTData->OpenCompound("Level"); // You need to open the right compounds before you can access the data in it
|
||||
|
||||
//NBTData->GetByteArray("Blocks");
|
||||
//for(unsigned int i = 0; i < 111; i++) {//re
|
||||
//printf("Blocks?: %i\n", NBTData->cNBTCompound::GetByteArray("Blocks")[0]);
|
||||
NBTData->OpenCompound("");
|
||||
NBTData->OpenCompound("Level"); // You need to open the right compounds before you can access the data in it
|
||||
printf("xPos: %i\n", NBTData->GetInteger("xPos") );
|
||||
//will print
|
||||
//xPos: 0
|
||||
printf("test: %i\n", NBTData->GetByteArray("Blocks")[0] );
|
||||
//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)
|
||||
//=============================
|
||||
|
||||
|
||||
for(unsigned int i = 0; i < 16384; i++) {
|
||||
//printf("array HM: %i\n", NBTData->GetByteArray("HeightMap")[i]);
|
||||
}
|
||||
for(unsigned int i = 0; i < 32768; i++) {
|
||||
//printf("array Blocks: %i\n", NBTData->GetByteArray("Blocks")[i]);
|
||||
}
|
||||
|
||||
//printf("xPos: %i\n", NBTData->GetInteger("xPos") );
|
||||
NBTData->CloseCompound();// Close the compounds after you're done
|
||||
NBTData->CloseCompound();
|
||||
//}
|
||||
return 1;
|
||||
|
||||
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 [] 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 ) { cout << "ERROR 2nkd READING FROM FILE " << SourceFile; fclose(f); return false; }
|
||||
frloc ++;
|
||||
@@ -206,18 +177,7 @@ int main () {
|
||||
//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;
|
||||
}
|
||||
@@ -238,95 +198,3 @@ int main () {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user