summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/cChunkLoader.h2
-rw-r--r--source/cFile.cpp31
-rw-r--r--source/cFile.h3
-rw-r--r--source/cFileFormatUpdater.cpp73
-rw-r--r--source/cPlayer.cpp124
-rw-r--r--source/cRecipeChecker.cpp77
-rw-r--r--source/cWebAdmin.cpp51
7 files changed, 210 insertions, 151 deletions
diff --git a/source/cChunkLoader.h b/source/cChunkLoader.h
index 5d9be39c5..aa6f3806f 100644
--- a/source/cChunkLoader.h
+++ b/source/cChunkLoader.h
@@ -1,7 +1,5 @@
#pragma once
-class cCriticalSection;
-class cEvent;
class cChunk;
class cChunkLoader
{
diff --git a/source/cFile.cpp b/source/cFile.cpp
index 84308984a..caaef010a 100644
--- a/source/cFile.cpp
+++ b/source/cFile.cpp
@@ -198,3 +198,34 @@ int cFile::Tell (void) const
+
+/// Returns the size of file, in bytes, or -1 for failure; asserts if not open
+int cFile::GetSize(void) const
+{
+ assert(IsOpen());
+
+ if (!IsOpen())
+ {
+ return -1;
+ }
+
+ int CurPos = ftell(m_File);
+ if (CurPos < 0)
+ {
+ return -1;
+ }
+ if (fseek(m_File, 0, SEEK_END) != 0)
+ {
+ return -1;
+ }
+ int res = ftell(m_File);
+ if (fseek(m_File, CurPos, SEEK_SET) != 0)
+ {
+ return -1;
+ }
+ return res;
+}
+
+
+
+
diff --git a/source/cFile.h b/source/cFile.h
index 0c3abb1d9..9f14c93f6 100644
--- a/source/cFile.h
+++ b/source/cFile.h
@@ -80,6 +80,9 @@ public:
/// Returns the current position (bytes from file start) or -1 for failure; asserts if not open
int Tell (void) const;
+ /// Returns the size of file, in bytes, or -1 for failure; asserts if not open
+ int GetSize(void) const;
+
private:
#ifdef USE_STDIO_FILE
FILE * m_File;
diff --git a/source/cFileFormatUpdater.cpp b/source/cFileFormatUpdater.cpp
index e95a66095..e117c8075 100644
--- a/source/cFileFormatUpdater.cpp
+++ b/source/cFileFormatUpdater.cpp
@@ -33,6 +33,17 @@ void cFileFormatUpdater::UpdatePlayersOfWorld( const char* a_WorldName )
}
}
+
+
+
+
+#define READ(File, Var) \
+ if (File.Read(&Var, sizeof(Var)) != sizeof(Var)) \
+ { \
+ LOGERROR("ERROR READING \"%s\" FROM FILE \"%s\"", #Var, a_FileName); \
+ return; \
+ }
+
// Converts player binary files to human readable JSON
void cFileFormatUpdater::PlayerBINtoJSON( const char* a_FileName )
{
@@ -43,33 +54,30 @@ void cFileFormatUpdater::PlayerBINtoJSON( const char* a_FileName )
const unsigned int NumInventorySlots = 45; // At this time the player inventory has/had 45 slots
cItem IventoryItems[ NumInventorySlots ];
- FILE* f;
-#ifdef _WIN32
- if( fopen_s(&f, a_FileName, "rb" ) == 0 ) // no error
-#else
- if( (f = fopen(a_FileName, "rb" ) ) != 0 ) // no error
-#endif
+ cFile f;
+ if (!f.Open(a_FileName, cFile::fmRead))
{
- // First read player position, rotation and health
- if( fread( &PlayerPos.x, sizeof(double), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
- if( fread( &PlayerPos.y, sizeof(double), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
- if( fread( &PlayerPos.z, sizeof(double), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
- if( fread( &PlayerRot.x, sizeof(float), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
- if( fread( &PlayerRot.y, sizeof(float), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
- if( fread( &PlayerRot.z, sizeof(float), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
- if( fread( &PlayerHealth, sizeof(short), 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", a_FileName); fclose(f); return; }
+ return;
+ }
+ // First read player position, rotation and health
+ READ(f, PlayerPos.x);
+ READ(f, PlayerPos.y);
+ READ(f, PlayerPos.z);
+ READ(f, PlayerRot.x);
+ READ(f, PlayerRot.y);
+ READ(f, PlayerRot.z);
+ READ(f, PlayerHealth);
- for(unsigned int i = 0; i < NumInventorySlots; i++)
- {
- cItem & Item = IventoryItems[i];
- if( fread( &Item.m_ItemID, sizeof(Item.m_ItemID), 1, f) != 1 ) { LOGERROR("ERROR READING INVENTORY FROM FILE"); return; }
- if( fread( &Item.m_ItemCount, sizeof(Item.m_ItemCount), 1, f) != 1 ) { LOGERROR("ERROR READING INVENTORY FROM FILE"); return; }
- if( fread( &Item.m_ItemHealth, sizeof(Item.m_ItemHealth), 1, f)!= 1 ) { LOGERROR("ERROR READING INVENTORY FROM FILE"); return; }
- }
- fclose(f);
+ for(unsigned int i = 0; i < NumInventorySlots; i++)
+ {
+ cItem & Item = IventoryItems[i];
+ READ(f, Item.m_ItemID);
+ READ(f, Item.m_ItemCount);
+ READ(f, Item.m_ItemHealth);
}
+ f.Close();
// Loaded all the data, now create the JSON data
Json::Value JSON_PlayerPosition;
@@ -105,24 +113,25 @@ void cFileFormatUpdater::PlayerBINtoJSON( const char* a_FileName )
std::string FileNameJson = FileNameWithoutExt + ".json";
// Write to file
-#ifdef _WIN32
- if( fopen_s(&f, FileNameJson.c_str(), "wb" ) == 0 ) // no error
-#else
- if( (f = fopen(FileNameJson.c_str(), "wb" ) ) != 0 ) // no error
-#endif
+ if (!f.Open(FileNameJson.c_str(), cFile::fmWrite))
{
- if( fwrite( JsonData.c_str(), JsonData.size(), 1, f ) != 1 ) { LOGERROR("ERROR WRITING PLAYER JSON TO FILE %s", FileNameJson.c_str() ); return; }
- fclose( f );
+ return;
+ }
+ if (f.Write(JsonData.c_str(), JsonData.size()) != JsonData.size())
+ {
+ LOGERROR("ERROR WRITING PLAYER JSON TO FILE \"%s\"", FileNameJson.c_str());
+ return;
}
+ f.Close();
// Delete old format file, only do this when conversion has succeeded
- if( std::remove( a_FileName ) != 0 )
+ if (std::remove(a_FileName) != 0)
{
- LOGERROR("COULD NOT DELETE FILE %s", a_FileName );
+ LOGERROR("COULD NOT DELETE old format file \"%s\"", a_FileName);
return;
}
- LOGINFO("Successfully converted binary to Json %s", FileNameJson.c_str() );
+ LOGINFO("Successfully converted binary to Json \"%s\"", FileNameJson.c_str() );
}
diff --git a/source/cPlayer.cpp b/source/cPlayer.cpp
index c3ba32840..8e161900c 100644
--- a/source/cPlayer.cpp
+++ b/source/cPlayer.cpp
@@ -764,6 +764,10 @@ void cPlayer::LoadPermissionsFromDisk()
ResolvePermissions();
}
+
+
+
+
bool cPlayer::LoadFromDisk() // TODO - This should also get/set/whatever the correct world for this player
{
LoadPermissionsFromDisk();
@@ -778,59 +782,62 @@ bool cPlayer::LoadFromDisk() // TODO - This should also get/set/whatever the cor
char SourceFile[128];
sprintf_s(SourceFile, 128, "players/%s.json", m_pState->PlayerName.c_str() );
- FILE* f;
- #ifdef _WIN32
- if( fopen_s(&f, SourceFile, "rb" ) == 0 ) // no error
- #else
- if( (f = fopen(SourceFile, "rb" ) ) != 0 ) // no error
- #endif
- {
- // Get file size
- fseek (f , 0 , SEEK_END);
- long FileSize = ftell (f);
- rewind(f);
-
- char* buffer = new char[ FileSize ];
- if( fread( buffer, FileSize, 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", SourceFile); fclose(f); return false; }
- fclose(f);
-
- Json::Value root;
- Json::Reader reader;
- if( !reader.parse( buffer, root, false ) )
- {
- LOGERROR("ERROR WHILE PARSING JSON FROM FILE %s", SourceFile);
- }
-
- delete [] buffer;
-
- Json::Value & JSON_PlayerPosition = root["position"];
- if( JSON_PlayerPosition.size() == 3 )
- {
- m_Pos->x = JSON_PlayerPosition[(unsigned int)0].asDouble();
- m_Pos->y = JSON_PlayerPosition[(unsigned int)1].asDouble();
- m_Pos->z = JSON_PlayerPosition[(unsigned int)2].asDouble();
- }
+ cFile f;
+ if (!f.Open(SourceFile, cFile::fmRead))
+ {
+ return false;
+ }
- Json::Value & JSON_PlayerRotation = root["rotation"];
- if( JSON_PlayerRotation.size() == 3 )
- {
- m_Rot->x = (float)JSON_PlayerRotation[(unsigned int)0].asDouble();
- m_Rot->y = (float)JSON_PlayerRotation[(unsigned int)1].asDouble();
- m_Rot->z = (float)JSON_PlayerRotation[(unsigned int)2].asDouble();
- }
+ // Get file size
+ long FileSize = f.GetSize();
- m_Health = (short)root.get("health", 0 ).asInt();
- m_FoodLevel = (short)root.get("food", 0 ).asInt();
- m_Inventory->LoadFromJson(root["inventory"]);
- m_CreativeInventory->LoadFromJson(root["creativeinventory"]);
+ char * buffer = new char[FileSize];
+ if (f.Read(buffer, FileSize) != FileSize )
+ {
+ LOGERROR("ERROR READING FROM FILE \"%s\"", SourceFile);
+ return false;
+ }
+ f.Close();
- m_pState->LoadedWorldName = root.get("world", "world").asString();
+ Json::Value root;
+ Json::Reader reader;
+ if( !reader.parse( buffer, root, false ) )
+ {
+ LOGERROR("ERROR WHILE PARSING JSON FROM FILE %s", SourceFile);
+ }
- return true;
+ delete [] buffer;
+
+ Json::Value & JSON_PlayerPosition = root["position"];
+ if( JSON_PlayerPosition.size() == 3 )
+ {
+ m_Pos->x = JSON_PlayerPosition[(unsigned int)0].asDouble();
+ m_Pos->y = JSON_PlayerPosition[(unsigned int)1].asDouble();
+ m_Pos->z = JSON_PlayerPosition[(unsigned int)2].asDouble();
}
- return false;
+
+ Json::Value & JSON_PlayerRotation = root["rotation"];
+ if( JSON_PlayerRotation.size() == 3 )
+ {
+ m_Rot->x = (float)JSON_PlayerRotation[(unsigned int)0].asDouble();
+ m_Rot->y = (float)JSON_PlayerRotation[(unsigned int)1].asDouble();
+ m_Rot->z = (float)JSON_PlayerRotation[(unsigned int)2].asDouble();
+ }
+
+ m_Health = (short)root.get("health", 0 ).asInt();
+ m_FoodLevel = (short)root.get("food", 0 ).asInt();
+ m_Inventory->LoadFromJson(root["inventory"]);
+ m_CreativeInventory->LoadFromJson(root["creativeinventory"]);
+
+ m_pState->LoadedWorldName = root.get("world", "world").asString();
+
+ return true;
}
+
+
+
+
bool cPlayer::SaveToDisk()
{
cMakeDir::MakeDir("players");
@@ -867,23 +874,24 @@ bool cPlayer::SaveToDisk()
char SourceFile[128];
sprintf_s(SourceFile, 128, "players/%s.json", m_pState->PlayerName.c_str() );
- FILE* f;
- #ifdef _WIN32
- if( fopen_s(&f, SourceFile, "wb" ) == 0 ) // no error
- #else
- if( (f = fopen(SourceFile, "wb" ) ) != 0 ) // no error
- #endif
+ cFile f;
+ if (!f.Open(SourceFile, cFile::fmWrite))
{
- if( fwrite( JsonData.c_str(), JsonData.size(), 1, f ) != 1 ) { LOGERROR("ERROR WRITING PLAYER JSON TO FILE %s", SourceFile ); return false; }
- fclose(f);
- return true;
+ LOGERROR("ERROR WRITING PLAYER \"%s\" TO FILE \"%s\" - cannot open file", m_pState->PlayerName.c_str(), SourceFile);
+ return false;
}
-
- LOGERROR("ERROR WRITING PLAYER %s TO FILE %s", m_pState->PlayerName.c_str(), SourceFile);
- return false;
+ if (f.Write(JsonData.c_str(), JsonData.size()) != JsonData.size())
+ {
+ LOGERROR("ERROR WRITING PLAYER JSON TO FILE \"%s\"", SourceFile);
+ return false;
+ }
+ return true;
}
+
+
+
const char* cPlayer::GetName()
{
return m_pState->PlayerName.c_str();
diff --git a/source/cRecipeChecker.cpp b/source/cRecipeChecker.cpp
index d26d8d9df..ac90c7a00 100644
--- a/source/cRecipeChecker.cpp
+++ b/source/cRecipeChecker.cpp
@@ -19,35 +19,56 @@
typedef std::list< cRecipeChecker::Recipe* > RecipeList;
+
struct cRecipeChecker::sRecipeCheckerState
{
RecipeList Recipes;
};
+
+
+
+
cRecipeChecker* cRecipeChecker::GetRecipeChecker()
{
LOGWARN("WARNING: Using deprecated function cRecipeChecker::GetRecipeChecker() use cRoot::Get()->GetRecipeChecker() instead!");
return cRoot::Get()->GetRecipeChecker();
}
+
+
+
+
cRecipeChecker::Recipe::~Recipe()
{
delete [] Slots;
Slots = 0;
}
+
+
+
+
cRecipeChecker::~cRecipeChecker()
{
ClearRecipes();
delete m_pState;
}
+
+
+
+
cRecipeChecker::cRecipeChecker()
: m_pState( new sRecipeCheckerState )
{
ReloadRecipes();
}
+
+
+
+
void cRecipeChecker::ClearRecipes()
{
while( m_pState->Recipes.size() > 0 )
@@ -57,6 +78,10 @@ void cRecipeChecker::ClearRecipes()
}
}
+
+
+
+
void PrintRecipe( std::vector< cRecipeChecker::RecipeSlot > & RecipeSlots )
{
LOG("Recipe:");
@@ -67,6 +92,10 @@ void PrintRecipe( std::vector< cRecipeChecker::RecipeSlot > & RecipeSlots )
}
}
+
+
+
+
void PrintNear( std::ifstream & f, int a_History = 64 )
{
f.clear();
@@ -91,34 +120,14 @@ void PrintNear( std::ifstream & f, int a_History = 64 )
LOGERROR("Error near: \"%s\"", Near.c_str() );
}
-void cRecipeChecker::ReloadRecipes()
-{
- LOG("--Loading recipes--");
- ClearRecipes();
- /*
- char a_File[] = "recipes.txt";
- FILE* f = 0;
- #ifdef _WIN32
- if( fopen_s(&f, a_File, "rb" ) == 0 ) // no error
- #else
- if( (f = fopen(a_File, "rb" )) != 0 ) // no error
- #endif
- {
- char c;
- while( fread( &c, sizeof(char), 1, f) == 1 )
- {
- }
- }
- else
- {
- LOG("Could not open file for recipes: %s", a_File);
- return;
- }
- */
+void cRecipeChecker::ReloadRecipes()
+{
+ LOG("--Loading recipes--");
+ ClearRecipes();
std::ifstream f;
@@ -361,19 +370,13 @@ void cRecipeChecker::ReloadRecipes()
}
f.close();
- LOG("Found %i recipes", m_pState->Recipes.size() );
-// for(RecipeList::iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr )
-// {
-// LOG("Recipe for %i times %i", (*itr)->Result.m_ItemCount, (*itr)->Result.m_ItemID );
-// for(unsigned int j = 0; j < (*itr)->NumItems; j++)
-// {
-// RecipeSlot Slot = (*itr)->Slots[j];
-// LOG("%i %i %i %i", Slot.x, Slot.y, Slot.Item.m_ItemID, Slot.Item.m_ItemCount );
-// }
-// }
- LOG("--Done loading recipes--");
+ LOG("--Done loading recipes, found %i recipes", m_pState->Recipes.size() );
}
+
+
+
+
cItem cRecipeChecker::CookIngredients( cItem* a_Items, int a_Width, int a_Height, bool a_bConsumeIngredients /* = false */ )
{
int iLeft = 999, iTop = 999;
@@ -459,3 +462,7 @@ cItem cRecipeChecker::CookIngredients( cItem* a_Items, int a_Width, int a_Height
}
return cItem();
}
+
+
+
+
diff --git a/source/cWebAdmin.cpp b/source/cWebAdmin.cpp
index 23348fcdc..70f89528c 100644
--- a/source/cWebAdmin.cpp
+++ b/source/cWebAdmin.cpp
@@ -245,6 +245,10 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
}
}
+
+
+
+
bool cWebAdmin::Init( int a_Port )
{
m_Port = a_Port;
@@ -289,45 +293,44 @@ void *cWebAdmin::ListenThread( void *lpParam )
return 0;
}
+
+
+
+
std::string cWebAdmin::GetTemplate()
{
std::string retVal = "";
char SourceFile[] = "webadmin/template.html";
- FILE* f;
-#ifdef _WIN32
- if( fopen_s(&f, SourceFile, "rb" ) == 0 ) // no error
-#else
- if( (f = fopen(SourceFile, "rb" ) ) != 0 ) // no error
-#endif
+ cFile f;
+ if (!f.Open(SourceFile, cFile::fmRead))
{
- // obtain file size:
- fseek (f , 0 , SEEK_END);
- long lSize = ftell (f);
- rewind (f);
+ return "";
+ }
- // allocate memory to contain the whole file:
- char* buffer = (char*) malloc (sizeof(char)*lSize);
+ // obtain file size:
+ int lSize = f.GetSize();
- // copy the file into the buffer:
- size_t result = fread (buffer, 1, lSize, f);
- if ((long)result != lSize)
- {
- LOG ("WEBADMIN: Could not read file %s", SourceFile);
- free( buffer );
- return "";
- }
+ // allocate memory to contain the whole file:
+ std::auto_ptr<char> buffer(new char[lSize]); // auto_ptr deletes the memory in its destructor
- retVal.assign( buffer, lSize );
-
- free( buffer );
- fclose(f);
+ // copy the file into the buffer:
+ if (f.Read(buffer.get(), lSize) != lSize)
+ {
+ LOG ("WEBADMIN: Could not read file \"%s\"", SourceFile);
+ return "";
}
+
+ retVal.assign(buffer.get(), lSize );
+
return retVal;
}
+
+
+
void cWebAdmin::RemovePlugin( lua_State* L )
{
for( PluginList::iterator itr = m_Plugins.begin(); itr != m_Plugins.end(); )