2011-06-17 23:46:50 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-06-17 23:46:50 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-06-17 23:46:50 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2011-06-17 23:46:50 +02:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2011-06-17 23:46:50 +02:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "content_nodemeta.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "nodemetadata.h"
|
|
|
|
#include "nodetimer.h"
|
2011-06-17 23:46:50 +02:00
|
|
|
#include "inventory.h"
|
2011-10-16 15:28:12 +02:00
|
|
|
#include "log.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "serialization.h"
|
2012-06-17 01:40:36 +02:00
|
|
|
#include "util/serialize.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
#include "constants.h" // MAP_BLOCKSIZE
|
2012-06-02 23:32:49 +02:00
|
|
|
#include <sstream>
|
2011-11-28 16:11:14 +01:00
|
|
|
|
|
|
|
#define NODEMETA_GENERIC 1
|
2011-11-16 13:08:31 +01:00
|
|
|
#define NODEMETA_SIGN 14
|
|
|
|
#define NODEMETA_CHEST 15
|
|
|
|
#define NODEMETA_FURNACE 16
|
2011-11-28 16:11:14 +01:00
|
|
|
#define NODEMETA_LOCKABLE_CHEST 17
|
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
// Returns true if node timer must be set
|
|
|
|
static bool content_nodemeta_deserialize_legacy_body(
|
|
|
|
std::istream &is, s16 id, NodeMetadata *meta)
|
2011-06-17 23:46:50 +02:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
meta->clear();
|
2011-06-17 23:46:50 +02:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
if(id == NODEMETA_GENERIC) // GenericNodeMetadata (0.4-dev)
|
|
|
|
{
|
|
|
|
meta->getInventory()->deSerialize(is);
|
|
|
|
deSerializeLongString(is); // m_text
|
|
|
|
deSerializeString(is); // m_owner
|
2011-09-22 11:11:48 +02:00
|
|
|
|
2012-06-01 15:10:53 +02:00
|
|
|
meta->setString("infotext",deSerializeString(is));
|
|
|
|
meta->setString("formspec",deSerializeString(is));
|
2012-03-19 01:08:04 +01:00
|
|
|
readU8(is); // m_allow_text_input
|
2012-06-01 15:10:53 +02:00
|
|
|
readU8(is); // m_allow_removal
|
2012-03-19 01:08:04 +01:00
|
|
|
readU8(is); // m_enforce_owner
|
2011-09-22 11:11:48 +02:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
int num_vars = readU32(is);
|
|
|
|
for(int i=0; i<num_vars; i++){
|
|
|
|
std::string name = deSerializeString(is);
|
|
|
|
std::string var = deSerializeLongString(is);
|
|
|
|
meta->setString(name, var);
|
|
|
|
}
|
2011-09-22 11:11:48 +02:00
|
|
|
return false;
|
2012-03-19 01:08:04 +01:00
|
|
|
}
|
|
|
|
else if(id == NODEMETA_SIGN) // SignNodeMetadata
|
|
|
|
{
|
2012-06-04 17:19:23 +02:00
|
|
|
meta->setString("text", deSerializeString(is));
|
2012-06-01 19:51:15 +02:00
|
|
|
//meta->setString("infotext","\"${text}\"");
|
|
|
|
meta->setString("infotext",
|
|
|
|
std::string("\"") + meta->getString("text") + "\"");
|
2012-07-15 18:19:38 +02:00
|
|
|
meta->setString("formspec","field[text;;${text}]");
|
2011-09-22 11:11:48 +02:00
|
|
|
return false;
|
2012-03-19 01:08:04 +01:00
|
|
|
}
|
|
|
|
else if(id == NODEMETA_CHEST) // ChestNodeMetadata
|
2012-01-12 06:10:39 +01:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
meta->getInventory()->deSerialize(is);
|
2012-06-01 18:51:45 +02:00
|
|
|
|
|
|
|
// Rename inventory list "0" to "main"
|
|
|
|
Inventory *inv = meta->getInventory();
|
|
|
|
if(!inv->getList("main") && inv->getList("0")){
|
2012-06-02 23:32:49 +02:00
|
|
|
inv->getList("0")->setName("main");
|
2012-06-01 18:51:45 +02:00
|
|
|
}
|
2012-06-02 23:32:49 +02:00
|
|
|
assert(inv->getList("main") && !inv->getList("0"));
|
2015-09-18 13:45:42 +02:00
|
|
|
|
2012-07-15 18:19:38 +02:00
|
|
|
meta->setString("formspec","size[8,9]"
|
2012-06-01 18:51:45 +02:00
|
|
|
"list[current_name;main;0,0;8,4;]"
|
2012-03-19 01:08:04 +01:00
|
|
|
"list[current_player;main;0,5;8,4;]");
|
|
|
|
return false;
|
2012-01-12 06:10:39 +01:00
|
|
|
}
|
2012-03-19 01:08:04 +01:00
|
|
|
else if(id == NODEMETA_LOCKABLE_CHEST) // LockingChestNodeMetadata
|
2012-01-12 06:10:39 +01:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
meta->setString("owner", deSerializeString(is));
|
|
|
|
meta->getInventory()->deSerialize(is);
|
2012-06-01 18:51:45 +02:00
|
|
|
|
|
|
|
// Rename inventory list "0" to "main"
|
|
|
|
Inventory *inv = meta->getInventory();
|
|
|
|
if(!inv->getList("main") && inv->getList("0")){
|
2012-06-02 23:32:49 +02:00
|
|
|
inv->getList("0")->setName("main");
|
2012-06-01 18:51:45 +02:00
|
|
|
}
|
2012-06-02 23:32:49 +02:00
|
|
|
assert(inv->getList("main") && !inv->getList("0"));
|
2015-09-18 13:45:42 +02:00
|
|
|
|
2012-07-15 18:19:38 +02:00
|
|
|
meta->setString("formspec","size[8,9]"
|
2012-06-01 18:51:45 +02:00
|
|
|
"list[current_name;main;0,0;8,4;]"
|
2012-03-19 01:08:04 +01:00
|
|
|
"list[current_player;main;0,5;8,4;]");
|
|
|
|
return false;
|
2012-01-12 06:10:39 +01:00
|
|
|
}
|
2012-03-19 01:08:04 +01:00
|
|
|
else if(id == NODEMETA_FURNACE) // FurnaceNodeMetadata
|
|
|
|
{
|
|
|
|
meta->getInventory()->deSerialize(is);
|
|
|
|
int temp = 0;
|
|
|
|
is>>temp;
|
|
|
|
meta->setString("fuel_totaltime", ftos((float)temp/10));
|
|
|
|
temp = 0;
|
|
|
|
is>>temp;
|
|
|
|
meta->setString("fuel_time", ftos((float)temp/10));
|
|
|
|
temp = 0;
|
|
|
|
is>>temp;
|
|
|
|
//meta->setString("src_totaltime", ftos((float)temp/10));
|
|
|
|
temp = 0;
|
|
|
|
is>>temp;
|
|
|
|
meta->setString("src_time", ftos((float)temp/10));
|
|
|
|
|
2012-07-15 18:19:38 +02:00
|
|
|
meta->setString("formspec","size[8,9]"
|
2012-03-19 01:08:04 +01:00
|
|
|
"list[current_name;fuel;2,3;1,1;]"
|
|
|
|
"list[current_name;src;2,1;1,1;]"
|
|
|
|
"list[current_name;dst;5,1;2,2;]"
|
|
|
|
"list[current_player;main;0,5;8,4;]");
|
2011-08-25 00:53:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-19 01:08:04 +01:00
|
|
|
else
|
2011-06-17 23:46:50 +02:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
throw SerializationError("Unknown legacy node metadata");
|
2011-06-17 23:46:50 +02:00
|
|
|
}
|
2011-06-18 01:00:01 +02:00
|
|
|
}
|
2012-01-12 06:10:39 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
static bool content_nodemeta_deserialize_legacy_meta(
|
|
|
|
std::istream &is, NodeMetadata *meta)
|
2012-01-12 06:10:39 +01:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
// Read id
|
|
|
|
s16 id = readS16(is);
|
2012-01-12 06:10:39 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
// Read data
|
|
|
|
std::string data = deSerializeString(is);
|
|
|
|
std::istringstream tmp_is(data, std::ios::binary);
|
|
|
|
return content_nodemeta_deserialize_legacy_body(tmp_is, id, meta);
|
2012-01-12 06:10:39 +01:00
|
|
|
}
|
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
void content_nodemeta_deserialize_legacy(std::istream &is,
|
|
|
|
NodeMetadataList *meta, NodeTimerList *timers,
|
2015-09-18 13:45:42 +02:00
|
|
|
IItemDefManager *item_def_mgr)
|
2011-11-28 16:11:14 +01:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
meta->clear();
|
|
|
|
timers->clear();
|
2011-11-28 16:11:14 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
u16 version = readU16(is);
|
2011-11-28 16:11:14 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
if(version > 1)
|
2011-11-28 16:11:14 +01:00
|
|
|
{
|
2015-10-14 08:39:30 +02:00
|
|
|
infostream<<FUNCTION_NAME<<": version "<<version<<" not supported"
|
2012-03-19 01:08:04 +01:00
|
|
|
<<std::endl;
|
2015-10-14 08:39:30 +02:00
|
|
|
throw SerializationError(FUNCTION_NAME);
|
2011-11-28 16:11:14 +01:00
|
|
|
}
|
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
u16 count = readU16(is);
|
2011-11-28 16:11:14 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
for(u16 i=0; i<count; i++)
|
2011-11-28 16:11:14 +01:00
|
|
|
{
|
2012-03-19 01:08:04 +01:00
|
|
|
u16 p16 = readU16(is);
|
2011-11-28 16:11:14 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
v3s16 p(0,0,0);
|
|
|
|
p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
|
|
|
|
p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
|
|
|
|
p.Y += p16 / MAP_BLOCKSIZE;
|
|
|
|
p16 -= p.Y * MAP_BLOCKSIZE;
|
|
|
|
p.X += p16;
|
2011-11-28 16:11:14 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
if(meta->get(p) != NULL)
|
|
|
|
{
|
2015-10-14 08:39:30 +02:00
|
|
|
warningstream<<FUNCTION_NAME<<": "
|
2012-03-19 01:08:04 +01:00
|
|
|
<<"already set data at position"
|
|
|
|
<<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
|
|
|
|
<<std::endl;
|
|
|
|
continue;
|
2011-11-28 16:11:14 +01:00
|
|
|
}
|
|
|
|
|
2015-09-18 13:45:42 +02:00
|
|
|
NodeMetadata *data = new NodeMetadata(item_def_mgr);
|
2012-03-19 01:08:04 +01:00
|
|
|
bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data);
|
|
|
|
meta->set(p, data);
|
2011-11-28 16:11:14 +01:00
|
|
|
|
2012-03-19 01:08:04 +01:00
|
|
|
if(need_timer)
|
2016-03-21 12:58:52 +01:00
|
|
|
timers->set(NodeTimer(1., 0., p));
|
2011-11-28 16:11:14 +01:00
|
|
|
}
|
2012-03-19 01:08:04 +01:00
|
|
|
}
|