mirror of
https://github.com/OpenMW/openmw.git
synced 2025-05-31 18:41:31 +00:00
Merge branch 'lua_settings' into 'master'
Add Lua package 'openmw.settings' See merge request OpenMW/openmw!1017
This commit is contained in:
commit
c5d49b44ba
11 changed files with 124 additions and 7 deletions
|
@ -57,7 +57,7 @@ add_openmw_dir (mwscript
|
||||||
|
|
||||||
add_openmw_dir (mwlua
|
add_openmw_dir (mwlua
|
||||||
luamanagerimp actions object worldview userdataserializer eventqueue query
|
luamanagerimp actions object worldview userdataserializer eventqueue query
|
||||||
luabindings localscripts objectbindings cellbindings asyncbindings camerabindings uibindings
|
luabindings localscripts objectbindings cellbindings asyncbindings camerabindings uibindings settingsbindings
|
||||||
)
|
)
|
||||||
|
|
||||||
add_openmw_dir (mwsound
|
add_openmw_dir (mwsound
|
||||||
|
|
|
@ -31,7 +31,7 @@ namespace MWLua
|
||||||
{
|
{
|
||||||
auto* lua = context.mLua;
|
auto* lua = context.mLua;
|
||||||
sol::table api(lua->sol(), sol::create);
|
sol::table api(lua->sol(), sol::create);
|
||||||
api["API_VERSION"] = 0;
|
api["API_REVISION"] = 1;
|
||||||
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData)
|
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData)
|
||||||
{
|
{
|
||||||
context.mGlobalEventQueue->push_back({std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer)});
|
context.mGlobalEventQueue->push_back({std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer)});
|
||||||
|
|
|
@ -59,6 +59,11 @@ namespace MWLua
|
||||||
// Implemented in uibindings.cpp
|
// Implemented in uibindings.cpp
|
||||||
sol::table initUserInterfacePackage(const Context&);
|
sol::table initUserInterfacePackage(const Context&);
|
||||||
|
|
||||||
|
// Implemented in settingsbindings.cpp
|
||||||
|
sol::table initGlobalSettingsPackage(const Context&);
|
||||||
|
sol::table initLocalSettingsPackage(const Context&);
|
||||||
|
sol::table initPlayerSettingsPackage(const Context&);
|
||||||
|
|
||||||
// openmw.self package is implemented in localscripts.cpp
|
// openmw.self package is implemented in localscripts.cpp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,10 @@ namespace MWLua
|
||||||
mLocalLoader = createUserdataSerializer(true, mWorldView.getObjectRegistry(), &mContentFileMapping);
|
mLocalLoader = createUserdataSerializer(true, mWorldView.getObjectRegistry(), &mContentFileMapping);
|
||||||
|
|
||||||
mGlobalScripts.setSerializer(mGlobalSerializer.get());
|
mGlobalScripts.setSerializer(mGlobalSerializer.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
void LuaManager::init()
|
||||||
|
{
|
||||||
Context context;
|
Context context;
|
||||||
context.mIsGlobal = true;
|
context.mIsGlobal = true;
|
||||||
context.mLuaManager = this;
|
context.mLuaManager = this;
|
||||||
|
@ -57,17 +60,18 @@ namespace MWLua
|
||||||
mLua.addCommonPackage("openmw.core", initCorePackage(context));
|
mLua.addCommonPackage("openmw.core", initCorePackage(context));
|
||||||
mLua.addCommonPackage("openmw.query", initQueryPackage(context));
|
mLua.addCommonPackage("openmw.query", initQueryPackage(context));
|
||||||
mGlobalScripts.addPackage("openmw.world", initWorldPackage(context));
|
mGlobalScripts.addPackage("openmw.world", initWorldPackage(context));
|
||||||
|
mGlobalScripts.addPackage("openmw.settings", initGlobalSettingsPackage(context));
|
||||||
mCameraPackage = initCameraPackage(localContext);
|
mCameraPackage = initCameraPackage(localContext);
|
||||||
mUserInterfacePackage = initUserInterfacePackage(localContext);
|
mUserInterfacePackage = initUserInterfacePackage(localContext);
|
||||||
mNearbyPackage = initNearbyPackage(localContext);
|
mNearbyPackage = initNearbyPackage(localContext);
|
||||||
}
|
mLocalSettingsPackage = initLocalSettingsPackage(localContext);
|
||||||
|
mPlayerSettingsPackage = initPlayerSettingsPackage(localContext);
|
||||||
|
|
||||||
void LuaManager::init()
|
|
||||||
{
|
|
||||||
mKeyPressEvents.clear();
|
mKeyPressEvents.clear();
|
||||||
for (const std::string& path : mGlobalScriptList)
|
for (const std::string& path : mGlobalScriptList)
|
||||||
if (mGlobalScripts.addNewScript(path))
|
if (mGlobalScripts.addNewScript(path))
|
||||||
Log(Debug::Info) << "Global script started: " << path;
|
Log(Debug::Info) << "Global script started: " << path;
|
||||||
|
mInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaManager::update(bool paused, float dt)
|
void LuaManager::update(bool paused, float dt)
|
||||||
|
@ -198,6 +202,8 @@ namespace MWLua
|
||||||
|
|
||||||
void LuaManager::setupPlayer(const MWWorld::Ptr& ptr)
|
void LuaManager::setupPlayer(const MWWorld::Ptr& ptr)
|
||||||
{
|
{
|
||||||
|
if (!mInitialized)
|
||||||
|
return;
|
||||||
if (!mPlayer.isEmpty())
|
if (!mPlayer.isEmpty())
|
||||||
throw std::logic_error("Player is initialized twice");
|
throw std::logic_error("Player is initialized twice");
|
||||||
mWorldView.objectAddedToScene(ptr);
|
mWorldView.objectAddedToScene(ptr);
|
||||||
|
@ -279,6 +285,7 @@ namespace MWLua
|
||||||
|
|
||||||
LocalScripts* LuaManager::createLocalScripts(const MWWorld::Ptr& ptr)
|
LocalScripts* LuaManager::createLocalScripts(const MWWorld::Ptr& ptr)
|
||||||
{
|
{
|
||||||
|
assert(mInitialized);
|
||||||
std::shared_ptr<LocalScripts> scripts;
|
std::shared_ptr<LocalScripts> scripts;
|
||||||
// When loading a game, it can be called before LuaManager::setPlayer,
|
// When loading a game, it can be called before LuaManager::setPlayer,
|
||||||
// so we can't just check ptr == mPlayer here.
|
// so we can't just check ptr == mPlayer here.
|
||||||
|
@ -287,9 +294,13 @@ namespace MWLua
|
||||||
scripts = std::make_shared<PlayerScripts>(&mLua, LObject(getId(ptr), mWorldView.getObjectRegistry()));
|
scripts = std::make_shared<PlayerScripts>(&mLua, LObject(getId(ptr), mWorldView.getObjectRegistry()));
|
||||||
scripts->addPackage("openmw.ui", mUserInterfacePackage);
|
scripts->addPackage("openmw.ui", mUserInterfacePackage);
|
||||||
scripts->addPackage("openmw.camera", mCameraPackage);
|
scripts->addPackage("openmw.camera", mCameraPackage);
|
||||||
|
scripts->addPackage("openmw.settings", mPlayerSettingsPackage);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
scripts = std::make_shared<LocalScripts>(&mLua, LObject(getId(ptr), mWorldView.getObjectRegistry()));
|
scripts = std::make_shared<LocalScripts>(&mLua, LObject(getId(ptr), mWorldView.getObjectRegistry()));
|
||||||
|
scripts->addPackage("openmw.settings", mLocalSettingsPackage);
|
||||||
|
}
|
||||||
scripts->addPackage("openmw.nearby", mNearbyPackage);
|
scripts->addPackage("openmw.nearby", mNearbyPackage);
|
||||||
scripts->setSerializer(mLocalSerializer.get());
|
scripts->setSerializer(mLocalSerializer.get());
|
||||||
|
|
||||||
|
|
|
@ -70,10 +70,13 @@ namespace MWLua
|
||||||
private:
|
private:
|
||||||
LocalScripts* createLocalScripts(const MWWorld::Ptr& ptr);
|
LocalScripts* createLocalScripts(const MWWorld::Ptr& ptr);
|
||||||
|
|
||||||
|
bool mInitialized = false;
|
||||||
LuaUtil::LuaState mLua;
|
LuaUtil::LuaState mLua;
|
||||||
sol::table mNearbyPackage;
|
sol::table mNearbyPackage;
|
||||||
sol::table mUserInterfacePackage;
|
sol::table mUserInterfacePackage;
|
||||||
sol::table mCameraPackage;
|
sol::table mCameraPackage;
|
||||||
|
sol::table mLocalSettingsPackage;
|
||||||
|
sol::table mPlayerSettingsPackage;
|
||||||
|
|
||||||
std::vector<std::string> mGlobalScriptList;
|
std::vector<std::string> mGlobalScriptList;
|
||||||
GlobalScripts mGlobalScripts{&mLua};
|
GlobalScripts mGlobalScripts{&mLua};
|
||||||
|
|
72
apps/openmw/mwlua/settingsbindings.cpp
Normal file
72
apps/openmw/mwlua/settingsbindings.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
#include "luabindings.hpp"
|
||||||
|
|
||||||
|
#include <components/settings/settings.hpp>
|
||||||
|
|
||||||
|
#include "../mwworld/esmstore.hpp"
|
||||||
|
#include "../mwworld/store.hpp"
|
||||||
|
|
||||||
|
namespace MWLua
|
||||||
|
{
|
||||||
|
|
||||||
|
static sol::table initSettingsPackage(const Context& context, bool /*global*/, bool player)
|
||||||
|
{
|
||||||
|
LuaUtil::LuaState* lua = context.mLua;
|
||||||
|
sol::table config(lua->sol(), sol::create);
|
||||||
|
|
||||||
|
// Access to settings.cfg. Temporary, will be removed at some point.
|
||||||
|
auto checkRead = [player](std::string_view category)
|
||||||
|
{
|
||||||
|
if ((category == "Camera" || category == "GUI" || category == "Hud" ||
|
||||||
|
category == "Windows" || category == "Input") && !player)
|
||||||
|
throw std::runtime_error("This setting is only available in player scripts");
|
||||||
|
};
|
||||||
|
config["_getBoolFromSettingsCfg"] = [=](const std::string& category, const std::string& setting)
|
||||||
|
{
|
||||||
|
checkRead(category);
|
||||||
|
return Settings::Manager::getBool(setting, category);
|
||||||
|
};
|
||||||
|
config["_getIntFromSettingsCfg"] = [=](const std::string& category, const std::string& setting)
|
||||||
|
{
|
||||||
|
checkRead(category);
|
||||||
|
return Settings::Manager::getInt(setting, category);
|
||||||
|
};
|
||||||
|
config["_getFloatFromSettingsCfg"] = [=](const std::string& category, const std::string& setting)
|
||||||
|
{
|
||||||
|
checkRead(category);
|
||||||
|
return Settings::Manager::getFloat(setting, category);
|
||||||
|
};
|
||||||
|
config["_getStringFromSettingsCfg"] = [=](const std::string& category, const std::string& setting)
|
||||||
|
{
|
||||||
|
checkRead(category);
|
||||||
|
return Settings::Manager::getString(setting, category);
|
||||||
|
};
|
||||||
|
config["_getVector2FromSettingsCfg"] = [=](const std::string& category, const std::string& setting)
|
||||||
|
{
|
||||||
|
checkRead(category);
|
||||||
|
return Settings::Manager::getVector2(setting, category);
|
||||||
|
};
|
||||||
|
config["_getVector3FromSettingsCfg"] = [=](const std::string& category, const std::string& setting)
|
||||||
|
{
|
||||||
|
checkRead(category);
|
||||||
|
return Settings::Manager::getVector3(setting, category);
|
||||||
|
};
|
||||||
|
|
||||||
|
const MWWorld::Store<ESM::GameSetting>* gmst = &MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
|
||||||
|
config["getGMST"] = [lua, gmst](const std::string setting) -> sol::object
|
||||||
|
{
|
||||||
|
const ESM::Variant& value = gmst->find(setting)->mValue;
|
||||||
|
if (value.getType() == ESM::VT_String)
|
||||||
|
return sol::make_object<std::string>(lua->sol(), value.getString());
|
||||||
|
else if (value.getType() == ESM::VT_Int)
|
||||||
|
return sol::make_object<int>(lua->sol(), value.getInteger());
|
||||||
|
else
|
||||||
|
return sol::make_object<float>(lua->sol(), value.getFloat());
|
||||||
|
};
|
||||||
|
return lua->makeReadOnly(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::table initGlobalSettingsPackage(const Context& context) { return initSettingsPackage(context, true, false); }
|
||||||
|
sol::table initLocalSettingsPackage(const Context& context) { return initSettingsPackage(context, false, false); }
|
||||||
|
sol::table initPlayerSettingsPackage(const Context& context) { return initSettingsPackage(context, false, true); }
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ Lua API reference
|
||||||
|
|
||||||
engine_handlers
|
engine_handlers
|
||||||
openmw_util
|
openmw_util
|
||||||
|
openmw_settings
|
||||||
openmw_core
|
openmw_core
|
||||||
openmw_async
|
openmw_async
|
||||||
openmw_query
|
openmw_query
|
||||||
|
@ -37,6 +38,9 @@ Player scripts are local scripts that are attached to a player.
|
||||||
|:ref:`openmw.util <Package openmw.util>` | everywhere | | Defines utility functions and classes like 3D vectors, |
|
|:ref:`openmw.util <Package openmw.util>` | everywhere | | Defines utility functions and classes like 3D vectors, |
|
||||||
| | | | that don't depend on the game world. |
|
| | | | that don't depend on the game world. |
|
||||||
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
||||||
|
|:ref:`openmw.settings <Package openmw.settings>` | everywhere | | Access to GMST records in content files (implemented) and |
|
||||||
|
| | | | to mod settings (not implemented). |
|
||||||
|
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
||||||
|:ref:`openmw.core <Package openmw.core>` | everywhere | | Functions that are common for both global and local scripts |
|
|:ref:`openmw.core <Package openmw.core>` | everywhere | | Functions that are common for both global and local scripts |
|
||||||
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
||||||
|:ref:`openmw.async <Package openmw.async>` | everywhere | | Timers (implemented) and coroutine utils (not implemented) |
|
|:ref:`openmw.async <Package openmw.async>` | everywhere | | Timers (implemented) and coroutine utils (not implemented) |
|
||||||
|
|
5
docs/source/reference/lua-scripting/openmw_settings.rst
Normal file
5
docs/source/reference/lua-scripting/openmw_settings.rst
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
Package openmw.settings
|
||||||
|
=======================
|
||||||
|
|
||||||
|
.. raw:: html
|
||||||
|
:file: generated_html/openmw_settings.html
|
|
@ -299,6 +299,9 @@ Player scripts are local scripts that are attached to a player.
|
||||||
|:ref:`openmw.util <Package openmw.util>` | everywhere | | Defines utility functions and classes like 3D vectors, |
|
|:ref:`openmw.util <Package openmw.util>` | everywhere | | Defines utility functions and classes like 3D vectors, |
|
||||||
| | | | that don't depend on the game world. |
|
| | | | that don't depend on the game world. |
|
||||||
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
||||||
|
|:ref:`openmw.settings <Package openmw.settings>` | everywhere | | Access to GMST records in content files (implemented) and |
|
||||||
|
| | | | to mod settings (not implemented). |
|
||||||
|
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
||||||
|:ref:`openmw.core <Package openmw.core>` | everywhere | | Functions that are common for both global and local scripts |
|
|:ref:`openmw.core <Package openmw.core>` | everywhere | | Functions that are common for both global and local scripts |
|
||||||
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
+---------------------------------------------------------+--------------------+---------------------------------------------------------------+
|
||||||
|:ref:`openmw.async <Package openmw.async>` | everywhere | | Timers (implemented) and coroutine utils (not implemented) |
|
|:ref:`openmw.async <Package openmw.async>` | everywhere | | Timers (implemented) and coroutine utils (not implemented) |
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- The version of OpenMW Lua API. It is an integer that is incremented every time the API is changed.
|
-- The revision of OpenMW Lua API. It is an integer that is incremented every time the API is changed.
|
||||||
-- @field [parent=#core] #number API_VERSION
|
-- @field [parent=#core] #number API_REVISION
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Send an event to global scripts.
|
-- Send an event to global scripts.
|
||||||
|
|
14
files/lua_api/openmw/settings.lua
Normal file
14
files/lua_api/openmw/settings.lua
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
-- `openmw.settings` provides read-only access to GMST records in content files.
|
||||||
|
-- @module settings
|
||||||
|
-- @usage
|
||||||
|
-- local settings = require('openmw.settings')
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
-- Get a GMST setting from content files.
|
||||||
|
-- @function [parent=#settings] getGMST
|
||||||
|
-- @param #string setting
|
||||||
|
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue