summaryrefslogtreecommitdiff
path: root/src/nodemetadata.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/nodemetadata.cpp')
-rw-r--r--src/nodemetadata.cpp103
1 files changed, 52 insertions, 51 deletions
diff --git a/src/nodemetadata.cpp b/src/nodemetadata.cpp
index 0801a028b..0e8195c34 100644
--- a/src/nodemetadata.cpp
+++ b/src/nodemetadata.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "inventory.h"
#include "log.h"
#include "util/serialize.h"
+#include "util/basic_macros.h"
#include "constants.h" // MAP_BLOCKSIZE
#include <sstream>
@@ -31,38 +32,46 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
- m_stringvars(),
m_inventory(new Inventory(item_def_mgr))
-{
-}
+{}
NodeMetadata::~NodeMetadata()
{
delete m_inventory;
}
-void NodeMetadata::serialize(std::ostream &os) const
+void NodeMetadata::serialize(std::ostream &os, u8 version, bool disk) const
{
- int num_vars = m_stringvars.size();
+ int num_vars = disk ? m_stringvars.size() : countNonPrivate();
writeU32(os, num_vars);
for (StringMap::const_iterator
it = m_stringvars.begin();
it != m_stringvars.end(); ++it) {
+ bool priv = isPrivate(it->first);
+ if (!disk && priv)
+ continue;
+
os << serializeString(it->first);
os << serializeLongString(it->second);
+ if (version >= 2)
+ writeU8(os, (priv) ? 1 : 0);
}
m_inventory->serialize(os);
}
-void NodeMetadata::deSerialize(std::istream &is)
+void NodeMetadata::deSerialize(std::istream &is, u8 version)
{
- m_stringvars.clear();
+ clear();
int num_vars = readU32(is);
for(int i=0; i<num_vars; i++){
std::string name = deSerializeString(is);
std::string var = deSerializeLongString(is);
m_stringvars[name] = var;
+ if (version >= 2) {
+ if (readU8(is) == 1)
+ markPrivate(name, true);
+ }
}
m_inventory->deSerialize(is);
@@ -70,20 +79,44 @@ void NodeMetadata::deSerialize(std::istream &is)
void NodeMetadata::clear()
{
- m_stringvars.clear();
+ Metadata::clear();
+ m_privatevars.clear();
m_inventory->clear();
}
bool NodeMetadata::empty() const
{
- return m_stringvars.size() == 0 && m_inventory->getLists().size() == 0;
+ return Metadata::empty() && m_inventory->getLists().size() == 0;
+}
+
+
+void NodeMetadata::markPrivate(const std::string &name, bool set)
+{
+ if (set)
+ m_privatevars.insert(name);
+ else
+ m_privatevars.erase(name);
+}
+
+int NodeMetadata::countNonPrivate() const
+{
+ // m_privatevars can contain names not actually present
+ // DON'T: return m_stringvars.size() - m_privatevars.size();
+ int n = 0;
+ for (StringMap::const_iterator
+ it = m_stringvars.begin();
+ it != m_stringvars.end(); ++it) {
+ if (isPrivate(it->first) == false)
+ n++;
+ }
+ return n;
}
/*
NodeMetadataList
*/
-void NodeMetadataList::serialize(std::ostream &os) const
+void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk) const
{
/*
Version 0 is a placeholder for "nothing to see here; go away."
@@ -95,7 +128,8 @@ void NodeMetadataList::serialize(std::ostream &os) const
return;
}
- writeU8(os, 1); // version
+ u8 version = (blockver > 27) ? 2 : 1;
+ writeU8(os, version);
writeU16(os, count);
for(std::map<v3s16, NodeMetadata*>::const_iterator
@@ -110,7 +144,7 @@ void NodeMetadataList::serialize(std::ostream &os) const
u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
writeU16(os, p16);
- data->serialize(os);
+ data->serialize(os, version, disk);
}
}
@@ -125,7 +159,7 @@ void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_m
return;
}
- if (version != 1) {
+ if (version > 2) {
std::string err_str = std::string(FUNCTION_NAME)
+ ": version " + itos(version) + " not supported";
infostream << err_str << std::endl;
@@ -134,7 +168,7 @@ void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_m
u16 count = readU16(is);
- for (u16 i=0; i < count; i++) {
+ for (u16 i = 0; i < count; i++) {
u16 p16 = readU16(is);
v3s16 p;
@@ -145,15 +179,14 @@ void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_m
p.X = p16;
if (m_data.find(p) != m_data.end()) {
- warningstream<<"NodeMetadataList::deSerialize(): "
- <<"already set data at position"
- <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
- <<std::endl;
+ warningstream << "NodeMetadataList::deSerialize(): "
+ << "already set data at position " << PP(p)
+ << ": Ignoring." << std::endl;
continue;
}
NodeMetadata *data = new NodeMetadata(item_def_mgr);
- data->deSerialize(is);
+ data->deSerialize(is, version);
m_data[p] = data;
}
}
@@ -216,35 +249,3 @@ int NodeMetadataList::countNonEmpty() const
}
return n;
}
-
-std::string NodeMetadata::getString(const std::string &name,
- unsigned short recursion) const
-{
- StringMap::const_iterator it = m_stringvars.find(name);
- if (it == m_stringvars.end())
- return "";
-
- return resolveString(it->second, recursion);
-}
-
-void NodeMetadata::setString(const std::string &name, const std::string &var)
-{
- if (var.empty()) {
- m_stringvars.erase(name);
- } else {
- m_stringvars[name] = var;
- }
-}
-
-std::string NodeMetadata::resolveString(const std::string &str,
- unsigned short recursion) const
-{
- if (recursion > 1) {
- return str;
- }
- if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
- return getString(str.substr(2, str.length() - 3), recursion + 1);
- }
- return str;
-}
-