mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Private nodemeta (#5702)
* Private node metadata that isn't sent to the client
This commit is contained in:
parent
6945f807ab
commit
071e114ffa
10 changed files with 114 additions and 27 deletions
|
@ -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>
|
||||
|
||||
|
@ -39,28 +40,38 @@ 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);
|
||||
|
@ -69,6 +80,7 @@ void NodeMetadata::deSerialize(std::istream &is)
|
|||
void NodeMetadata::clear()
|
||||
{
|
||||
Metadata::clear();
|
||||
m_privatevars.clear();
|
||||
m_inventory->clear();
|
||||
}
|
||||
|
||||
|
@ -77,11 +89,34 @@ bool NodeMetadata::empty() const
|
|||
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."
|
||||
|
@ -93,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
|
||||
|
@ -108,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,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;
|
||||
|
@ -132,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;
|
||||
|
@ -143,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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue