From 9ae31d913c40c86988f12ffc51d3e9e7ab982822 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 23 Jan 2014 14:21:56 +0100 Subject: Improved code safety for the Compact world storage. That was a huge chunk of smelly code. --- src/WorldStorage/WSSCompact.cpp | 176 ++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 104 deletions(-) diff --git a/src/WorldStorage/WSSCompact.cpp b/src/WorldStorage/WSSCompact.cpp index d020b73cc..4c0684dd8 100644 --- a/src/WorldStorage/WSSCompact.cpp +++ b/src/WorldStorage/WSSCompact.cpp @@ -265,146 +265,114 @@ bool cWSSCompact::EraseChunkData(const cChunkCoords & a_Chunk) void cWSSCompact::LoadEntitiesFromJson(Json::Value & a_Value, cEntityList & a_Entities, cBlockEntityList & a_BlockEntities, cWorld * a_World) { - // Load chests + // Load chests: Json::Value AllChests = a_Value.get("Chests", Json::nullValue); if (!AllChests.empty()) { for (Json::Value::iterator itr = AllChests.begin(); itr != AllChests.end(); ++itr ) { - Json::Value & Chest = *itr; - cChestEntity * ChestEntity = new cChestEntity(0,0,0, a_World); - if (!ChestEntity->LoadFromJson( Chest ) ) + std::auto_ptr ChestEntity(new cChestEntity(0, 0, 0, a_World)); + if (!ChestEntity->LoadFromJson(*itr)) { - LOGERROR("ERROR READING CHEST FROM JSON!" ); - delete ChestEntity; + LOGWARNING("ERROR READING CHEST FROM JSON!" ); } else { - a_BlockEntities.push_back( ChestEntity ); + a_BlockEntities.push_back(ChestEntity.release()); } } // for itr - AllChests[] } - // Load dispensers + // Load dispensers: Json::Value AllDispensers = a_Value.get("Dispensers", Json::nullValue); - if( !AllDispensers.empty() ) + for (Json::Value::iterator itr = AllDispensers.begin(); itr != AllDispensers.end(); ++itr) { - for( Json::Value::iterator itr = AllDispensers.begin(); itr != AllDispensers.end(); ++itr ) + std::auto_ptr DispenserEntity(new cDispenserEntity(0, 0, 0, a_World)); + if (!DispenserEntity->LoadFromJson(*itr)) { - Json::Value & Dispenser = *itr; - cDispenserEntity * DispenserEntity = new cDispenserEntity(0,0,0, a_World); - if( !DispenserEntity->LoadFromJson( Dispenser ) ) - { - LOGERROR("ERROR READING DISPENSER FROM JSON!" ); - delete DispenserEntity; - } - else - { - a_BlockEntities.push_back( DispenserEntity ); - } - } // for itr - AllDispensers[] - } + LOGWARNING("ERROR READING DISPENSER FROM JSON!" ); + } + else + { + a_BlockEntities.push_back(DispenserEntity.release()); + } + } // for itr - AllDispensers[] - // Load furnaces + // Load furnaces: Json::Value AllFurnaces = a_Value.get("Furnaces", Json::nullValue); - if( !AllFurnaces.empty() ) + for (Json::Value::iterator itr = AllFurnaces.begin(); itr != AllFurnaces.end(); ++itr) { - for( Json::Value::iterator itr = AllFurnaces.begin(); itr != AllFurnaces.end(); ++itr ) + // TODO: The block type and meta aren't correct, there's no way to get them here + std::auto_ptr FurnaceEntity(new cFurnaceEntity(0, 0, 0, E_BLOCK_FURNACE, 0, a_World)); + if (!FurnaceEntity->LoadFromJson(*itr)) { - Json::Value & Furnace = *itr; - // TODO: The block type and meta aren't correct, there's no way to get them here - cFurnaceEntity * FurnaceEntity = new cFurnaceEntity(0, 0, 0, E_BLOCK_FURNACE, 0, a_World); - if (!FurnaceEntity->LoadFromJson(Furnace)) - { - LOGERROR("ERROR READING FURNACE FROM JSON!" ); - delete FurnaceEntity; - } - else - { - a_BlockEntities.push_back(FurnaceEntity); - } - } // for itr - AllFurnaces[] - } + LOGWARNING("ERROR READING FURNACE FROM JSON!" ); + } + else + { + a_BlockEntities.push_back(FurnaceEntity.release()); + } + } // for itr - AllFurnaces[] - // Load signs + // Load signs: Json::Value AllSigns = a_Value.get("Signs", Json::nullValue); - if( !AllSigns.empty() ) + for (Json::Value::iterator itr = AllSigns.begin(); itr != AllSigns.end(); ++itr) { - for( Json::Value::iterator itr = AllSigns.begin(); itr != AllSigns.end(); ++itr ) + std::auto_ptr SignEntity(new cSignEntity(E_BLOCK_SIGN_POST, 0, 0, 0, a_World)); + if (!SignEntity->LoadFromJson(*itr)) { - Json::Value & Sign = *itr; - cSignEntity * SignEntity = new cSignEntity( E_BLOCK_SIGN_POST, 0,0,0, a_World); - if ( !SignEntity->LoadFromJson( Sign ) ) - { - LOGERROR("ERROR READING SIGN FROM JSON!" ); - delete SignEntity; - } - else - { - a_BlockEntities.push_back( SignEntity ); - } - } // for itr - AllSigns[] - } + LOGWARNING("ERROR READING SIGN FROM JSON!"); + } + else + { + a_BlockEntities.push_back(SignEntity.release()); + } + } // for itr - AllSigns[] - // Load note blocks + // Load note blocks: Json::Value AllNotes = a_Value.get("Notes", Json::nullValue); - if( !AllNotes.empty() ) + for( Json::Value::iterator itr = AllNotes.begin(); itr != AllNotes.end(); ++itr ) { - for( Json::Value::iterator itr = AllNotes.begin(); itr != AllNotes.end(); ++itr ) + std::auto_ptr NoteEntity(new cNoteEntity(0, 0, 0, a_World)); + if (!NoteEntity->LoadFromJson(*itr)) { - Json::Value & Note = *itr; - cNoteEntity * NoteEntity = new cNoteEntity(0, 0, 0, a_World); - if ( !NoteEntity->LoadFromJson( Note ) ) - { - LOGERROR("ERROR READING NOTE BLOCK FROM JSON!" ); - delete NoteEntity; - } - else - { - a_BlockEntities.push_back( NoteEntity ); - } - } // for itr - AllNotes[] - } + LOGWARNING("ERROR READING NOTE BLOCK FROM JSON!" ); + } + else + { + a_BlockEntities.push_back(NoteEntity.release()); + } + } // for itr - AllNotes[] - // Load jukeboxes + // Load jukeboxes: Json::Value AllJukeboxes = a_Value.get("Jukeboxes", Json::nullValue); - if( !AllJukeboxes.empty() ) + for( Json::Value::iterator itr = AllJukeboxes.begin(); itr != AllJukeboxes.end(); ++itr ) { - for( Json::Value::iterator itr = AllJukeboxes.begin(); itr != AllJukeboxes.end(); ++itr ) + std::auto_ptr JukeboxEntity(new cJukeboxEntity(0, 0, 0, a_World)); + if (!JukeboxEntity->LoadFromJson(*itr)) { - Json::Value & Jukebox = *itr; - cJukeboxEntity * JukeboxEntity = new cJukeboxEntity(0, 0, 0, a_World); - if ( !JukeboxEntity->LoadFromJson( Jukebox ) ) - { - LOGERROR("ERROR READING JUKEBOX FROM JSON!" ); - delete JukeboxEntity; - } - else - { - a_BlockEntities.push_back( JukeboxEntity ); - } - } // for itr - AllJukeboxes[] - } + LOGWARNING("ERROR READING JUKEBOX FROM JSON!" ); + } + else + { + a_BlockEntities.push_back(JukeboxEntity.release()); + } + } // for itr - AllJukeboxes[] - // Load command blocks + // Load command blocks: Json::Value AllCommandBlocks = a_Value.get("CommandBlocks", Json::nullValue); - if( !AllCommandBlocks.empty() ) + for( Json::Value::iterator itr = AllCommandBlocks.begin(); itr != AllCommandBlocks.end(); ++itr ) { - for( Json::Value::iterator itr = AllCommandBlocks.begin(); itr != AllCommandBlocks.end(); ++itr ) + std::auto_ptr CommandBlockEntity(new cCommandBlockEntity(0, 0, 0, a_World)); + if (!CommandBlockEntity->LoadFromJson(*itr)) { - Json::Value & CommandBlock = *itr; - cCommandBlockEntity * CommandBlockEntity = new cCommandBlockEntity(0, 0, 0, a_World); - if ( !CommandBlockEntity->LoadFromJson( CommandBlock ) ) - { - LOGERROR("ERROR READING COMMAND BLOCK FROM JSON!" ); - delete CommandBlockEntity; - } - else - { - a_BlockEntities.push_back( CommandBlockEntity ); - } - } // for itr - AllCommandBlocks[] - } + LOGWARNING("ERROR READING COMMAND BLOCK FROM JSON!" ); + } + else + { + a_BlockEntities.push_back(CommandBlockEntity.release()); + } + } // for itr - AllCommandBlocks[] } -- cgit v1.2.3