Skip to content

Commit aa172bd

Browse files
committedNov 17, 2013
Handle blank blocks in database
Fix screwed-up indentation
1 parent a439aea commit aa172bd

File tree

1 file changed

+96
-85
lines changed

1 file changed

+96
-85
lines changed
 

‎src/database-sqlite3.cpp

+96-85
Original file line numberDiff line numberDiff line change
@@ -208,93 +208,104 @@ void Database_SQLite3::saveBlock(MapBlock *block)
208208
MapBlock* Database_SQLite3::loadBlock(v3s16 blockpos)
209209
{
210210
v2s16 p2d(blockpos.X, blockpos.Z);
211-
verifyDatabase();
212-
213-
if(sqlite3_bind_int64(m_database_read, 1, getBlockAsInteger(blockpos)) != SQLITE_OK)
214-
infostream<<"WARNING: Could not bind block position for load: "
215-
<<sqlite3_errmsg(m_database)<<std::endl;
216-
if(sqlite3_step(m_database_read) == SQLITE_ROW) {
217-
/*
218-
Make sure sector is loaded
219-
*/
220-
MapSector *sector = srvmap->createSector(p2d);
221-
222-
/*
223-
Load block
224-
*/
225-
const char * data = (const char *)sqlite3_column_blob(m_database_read, 0);
226-
size_t len = sqlite3_column_bytes(m_database_read, 0);
227-
228-
std::string datastr(data, len);
229-
230-
// srvmap->loadBlock(&datastr, blockpos, sector, false);
211+
verifyDatabase();
212+
213+
if (sqlite3_bind_int64(m_database_read, 1, getBlockAsInteger(blockpos)) != SQLITE_OK) {
214+
infostream << "WARNING: Could not bind block position for load: "
215+
<< sqlite3_errmsg(m_database)<<std::endl;
216+
}
217+
218+
if (sqlite3_step(m_database_read) == SQLITE_ROW) {
219+
/*
220+
Make sure sector is loaded
221+
*/
222+
MapSector *sector = srvmap->createSector(p2d);
223+
224+
/*
225+
Load block
226+
*/
227+
const char *data = (const char *)sqlite3_column_blob(m_database_read, 0);
228+
size_t len = sqlite3_column_bytes(m_database_read, 0);
229+
if (data == NULL || len == 0) {
230+
errorstream << "Blank block data in database (data == NULL || len"
231+
" == 0) (" << blockpos.X << "," << blockpos.Y << ","
232+
<< blockpos.Z << ")" << std::endl;
233+
234+
if (g_settings->getBool("ignore_world_load_errors")) {
235+
errorstream << "Ignoring block load error. Duck and cover! "
236+
<< "(ignore_world_load_errors)" << std::endl;
237+
} else {
238+
throw SerializationError("Blank block data in database");
239+
}
240+
}
241+
242+
std::string datastr(data, len);
243+
244+
//srvmap->loadBlock(&datastr, blockpos, sector, false);
231245

232246
try {
233-
std::istringstream is(datastr, std::ios_base::binary);
234-
235-
u8 version = SER_FMT_VER_INVALID;
236-
is.read((char*)&version, 1);
237-
238-
if(is.fail())
239-
throw SerializationError("ServerMap::loadBlock(): Failed"
240-
" to read MapBlock version");
241-
242-
MapBlock *block = NULL;
243-
bool created_new = false;
244-
block = sector->getBlockNoCreateNoEx(blockpos.Y);
245-
if(block == NULL)
246-
{
247-
block = sector->createBlankBlockNoInsert(blockpos.Y);
248-
created_new = true;
249-
}
250-
251-
// Read basic data
252-
block->deSerialize(is, version, true);
253-
254-
// If it's a new block, insert it to the map
255-
if(created_new)
256-
sector->insertBlock(block);
257-
258-
/*
259-
Save blocks loaded in old format in new format
260-
*/
261-
262-
//if(version < SER_FMT_VER_HIGHEST || save_after_load)
263-
// Only save if asked to; no need to update version
264-
//if(save_after_load)
265-
// saveBlock(block);
266-
267-
// We just loaded it from, so it's up-to-date.
268-
block->resetModified();
269-
270-
}
271-
catch(SerializationError &e)
272-
{
273-
errorstream<<"Invalid block data in database"
274-
<<" ("<<blockpos.X<<","<<blockpos.Y<<","<<blockpos.Z<<")"
275-
<<" (SerializationError): "<<e.what()<<std::endl;
276-
277-
// TODO: Block should be marked as invalid in memory so that it is
278-
// not touched but the game can run
279-
280-
if(g_settings->getBool("ignore_world_load_errors")){
281-
errorstream<<"Ignoring block load error. Duck and cover! "
282-
<<"(ignore_world_load_errors)"<<std::endl;
283-
} else {
284-
throw SerializationError("Invalid block data in database");
285-
//assert(0);
286-
}
287-
}
288-
289-
290-
sqlite3_step(m_database_read);
291-
// We should never get more than 1 row, so ok to reset
292-
sqlite3_reset(m_database_read);
293-
294-
return srvmap->getBlockNoCreateNoEx(blockpos); // should not be using this here
295-
}
296-
sqlite3_reset(m_database_read);
297-
return(NULL);
247+
std::istringstream is(datastr, std::ios_base::binary);
248+
249+
u8 version = SER_FMT_VER_INVALID;
250+
is.read((char*)&version, 1);
251+
252+
if (is.fail())
253+
throw SerializationError("ServerMap::loadBlock(): Failed"
254+
" to read MapBlock version");
255+
256+
MapBlock *block = NULL;
257+
bool created_new = false;
258+
block = sector->getBlockNoCreateNoEx(blockpos.Y);
259+
if (block == NULL)
260+
{
261+
block = sector->createBlankBlockNoInsert(blockpos.Y);
262+
created_new = true;
263+
}
264+
265+
// Read basic data
266+
block->deSerialize(is, version, true);
267+
268+
// If it's a new block, insert it to the map
269+
if (created_new)
270+
sector->insertBlock(block);
271+
272+
/*
273+
Save blocks loaded in old format in new format
274+
*/
275+
//if(version < SER_FMT_VER_HIGHEST || save_after_load)
276+
// Only save if asked to; no need to update version
277+
//if(save_after_load)
278+
// saveBlock(block);
279+
280+
// We just loaded it from, so it's up-to-date.
281+
block->resetModified();
282+
}
283+
catch(SerializationError &e)
284+
{
285+
errorstream << "Invalid block data in database"
286+
<< " (" << blockpos.X << "," << blockpos.Y << "," << blockpos.Z << ")"
287+
<< " (SerializationError): " << e.what() << std::endl;
288+
289+
// TODO: Block should be marked as invalid in memory so that it is
290+
// not touched but the game can run
291+
292+
if (g_settings->getBool("ignore_world_load_errors")) {
293+
errorstream << "Ignoring block load error. Duck and cover! "
294+
<< "(ignore_world_load_errors)" << std::endl;
295+
} else {
296+
throw SerializationError("Invalid block data in database");
297+
//assert(0);
298+
}
299+
}
300+
301+
sqlite3_step(m_database_read);
302+
// We should never get more than 1 row, so ok to reset
303+
sqlite3_reset(m_database_read);
304+
305+
return srvmap->getBlockNoCreateNoEx(blockpos); // should not be using this here
306+
}
307+
sqlite3_reset(m_database_read);
308+
return NULL;
298309
}
299310

300311
void Database_SQLite3::createDatabase()

0 commit comments

Comments
 (0)
Please sign in to comment.