mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-02 07:21:07 +00:00
Allow passing initData
to the :addSript call (#7091)
This commit is contained in:
parent
500e8bdf4c
commit
b248c3e173
@ -401,7 +401,7 @@ namespace MWLua
|
|||||||
return localScripts->getActorControls();
|
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();
|
LocalScripts* localScripts = ptr.getRefData().getLuaScripts();
|
||||||
if (!localScripts)
|
if (!localScripts)
|
||||||
@ -411,7 +411,7 @@ namespace MWLua
|
|||||||
if (ptr.isInCell() && MWBase::Environment::get().getWorldScene()->isCellActive(*ptr.getCell()))
|
if (ptr.isInCell() && MWBase::Environment::get().getWorldScene()->isCellActive(*ptr.getCell()))
|
||||||
mActiveLocalScripts.insert(localScripts);
|
mActiveLocalScripts.insert(localScripts);
|
||||||
}
|
}
|
||||||
localScripts->addCustomScript(scriptId);
|
localScripts->addCustomScript(scriptId, initData);
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalScripts* LuaManager::createLocalScripts(
|
LocalScripts* LuaManager::createLocalScripts(
|
||||||
|
@ -57,7 +57,7 @@ namespace MWLua
|
|||||||
void setupPlayer(const MWWorld::Ptr& ptr) override; // Should be called once after each "clear".
|
void setupPlayer(const MWWorld::Ptr& ptr) override; // Should be called once after each "clear".
|
||||||
|
|
||||||
// Used only in Lua bindings
|
// 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 addUIMessage(std::string_view message) { mUIMessages.emplace_back(message); }
|
||||||
void addInGameConsoleMessage(const std::string& msg, const Misc::Color& color)
|
void addInGameConsoleMessage(const std::string& msg, const Misc::Color& color)
|
||||||
{
|
{
|
||||||
|
@ -200,9 +200,9 @@ namespace MWLua
|
|||||||
|
|
||||||
if constexpr (std::is_same_v<ObjectT, GObject>)
|
if constexpr (std::is_same_v<ObjectT, GObject>)
|
||||||
{ // Only for global scripts
|
{ // Only for global scripts
|
||||||
objectT["addScript"] = [lua = context.mLua, luaManager = context.mLuaManager](
|
objectT["addScript"] = [context](const GObject& object, std::string_view path,
|
||||||
const GObject& object, std::string_view path) {
|
sol::optional<sol::table> initData) {
|
||||||
const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration();
|
const LuaUtil::ScriptsConfiguration& cfg = context.mLua->getConfiguration();
|
||||||
std::optional<int> scriptId = cfg.findId(path);
|
std::optional<int> scriptId = cfg.findId(path);
|
||||||
if (!scriptId)
|
if (!scriptId)
|
||||||
throw std::runtime_error("Unknown script: " + std::string(path));
|
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));
|
"Script without CUSTOM tag can not be added dynamically: " + std::string(path));
|
||||||
if (object.ptr().getType() == ESM::REC_STAT)
|
if (object.ptr().getType() == ESM::REC_STAT)
|
||||||
throw std::runtime_error("Attaching scripts to Static is not allowed: " + std::string(path));
|
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) {
|
objectT["hasScript"] = [lua = context.mLua](const GObject& object, std::string_view path) {
|
||||||
const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration();
|
const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration();
|
||||||
|
@ -35,14 +35,13 @@ namespace LuaUtil
|
|||||||
mAPI.emplace(std::move(packageName), makeReadOnly(std::move(package)));
|
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(mLua.getConfiguration().isCustomScript(scriptId));
|
||||||
assert(conf.isCustomScript(scriptId));
|
|
||||||
std::optional<sol::function> onInit, onLoad;
|
std::optional<sol::function> onInit, onLoad;
|
||||||
bool ok = addScript(scriptId, onInit, onLoad);
|
bool ok = addScript(scriptId, onInit, onLoad);
|
||||||
if (ok && onInit)
|
if (ok && onInit)
|
||||||
callOnInit(scriptId, *onInit, conf[scriptId].mInitializationData);
|
callOnInit(scriptId, *onInit, initData);
|
||||||
return ok;
|
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
|
// 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
|
// 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.
|
// 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; }
|
bool hasScript(int scriptId) const { return mScripts.count(scriptId) != 0; }
|
||||||
void removeScript(int scriptId);
|
void removeScript(int scriptId);
|
||||||
|
@ -144,6 +144,7 @@
|
|||||||
-- @function [parent=#GameObject] addScript
|
-- @function [parent=#GameObject] addScript
|
||||||
-- @param self
|
-- @param self
|
||||||
-- @param #string scriptPath Path to the script in OpenMW virtual filesystem.
|
-- @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.
|
-- Whether a script with given path is attached to this object.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user