Fix static_save=false not working & related cleanups

This commit is contained in:
sfan5 2024-03-01 20:46:07 +01:00
parent ef0009aea7
commit 2386bfda7e
4 changed files with 21 additions and 16 deletions

@ -94,7 +94,7 @@ bool MapBlock::onObjectsActivation()
const auto count = m_static_objects.getStoredSize(); const auto count = m_static_objects.getStoredSize();
verbosestream << "MapBlock::onObjectsActivation(): " verbosestream << "MapBlock::onObjectsActivation(): "
<< "activating " << count << "objects in block " << getPos() << "activating " << count << " objects in block " << getPos()
<< std::endl; << std::endl;
if (count > g_settings->getU16("max_objects_per_block")) { if (count > g_settings->getU16("max_objects_per_block")) {

@ -71,7 +71,7 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &d
break; break;
} }
// create object // create object
infostream << "LuaEntitySAO::create(name=\"" << name << "\" state is"; infostream << "LuaEntitySAO(name=\"" << name << "\" state is ";
if (state.empty()) if (state.empty())
infostream << "empty"; infostream << "empty";
else else
@ -289,6 +289,8 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
void LuaEntitySAO::getStaticData(std::string *result) const void LuaEntitySAO::getStaticData(std::string *result) const
{ {
assert(isStaticAllowed());
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
// version must be 1 to keep backwards-compatibility. See version2 // version must be 1 to keep backwards-compatibility. See version2
writeU8(os, 1); writeU8(os, 1);

@ -1251,12 +1251,11 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
// Delete static object if block is loaded // Delete static object if block is loaded
deleteStaticFromBlock(obj, id, MOD_REASON_CLEAR_ALL_OBJECTS, true); deleteStaticFromBlock(obj, id, MOD_REASON_CLEAR_ALL_OBJECTS, true);
obj->markForRemoval();
// If known by some client, don't delete immediately // If known by some client, don't delete immediately
if (obj->m_known_by_count > 0) { if (obj->m_known_by_count > 0)
obj->markForRemoval();
return false; return false;
}
processActiveObjectRemove(obj); processActiveObjectRemove(obj);
@ -1894,6 +1893,12 @@ u16 ServerEnvironment::addActiveObjectRaw(std::unique_ptr<ServerActiveObject> ob
return 0; return 0;
} }
// Register reference in scripting api (must be done before post-init)
m_script->addObjectReference(object);
// Post-initialize object
// Note that this can change the value of isStaticAllowed() in case of LuaEntitySAO
object->addedToEnvironment(dtime_s);
// Add static data to block // Add static data to block
if (object->isStaticAllowed()) { if (object->isStaticAllowed()) {
// Add static object to active static list of the block // Add static object to active static list of the block
@ -1916,15 +1921,14 @@ u16 ServerEnvironment::addActiveObjectRaw(std::unique_ptr<ServerActiveObject> ob
<< "could not emerge block " << p << " for storing id=" << "could not emerge block " << p << " for storing id="
<< object->getId() << " statically" << std::endl; << object->getId() << " statically" << std::endl;
// clean in case of error // clean in case of error
object->markForRemoval();
processActiveObjectRemove(object);
m_ao_manager.removeObject(object->getId()); m_ao_manager.removeObject(object->getId());
return 0; return 0;
} }
} }
// Register reference in scripting api (must be done before post-init) assert(object->m_static_exists == object->isStaticAllowed());
m_script->addObjectReference(object);
// Post-initialize object
object->addedToEnvironment(dtime_s);
return object->getId(); return object->getId();
} }
@ -1937,13 +1941,6 @@ void ServerEnvironment::removeRemovedObjects()
ScopeProfiler sp(g_profiler, "ServerEnvironment::removeRemovedObjects()", SPT_AVG); ScopeProfiler sp(g_profiler, "ServerEnvironment::removeRemovedObjects()", SPT_AVG);
auto clear_cb = [this](ServerActiveObject *obj, u16 id) { auto clear_cb = [this](ServerActiveObject *obj, u16 id) {
// This shouldn't happen but check it
if (!obj) {
errorstream << "ServerEnvironment::removeRemovedObjects(): "
<< "NULL object found. id=" << id << std::endl;
return true;
}
/* /*
We will handle objects marked for removal or deactivation We will handle objects marked for removal or deactivation
*/ */
@ -2280,6 +2277,11 @@ bool ServerEnvironment::saveStaticToBlock(
void ServerEnvironment::processActiveObjectRemove(ServerActiveObject *obj) void ServerEnvironment::processActiveObjectRemove(ServerActiveObject *obj)
{ {
// markForRemoval or markForDeactivation should have been called before
// Not because it's strictly necessary but because the Lua callback is
// bound to that.
assert(obj->isGone());
// Tell the object about removal // Tell the object about removal
obj->removingFromEnvironment(); obj->removingFromEnvironment();
// Deregister in scripting api // Deregister in scripting api

@ -25,6 +25,7 @@ StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
type(s_obj->getType()), type(s_obj->getType()),
pos(pos_) pos(pos_)
{ {
assert(s_obj->isStaticAllowed());
s_obj->getStaticData(&data); s_obj->getStaticData(&data);
} }