1
0

I think I fixed the memory leaks in the converter

Updated makefile for Unix

I changed how the converter works, now you simply put the .exe in the folder you want to convert and run it, it'll output all the files in ./world

git-svn-id: http://mc-server.googlecode.com/svn/trunk@132 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
faketruth 2011-12-27 17:59:08 +00:00
parent dca6401fb3
commit 8a21fbf0ce
15 changed files with 361 additions and 219 deletions

Binary file not shown.

Binary file not shown.

View File

@ -36,12 +36,14 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>../zlib-1.2.5</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
@ -65,6 +67,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="source\cDeNotch.cpp" />
<ClCompile Include="source\cMakeDir.cpp" />
<ClCompile Include="source\cNBTData.cpp" />
<ClCompile Include="source\cQuicksort.cpp" />
<ClCompile Include="source\cTimer.cpp" />
@ -73,9 +76,11 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="source\cDeNotch.h" />
<ClInclude Include="source\cMakeDir.h" />
<ClInclude Include="source\cNBTData.h" />
<ClInclude Include="source\cQuicksort.h" />
<ClInclude Include="source\cTimer.h" />
<ClInclude Include="source\MemoryLeak.h" />
<ClInclude Include="source\wdirent.h" />
</ItemGroup>
<ItemGroup>

View File

@ -33,6 +33,9 @@
<ClCompile Include="source\printdir.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="source\cMakeDir.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="source\cDeNotch.h">
@ -50,5 +53,11 @@
<ClInclude Include="source\wdirent.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="source\cMakeDir.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="source\MemoryLeak.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,3 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(ProjectDir)/region</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerWorkingDirectory>$(ProjectDir)/region</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>

View File

@ -16,10 +16,9 @@ del release\*.* /Q
del x64\*.* /Q
del "My Inspector Results"\*.* /Q
del ipch\*.* /Q
del build\*.* /Q
rd release /S /Q
rd debug /S /Q
rd ipch /S /Q
rd x64 /Q
rd "My Inspector Results" /Q
pause

Binary file not shown.

View File

@ -13,6 +13,7 @@ denotch: \
build/cNBTData.o\
build/cTimer.o\
build/cQuicksort.o\
build/cMakeDir.o\
build/adler32.o\
build/compress.o\
build/crc32.o\
@ -28,12 +29,14 @@ denotch: \
build/trees.o\
build/uncompr.o\
build/zutil.o
$(CC) $(LNK_OPTIONS) \
build/main.o\
build/cDeNotch.o\
build/cNBTData.o\
build/cTimer.o\
build/cQuicksort.o\
build/cMakeDir.o\
build/adler32.o\
build/compress.o\
build/crc32.o\
@ -58,6 +61,7 @@ clean:
build/cNBTData.o\
build/cTimer.o\
build/cQuicksort.o\
build/cMakeDir.o\
build/adler32.o\
build/compress.o\
build/crc32.o\
@ -96,6 +100,8 @@ build/cTimer.o : source/cTimer.cpp
build/cQuicksort.o : source/cQuicksort.cpp
$(CC) $(CC_OPTIONS) source/cQuicksort.cpp -c $(INCLUDE) -o build/cQuicksort.o
build/cMakeDir.o : source/cMakeDir.cpp
$(CC) $(CC_OPTIONS) source/cMakeDir.cpp -c $(INCLUDE) -o build/cMakeDir.o
# Item # 104 -- adler32 --
build/adler32.o : ../zlib-1.2.5/adler32.c

View File

@ -0,0 +1,18 @@
#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

View File

@ -1,3 +1,5 @@
#include "MemoryLeak.h"
#include "cDeNotch.h"
#include <iostream>
#include <fstream>
@ -9,45 +11,55 @@
#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 ( ) {
cDeNotch::cDeNotch ( )
{
}
int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput ) {
char SourceFile[128];
char OutputFile[128];
int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput )
{
std::string OutDir = "world/";
FILE* f = 0;
FILE* wf = 0;
#ifdef _WIN32
sprintf_s(SourceFile, 128, "region\\%s", mcrSource.c_str() ); //replace hard coded file with file array variable
sprintf_s(OutputFile, 128, "world\\%s", pakOutput.c_str() ); //parce x and z from file array variable and place into pak file format
if( fopen_s(&wf, OutputFile, "wb" ) == 0 ) {} else { std::cout << "uhoh!" << std::endl; return false; } //open new pak file for writing
#else
sprintf(SourceFile, "region/%s", mcrSource.c_str() ); //same as above but for linux
sprintf(OutputFile, "world/%s", pakOutput.c_str() );
if( (wf = fopen(OutputFile, "wb" )) != 0 ) {} else { std::cout << "uhoh!" << std::endl; return false; }
#endif
std::string SourceFileName = mcrSource;
std::string OutFileName = OutDir + pakOutput;
printf ("Now Converting %s to %s\n", mcrSource.c_str(), pakOutput.c_str() );
if( (f = fopen(SourceFile, "rb" )) != 0 ) { // no error
cMakeDir::MakeDir( OutDir.c_str() );
if( !fOpenFile(wf, OutFileName.c_str(), "wb") )
{
printf("Could not open/create file %s\n", OutFileName.c_str() );
}
char* t_FakeHeader;
t_FakeHeader = new char[1*1024*1024]; //1MB Temp FakeHeader array
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;
t_CompChunk = new char[5*1024*1024]; //5MB Temp Compressed Chunk Data 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;
@ -68,44 +80,51 @@ int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput ) {
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
for( short i = 0; i < 1024 ; ++i )
{ //loop through first 4096 bytes of data, 4 bytes at a time
//Region files begin with an 8kiB header containing information about which chunks are present in the region file, when they were last updated, and where they can be found. The location in the region file of a chunk at (x, z) (in chunk coordinates) can be found at byte offset 4 * ((x mod 32) + (z mod 32) * 32) in its region file. Its timestamp can be found 4096 bytes later in the file. The remainder of the file consists of data for up to 1024 chunks, interspersed with an arbitrary amount of unused space.
//we are only using the first 4096 bytes. We don't need the timestamps right now.
if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { std::cout << "ERROR 21hs READING FROM FILE " << SourceFile; fclose(f); return false; }
if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { std::cout << "ERROR ks93 READING FROM FILE " << SourceFile; fclose(f); return false; }
if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { std::cout << "ERROR 2s5f READING FROM FILE " << SourceFile; fclose(f); return false; }//first three bytes area big-endian representation of the chunk offsets in no particular order.
if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR dhj3 READING FROM FILE " << SourceFile; fclose(f); return false; }//we don't need to use this byte right now.
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 locatiosn in the fle.
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.
for ( short i = 0; i < 4096; i++ )
{ //loop through next 4096 bytes of the header.
//keeping this code here in case we need it later. not using it right now.
if( fread( &trash, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR 2jkd READING FROM FILE " << SourceFile; fclose(f); return false; }
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 larget offset locations so we only have to read through the file once.
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.
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
}
else
{ // found a chunk offset value
//Chunk data begins with a (big-endian) four-byte length field which indicates the exact length of the remaining chunk data in bytes. The following byte indicates the compression scheme used for chunk data, and the remaining (length-1) bytes are the compressed chunk data.
//printf("Working on chunk %i :: %i\n", ia, toffarr[ia]);
if( fread( &byte1, sizeof(byte1), 1, f) != 1 ) { std::cout << "ERROR 2t32 READING FROM FILE " << SourceFile; fclose(f); return false; }
if( fread( &byte2, sizeof(byte2), 1, f) != 1 ) { std::cout << "ERROR 2y51 READING FROM FILE " << SourceFile; fclose(f); return false; }
if( fread( &byte3, sizeof(byte3), 1, f) != 1 ) { std::cout << "ERROR 3424 READING FROM FILE " << SourceFile; fclose(f); return false; }
if( fread( &byte4, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR sd22 READING FROM FILE " << SourceFile; fclose(f); return false; }
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 " << SourceFile; fclose(f); return false; } //compression type, 1 = GZip (RFC1952) (unused in practice) , 2 = Zlib (RFC1950)
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 " << SourceFile; fclose(f); return false; }
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
@ -164,12 +183,13 @@ int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput ) {
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: inserert json code and add it to chunk data
//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;
@ -194,17 +214,19 @@ int cDeNotch:: Converter ( std::string mcrSource, std::string pakOutput ) {
delete [] CompressedChunk;
delete [] compBlockData;
delete [] BlockData;
//delete [] NBTData;
while ( (frloc < toffarr[ia+1]) && (ia<1023) ) { //loop through Notch's junk data until we get to another chunk offset possition to start the loop again
if( fread( &trash, sizeof(byte4), 1, f) != 1 ) { std::cout << "ERROR 2nkd READING FROM FILE " << SourceFile; fclose(f); return false; }
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 );

View File

@ -0,0 +1,24 @@
#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
}

View File

@ -0,0 +1,7 @@
#pragma once
class cMakeDir
{
public:
static void MakeDir( const char* a_Directory );
};

View File

@ -1,3 +1,5 @@
#include "MemoryLeak.h"
#include "cNBTData.h"
#include <string> // memcpy
#include <stdio.h>
@ -40,8 +42,10 @@ cNBTData::cNBTData( char* a_Buffer, unsigned int a_BufferSize )
m_ParseFunctions[TAG_ByteArray] = &cNBTData::ParseByteArray;
m_Buffer = a_Buffer;
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
@ -479,6 +483,10 @@ void cNBTData::ParseData()
}
ParseTags();
}
delete [] m_Buffer;
m_Buffer = 0; m_BufferSize = 0;
}
void cNBTData::ParseTags()
@ -682,9 +690,10 @@ void cNBTData::ParseByteArray( bool a_bNamed )
int Length = ReadInt();
std::string String;
char* ByteArray = new char[ Length ];
char* ByteArray = 0;
if( Length > 0 )
{
ByteArray = new char[ Length ];
memcpy( ByteArray, &m_Buffer[ m_Index ], Length );
m_Index += Length;
}
@ -856,8 +865,9 @@ void cNBTList::Serialize(std::string & a_Buffer)
void cNBTData::Clear()
{
while( m_CurrentCompound != this ) CloseCompound();
m_CurrentCompound->Clear();
while( m_CurrentCompound != this ) CloseCompound(); // Close ALL the compounds!!
m_CurrentCompound->Clear(); // This recursively clears all compounds
if( m_Buffer )
{
@ -886,9 +896,22 @@ void cNBTCompound::Clear()
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();
}

View File

@ -1,3 +1,5 @@
#include "MemoryLeak.h"
#include <iostream>
#include "cNBTData.h"
#include "cTimer.h"
@ -9,7 +11,12 @@
#include <dirent.h>
#endif
int main () {
int main ()
{
#ifdef _DEBUG
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
cTimer Timer;
clock_t progBegin = clock(); //start main program timer
@ -26,15 +33,15 @@ int main () {
//string* dir_array;
int dir_num_files = 0;
int ctr = 0;
#ifdef _WIN32
if(dp = opendir("region\\")){
#else
if(dp = opendir("region/")){
#endif
while(entry = readdir(dp)){
if(dp = opendir("./"))
{
while(entry = readdir(dp))
{
entrys = entry->d_name;
found = entrys.find(".mcr");
if ( (found!=std::string::npos) ) {
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));
@ -55,5 +62,14 @@ int main () {
}
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;
};

View File

@ -1,3 +1,5 @@
#if 0
/* list contents of a directory */
#include <stdio.h>
#include <stdlib.h>
@ -50,3 +52,6 @@ int main (int argc, char *argv[]) {
}
return EXIT_SUCCESS;
}
#endif