Allow passing `initData` to the :addSript call (#7091)

iwyu_full
Petr Mikheev 2 years ago
parent 500e8bdf4c
commit b248c3e173

@ -401,7 +401,7 @@ namespace MWLua
return localScripts->getActorControls();
}
void LuaManager::addCustomLocalScript(const MWWorld::Ptr& ptr, int scriptId)
void LuaManager::addCustomLocalScript(const MWWorld::Ptr& ptr, int scriptId, std::string_view initData)
{
LocalScripts* localScripts = ptr.getRefData().getLuaScripts();
if (!localScripts)
@ -411,7 +411,7 @@ namespace MWLua
if (ptr.isInCell() && MWBase::Environment::get().getWorldScene()->isCellActive(*ptr.getCell()))
mActiveLocalScripts.insert(localScripts);
}
localScripts->addCustomScript(scriptId);
localScripts->addCustomScript(scriptId, initData);
}
LocalScripts* LuaManager::createLocalScripts(

@ -57,7 +57,7 @@ namespace MWLua
void setupPlayer(const MWWorld::Ptr& ptr) override; // Should be called once after each "clear".
// Used only in Lua bindings
void addCustomLocalScript(const MWWorld::Ptr&, int scriptId);
void addCustomLocalScript(const MWWorld::Ptr&, int scriptId, std::string_view initData);
void addUIMessage(std::string_view message) { mUIMessages.emplace_back(message); }
void addInGameConsoleMessage(const std::string& msg, const Misc::Color& color)
{

@ -200,9 +200,9 @@ namespace MWLua
if constexpr (std::is_same_v<ObjectT, GObject>)
{ // Only for global scripts
objectT["addScript"] = [lua = context.mLua, luaManager = context.mLuaManager](
const GObject& object, std::string_view path) {
const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration();
objectT["addScript"] = [context](const GObject& object, std::string_view path,
sol::optional<sol::table> initData) {
const LuaUtil::ScriptsConfiguration& cfg = context.mLua->getConfiguration();
std::optional<int> scriptId = cfg.findId(path);
if (!scriptId)
throw std::runtime_error("Unknown script: " + std::string(path));
@ -211,7 +211,12 @@ namespace MWLua
"Script without CUSTOM tag can not be added dynamically: " + std::string(path));
if (object.ptr().getType() == ESM::REC_STAT)
throw std::runtime_error("Attaching scripts to Static is not allowed: " + std::string(path));
luaManager->addCustomLocalScript(object.ptr(), *scriptId);
if (initData)
context.mLuaManager->addCustomLocalScript(
object.ptr(), *scriptId, LuaUtil::serialize(*initData, context.mSerializer));
else
context.mLuaManager->addCustomLocalScript(
object.ptr(), *scriptId, cfg[*scriptId].mInitializationData);
};
objectT["hasScript"] = [lua = context.mLua](const GObject& object, std::string_view path) {
const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration();

@ -35,14 +35,13 @@ namespace LuaUtil
mAPI.emplace(std::move(packageName), makeReadOnly(std::move(package)));
}
bool ScriptsContainer::addCustomScript(int scriptId)
bool ScriptsContainer::addCustomScript(int scriptId, std::string_view initData)
{
const ScriptsConfiguration& conf = mLua.getConfiguration();
assert(conf.isCustomScript(scriptId));
assert(mLua.getConfiguration().isCustomScript(scriptId));
std::optional<sol::function> onInit, onLoad;
bool ok = addScript(scriptId, onInit, onLoad);
if (ok && onInit)
callOnInit(scriptId, *onInit, conf[scriptId].mInitializationData);
callOnInit(scriptId, *onInit, initData);
return ok;
}

@ -91,7 +91,7 @@ namespace LuaUtil
// new script, adds it to the container, and calls onInit for this script. Returns `true` if the script was
// successfully added. The script should have CUSTOM flag. If the flag is not set, or file not found, or has
// syntax errors, returns false. If such script already exists in the container, then also returns false.
bool addCustomScript(int scriptId);
bool addCustomScript(int scriptId, std::string_view initData = "");
bool hasScript(int scriptId) const { return mScripts.count(scriptId) != 0; }
void removeScript(int scriptId);

@ -144,6 +144,7 @@
-- @function [parent=#GameObject] addScript
-- @param self
-- @param #string scriptPath Path to the script in OpenMW virtual filesystem.
-- @param #table initData (optional) Initialization data to be passed to onInit. If missed then Lua initialization data from content files will be used (if exists for this script).
---
-- Whether a script with given path is attached to this object.

Loading…
Cancel
Save