mirror of
https://github.com/OpenMW/openmw.git
synced 2025-04-02 17:06:41 +00:00
Merge branch 'lua_context_api' into 'master'
Remove getRealFrameDuration in global context, context caching helpers See merge request OpenMW/openmw!4273
This commit is contained in:
commit
312b7af954
9 changed files with 186 additions and 105 deletions
|
@ -1,6 +1,8 @@
|
||||||
#ifndef MWLUA_CONTEXT_H
|
#ifndef MWLUA_CONTEXT_H
|
||||||
#define MWLUA_CONTEXT_H
|
#define MWLUA_CONTEXT_H
|
||||||
|
|
||||||
|
#include <components/lua/luastate.hpp>
|
||||||
|
|
||||||
namespace LuaUtil
|
namespace LuaUtil
|
||||||
{
|
{
|
||||||
class LuaState;
|
class LuaState;
|
||||||
|
@ -15,13 +17,83 @@ namespace MWLua
|
||||||
|
|
||||||
struct Context
|
struct Context
|
||||||
{
|
{
|
||||||
bool mIsMenu;
|
enum Type
|
||||||
bool mIsGlobal;
|
{
|
||||||
|
Menu,
|
||||||
|
Global,
|
||||||
|
Local,
|
||||||
|
};
|
||||||
|
Type mType;
|
||||||
LuaManager* mLuaManager;
|
LuaManager* mLuaManager;
|
||||||
LuaUtil::LuaState* mLua;
|
LuaUtil::LuaState* mLua;
|
||||||
LuaUtil::UserdataSerializer* mSerializer;
|
LuaUtil::UserdataSerializer* mSerializer;
|
||||||
ObjectLists* mObjectLists;
|
ObjectLists* mObjectLists;
|
||||||
LuaEvents* mLuaEvents;
|
LuaEvents* mLuaEvents;
|
||||||
|
|
||||||
|
std::string_view typeName() const
|
||||||
|
{
|
||||||
|
switch (mType)
|
||||||
|
{
|
||||||
|
case Menu:
|
||||||
|
return "menu";
|
||||||
|
case Global:
|
||||||
|
return "global";
|
||||||
|
case Local:
|
||||||
|
return "local";
|
||||||
|
default:
|
||||||
|
throw std::domain_error("Unhandled context type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Str>
|
||||||
|
sol::object getCachedPackage(std::string_view first, const Str&... str) const
|
||||||
|
{
|
||||||
|
sol::object package = mLua->sol()[first];
|
||||||
|
if constexpr (sizeof...(str) == 0)
|
||||||
|
return package;
|
||||||
|
else
|
||||||
|
return LuaUtil::getFieldOrNil(package, str...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Str>
|
||||||
|
const sol::object& setCachedPackage(const sol::object& value, std::string_view first, const Str&... str) const
|
||||||
|
{
|
||||||
|
sol::state_view& lua = mLua->sol();
|
||||||
|
if constexpr (sizeof...(str) == 0)
|
||||||
|
lua[first] = value;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (lua[first] == sol::nil)
|
||||||
|
lua[first] = sol::table(lua, sol::create);
|
||||||
|
sol::table table = lua[first];
|
||||||
|
LuaUtil::setDeepField(table, value, str...);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::object getTypePackage(std::string_view key) const { return getCachedPackage(key, typeName()); }
|
||||||
|
|
||||||
|
const sol::object& setTypePackage(const sol::object& value, std::string_view key) const
|
||||||
|
{
|
||||||
|
return setCachedPackage(value, key, typeName());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Factory>
|
||||||
|
sol::object cachePackage(std::string_view key, Factory factory) const
|
||||||
|
{
|
||||||
|
sol::object cached = getCachedPackage(key);
|
||||||
|
if (cached != sol::nil)
|
||||||
|
return cached;
|
||||||
|
else
|
||||||
|
return setCachedPackage(factory(), key);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool initializeOnce(std::string_view key) const
|
||||||
|
{
|
||||||
|
sol::object flag = mLua->sol()[key];
|
||||||
|
mLua->sol()[key] = sol::make_object(mLua->sol(), true);
|
||||||
|
return flag == sol::nil;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,8 @@ namespace MWLua
|
||||||
api["getRealTime"] = []() {
|
api["getRealTime"] = []() {
|
||||||
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||||
};
|
};
|
||||||
// TODO: remove in global context?
|
|
||||||
|
if (context.mType != Context::Global)
|
||||||
api["getRealFrameDuration"] = []() { return MWBase::Environment::get().getFrameDuration(); };
|
api["getRealFrameDuration"] = []() { return MWBase::Environment::get().getFrameDuration(); };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,8 +73,9 @@ namespace MWLua
|
||||||
{
|
{
|
||||||
auto* lua = context.mLua;
|
auto* lua = context.mLua;
|
||||||
|
|
||||||
if (lua->sol()["openmw_core"] != sol::nil)
|
sol::object cached = context.getTypePackage("openmw_core");
|
||||||
return lua->sol()["openmw_core"];
|
if (cached != sol::nil)
|
||||||
|
return cached;
|
||||||
|
|
||||||
sol::table api(lua->sol(), sol::create);
|
sol::table api(lua->sol(), sol::create);
|
||||||
api["API_REVISION"] = Version::getLuaApiRevision(); // specified in CMakeLists.txt
|
api["API_REVISION"] = Version::getLuaApiRevision(); // specified in CMakeLists.txt
|
||||||
|
@ -81,13 +83,7 @@ namespace MWLua
|
||||||
Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback();
|
Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback();
|
||||||
MWBase::Environment::get().getStateManager()->requestQuit();
|
MWBase::Environment::get().getStateManager()->requestQuit();
|
||||||
};
|
};
|
||||||
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData) {
|
|
||||||
context.mLuaEvents->addGlobalEvent(
|
|
||||||
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
|
|
||||||
};
|
|
||||||
api["contentFiles"] = initContentFilesBindings(lua->sol());
|
api["contentFiles"] = initContentFilesBindings(lua->sol());
|
||||||
api["sound"] = initCoreSoundBindings(context);
|
|
||||||
api["vfx"] = initCoreVfxBindings(context);
|
|
||||||
api["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string {
|
api["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string {
|
||||||
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
|
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
|
||||||
for (size_t i = 0; i < contentList.size(); ++i)
|
for (size_t i = 0; i < contentList.size(); ++i)
|
||||||
|
@ -96,12 +92,19 @@ namespace MWLua
|
||||||
throw std::runtime_error("Content file not found: " + std::string(contentFile));
|
throw std::runtime_error("Content file not found: " + std::string(contentFile));
|
||||||
};
|
};
|
||||||
addCoreTimeBindings(api, context);
|
addCoreTimeBindings(api, context);
|
||||||
api["magic"] = initCoreMagicBindings(context);
|
|
||||||
api["stats"] = initCoreStatsBindings(context);
|
|
||||||
|
|
||||||
api["factions"] = initCoreFactionBindings(context);
|
api["magic"]
|
||||||
api["dialogue"] = initCoreDialogueBindings(context);
|
= context.cachePackage("openmw_core_magic", [context]() { return initCoreMagicBindings(context); });
|
||||||
api["l10n"] = LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager());
|
|
||||||
|
api["stats"]
|
||||||
|
= context.cachePackage("openmw_core_stats", [context]() { return initCoreStatsBindings(context); });
|
||||||
|
|
||||||
|
api["factions"]
|
||||||
|
= context.cachePackage("openmw_core_factions", [context]() { return initCoreFactionBindings(context); });
|
||||||
|
api["dialogue"]
|
||||||
|
= context.cachePackage("openmw_core_dialogue", [context]() { return initCoreDialogueBindings(context); });
|
||||||
|
api["l10n"] = context.cachePackage("openmw_core_l10n",
|
||||||
|
[lua]() { return LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager()); });
|
||||||
const MWWorld::Store<ESM::GameSetting>* gmstStore
|
const MWWorld::Store<ESM::GameSetting>* gmstStore
|
||||||
= &MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
|
= &MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
|
||||||
api["getGMST"] = [lua = context.mLua, gmstStore](const std::string& setting) -> sol::object {
|
api["getGMST"] = [lua = context.mLua, gmstStore](const std::string& setting) -> sol::object {
|
||||||
|
@ -126,15 +129,18 @@ namespace MWLua
|
||||||
return sol::nil;
|
return sol::nil;
|
||||||
};
|
};
|
||||||
|
|
||||||
lua->sol()["openmw_core"] = LuaUtil::makeReadOnly(api);
|
if (context.mType != Context::Menu)
|
||||||
return lua->sol()["openmw_core"];
|
{
|
||||||
}
|
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData) {
|
||||||
|
context.mLuaEvents->addGlobalEvent(
|
||||||
sol::table initCorePackageForMenuScripts(const Context& context)
|
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
|
||||||
|
};
|
||||||
|
api["sound"]
|
||||||
|
= context.cachePackage("openmw_core_sound", [context]() { return initCoreSoundBindings(context); });
|
||||||
|
api["vfx"] = initCoreVfxBindings(context);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
sol::table api(context.mLua->sol(), sol::create);
|
|
||||||
for (auto& [k, v] : LuaUtil::getMutableFromReadOnly(initCorePackage(context)))
|
|
||||||
api[k] = v;
|
|
||||||
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData) {
|
api["sendGlobalEvent"] = [context](std::string eventName, const sol::object& eventData) {
|
||||||
if (MWBase::Environment::get().getStateManager()->getState() == MWBase::StateManager::State_NoGame)
|
if (MWBase::Environment::get().getStateManager()->getState() == MWBase::StateManager::State_NoGame)
|
||||||
{
|
{
|
||||||
|
@ -143,8 +149,9 @@ namespace MWLua
|
||||||
context.mLuaEvents->addGlobalEvent(
|
context.mLuaEvents->addGlobalEvent(
|
||||||
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
|
{ std::move(eventName), LuaUtil::serialize(eventData, context.mSerializer) });
|
||||||
};
|
};
|
||||||
api["sound"] = sol::nil;
|
}
|
||||||
api["vfx"] = sol::nil;
|
|
||||||
return LuaUtil::makeReadOnly(api);
|
sol::table readOnly = LuaUtil::makeReadOnly(api);
|
||||||
|
return context.setTypePackage(readOnly, "openmw_core");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,6 @@ namespace MWLua
|
||||||
void addCoreTimeBindings(sol::table& api, const Context& context);
|
void addCoreTimeBindings(sol::table& api, const Context& context);
|
||||||
|
|
||||||
sol::table initCorePackage(const Context&);
|
sol::table initCorePackage(const Context&);
|
||||||
|
|
||||||
// Returns `openmw.core`, but disables the functionality that shouldn't
|
|
||||||
// be availabe in menu scripts (to prevent cheating in mutiplayer via menu console).
|
|
||||||
sol::table initCorePackageForMenuScripts(const Context&);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // MWLUA_COREBINDINGS_H
|
#endif // MWLUA_COREBINDINGS_H
|
||||||
|
|
|
@ -80,7 +80,7 @@ namespace MWLua
|
||||||
std::map<std::string, sol::object> initMenuPackages(const Context& context)
|
std::map<std::string, sol::object> initMenuPackages(const Context& context)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
{ "openmw.core", initCorePackageForMenuScripts(context) },
|
{ "openmw.core", initCorePackage(context) },
|
||||||
{ "openmw.ambient", initAmbientPackage(context) },
|
{ "openmw.ambient", initAmbientPackage(context) },
|
||||||
{ "openmw.ui", initUserInterfacePackage(context) },
|
{ "openmw.ui", initUserInterfacePackage(context) },
|
||||||
{ "openmw.menu", initMenuPackage(context) },
|
{ "openmw.menu", initMenuPackage(context) },
|
||||||
|
|
|
@ -83,25 +83,24 @@ namespace MWLua
|
||||||
|
|
||||||
void LuaManager::init()
|
void LuaManager::init()
|
||||||
{
|
{
|
||||||
Context context;
|
Context globalContext;
|
||||||
context.mIsMenu = false;
|
globalContext.mType = Context::Global;
|
||||||
context.mIsGlobal = true;
|
globalContext.mLuaManager = this;
|
||||||
context.mLuaManager = this;
|
globalContext.mLua = &mLua;
|
||||||
context.mLua = &mLua;
|
globalContext.mObjectLists = &mObjectLists;
|
||||||
context.mObjectLists = &mObjectLists;
|
globalContext.mLuaEvents = &mLuaEvents;
|
||||||
context.mLuaEvents = &mLuaEvents;
|
globalContext.mSerializer = mGlobalSerializer.get();
|
||||||
context.mSerializer = mGlobalSerializer.get();
|
|
||||||
|
|
||||||
Context localContext = context;
|
Context localContext = globalContext;
|
||||||
localContext.mIsGlobal = false;
|
localContext.mType = Context::Local;
|
||||||
localContext.mSerializer = mLocalSerializer.get();
|
localContext.mSerializer = mLocalSerializer.get();
|
||||||
|
|
||||||
Context menuContext = context;
|
Context menuContext = globalContext;
|
||||||
menuContext.mIsMenu = true;
|
menuContext.mType = Context::Menu;
|
||||||
|
|
||||||
for (const auto& [name, package] : initCommonPackages(context))
|
for (const auto& [name, package] : initCommonPackages(globalContext))
|
||||||
mLua.addCommonPackage(name, package);
|
mLua.addCommonPackage(name, package);
|
||||||
for (const auto& [name, package] : initGlobalPackages(context))
|
for (const auto& [name, package] : initGlobalPackages(globalContext))
|
||||||
mGlobalScripts.addPackage(name, package);
|
mGlobalScripts.addPackage(name, package);
|
||||||
for (const auto& [name, package] : initMenuPackages(menuContext))
|
for (const auto& [name, package] : initMenuPackages(menuContext))
|
||||||
mMenuScripts.addPackage(name, package);
|
mMenuScripts.addPackage(name, package);
|
||||||
|
|
|
@ -80,8 +80,10 @@ namespace MWLua
|
||||||
}();
|
}();
|
||||||
}
|
}
|
||||||
|
|
||||||
sol::table registerUiApi(const Context& context, bool menu)
|
sol::table registerUiApi(const Context& context)
|
||||||
{
|
{
|
||||||
|
bool menu = context.mType == Context::Menu;
|
||||||
|
|
||||||
MWBase::WindowManager* windowManager = MWBase::Environment::get().getWindowManager();
|
MWBase::WindowManager* windowManager = MWBase::Environment::get().getWindowManager();
|
||||||
|
|
||||||
sol::table api = context.mLua->newTable();
|
sol::table api = context.mLua->newTable();
|
||||||
|
@ -293,15 +295,8 @@ namespace MWLua
|
||||||
|
|
||||||
sol::table initUserInterfacePackage(const Context& context)
|
sol::table initUserInterfacePackage(const Context& context)
|
||||||
{
|
{
|
||||||
std::string_view menuCache = "openmw_ui_menu";
|
if (context.initializeOnce("openmw_ui_usertypes"))
|
||||||
std::string_view gameCache = "openmw_ui_game";
|
|
||||||
std::string_view cacheKey = context.mIsMenu ? menuCache : gameCache;
|
|
||||||
{
|
{
|
||||||
sol::state_view& lua = context.mLua->sol();
|
|
||||||
if (lua[cacheKey] != sol::nil)
|
|
||||||
return lua[cacheKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
auto element = context.mLua->sol().new_usertype<LuaUi::Element>("UiElement");
|
auto element = context.mLua->sol().new_usertype<LuaUi::Element>("UiElement");
|
||||||
element[sol::meta_function::to_string] = [](const LuaUi::Element& element) {
|
element[sol::meta_function::to_string] = [](const LuaUi::Element& element) {
|
||||||
std::stringstream res;
|
std::stringstream res;
|
||||||
|
@ -327,17 +322,20 @@ namespace MWLua
|
||||||
};
|
};
|
||||||
|
|
||||||
auto uiLayer = context.mLua->sol().new_usertype<LuaUi::Layer>("UiLayer");
|
auto uiLayer = context.mLua->sol().new_usertype<LuaUi::Layer>("UiLayer");
|
||||||
uiLayer["name"] = sol::readonly_property([](LuaUi::Layer& self) -> std::string_view { return self.name(); });
|
uiLayer["name"]
|
||||||
|
= sol::readonly_property([](LuaUi::Layer& self) -> std::string_view { return self.name(); });
|
||||||
uiLayer["size"] = sol::readonly_property([](LuaUi::Layer& self) { return self.size(); });
|
uiLayer["size"] = sol::readonly_property([](LuaUi::Layer& self) { return self.size(); });
|
||||||
uiLayer[sol::meta_function::to_string]
|
uiLayer[sol::meta_function::to_string]
|
||||||
= [](LuaUi::Layer& self) { return Misc::StringUtils::format("UiLayer(%s)", self.name()); };
|
= [](LuaUi::Layer& self) { return Misc::StringUtils::format("UiLayer(%s)", self.name()); };
|
||||||
|
}
|
||||||
|
|
||||||
sol::table menuApi = registerUiApi(context, true);
|
sol::object cached = context.getTypePackage("openmw_ui");
|
||||||
sol::table gameApi = registerUiApi(context, false);
|
if (cached != sol::nil)
|
||||||
|
return cached;
|
||||||
sol::state_view& lua = context.mLua->sol();
|
else
|
||||||
lua[menuCache] = LuaUtil::makeReadOnly(menuApi);
|
{
|
||||||
lua[gameCache] = LuaUtil::makeReadOnly(gameApi);
|
sol::table api = LuaUtil::makeReadOnly(registerUiApi(context));
|
||||||
return lua[cacheKey];
|
return context.setTypePackage(api, "openmw_ui");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -265,6 +265,20 @@ namespace LuaUtil
|
||||||
return getFieldOrNil(value, str...);
|
return getFieldOrNil(value, str...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class... Str>
|
||||||
|
void setDeepField(sol::table& table, const sol::object& value, std::string_view first, const Str&... str)
|
||||||
|
{
|
||||||
|
if constexpr (sizeof...(str) == 0)
|
||||||
|
table[first] = value;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (table[first] == sol::nil)
|
||||||
|
table[first] = sol::table(table.lua_state(), sol::create);
|
||||||
|
sol::table nextTable = table[first];
|
||||||
|
setDeepField(nextTable, value, str...);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// String representation of a Lua object. Should be used for debugging/logging purposes only.
|
// String representation of a Lua object. Should be used for debugging/logging purposes only.
|
||||||
std::string toString(const sol::object&);
|
std::string toString(const sol::object&);
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@
|
||||||
-- @return #number
|
-- @return #number
|
||||||
|
|
||||||
---
|
---
|
||||||
-- Frame duration in seconds
|
-- Frame duration in seconds. Not available in global scripts.
|
||||||
-- @function [parent=#core] getRealFrameDuration
|
-- @function [parent=#core] getRealFrameDuration
|
||||||
-- @return #number
|
-- @return #number
|
||||||
|
|
||||||
|
|
|
@ -118,11 +118,6 @@
|
||||||
-- @function [parent=#world] setGameTimeScale
|
-- @function [parent=#world] setGameTimeScale
|
||||||
-- @param #number ratio
|
-- @param #number ratio
|
||||||
|
|
||||||
---
|
|
||||||
-- Frame duration in seconds
|
|
||||||
-- @function [parent=#world] getRealFrameDuration
|
|
||||||
-- @return #number
|
|
||||||
|
|
||||||
---
|
---
|
||||||
-- Whether the world is paused (onUpdate doesn't work when the world is paused).
|
-- Whether the world is paused (onUpdate doesn't work when the world is paused).
|
||||||
-- @function [parent=#world] isWorldPaused
|
-- @function [parent=#world] isWorldPaused
|
||||||
|
|
Loading…
Reference in a new issue