Replaced most FILE operations with a cFile object
git-svn-id: http://mc-server.googlecode.com/svn/trunk@196 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
parent
b834841aa1
commit
f4583fda98
@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
class cCriticalSection;
|
||||
class cEvent;
|
||||
class cChunk;
|
||||
class cChunkLoader
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,34 +54,31 @@ 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; }
|
||||
|
||||
|
||||
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);
|
||||
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];
|
||||
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;
|
||||
JSON_PlayerPosition.append( Json::Value( PlayerPos.x ) );
|
||||
@ -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() );
|
||||
}
|
||||
|
||||
|
||||
|
@ -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
|
||||
cFile f;
|
||||
if (!f.Open(SourceFile, cFile::fmRead))
|
||||
{
|
||||
// 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();
|
||||
}
|
||||
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
// Get file size
|
||||
long FileSize = f.GetSize();
|
||||
|
||||
char * buffer = new char[FileSize];
|
||||
if (f.Read(buffer, FileSize) != FileSize )
|
||||
{
|
||||
LOGERROR("ERROR READING FROM FILE \"%s\"", SourceFile);
|
||||
return false;
|
||||
}
|
||||
f.Close();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -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,35 +120,15 @@ 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;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
std::ifstream f;
|
||||
|
||||
char a_File[] = "recipes.txt";
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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);
|
||||
|
||||
// allocate memory to contain the whole file:
|
||||
char* buffer = (char*) malloc (sizeof(char)*lSize);
|
||||
|
||||
// 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 "";
|
||||
}
|
||||
|
||||
retVal.assign( buffer, lSize );
|
||||
|
||||
free( buffer );
|
||||
fclose(f);
|
||||
return "";
|
||||
}
|
||||
|
||||
// obtain file size:
|
||||
int lSize = f.GetSize();
|
||||
|
||||
// allocate memory to contain the whole file:
|
||||
std::auto_ptr<char> buffer(new char[lSize]); // auto_ptr deletes the memory in its destructor
|
||||
|
||||
// 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(); )
|
||||
|
Loading…
Reference in New Issue
Block a user