#include "staticobject.h"
#include "util/serialize.h"
+#include "content_sao.h"
+
+StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
+ type(s_obj->getType()),
+ pos(pos_)
+{
+ s_obj->getStaticData(&data);
+}
void StaticObject::serialize(std::ostream &os)
{
- char buf[12];
// type
- buf[0] = type;
- os.write(buf, 1);
+ writeU8(os, type);
// pos
- writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
- os.write(buf, 12);
+ writeV3F1000(os, pos);
// data
os<<serializeString(data);
}
void StaticObject::deSerialize(std::istream &is, u8 version)
{
- char buf[12];
// type
- is.read(buf, 1);
- type = buf[0];
+ type = readU8(is);
// pos
- is.read(buf, 12);
- v3s32 intp = readV3S32((u8*)buf);
- pos.X = (f32)intp.X/1000;
- pos.Y = (f32)intp.Y/1000;
- pos.Z = (f32)intp.Z/1000;
+ pos = readV3F1000(is);
// data
data = deSerializeString(is);
}
void StaticObjectList::serialize(std::ostream &os)
{
- char buf[12];
// version
- buf[0] = 0;
- os.write(buf, 1);
+ u8 version = 0;
+ writeU8(os, version);
+
// count
- u16 count = m_stored.size() + m_active.size();
- writeU16((u8*)buf, count);
- os.write(buf, 2);
- for(core::list<StaticObject>::Iterator
- i = m_stored.begin();
- i != m_stored.end(); i++)
- {
- StaticObject &s_obj = *i;
+ size_t count = m_stored.size() + m_active.size();
+ // Make sure it fits into u16, else it would get truncated and cause e.g.
+ // issue #2610 (Invalid block data in database: unsupported NameIdMapping version).
+ if (count > U16_MAX) {
+ errorstream << "StaticObjectList::serialize(): "
+ << "too many objects (" << count << ") in list, "
+ << "not writing them to disk." << std::endl;
+ writeU16(os, 0); // count = 0
+ return;
+ }
+ writeU16(os, count);
+
+ for (StaticObject &s_obj : m_stored) {
s_obj.serialize(os);
}
- for(core::map<u16, StaticObject>::Iterator
- i = m_active.getIterator();
- i.atEnd()==false; i++)
- {
- StaticObject s_obj = i.getNode()->getValue();
+
+ for (auto &i : m_active) {
+ StaticObject s_obj = i.second;
s_obj.serialize(os);
}
}
void StaticObjectList::deSerialize(std::istream &is)
{
- char buf[12];
+ if (m_active.size()) {
+ errorstream << "StaticObjectList::deSerialize(): "
+ << "deserializing objects while " << m_active.size()
+ << " active objects already exist (not cleared). "
+ << m_stored.size() << " stored objects _were_ cleared"
+ << std::endl;
+ }
+ m_stored.clear();
+
// version
- is.read(buf, 1);
- u8 version = buf[0];
+ u8 version = readU8(is);
// count
- is.read(buf, 2);
- u16 count = readU16((u8*)buf);
- for(u16 i=0; i<count; i++)
- {
+ u16 count = readU16(is);
+ for(u16 i = 0; i < count; i++) {
StaticObject s_obj;
s_obj.deSerialize(is, version);
m_stored.push_back(s_obj);