Various style cleanups + unused code removal

-> Don't pass pointer to whole IGameDef to NodeMetadata constructors
	and deserializers, but only to IItemDefManager, which is needed
-> Remove the unused content_mapnode_get_new_name() method
-> Fix style for MapBlock::deSerialize and MapBlock::deSerialize_pre22,
	improving accuracy of error messages a bit
-> Fix style at other serialisation methods too
-> Improve accuracy of some comments
This commit is contained in:
est31 2015-09-18 13:45:42 +02:00
parent 9c635f28ac
commit 452df1c723
11 changed files with 75 additions and 169 deletions

View File

@ -167,69 +167,3 @@ void content_mapnode_get_name_id_mapping(NameIdMapping *nimap)
nimap->set(CONTENT_AIR, "air"); nimap->set(CONTENT_AIR, "air");
} }
class NewNameGetter
{
public:
NewNameGetter()
{
old_to_new["CONTENT_STONE"] = "default:stone";
old_to_new["CONTENT_WATER"] = "default:water_flowing";
old_to_new["CONTENT_TORCH"] = "default:torch";
old_to_new["CONTENT_WATERSOURCE"] = "default:water_source";
old_to_new["CONTENT_SIGN_WALL"] = "default:sign_wall";
old_to_new["CONTENT_CHEST"] = "default:chest";
old_to_new["CONTENT_FURNACE"] = "default:furnace";
old_to_new["CONTENT_LOCKABLE_CHEST"] = "default:locked_chest";
old_to_new["CONTENT_FENCE"] = "default:wooden_fence";
old_to_new["CONTENT_RAIL"] = "default:rail";
old_to_new["CONTENT_LADDER"] = "default:ladder";
old_to_new["CONTENT_LAVA"] = "default:lava_flowing";
old_to_new["CONTENT_LAVASOURCE"] = "default:lava_source";
old_to_new["CONTENT_GRASS"] = "default:dirt_with_grass";
old_to_new["CONTENT_TREE"] = "default:tree";
old_to_new["CONTENT_LEAVES"] = "default:leaves";
old_to_new["CONTENT_GRASS_FOOTSTEPS"] = "default:dirt_with_grass_footsteps";
old_to_new["CONTENT_MESE"] = "default:mese";
old_to_new["CONTENT_MUD"] = "default:dirt";
old_to_new["CONTENT_CLOUD"] = "default:cloud";
old_to_new["CONTENT_COALSTONE"] = "default:coalstone";
old_to_new["CONTENT_WOOD"] = "default:wood";
old_to_new["CONTENT_SAND"] = "default:sand";
old_to_new["CONTENT_COBBLE"] = "default:cobble";
old_to_new["CONTENT_STEEL"] = "default:steel";
old_to_new["CONTENT_GLASS"] = "default:glass";
old_to_new["CONTENT_MOSSYCOBBLE"] = "default:mossycobble";
old_to_new["CONTENT_GRAVEL"] = "default:gravel";
old_to_new["CONTENT_SANDSTONE"] = "default:sandstone";
old_to_new["CONTENT_CACTUS"] = "default:cactus";
old_to_new["CONTENT_BRICK"] = "default:brick";
old_to_new["CONTENT_CLAY"] = "default:clay";
old_to_new["CONTENT_PAPYRUS"] = "default:papyrus";
old_to_new["CONTENT_BOOKSHELF"] = "default:bookshelf";
old_to_new["CONTENT_JUNGLETREE"] = "default:jungletree";
old_to_new["CONTENT_JUNGLEGRASS"] = "default:junglegrass";
old_to_new["CONTENT_NC"] = "default:nyancat";
old_to_new["CONTENT_NC_RB"] = "default:nyancat_rainbow";
old_to_new["CONTENT_APPLE"] = "default:apple";
old_to_new["CONTENT_SAPLING"] = "default:sapling";
// Just in case
old_to_new["CONTENT_IGNORE"] = "ignore";
old_to_new["CONTENT_AIR"] = "air";
}
std::string get(const std::string &old)
{
StringMap::const_iterator it = old_to_new.find(old);
if (it == old_to_new.end())
return "";
return it->second;
}
private:
StringMap old_to_new;
};
NewNameGetter newnamegetter;
std::string content_mapnode_get_new_name(const std::string &oldname)
{
return newnamegetter.get(oldname);
}

View File

@ -34,8 +34,4 @@ MapNode mapnode_translate_to_internal(MapNode n_from, u8 version);
class NameIdMapping; class NameIdMapping;
void content_mapnode_get_name_id_mapping(NameIdMapping *nimap); void content_mapnode_get_name_id_mapping(NameIdMapping *nimap);
// Convert "CONTENT_STONE"-style names to dynamic ids
std::string content_mapnode_get_new_name(const std::string &oldname);
class INodeDefManager;
#endif #endif

View File

@ -79,7 +79,7 @@ static bool content_nodemeta_deserialize_legacy_body(
inv->getList("0")->setName("main"); inv->getList("0")->setName("main");
} }
assert(inv->getList("main") && !inv->getList("0")); assert(inv->getList("main") && !inv->getList("0"));
meta->setString("formspec","size[8,9]" meta->setString("formspec","size[8,9]"
"list[current_name;main;0,0;8,4;]" "list[current_name;main;0,0;8,4;]"
"list[current_player;main;0,5;8,4;]"); "list[current_player;main;0,5;8,4;]");
@ -96,7 +96,7 @@ static bool content_nodemeta_deserialize_legacy_body(
inv->getList("0")->setName("main"); inv->getList("0")->setName("main");
} }
assert(inv->getList("main") && !inv->getList("0")); assert(inv->getList("main") && !inv->getList("0"));
meta->setString("formspec","size[8,9]" meta->setString("formspec","size[8,9]"
"list[current_name;main;0,0;8,4;]" "list[current_name;main;0,0;8,4;]"
"list[current_player;main;0,5;8,4;]"); "list[current_player;main;0,5;8,4;]");
@ -145,7 +145,7 @@ static bool content_nodemeta_deserialize_legacy_meta(
void content_nodemeta_deserialize_legacy(std::istream &is, void content_nodemeta_deserialize_legacy(std::istream &is,
NodeMetadataList *meta, NodeTimerList *timers, NodeMetadataList *meta, NodeTimerList *timers,
IGameDef *gamedef) IItemDefManager *item_def_mgr)
{ {
meta->clear(); meta->clear();
timers->clear(); timers->clear();
@ -181,7 +181,7 @@ void content_nodemeta_deserialize_legacy(std::istream &is,
continue; continue;
} }
NodeMetadata *data = new NodeMetadata(gamedef); NodeMetadata *data = new NodeMetadata(item_def_mgr);
bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data); bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data);
meta->set(p, data); meta->set(p, data);

View File

@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class NodeMetadataList; class NodeMetadataList;
class NodeTimerList; class NodeTimerList;
class IGameDef; class IItemDefManager;
/* /*
Legacy nodemeta definitions Legacy nodemeta definitions
@ -32,7 +32,7 @@ class IGameDef;
void content_nodemeta_deserialize_legacy(std::istream &is, void content_nodemeta_deserialize_legacy(std::istream &is,
NodeMetadataList *meta, NodeTimerList *timers, NodeMetadataList *meta, NodeTimerList *timers,
IGameDef *gamedef); IItemDefManager *item_def_mgr);
void content_nodemeta_serialize_legacy(std::ostream &os, NodeMetadataList *meta); void content_nodemeta_serialize_legacy(std::ostream &os, NodeMetadataList *meta);

View File

@ -696,19 +696,17 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos()) TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
<<": Node metadata"<<std::endl); <<": Node metadata"<<std::endl);
// Ignore errors // Ignore errors
try{ try {
std::ostringstream oss(std::ios_base::binary); std::ostringstream oss(std::ios_base::binary);
decompressZlib(is, oss); decompressZlib(is, oss);
std::istringstream iss(oss.str(), std::ios_base::binary); std::istringstream iss(oss.str(), std::ios_base::binary);
if(version >= 23) if (version >= 23)
m_node_metadata.deSerialize(iss, m_gamedef); m_node_metadata.deSerialize(iss, m_gamedef->idef());
else else
content_nodemeta_deserialize_legacy(iss, content_nodemeta_deserialize_legacy(iss,
&m_node_metadata, &m_node_timers, &m_node_metadata, &m_node_timers,
m_gamedef); m_gamedef->idef());
} } catch(SerializationError &e) {
catch(SerializationError &e)
{
errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error" errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error"
<<" while deserializing node metadata at (" <<" while deserializing node metadata at ("
<<PP(getPos())<<": "<<e.what()<<std::endl; <<PP(getPos())<<": "<<e.what()<<std::endl;
@ -794,23 +792,20 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
SharedBuffer<u8> databuf_nodelist(nodecount * ser_length); SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
// These have no compression // These have no compression
if(version <= 3 || version == 5 || version == 6) if (version <= 3 || version == 5 || version == 6) {
{
char tmp; char tmp;
is.read(&tmp, 1); is.read(&tmp, 1);
if(is.gcount() != 1) if (is.gcount() != 1)
throw SerializationError throw SerializationError(std::string(__FUNCTION_NAME)
("MapBlock::deSerialize: no enough input data"); + ": no enough input data");
is_underground = tmp; is_underground = tmp;
is.read((char*)*databuf_nodelist, nodecount * ser_length); is.read((char *)*databuf_nodelist, nodecount * ser_length);
if((u32)is.gcount() != nodecount * ser_length) if ((u32)is.gcount() != nodecount * ser_length)
throw SerializationError throw SerializationError(std::string(__FUNCTION_NAME)
("MapBlock::deSerialize: no enough input data"); + ": no enough input data");
} } else if (version <= 10) {
else if(version <= 10)
{
u8 t8; u8 t8;
is.read((char*)&t8, 1); is.read((char *)&t8, 1);
is_underground = t8; is_underground = t8;
{ {
@ -818,11 +813,10 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
decompress(is, os, version); decompress(is, os, version);
std::string s = os.str(); std::string s = os.str();
if(s.size() != nodecount) if (s.size() != nodecount)
throw SerializationError throw SerializationError(std::string(__FUNCTION_NAME)
("MapBlock::deSerialize: invalid format"); + ": no enough input data");
for(u32 i=0; i<s.size(); i++) for (u32 i = 0; i < s.size(); i++) {
{
databuf_nodelist[i*ser_length] = s[i]; databuf_nodelist[i*ser_length] = s[i];
} }
} }
@ -831,33 +825,27 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
decompress(is, os, version); decompress(is, os, version);
std::string s = os.str(); std::string s = os.str();
if(s.size() != nodecount) if (s.size() != nodecount)
throw SerializationError throw SerializationError(std::string(__FUNCTION_NAME)
("MapBlock::deSerialize: invalid format"); + ": no enough input data");
for(u32 i=0; i<s.size(); i++) for (u32 i = 0; i < s.size(); i++) {
{
databuf_nodelist[i*ser_length + 1] = s[i]; databuf_nodelist[i*ser_length + 1] = s[i];
} }
} }
if(version >= 10) if (version >= 10) {
{
// Uncompress and set param2 data // Uncompress and set param2 data
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
decompress(is, os, version); decompress(is, os, version);
std::string s = os.str(); std::string s = os.str();
if(s.size() != nodecount) if (s.size() != nodecount)
throw SerializationError throw SerializationError(std::string(__FUNCTION_NAME)
("MapBlock::deSerialize: invalid format"); + ": no enough input data");
for(u32 i=0; i<s.size(); i++) for (u32 i = 0; i < s.size(); i++) {
{
databuf_nodelist[i*ser_length + 2] = s[i]; databuf_nodelist[i*ser_length + 2] = s[i];
} }
} }
} } else { // All other versions (10 to 21)
// All other versions (newest)
else
{
u8 flags; u8 flags;
is.read((char*)&flags, 1); is.read((char*)&flags, 1);
is_underground = (flags & 0x01) ? true : false; is_underground = (flags & 0x01) ? true : false;
@ -870,14 +858,12 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
decompress(is, os, version); decompress(is, os, version);
std::string s = os.str(); std::string s = os.str();
if(s.size() != nodecount*3) if (s.size() != nodecount * 3)
throw SerializationError throw SerializationError(std::string(__FUNCTION_NAME)
("MapBlock::deSerialize: decompress resulted in size" + ": decompress resulted in size other than nodecount*3");
" other than nodecount*3");
// deserialize nodes from buffer // deserialize nodes from buffer
for(u32 i=0; i<nodecount; i++) for (u32 i = 0; i < nodecount; i++) {
{
databuf_nodelist[i*ser_length] = s[i]; databuf_nodelist[i*ser_length] = s[i];
databuf_nodelist[i*ser_length + 1] = s[i+nodecount]; databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2]; databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
@ -886,31 +872,25 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
/* /*
NodeMetadata NodeMetadata
*/ */
if(version >= 14) if (version >= 14) {
{
// Ignore errors // Ignore errors
try{ try {
if(version <= 15) if (version <= 15) {
{
std::string data = deSerializeString(is); std::string data = deSerializeString(is);
std::istringstream iss(data, std::ios_base::binary); std::istringstream iss(data, std::ios_base::binary);
content_nodemeta_deserialize_legacy(iss, content_nodemeta_deserialize_legacy(iss,
&m_node_metadata, &m_node_timers, &m_node_metadata, &m_node_timers,
m_gamedef); m_gamedef->idef());
} } else {
else
{
//std::string data = deSerializeLongString(is); //std::string data = deSerializeLongString(is);
std::ostringstream oss(std::ios_base::binary); std::ostringstream oss(std::ios_base::binary);
decompressZlib(is, oss); decompressZlib(is, oss);
std::istringstream iss(oss.str(), std::ios_base::binary); std::istringstream iss(oss.str(), std::ios_base::binary);
content_nodemeta_deserialize_legacy(iss, content_nodemeta_deserialize_legacy(iss,
&m_node_metadata, &m_node_timers, &m_node_metadata, &m_node_timers,
m_gamedef); m_gamedef->idef());
} }
} } catch(SerializationError &e) {
catch(SerializationError &e)
{
errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error" errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error"
<<" while deserializing node metadata"<<std::endl; <<" while deserializing node metadata"<<std::endl;
} }
@ -918,17 +898,15 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
} }
// Deserialize node data // Deserialize node data
for(u32 i=0; i<nodecount; i++) for (u32 i = 0; i < nodecount; i++) {
{ data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
data[i].deSerialize(&databuf_nodelist[i*ser_length], version);
} }
if(disk) if (disk) {
{
/* /*
Versions up from 9 have block objects. (DEPRECATED) Versions up from 9 have block objects. (DEPRECATED)
*/ */
if(version >= 9){ if (version >= 9) {
u16 count = readU16(is); u16 count = readU16(is);
// Not supported and length not known if count is not 0 // Not supported and length not known if count is not 0
if(count != 0){ if(count != 0){
@ -941,11 +919,11 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
/* /*
Versions up from 15 have static objects. Versions up from 15 have static objects.
*/ */
if(version >= 15) if (version >= 15)
m_static_objects.deSerialize(is); m_static_objects.deSerialize(is);
// Timestamp // Timestamp
if(version >= 17){ if (version >= 17) {
setTimestamp(readU32(is)); setTimestamp(readU32(is));
m_disk_timestamp = m_timestamp; m_disk_timestamp = m_timestamp;
} else { } else {
@ -955,7 +933,7 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
// Dynamically re-set ids based on node names // Dynamically re-set ids based on node names
NameIdMapping nimap; NameIdMapping nimap;
// If supported, read node definition id mapping // If supported, read node definition id mapping
if(version >= 21){ if (version >= 21) {
nimap.deSerialize(is); nimap.deSerialize(is);
// Else set the legacy mapping // Else set the legacy mapping
} else { } else {

View File

@ -29,7 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "content_sao.h" #include "content_sao.h"
#include "nodedef.h" #include "nodedef.h"
#include "emerge.h" #include "emerge.h"
#include "content_mapnode.h" // For content_mapnode_get_new_name
#include "voxelalgorithms.h" #include "voxelalgorithms.h"
#include "porting.h" #include "porting.h"
#include "profiler.h" #include "profiler.h"

View File

@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
//#include "serverobject.h" //#include "serverobject.h"
#include "content_sao.h" #include "content_sao.h"
#include "nodedef.h" #include "nodedef.h"
#include "content_mapnode.h" // For content_mapnode_get_new_name
#include "voxelalgorithms.h" #include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker //#include "profiler.h" // For TimeTaker
#include "settings.h" // For g_settings #include "settings.h" // For g_settings

View File

@ -30,9 +30,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
NodeMetadata NodeMetadata
*/ */
NodeMetadata::NodeMetadata(IGameDef *gamedef): NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
m_stringvars(), m_stringvars(),
m_inventory(new Inventory(gamedef->idef())) m_inventory(new Inventory(item_def_mgr))
{ {
} }
@ -101,34 +101,34 @@ void NodeMetadataList::serialize(std::ostream &os) const
v3s16 p = i->first; v3s16 p = i->first;
NodeMetadata *data = i->second; NodeMetadata *data = i->second;
u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X; u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
writeU16(os, p16); writeU16(os, p16);
data->serialize(os); data->serialize(os);
} }
} }
void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef) void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
{ {
clear(); clear();
u8 version = readU8(is); u8 version = readU8(is);
if(version == 0){ if (version == 0) {
// Nothing // Nothing
return; return;
} }
if(version != 1){ if (version != 1) {
infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported" std::string err_str = std::string(__FUNCTION_NAME)
<<std::endl; + ": version " + itos(version) + " not supported";
throw SerializationError("NodeMetadataList::deSerialize"); infostream << err_str << std::endl;
throw SerializationError(err_str);
} }
u16 count = readU16(is); u16 count = readU16(is);
for(u16 i=0; i<count; i++) for (u16 i=0; i < count; i++) {
{
u16 p16 = readU16(is); u16 p16 = readU16(is);
v3s16 p; v3s16 p;
@ -138,8 +138,7 @@ void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
p16 &= MAP_BLOCKSIZE - 1; p16 &= MAP_BLOCKSIZE - 1;
p.X = p16; p.X = p16;
if(m_data.find(p) != m_data.end()) if (m_data.find(p) != m_data.end()) {
{
infostream<<"WARNING: NodeMetadataList::deSerialize(): " infostream<<"WARNING: NodeMetadataList::deSerialize(): "
<<"already set data at position" <<"already set data at position"
<<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring." <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
@ -147,7 +146,7 @@ void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
continue; continue;
} }
NodeMetadata *data = new NodeMetadata(gamedef); NodeMetadata *data = new NodeMetadata(item_def_mgr);
data->deSerialize(is); data->deSerialize(is);
m_data[p] = data; m_data[p] = data;
} }

View File

@ -35,12 +35,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
class Inventory; class Inventory;
class IGameDef; class IItemDefManager;
class NodeMetadata class NodeMetadata
{ {
public: public:
NodeMetadata(IGameDef *gamedef); NodeMetadata(IItemDefManager *item_def_mgr);
~NodeMetadata(); ~NodeMetadata();
void serialize(std::ostream &os) const; void serialize(std::ostream &os) const;
@ -80,7 +80,7 @@ public:
~NodeMetadataList(); ~NodeMetadataList();
void serialize(std::ostream &os) const; void serialize(std::ostream &os) const;
void deSerialize(std::istream &is, IGameDef *gamedef); void deSerialize(std::istream &is, IItemDefManager *item_def_mgr);
// Add all keys in this list to the vector keys // Add all keys in this list to the vector keys
std::vector<v3s16> getAllKeys(); std::vector<v3s16> getAllKeys();

View File

@ -155,7 +155,7 @@ bool RollbackAction::applyRevert(Map *map, InventoryManager *imgr, IGameDef *gam
} else { } else {
NodeMetadata *meta = map->getNodeMetadata(p); NodeMetadata *meta = map->getNodeMetadata(p);
if (!meta) { if (!meta) {
meta = new NodeMetadata(gamedef); meta = new NodeMetadata(gamedef->idef());
if (!map->setNodeMetadata(p, meta)) { if (!map->setNodeMetadata(p, meta)) {
delete meta; delete meta;
infostream << "RollbackAction::applyRevert(): " infostream << "RollbackAction::applyRevert(): "

View File

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_content.h" #include "common/c_content.h"
#include "environment.h" #include "environment.h"
#include "map.h" #include "map.h"
#include "gamedef.h"
#include "nodemetadata.h" #include "nodemetadata.h"
@ -43,7 +44,7 @@ NodeMetadata* NodeMetaRef::getmeta(NodeMetaRef *ref, bool auto_create)
{ {
NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p); NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
if(meta == NULL && auto_create) { if(meta == NULL && auto_create) {
meta = new NodeMetadata(ref->m_env->getGameDef()); meta = new NodeMetadata(ref->m_env->getGameDef()->idef());
if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) { if(!ref->m_env->getMap().setNodeMetadata(ref->m_p, meta)) {
delete meta; delete meta;
return NULL; return NULL;