163 changes: 141 additions & 22 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,27 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "itemdef.h"
#include "util/strfnd.h"
#include "content_mapnode.h" // For loading legacy MaterialItems
#include "nameidmapping.h" // For loading legacy MaterialItems
#include "util/serialize.h"
#include "util/string.h"

/*
ItemStack
*/

static content_t content_translate_from_19_to_internal(content_t c_from)
{
for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
{
if(trans_table_19[i][1] == c_from)
{
return trans_table_19[i][0];
}
}
return c_from;
}

ItemStack::ItemStack(const std::string &name_, u16 count_,
u16 wear_, IItemDefManager *itemdef) :
name(itemdef->getAlias(name_)),
Expand Down Expand Up @@ -85,35 +99,140 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
if(!tmp.empty())
throw SerializationError("Unexpected text after item name");

do { // This loop is just to allow "break;"
// The real thing

// Apply item aliases
if(name == "MaterialItem")
{
// Obsoleted on 2011-07-30

u16 material;
is>>material;
u16 materialcount;
is>>materialcount;
// Convert old materials
if(material <= 0xff)
material = content_translate_from_19_to_internal(material);
if(material > 0xfff)
throw SerializationError("Too large material number");
// Convert old id to name
NameIdMapping legacy_nimap;
content_mapnode_get_name_id_mapping(&legacy_nimap);
legacy_nimap.getName(material, name);
if(name == "")
name = "unknown_block";
if (itemdef)
name = itemdef->getAlias(name);

// Read the count
std::string count_str;
std::getline(is, count_str, ' ');
if (count_str.empty()) {
count = materialcount;
}
else if(name == "MaterialItem2")
{
// Obsoleted on 2011-11-16

u16 material;
is>>material;
u16 materialcount;
is>>materialcount;
if(material > 0xfff)
throw SerializationError("Too large material number");
// Convert old id to name
NameIdMapping legacy_nimap;
content_mapnode_get_name_id_mapping(&legacy_nimap);
legacy_nimap.getName(material, name);
if(name == "")
name = "unknown_block";
if (itemdef)
name = itemdef->getAlias(name);
count = materialcount;
}
else if(name == "node" || name == "NodeItem" || name == "MaterialItem3"
|| name == "craft" || name == "CraftItem")
{
// Obsoleted on 2012-01-07

std::string all;
std::getline(is, all, '\n');
// First attempt to read inside ""
Strfnd fnd(all);
fnd.next("\"");
// If didn't skip to end, we have ""s
if(!fnd.at_end()){
name = fnd.next("\"");
} else { // No luck, just read a word then
fnd.start(all);
name = fnd.next(" ");
}
fnd.skip_over(" ");
if (itemdef)
name = itemdef->getAlias(name);
count = stoi(trim(fnd.next("")));
if(count == 0)
count = 1;
break;
} else {
count = stoi(count_str);
}
else if(name == "MBOItem")
{
// Obsoleted on 2011-10-14
throw SerializationError("MBOItem not supported anymore");
}
else if(name == "tool" || name == "ToolItem")
{
// Obsoleted on 2012-01-07

std::string all;
std::getline(is, all, '\n');
// First attempt to read inside ""
Strfnd fnd(all);
fnd.next("\"");
// If didn't skip to end, we have ""s
if(!fnd.at_end()){
name = fnd.next("\"");
} else { // No luck, just read a word then
fnd.start(all);
name = fnd.next(" ");
}
count = 1;
// Then read wear
fnd.skip_over(" ");
if (itemdef)
name = itemdef->getAlias(name);
wear = stoi(trim(fnd.next("")));
}
else
{
do // This loop is just to allow "break;"
{
// The real thing

// Apply item aliases
if (itemdef)
name = itemdef->getAlias(name);

// Read the count
std::string count_str;
std::getline(is, count_str, ' ');
if(count_str.empty())
{
count = 1;
break;
}
else
count = stoi(count_str);

// Read the wear
std::string wear_str;
std::getline(is, wear_str, ' ');
if (wear_str.empty())
break;
else
wear = stoi(wear_str);
// Read the wear
std::string wear_str;
std::getline(is, wear_str, ' ');
if(wear_str.empty())
break;
else
wear = stoi(wear_str);

// Read metadata
metadata.deSerialize(is);
// Read metadata
metadata.deSerialize(is);

} while(false);
// In case fields are added after metadata, skip space here:
//std::getline(is, tmp, ' ');
//if(!tmp.empty())
// throw SerializationError("Unexpected text after metadata");

} while(false);
}

if (name.empty() || count == 0)
clear();
Expand Down
8 changes: 7 additions & 1 deletion src/mapblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gamedef.h"
#include "log.h"
#include "nameidmapping.h"
#include "content_mapnode.h" // For legacy name-id mapping
#include "content_nodemeta.h" // For legacy deserialization
#include "serialization.h"
#ifndef SERVER
Expand Down Expand Up @@ -930,7 +931,12 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
// Dynamically re-set ids based on node names
NameIdMapping nimap;
// If supported, read node definition id mapping
nimap.deSerialize(is);
if (version >= 21) {
nimap.deSerialize(is);
// Else set the legacy mapping
} else {
content_mapnode_get_name_id_mapping(&nimap);
}
correctBlockNodeIds(&nimap, data, m_gamedef);
}

Expand Down
70 changes: 59 additions & 11 deletions src/mapnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "nodedef.h"
#include "map.h"
#include "content_mapnode.h" // For mapnode_translate_*_internal
#include "serialization.h" // For ser_ver_supported
#include "util/serialize.h"
#include "log.h"
Expand Down Expand Up @@ -630,19 +631,25 @@ void MapNode::serialize(u8 *dest, u8 version)
}
void MapNode::deSerialize(u8 *source, u8 version)
{
if (!ser_ver_supported(version))
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapNode format not supported");

if (version >= 24) {
param0 = readU16(source + 0);
param1 = readU8(source + 2);
param2 = readU8(source + 3);
} else {
param0 = readU8(source + 0);
param1 = readU8(source + 1);
param2 = readU8(source + 2);
if (param0 > 0x7F) {
param0 |= ((param2 & 0xF0) << 4);
if(version <= 21)
{
deSerialize_pre22(source, version);
return;
}

if(version >= 24){
param0 = readU16(source+0);
param1 = readU8(source+2);
param2 = readU8(source+3);
}else{
param0 = readU8(source+0);
param1 = readU8(source+1);
param2 = readU8(source+2);
if(param0 > 0x7F){
param0 |= ((param2&0xF0)<<4);
param2 &= 0x0F;
}
}
Expand Down Expand Up @@ -764,3 +771,44 @@ void MapNode::deSerializeBulk(std::istream &is, int version,
}
}

/*
Legacy serialization
*/
void MapNode::deSerialize_pre22(u8 *source, u8 version)
{
if(version <= 1)
{
param0 = source[0];
}
else if(version <= 9)
{
param0 = source[0];
param1 = source[1];
}
else
{
param0 = source[0];
param1 = source[1];
param2 = source[2];
if(param0 > 0x7f){
param0 <<= 4;
param0 |= (param2&0xf0)>>4;
param2 &= 0x0f;
}
}

// Convert special values from old version to new
if(version <= 19)
{
// In these versions, CONTENT_IGNORE and CONTENT_AIR
// are 255 and 254
// Version 19 is fucked up with sometimes the old values and sometimes not
if(param0 == 255)
param0 = CONTENT_IGNORE;
else if(param0 == 254)
param0 = CONTENT_AIR;
}

// Translate to our known version
*this = mapnode_translate_to_internal(*this, version);
}
1 change: 1 addition & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "emerge.h"
#include "mapgen.h"
#include "mg_biome.h"
#include "content_mapnode.h"
#include "content_nodemeta.h"
#include "content_abm.h"
#include "content_sao.h"
Expand Down
1 change: 1 addition & 0 deletions src/unittest/test_mapnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "gamedef.h"
#include "nodedef.h"
#include "content_mapnode.h"

class TestMapNode : public TestBase
{
Expand Down
1 change: 1 addition & 0 deletions util/travis/clang-format-whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ src/content_cso.cpp
src/content_cso.h
src/content_mapblock.cpp
src/content_mapblock.h
src/content_mapnode.cpp
src/content_nodemeta.cpp
src/content_nodemeta.h
src/content_sao.cpp
Expand Down