本文整理汇总了C++中NameIdMapping类的典型用法代码示例。如果您正苦于以下问题:C++ NameIdMapping类的具体用法?C++ NameIdMapping怎么用?C++ NameIdMapping使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NameIdMapping类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: writeU16
void MapBlock::serializeDiskExtra(std::ostream &os, u8 version)
{
// Versions up from 9 have block objects. (DEPRECATED)
if(version >= 9)
{
// count=0
writeU16(os, 0);
}
// Versions up from 15 have static objects.
if(version >= 15)
{
m_static_objects.serialize(os);
}
// Timestamp
if(version >= 17)
{
writeU32(os, getTimestamp());
}
// Scan and write node definition id mapping
if(version >= 21){
NameIdMapping nimap;
getBlockNodeIdMapping(&nimap, this, m_gamedef->ndef());
nimap.serialize(os);
}
}
示例2: readU16
void MapBlock::deSerializeDiskExtra(std::istream &is, u8 version)
{
/*
Versions up from 9 have block objects. (DEPRECATED)
*/
if(version >= 9){
u16 count = readU16(is);
// Not supported and length not known if count is not 0
if(count != 0){
errorstream<<"WARNING: MapBlock::deSerializeDiskExtra(): "
<<"Ignoring stuff coming at and after MBOs"<<std::endl;
return;
}
}
/*
Versions up from 15 have static objects.
*/
if(version >= 15)
m_static_objects.deSerialize(is);
// Timestamp
if(version >= 17){
setTimestamp(readU32(is));
m_disk_timestamp = m_timestamp;
} else {
setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
}
// Dynamically re-set ids based on node names
NameIdMapping nimap;
// If supported, read node definition id mapping
if(version >= 21){
nimap.deSerialize(is);
// Else set the legacy mapping
} else {
content_mapnode_get_name_id_mapping(&nimap);
}
correctBlockNodeIds(&nimap, this, m_gamedef);
}
示例3: allocateId
// IWritableNodeDefManager
content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
{
// Pre-conditions
if (name == "")
return CONTENT_IGNORE;
if (name != def.name)
return CONTENT_IGNORE;
// Don't allow redefining ignore (but allow air and unknown)
if (name == "ignore") {
infostream << "NodeDefManager: WARNING: Ignoring "
"CONTENT_IGNORE redefinition"<<std::endl;
return CONTENT_IGNORE;
}
content_t id = CONTENT_IGNORE;
if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
// Get new id
id = allocateId();
if (id == CONTENT_IGNORE) {
infostream << "NodeDefManager: WARNING: Absolute "
"limit reached" << std::endl;
return CONTENT_IGNORE;
}
if (id == CONTENT_IGNORE)
return CONTENT_IGNORE;
addNameIdMapping(id, name);
}
m_content_features[id] = def;
verbosestream << "NodeDefManager: registering content id \"" << id
<< "\": name=\"" << def.name << "\""<<std::endl;
// Add this content to the list of all groups it belongs to
// FIXME: This should remove a node from groups it no longer
// belongs to when a node is re-registered
for (ItemGroupList::const_iterator i = def.groups.begin();
i != def.groups.end(); ++i) {
std::string group_name = i->first;
std::map<std::string, GroupItems>::iterator
j = m_group_to_items.find(group_name);
if (j == m_group_to_items.end()) {
m_group_to_items[group_name].push_back(
std::make_pair(id, i->second));
} else {
GroupItems &items = j->second;
items.push_back(std::make_pair(id, i->second));
}
}
return id;
}
示例4: clear
void CNodeDefManager::deSerialize(std::istream &is)
{
clear();
int version = readU8(is);
if (version != 1)
throw SerializationError("unsupported NodeDefinitionManager version");
u16 count = readU16(is);
std::istringstream is2(deSerializeLongString(is), std::ios::binary);
ContentFeatures f;
for (u16 n = 0; n < count; n++) {
u16 i = readU16(is2);
// Read it from the string wrapper
std::string wrapper = deSerializeString(is2);
std::istringstream wrapper_is(wrapper, std::ios::binary);
f.deSerialize(wrapper_is);
// Check error conditions
if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
warningstream << "NodeDefManager::deSerialize(): "
"not changing builtin node " << i << std::endl;
continue;
}
if (f.name.empty()) {
warningstream << "NodeDefManager::deSerialize(): "
"received empty name" << std::endl;
continue;
}
// Ignore aliases
u16 existing_id;
if (m_name_id_mapping.getId(f.name, existing_id) && i != existing_id) {
warningstream << "NodeDefManager::deSerialize(): "
"already defined with different ID: " << f.name << std::endl;
continue;
}
// All is ok, add node definition with the requested ID
if (i >= m_content_features.size())
m_content_features.resize((u32)(i) + 1);
m_content_features[i] = f;
addNameIdMapping(i, f.name);
verbosestream << "deserialized " << f.name << std::endl;
getNodeBoxUnion(f.selection_box, f, &m_selection_box_union);
fixSelectionBoxIntUnion();
}
}
示例5: clear
void CNodeDefManager::msgpack_unpack(msgpack::object o)
{
clear();
std::map<int, ContentFeatures> unpacked_features;
o.convert(&unpacked_features);
for (std::map<int, ContentFeatures>::iterator it = unpacked_features.begin();
it != unpacked_features.end(); ++it) {
unsigned int i = it->first;
ContentFeatures f = it->second;
if(i == CONTENT_IGNORE || i == CONTENT_AIR
|| i == CONTENT_UNKNOWN){
infostream<<"NodeDefManager::deSerialize(): WARNING: "
<<"not changing builtin node "<<i
<<std::endl;
continue;
}
if(f.name == ""){
infostream<<"NodeDefManager::deSerialize(): WARNING: "
<<"received empty name"<<std::endl;
continue;
}
u16 existing_id;
bool found = m_name_id_mapping.getId(f.name, existing_id); // ignore aliases
if(found && i != existing_id){
infostream<<"NodeDefManager::deSerialize(): WARNING: "
<<"already defined with different ID: "
<<f.name<<std::endl;
continue;
}
// All is ok, add node definition with the requested ID
if(i >= m_content_features.size())
m_content_features.resize((u32)(i) + 1);
m_content_features[i] = f;
addNameIdMapping(i, f.name);
verbosestream<<"deserialized "<<f.name<<std::endl;
}
}
示例6: assert
// IWritableNodeDefManager
content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &def)
{
// Pre-conditions
assert(name != "");
assert(name == def.name);
// Don't allow redefining ignore (but allow air and unknown)
if (name == "ignore") {
warningstream << "NodeDefManager: Ignoring "
"CONTENT_IGNORE redefinition"<<std::endl;
return CONTENT_IGNORE;
}
content_t id = CONTENT_IGNORE;
if (!m_name_id_mapping.getId(name, id)) { // ignore aliases
// Get new id
id = allocateId();
if (id == CONTENT_IGNORE) {
warningstream << "NodeDefManager: Absolute "
"limit reached" << std::endl;
return CONTENT_IGNORE;
}
assert(id != CONTENT_IGNORE);
addNameIdMapping(id, name);
}
m_content_features[id] = def;
verbosestream << "NodeDefManager: registering content id \"" << id
<< "\": name=\"" << def.name << "\""<<std::endl;
getNodeBoxUnion(def.selection_box, def, &m_selection_box_union);
fixSelectionBoxIntUnion();
// Add this content to the list of all groups it belongs to
// FIXME: This should remove a node from groups it no longer
// belongs to when a node is re-registered
for (const auto &group : def.groups) {
const std::string &group_name = group.first;
m_group_to_items[group_name].push_back(id);
}
return id;
}
示例7: resetNodeResolveState
void CNodeDefManager::clear()
{
m_content_features.clear();
m_name_id_mapping.clear();
m_name_id_mapping_with_aliases.clear();
m_group_to_items.clear();
m_next_id = 0;
resetNodeResolveState();
u32 initial_length = 0;
initial_length = MYMAX(initial_length, CONTENT_UNKNOWN + 1);
initial_length = MYMAX(initial_length, CONTENT_AIR + 1);
initial_length = MYMAX(initial_length, CONTENT_IGNORE + 1);
m_content_features.resize(initial_length);
// Set CONTENT_UNKNOWN
{
ContentFeatures f;
f.name = "unknown";
// Insert directly into containers
content_t c = CONTENT_UNKNOWN;
m_content_features[c] = f;
addNameIdMapping(c, f.name);
}
// Set CONTENT_AIR
{
ContentFeatures f;
f.name = "air";
f.drawtype = NDT_AIRLIKE;
f.param_type = CPT_LIGHT;
f.light_propagates = true;
f.sunlight_propagates = true;
f.walkable = false;
f.pointable = false;
f.diggable = false;
f.buildable_to = true;
f.floodable = true;
f.is_ground_content = true;
// Insert directly into containers
content_t c = CONTENT_AIR;
m_content_features[c] = f;
addNameIdMapping(c, f.name);
}
// Set CONTENT_IGNORE
{
ContentFeatures f;
f.name = "ignore";
f.drawtype = NDT_AIRLIKE;
f.param_type = CPT_NONE;
f.light_propagates = false;
f.sunlight_propagates = false;
f.walkable = false;
f.pointable = false;
f.diggable = false;
f.buildable_to = true; // A way to remove accidental CONTENT_IGNOREs
f.is_ground_content = true;
// Insert directly into containers
content_t c = CONTENT_IGNORE;
m_content_features[c] = f;
addNameIdMapping(c, f.name);
}
}
示例8: addNameIdMapping
void CNodeDefManager::addNameIdMapping(content_t i, std::string name)
{
m_name_id_mapping.set(i, name);
m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
}
示例9: lock_shared_rec
void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
{
auto lock = lock_shared_rec();
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported");
if(data == NULL)
{
throw SerializationError("ERROR: Not writing dummy block.");
}
FATAL_ERROR_IF(version < SER_FMT_CLIENT_VER_LOWEST, "Serialize version error");
// First byte
u8 flags = 0;
if(is_underground)
flags |= 0x01;
if(getDayNightDiff())
flags |= 0x02;
if(m_lighting_expired)
flags |= 0x04;
if(m_generated == false)
{
flags |= 0x08;
infostream<<" serialize not generated block"<<std::endl;
}
writeU8(os, flags);
/*
Bulk node data
*/
NameIdMapping nimap;
u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
if(disk)
{
MapNode *tmp_nodes = new MapNode[nodecount];
for(u32 i=0; i<nodecount; i++)
tmp_nodes[i] = data[i];
getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
u8 content_width = 2;
u8 params_width = 2;
writeU8(os, content_width);
writeU8(os, params_width);
MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
content_width, params_width, true);
delete[] tmp_nodes;
}
else
{
u8 content_width = 2;
u8 params_width = 2;
writeU8(os, content_width);
writeU8(os, params_width);
MapNode::serializeBulk(os, version, data, nodecount,
content_width, params_width, true);
}
/*
Node metadata
*/
std::ostringstream oss(std::ios_base::binary);
m_node_metadata.serialize(oss);
compressZlib(oss.str(), os);
/*
Data that goes to disk, but not the network
*/
if(disk)
{
if(version <= 24){
// Node timers
m_node_timers.serialize(os, version);
}
// Static objects
m_static_objects.serialize(os);
// Timestamp
writeU32(os, getTimestamp());
// Write block-specific node definition id mapping
nimap.serialize(os);
if(version >= 25){
// Node timers
m_node_timers.serialize(os, version);
}
}
}
示例10: if
//.........这里部分代码省略.........
compress(param2data, os, version);
}
}
// All other versions (newest)
else
{
// First byte
u8 flags = 0;
if(is_underground)
flags |= 0x01;
if(m_day_night_differs)
flags |= 0x02;
if(m_lighting_expired)
flags |= 0x04;
if(version >= 18)
{
if(m_generated == false)
flags |= 0x08;
}
writeU8(os, flags);
/*
Get data
*/
// Create buffer with different parameters sorted
SharedBuffer<u8> databuf(nodecount*3);
for(u32 i=0; i<nodecount; i++)
{
databuf[i] = databuf_nodelist[i*ser_length];
databuf[i+nodecount] = databuf_nodelist[i*ser_length+1];
databuf[i+nodecount*2] = databuf_nodelist[i*ser_length+2];
}
/*
Compress data to output stream
*/
compress(databuf, os, version);
/*
NodeMetadata
*/
if(version >= 14)
{
if(version <= 15)
{
try{
std::ostringstream oss(std::ios_base::binary);
m_node_metadata->serialize(oss);
os<<serializeString(oss.str());
}
// This will happen if the string is longer than 65535
catch(SerializationError &e)
{
// Use an empty string
os<<serializeString("");
}
}
else
{
std::ostringstream oss(std::ios_base::binary);
m_node_metadata->serialize(oss);
compressZlib(oss.str(), os);
//os<<serializeLongString(oss.str());
}
}
}
if(disk)
{
// Versions up from 9 have block objects. (DEPRECATED)
if(version >= 9)
{
// count=0
writeU16(os, 0);
}
// Versions up from 15 have static objects.
if(version >= 15)
{
m_static_objects.serialize(os);
}
// Timestamp
if(version >= 17)
{
writeU32(os, getTimestamp());
}
// Scan and write node definition id mapping
if(version >= 21)
{
NameIdMapping nimap;
getBlockNodeIdMapping_pre22(&nimap, data, m_gamedef->ndef());
nimap.serialize(os);
}
}
}
示例11: VersionMismatchException
void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported");
if(data == NULL)
{
throw SerializationError("ERROR: Not writing dummy block.");
}
if(version <= 21)
{
serialize_pre22(os, version, disk);
return;
}
// First byte
u8 flags = 0;
if(is_underground)
flags |= 0x01;
if(m_day_night_differs)
flags |= 0x02;
if(m_lighting_expired)
flags |= 0x04;
if(m_generated == false)
flags |= 0x08;
writeU8(os, flags);
/*
Bulk node data
*/
NameIdMapping nimap;
u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
if(disk)
{
MapNode *tmp_nodes = new MapNode[nodecount];
for(u32 i=0; i<nodecount; i++)
tmp_nodes[i] = data[i];
getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
u8 content_width = 1;
/*u8 content_width = (nimap.size() <= 255) ? 1 : 2;*/
u8 params_width = 2;
writeU8(os, content_width);
writeU8(os, params_width);
MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
content_width, params_width, true);
delete[] tmp_nodes;
}
else
{
u8 content_width = 1;
/*u8 content_width = 2;*/
u8 params_width = 2;
writeU8(os, content_width);
writeU8(os, params_width);
MapNode::serializeBulk(os, version, data, nodecount,
content_width, params_width, true);
}
/*
Node metadata
*/
std::ostringstream oss(std::ios_base::binary);
m_node_metadata->serialize(oss);
compressZlib(oss.str(), os);
/*
Data that goes to disk, but not the network
*/
if(disk)
{
// Static objects
m_static_objects.serialize(os);
// Timestamp
writeU32(os, getTimestamp());
// Write block-specific node definition id mapping
nimap.serialize(os);
}
}
示例12: VersionMismatchException
void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported");
if(data == NULL)
{
throw SerializationError("ERROR: Not writing dummy block.");
}
// Can't do this anymore; we have 16-bit dynamically allocated node IDs
// in memory; conversion just won't work in this direction.
if(version < 24)
throw SerializationError("MapBlock::serialize: serialization to "
"version < 24 not possible");
// First byte
u8 flags = 0;
if(is_underground)
flags |= 0x01;
if(getDayNightDiff())
flags |= 0x02;
if(m_lighting_expired)
flags |= 0x04;
if(m_generated == false)
flags |= 0x08;
writeU8(os, flags);
/*
Bulk node data
*/
NameIdMapping nimap;
u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
if(disk)
{
MapNode *tmp_nodes = new MapNode[nodecount];
for(u32 i=0; i<nodecount; i++)
tmp_nodes[i] = data[i];
getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
u8 content_width = 2;
u8 params_width = 2;
writeU8(os, content_width);
writeU8(os, params_width);
MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
content_width, params_width, true);
delete[] tmp_nodes;
}
else
{
u8 content_width = 2;
u8 params_width = 2;
writeU8(os, content_width);
writeU8(os, params_width);
MapNode::serializeBulk(os, version, data, nodecount,
content_width, params_width, true);
}
/*
Node metadata
*/
std::ostringstream oss(std::ios_base::binary);
m_node_metadata.serialize(oss);
compressZlib(oss.str(), os);
/*
Data that goes to disk, but not the network
*/
if(disk)
{
// Node timers
m_node_timers.serialize(os);
// Static objects
m_static_objects.serialize(os);
// Timestamp
writeU32(os, getTimestamp());
// Write block-specific node definition id mapping
nimap.serialize(os);
}
}
示例13: databuf_nodelist
//.........这里部分代码省略.........
catch(SerializationError &e)
{
errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error"
<<" while deserializing node metadata"<<std::endl;
}
}
}
// Deserialize node data
for(u32 i=0; i<nodecount; i++)
{
data[i].deSerialize(&databuf_nodelist[i*ser_length], version);
}
if(disk)
{
/*
Versions up from 9 have block objects. (DEPRECATED)
*/
if(version >= 9){
u16 count = readU16(is);
// Not supported and length not known if count is not 0
if(count != 0){
errorstream<<"WARNING: MapBlock::deSerialize_pre22(): "
<<"Ignoring stuff coming at and after MBOs"<<std::endl;
return;
}
}
/*
Versions up from 15 have static objects.
*/
if(version >= 15)
m_static_objects.deSerialize(is);
// Timestamp
if(version >= 17){
setTimestamp(readU32(is));
m_disk_timestamp = m_timestamp;
} else {
setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
}
// Dynamically re-set ids based on node names
NameIdMapping nimap;
// If supported, read node definition id mapping
if(version >= 21){
nimap.deSerialize(is);
// Else set the legacy mapping
} else {
content_mapnode_get_name_id_mapping(&nimap);
}
correctBlockNodeIds(&nimap, data, m_gamedef);
}
// Legacy data changes
// This code has to convert from pre-22 to post-22 format.
INodeDefManager *nodedef = m_gamedef->ndef();
for(u32 i=0; i<nodecount; i++)
{
const ContentFeatures &f = nodedef->get(data[i].getContent());
// Mineral
if(nodedef->getId("default:stone") == data[i].getContent()
&& data[i].getParam1() == 1)
{
data[i].setContent(nodedef->getId("default:stone_with_coal"));
data[i].setParam1(0);
}
else if(nodedef->getId("default:stone") == data[i].getContent()
&& data[i].getParam1() == 2)
{
data[i].setContent(nodedef->getId("default:stone_with_iron"));
data[i].setParam1(0);
}
// facedir_simple
if(f.legacy_facedir_simple)
{
data[i].setParam2(data[i].getParam1());
data[i].setParam1(0);
}
// wall_mounted
if(f.legacy_wallmounted)
{
u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
u8 dir_old_format = data[i].getParam2();
u8 dir_new_format = 0;
for(u8 j=0; j<8; j++)
{
if((dir_old_format & wallmounted_new_to_old[j]) != 0)
{
dir_new_format = j;
break;
}
}
data[i].setParam2(dir_new_format);
}
}
}
示例14: removeNode
void CNodeDefManager::removeNode(const std::string &name)
{
// Pre-condition
assert(name != "");
// Erase name from name ID mapping
content_t id = CONTENT_IGNORE;
if (m_name_id_mapping.getId(name, id)) {
m_name_id_mapping.eraseName(name);
m_name_id_mapping_with_aliases.erase(name);
}
// Erase node content from all groups it belongs to
for (std::unordered_map<std::string, std::vector<content_t>>::iterator iter_groups =
m_group_to_items.begin(); iter_groups != m_group_to_items.end();) {
std::vector<content_t> &items = iter_groups->second;
items.erase(std::remove(items.begin(), items.end(), id), items.end());
// Check if group is empty
if (items.empty())
m_group_to_items.erase(iter_groups++);
else
++iter_groups;
}
}
示例15: removeNode
void CNodeDefManager::removeNode(const std::string &name)
{
// Pre-condition
assert(name != "");
// Erase name from name ID mapping
content_t id = CONTENT_IGNORE;
if (m_name_id_mapping.getId(name, id)) {
m_name_id_mapping.eraseName(name);
m_name_id_mapping_with_aliases.erase(name);
}
// Erase node content from all groups it belongs to
for (UNORDERED_MAP<std::string, GroupItems>::iterator iter_groups =
m_group_to_items.begin();
iter_groups != m_group_to_items.end();) {
GroupItems &items = iter_groups->second;
for (GroupItems::iterator iter_groupitems = items.begin();
iter_groupitems != items.end();) {
if (iter_groupitems->first == id)
items.erase(iter_groupitems++);
else
iter_groupitems++;
}
// Check if group is empty
if (items.size() == 0)
m_group_to_items.erase(iter_groups++);
else
iter_groups++;
}
}