mirror of https://github.com/OpenMW/openmw.git
Merge branch 'menuscripts' into 'master'
Add new Lua context: menu scripts Closes #7805 and #7648 See merge request OpenMW/openmw!3464ini_importer_tests
commit
1338e884a9
@ -0,0 +1,141 @@
|
||||
#include "corebindings.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <components/debug/debuglog.hpp>
|
||||
#include <components/esm3/loadfact.hpp>
|
||||
#include <components/lua/l10n.hpp>
|
||||
#include <components/lua/luastate.hpp>
|
||||
#include <components/lua/serialization.hpp>
|
||||
#include <components/misc/strings/algorithm.hpp>
|
||||
#include <components/misc/strings/lower.hpp>
|
||||
#include <components/version/version.hpp>
|
||||
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/statemanager.hpp"
|
||||
#include "../mwbase/world.hpp"
|
||||
#include "../mwworld/datetimemanager.hpp"
|
||||
#include "../mwworld/esmstore.hpp"
|
||||
|
||||
#include "animationbindings.hpp"
|
||||
#include "factionbindings.hpp"
|
||||
#include "luaevents.hpp"
|
||||
#include "magicbindings.hpp"
|
||||
#include "soundbindings.hpp"
|
||||
#include "stats.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
static sol::table initContentFilesBindings(sol::state_view& lua)
|
||||
{
|
||||
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
|
||||
sol::table list(lua, sol::create);
|
||||
for (size_t i = 0; i < contentList.size(); ++i)
|
||||
list[i + 1] = Misc::StringUtils::lowerCase(contentList[i]);
|
||||
sol::table res(lua, sol::create);
|
||||
res["list"] = LuaUtil::makeReadOnly(list);
|
||||
res["indexOf"] = [&contentList](std::string_view contentFile) -> sol::optional<int> {
|
||||
for (size_t i = 0; i < contentList.size(); ++i)
|
||||
if (Misc::StringUtils::ciEqual(contentList[i], contentFile))
|
||||
return i + 1;
|
||||
return sol::nullopt;
|
||||
};
|
||||
res["has"] = [&contentList](std::string_view contentFile) -> bool {
|
||||
for (size_t i = 0; i < contentList.size(); ++i)
|
||||
if (Misc::StringUtils::ciEqual(contentList[i], contentFile))
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
return LuaUtil::makeReadOnly(res);
|
||||
}
|
||||
|
||||
void addCoreTimeBindings(sol::table& api, const Context& context)
|
||||
{
|
||||
MWWorld::DateTimeManager* timeManager = MWBase::Environment::get().getWorld()->getTimeManager();
|
||||
|
||||
api["getSimulationTime"] = [timeManager]() { return timeManager->getSimulationTime(); };
|
||||
api["getSimulationTimeScale"] = [timeManager]() { return timeManager->getSimulationTimeScale(); };
|
||||
api["getGameTime"] = [timeManager]() { return timeManager->getGameTime(); };
|
||||
api["getGameTimeScale"] = [timeManager]() { return timeManager->getGameTimeScale(); };
|
||||
api["isWorldPaused"] = [timeManager]() { return timeManager->isPaused(); };
|
||||
api["getRealTime"] = []() {
|
||||
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
};
|
||||
// TODO: remove in global context?
|
||||
api["getRealFrameDuration"] = []() { return MWBase::Environment::get().getFrameDuration(); };
|
||||
}
|
||||
|
||||
sol::table initCorePackage(const Context& context)
|
||||
{
|
||||
auto* lua = context.mLua;
|
||||
|
||||
if (lua->sol()["openmw_core"] != sol::nil)
|
||||
return lua->sol()["openmw_core"];
|
||||
|
||||
sol::table api(lua->sol(), sol::create);
|
||||
api["API_REVISION"] = Version::getLuaApiRevision(); // specified in CMakeLists.txt
|
||||
api["quit"] = [lua]() {
|
||||
Log(Debug::Warning) << "Quit requested by a Lua script.\n" << lua->debugTraceback();
|
||||
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["sound"] = initCoreSoundBindings(context);
|
||||
api["vfx"] = initCoreVfxBindings(context);
|
||||
api["getFormId"] = [](std::string_view contentFile, unsigned int index) -> std::string {
|
||||
const std::vector<std::string>& contentList = MWBase::Environment::get().getWorld()->getContentFiles();
|
||||
for (size_t i = 0; i < contentList.size(); ++i)
|
||||
if (Misc::StringUtils::ciEqual(contentList[i], contentFile))
|
||||
return ESM::RefId(ESM::FormId{ index, int(i) }).serializeText();
|
||||
throw std::runtime_error("Content file not found: " + std::string(contentFile));
|
||||
};
|
||||
addCoreTimeBindings(api, context);
|
||||
api["magic"] = initCoreMagicBindings(context);
|
||||
api["stats"] = initCoreStatsBindings(context);
|
||||
|
||||
initCoreFactionBindings(context);
|
||||
api["factions"] = &MWBase::Environment::get().getESMStore()->get<ESM::Faction>();
|
||||
|
||||
api["l10n"] = LuaUtil::initL10nLoader(lua->sol(), MWBase::Environment::get().getL10nManager());
|
||||
const MWWorld::Store<ESM::GameSetting>* gmstStore
|
||||
= &MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
|
||||
api["getGMST"] = [lua = context.mLua, gmstStore](const std::string& setting) -> sol::object {
|
||||
const ESM::GameSetting* gmst = gmstStore->search(setting);
|
||||
if (gmst == nullptr)
|
||||
return sol::nil;
|
||||
const ESM::Variant& value = gmst->mValue;
|
||||
switch (value.getType())
|
||||
{
|
||||
case ESM::VT_Float:
|
||||
return sol::make_object<float>(lua->sol(), value.getFloat());
|
||||
case ESM::VT_Short:
|
||||
case ESM::VT_Long:
|
||||
case ESM::VT_Int:
|
||||
return sol::make_object<int>(lua->sol(), value.getInteger());
|
||||
case ESM::VT_String:
|
||||
return sol::make_object<std::string>(lua->sol(), value.getString());
|
||||
case ESM::VT_Unknown:
|
||||
case ESM::VT_None:
|
||||
break;
|
||||
}
|
||||
return sol::nil;
|
||||
};
|
||||
|
||||
lua->sol()["openmw_core"] = LuaUtil::makeReadOnly(api);
|
||||
return lua->sol()["openmw_core"];
|
||||
}
|
||||
|
||||
sol::table initCorePackageForMenuScripts(const Context& context)
|
||||
{
|
||||
sol::table api(context.mLua->sol(), sol::create);
|
||||
for (auto& [k, v] : LuaUtil::getMutableFromReadOnly(initCorePackage(context)))
|
||||
api[k] = v;
|
||||
api["sendGlobalEvent"] = sol::nil;
|
||||
api["sound"] = sol::nil;
|
||||
api["vfx"] = sol::nil;
|
||||
return LuaUtil::makeReadOnly(api);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
#ifndef MWLUA_COREBINDINGS_H
|
||||
#define MWLUA_COREBINDINGS_H
|
||||
|
||||
#include <sol/forward.hpp>
|
||||
|
||||
#include "context.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
void addCoreTimeBindings(sol::table& api, const Context& 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
|
@ -0,0 +1,72 @@
|
||||
#ifndef MWLUA_INPUTPROCESSOR_H
|
||||
#define MWLUA_INPUTPROCESSOR_H
|
||||
|
||||
#include <SDL_events.h>
|
||||
|
||||
#include <components/sdlutil/events.hpp>
|
||||
|
||||
#include "../mwbase/luamanager.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
template <class Container>
|
||||
class InputProcessor
|
||||
{
|
||||
public:
|
||||
InputProcessor(Container* scriptsContainer)
|
||||
: mScriptsContainer(scriptsContainer)
|
||||
{
|
||||
mScriptsContainer->registerEngineHandlers({ &mKeyPressHandlers, &mKeyReleaseHandlers,
|
||||
&mControllerButtonPressHandlers, &mControllerButtonReleaseHandlers, &mActionHandlers, &mTouchpadPressed,
|
||||
&mTouchpadReleased, &mTouchpadMoved });
|
||||
}
|
||||
|
||||
void processInputEvent(const MWBase::LuaManager::InputEvent& event)
|
||||
{
|
||||
using InputEvent = MWBase::LuaManager::InputEvent;
|
||||
switch (event.mType)
|
||||
{
|
||||
case InputEvent::KeyPressed:
|
||||
mScriptsContainer->callEngineHandlers(mKeyPressHandlers, std::get<SDL_Keysym>(event.mValue));
|
||||
break;
|
||||
case InputEvent::KeyReleased:
|
||||
mScriptsContainer->callEngineHandlers(mKeyReleaseHandlers, std::get<SDL_Keysym>(event.mValue));
|
||||
break;
|
||||
case InputEvent::ControllerPressed:
|
||||
mScriptsContainer->callEngineHandlers(mControllerButtonPressHandlers, std::get<int>(event.mValue));
|
||||
break;
|
||||
case InputEvent::ControllerReleased:
|
||||
mScriptsContainer->callEngineHandlers(
|
||||
mControllerButtonReleaseHandlers, std::get<int>(event.mValue));
|
||||
break;
|
||||
case InputEvent::Action:
|
||||
mScriptsContainer->callEngineHandlers(mActionHandlers, std::get<int>(event.mValue));
|
||||
break;
|
||||
case InputEvent::TouchPressed:
|
||||
mScriptsContainer->callEngineHandlers(
|
||||
mTouchpadPressed, std::get<SDLUtil::TouchEvent>(event.mValue));
|
||||
break;
|
||||
case InputEvent::TouchReleased:
|
||||
mScriptsContainer->callEngineHandlers(
|
||||
mTouchpadReleased, std::get<SDLUtil::TouchEvent>(event.mValue));
|
||||
break;
|
||||
case InputEvent::TouchMoved:
|
||||
mScriptsContainer->callEngineHandlers(mTouchpadMoved, std::get<SDLUtil::TouchEvent>(event.mValue));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Container* mScriptsContainer;
|
||||
typename Container::EngineHandlerList mKeyPressHandlers{ "onKeyPress" };
|
||||
typename Container::EngineHandlerList mKeyReleaseHandlers{ "onKeyRelease" };
|
||||
typename Container::EngineHandlerList mControllerButtonPressHandlers{ "onControllerButtonPress" };
|
||||
typename Container::EngineHandlerList mControllerButtonReleaseHandlers{ "onControllerButtonRelease" };
|
||||
typename Container::EngineHandlerList mActionHandlers{ "onInputAction" };
|
||||
typename Container::EngineHandlerList mTouchpadPressed{ "onTouchPress" };
|
||||
typename Container::EngineHandlerList mTouchpadReleased{ "onTouchRelease" };
|
||||
typename Container::EngineHandlerList mTouchpadMoved{ "onTouchMove" };
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MWLUA_INPUTPROCESSOR_H
|
@ -0,0 +1,124 @@
|
||||
#include "menuscripts.hpp"
|
||||
|
||||
#include <components/misc/strings/lower.hpp>
|
||||
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/statemanager.hpp"
|
||||
#include "../mwstate/character.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
static const MWState::Character* findCharacter(std::string_view characterDir)
|
||||
{
|
||||
MWBase::StateManager* manager = MWBase::Environment::get().getStateManager();
|
||||
for (auto it = manager->characterBegin(); it != manager->characterEnd(); ++it)
|
||||
if (it->getPath().filename() == characterDir)
|
||||
return &*it;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static const MWState::Slot* findSlot(const MWState::Character* character, std::string_view slotName)
|
||||
{
|
||||
if (!character)
|
||||
return nullptr;
|
||||
for (const MWState::Slot& slot : *character)
|
||||
if (slot.mPath.filename() == slotName)
|
||||
return &slot;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sol::table initMenuPackage(const Context& context)
|
||||
{
|
||||
sol::state_view lua = context.mLua->sol();
|
||||
sol::table api(lua, sol::create);
|
||||
|
||||
api["STATE"]
|
||||
= LuaUtil::makeStrictReadOnly(context.mLua->tableFromPairs<std::string_view, MWBase::StateManager::State>({
|
||||
{ "NoGame", MWBase::StateManager::State_NoGame },
|
||||
{ "Running", MWBase::StateManager::State_Running },
|
||||
{ "Ended", MWBase::StateManager::State_Ended },
|
||||
}));
|
||||
|
||||
api["getState"] = []() -> int { return MWBase::Environment::get().getStateManager()->getState(); };
|
||||
|
||||
api["newGame"] = []() { MWBase::Environment::get().getStateManager()->requestNewGame(); };
|
||||
|
||||
api["loadGame"] = [](std::string_view dir, std::string_view slotName) {
|
||||
const MWState::Character* character = findCharacter(dir);
|
||||
const MWState::Slot* slot = findSlot(character, slotName);
|
||||
if (!slot)
|
||||
throw std::runtime_error("Save game slot not found: " + std::string(dir) + "/" + std::string(slotName));
|
||||
MWBase::Environment::get().getStateManager()->requestLoad(slot->mPath);
|
||||
};
|
||||
|
||||
api["deleteGame"] = [](std::string_view dir, std::string_view slotName) {
|
||||
const MWState::Character* character = findCharacter(dir);
|
||||
const MWState::Slot* slot = findSlot(character, slotName);
|
||||
if (!slot)
|
||||
throw std::runtime_error("Save game slot not found: " + std::string(dir) + "/" + std::string(slotName));
|
||||
MWBase::Environment::get().getStateManager()->deleteGame(character, slot);
|
||||
};
|
||||
|
||||
api["getCurrentSaveDir"] = []() -> sol::optional<std::string> {
|
||||
MWBase::StateManager* manager = MWBase::Environment::get().getStateManager();
|
||||
const MWState::Character* character = manager->getCurrentCharacter();
|
||||
if (character)
|
||||
return character->getPath().filename().string();
|
||||
else
|
||||
return sol::nullopt;
|
||||
};
|
||||
|
||||
api["saveGame"] = [](std::string_view description, sol::optional<std::string_view> slotName) {
|
||||
MWBase::StateManager* manager = MWBase::Environment::get().getStateManager();
|
||||
const MWState::Character* character = manager->getCurrentCharacter();
|
||||
const MWState::Slot* slot = nullptr;
|
||||
if (slotName)
|
||||
slot = findSlot(character, *slotName);
|
||||
manager->saveGame(description, slot);
|
||||
};
|
||||
|
||||
auto getSaves = [](sol::state_view lua, const MWState::Character& character) {
|
||||
sol::table saves(lua, sol::create);
|
||||
for (const MWState::Slot& slot : character)
|
||||
{
|
||||
sol::table slotInfo(lua, sol::create);
|
||||
slotInfo["description"] = slot.mProfile.mDescription;
|
||||
slotInfo["playerName"] = slot.mProfile.mPlayerName;
|
||||
slotInfo["playerLevel"] = slot.mProfile.mPlayerLevel;
|
||||
slotInfo["timePlayed"] = slot.mProfile.mTimePlayed;
|
||||
sol::table contentFiles(lua, sol::create);
|
||||
for (size_t i = 0; i < slot.mProfile.mContentFiles.size(); ++i)
|
||||
contentFiles[i + 1] = Misc::StringUtils::lowerCase(slot.mProfile.mContentFiles[i]);
|
||||
|
||||
{
|
||||
auto system_time = std::chrono::system_clock::now()
|
||||
- (std::filesystem::file_time_type::clock::now() - slot.mTimeStamp);
|
||||
slotInfo["creationTime"] = std::chrono::duration<double>(system_time.time_since_epoch()).count();
|
||||
}
|
||||
|
||||
slotInfo["contentFiles"] = contentFiles;
|
||||
saves[slot.mPath.filename().string()] = slotInfo;
|
||||
}
|
||||
return saves;
|
||||
};
|
||||
|
||||
api["getSaves"] = [getSaves](sol::this_state lua, std::string_view dir) -> sol::table {
|
||||
const MWState::Character* character = findCharacter(dir);
|
||||
if (!character)
|
||||
throw std::runtime_error("Saves not found: " + std::string(dir));
|
||||
return getSaves(lua, *character);
|
||||
};
|
||||
|
||||
api["getAllSaves"] = [getSaves](sol::this_state lua) -> sol::table {
|
||||
sol::table saves(lua, sol::create);
|
||||
MWBase::StateManager* manager = MWBase::Environment::get().getStateManager();
|
||||
for (auto it = manager->characterBegin(); it != manager->characterEnd(); ++it)
|
||||
saves[it->getPath().filename().string()] = getSaves(lua, *it);
|
||||
return saves;
|
||||
};
|
||||
|
||||
api["quit"] = []() { MWBase::Environment::get().getStateManager()->requestQuit(); };
|
||||
|
||||
return LuaUtil::makeReadOnly(api);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
#ifndef MWLUA_MENUSCRIPTS_H
|
||||
#define MWLUA_MENUSCRIPTS_H
|
||||
|
||||
#include <SDL_events.h>
|
||||
|
||||
#include <components/lua/luastate.hpp>
|
||||
#include <components/lua/scriptscontainer.hpp>
|
||||
#include <components/sdlutil/events.hpp>
|
||||
|
||||
#include "../mwbase/luamanager.hpp"
|
||||
|
||||
#include "context.hpp"
|
||||
#include "inputprocessor.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
sol::table initMenuPackage(const Context& context);
|
||||
|
||||
class MenuScripts : public LuaUtil::ScriptsContainer
|
||||
{
|
||||
public:
|
||||
MenuScripts(LuaUtil::LuaState* lua)
|
||||
: LuaUtil::ScriptsContainer(lua, "Menu")
|
||||
, mInputProcessor(this)
|
||||
{
|
||||
registerEngineHandlers({ &mOnFrameHandlers, &mStateChanged, &mConsoleCommandHandlers, &mUiModeChanged });
|
||||
}
|
||||
|
||||
void processInputEvent(const MWBase::LuaManager::InputEvent& event)
|
||||
{
|
||||
mInputProcessor.processInputEvent(event);
|
||||
}
|
||||
|
||||
void onFrame(float dt) { callEngineHandlers(mOnFrameHandlers, dt); }
|
||||
|
||||
void stateChanged() { callEngineHandlers(mStateChanged); }
|
||||
|
||||
bool consoleCommand(const std::string& consoleMode, const std::string& command)
|
||||
{
|
||||
callEngineHandlers(mConsoleCommandHandlers, consoleMode, command);
|
||||
return !mConsoleCommandHandlers.mList.empty();
|
||||
}
|
||||
|
||||
void uiModeChanged() { callEngineHandlers(mUiModeChanged); }
|
||||
|
||||
private:
|
||||
friend class MWLua::InputProcessor<MenuScripts>;
|
||||
MWLua::InputProcessor<MenuScripts> mInputProcessor;
|
||||
EngineHandlerList mOnFrameHandlers{ "onFrame" };
|
||||
EngineHandlerList mStateChanged{ "onStateChanged" };
|
||||
EngineHandlerList mConsoleCommandHandlers{ "onConsoleCommand" };
|
||||
EngineHandlerList mUiModeChanged{ "_onUiModeChanged" };
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MWLUA_GLOBALSCRIPTS_H
|
@ -0,0 +1,215 @@
|
||||
#include "worldbindings.hpp"
|
||||
|
||||
#include <components/esm3/loadacti.hpp>
|
||||
#include <components/esm3/loadalch.hpp>
|
||||
#include <components/esm3/loadarmo.hpp>
|
||||
#include <components/esm3/loadbook.hpp>
|
||||
#include <components/esm3/loadclot.hpp>
|
||||
#include <components/esm3/loadmisc.hpp>
|
||||
#include <components/esm3/loadskil.hpp>
|
||||
#include <components/esm3/loadweap.hpp>
|
||||
#include <components/lua/luastate.hpp>
|
||||
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/statemanager.hpp"
|
||||
#include "../mwbase/windowmanager.hpp"
|
||||
#include "../mwbase/world.hpp"
|
||||
#include "../mwworld/action.hpp"
|
||||
#include "../mwworld/class.hpp"
|
||||
#include "../mwworld/datetimemanager.hpp"
|
||||
#include "../mwworld/esmstore.hpp"
|
||||
#include "../mwworld/manualref.hpp"
|
||||
#include "../mwworld/store.hpp"
|
||||
#include "../mwworld/worldmodel.hpp"
|
||||
|
||||
#include "luamanagerimp.hpp"
|
||||
|
||||
#include "corebindings.hpp"
|
||||
#include "mwscriptbindings.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
struct CellsStore
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
namespace sol
|
||||
{
|
||||
template <>
|
||||
struct is_automagical<MWLua::CellsStore> : std::false_type
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
static void checkGameInitialized(LuaUtil::LuaState* lua)
|
||||
{
|
||||
if (MWBase::Environment::get().getStateManager()->getState() == MWBase::StateManager::State_NoGame)
|
||||
throw std::runtime_error(
|
||||
"This function cannot be used until the game is fully initialized.\n" + lua->debugTraceback());
|
||||
}
|
||||
|
||||
static void addWorldTimeBindings(sol::table& api, const Context& context)
|
||||
{
|
||||
MWWorld::DateTimeManager* timeManager = MWBase::Environment::get().getWorld()->getTimeManager();
|
||||
|
||||
api["setGameTimeScale"] = [timeManager](double scale) { timeManager->setGameTimeScale(scale); };
|
||||
api["setSimulationTimeScale"] = [context, timeManager](float scale) {
|
||||
context.mLuaManager->addAction([scale, timeManager] { timeManager->setSimulationTimeScale(scale); });
|
||||
};
|
||||
|
||||
api["pause"]
|
||||
= [timeManager](sol::optional<std::string_view> tag) { timeManager->pause(tag.value_or("paused")); };
|
||||
api["unpause"]
|
||||
= [timeManager](sol::optional<std::string_view> tag) { timeManager->unpause(tag.value_or("paused")); };
|
||||
api["getPausedTags"] = [timeManager](sol::this_state lua) {
|
||||
sol::table res(lua, sol::create);
|
||||
for (const std::string& tag : timeManager->getPausedTags())
|
||||
res[tag] = tag;
|
||||
return res;
|
||||
};
|
||||
}
|
||||
|
||||
static void addCellGetters(sol::table& api, const Context& context)
|
||||
{
|
||||
api["getCellByName"] = [](std::string_view name) {
|
||||
return GCell{ &MWBase::Environment::get().getWorldModel()->getCell(name, /*forceLoad=*/false) };
|
||||
};
|
||||
api["getExteriorCell"] = [](int x, int y, sol::object cellOrName) {
|
||||
ESM::RefId worldspace;
|
||||
if (cellOrName.is<GCell>())
|
||||
worldspace = cellOrName.as<GCell>().mStore->getCell()->getWorldSpace();
|
||||
else if (cellOrName.is<std::string_view>() && !cellOrName.as<std::string_view>().empty())
|
||||
worldspace = MWBase::Environment::get()
|
||||
.getWorldModel()
|
||||
->getCell(cellOrName.as<std::string_view>())
|
||||
.getCell()
|
||||
->getWorldSpace();
|
||||
else
|
||||
worldspace = ESM::Cell::sDefaultWorldspaceId;
|
||||
return GCell{ &MWBase::Environment::get().getWorldModel()->getExterior(
|
||||
ESM::ExteriorCellLocation(x, y, worldspace), /*forceLoad=*/false) };
|
||||
};
|
||||
|
||||
const MWWorld::Store<ESM::Cell>* cells3Store = &MWBase::Environment::get().getESMStore()->get<ESM::Cell>();
|
||||
const MWWorld::Store<ESM4::Cell>* cells4Store = &MWBase::Environment::get().getESMStore()->get<ESM4::Cell>();
|
||||
sol::usertype<CellsStore> cells = context.mLua->sol().new_usertype<CellsStore>("Cells");
|
||||
cells[sol::meta_function::length]
|
||||
= [cells3Store, cells4Store](const CellsStore&) { return cells3Store->getSize() + cells4Store->getSize(); };
|
||||
cells[sol::meta_function::index]
|
||||
= [cells3Store, cells4Store](const CellsStore&, size_t index) -> sol::optional<GCell> {
|
||||
if (index > cells3Store->getSize() + cells3Store->getSize() || index == 0)
|
||||
return sol::nullopt;
|
||||
|
||||
index--; // Translate from Lua's 1-based indexing.
|
||||
if (index < cells3Store->getSize())
|
||||
{
|
||||
const ESM::Cell* cellRecord = cells3Store->at(index);
|
||||
return GCell{ &MWBase::Environment::get().getWorldModel()->getCell(
|
||||
cellRecord->mId, /*forceLoad=*/false) };
|
||||
}
|
||||
else
|
||||
{
|
||||
const ESM4::Cell* cellRecord = cells4Store->at(index - cells3Store->getSize());
|
||||
return GCell{ &MWBase::Environment::get().getWorldModel()->getCell(
|
||||
cellRecord->mId, /*forceLoad=*/false) };
|
||||
}
|
||||
};
|
||||
cells[sol::meta_function::pairs] = context.mLua->sol()["ipairsForArray"].template get<sol::function>();
|
||||
cells[sol::meta_function::ipairs] = context.mLua->sol()["ipairsForArray"].template get<sol::function>();
|
||||
api["cells"] = CellsStore{};
|
||||
}
|
||||
|
||||
sol::table initWorldPackage(const Context& context)
|
||||
{
|
||||
sol::table api(context.mLua->sol(), sol::create);
|
||||
|
||||
addCoreTimeBindings(api, context);
|
||||
addWorldTimeBindings(api, context);
|
||||
addCellGetters(api, context);
|
||||
api["mwscript"] = initMWScriptBindings(context);
|
||||
|
||||
ObjectLists* objectLists = context.mObjectLists;
|
||||
api["activeActors"] = GObjectList{ objectLists->getActorsInScene() };
|
||||
api["players"] = GObjectList{ objectLists->getPlayers() };
|
||||
|
||||
api["createObject"] = [lua = context.mLua](std::string_view recordId, sol::optional<int> count) -> GObject {
|
||||
checkGameInitialized(lua);
|
||||
MWWorld::ManualRef mref(*MWBase::Environment::get().getESMStore(), ESM::RefId::deserializeText(recordId));
|
||||
const MWWorld::Ptr& ptr = mref.getPtr();
|
||||
ptr.getRefData().disable();
|
||||
MWWorld::CellStore& cell = MWBase::Environment::get().getWorldModel()->getDraftCell();
|
||||
MWWorld::Ptr newPtr = ptr.getClass().copyToCell(ptr, cell, count.value_or(1));
|
||||
return GObject(newPtr);
|
||||
};
|
||||
api["getObjectByFormId"] = [](std::string_view formIdStr) -> GObject {
|
||||
ESM::RefId refId = ESM::RefId::deserializeText(formIdStr);
|
||||
if (!refId.is<ESM::FormId>())
|
||||
throw std::runtime_error("FormId expected, got " + std::string(formIdStr) + "; use core.getFormId");
|
||||
return GObject(*refId.getIf<ESM::FormId>());
|
||||
};
|
||||
|
||||
// Creates a new record in the world database.
|
||||
api["createRecord"] = sol::overload(
|
||||
[lua = context.mLua](const ESM::Activator& activator) -> const ESM::Activator* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(activator);
|
||||
},
|
||||
[lua = context.mLua](const ESM::Armor& armor) -> const ESM::Armor* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(armor);
|
||||
},
|
||||
[lua = context.mLua](const ESM::Clothing& clothing) -> const ESM::Clothing* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(clothing);
|
||||
},
|
||||
[lua = context.mLua](const ESM::Book& book) -> const ESM::Book* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(book);
|
||||
},
|
||||
[lua = context.mLua](const ESM::Miscellaneous& misc) -> const ESM::Miscellaneous* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(misc);
|
||||
},
|
||||
[lua = context.mLua](const ESM::Potion& potion) -> const ESM::Potion* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(potion);
|
||||
},
|
||||
[lua = context.mLua](const ESM::Weapon& weapon) -> const ESM::Weapon* {
|
||||
checkGameInitialized(lua);
|
||||
return MWBase::Environment::get().getESMStore()->insert(weapon);
|
||||
});
|
||||
|
||||
api["_runStandardActivationAction"] = [context](const GObject& object, const GObject& actor) {
|
||||
if (!object.ptr().getRefData().activate())
|
||||
return;
|
||||
context.mLuaManager->addAction(
|
||||
[object, actor] {
|
||||
const MWWorld::Ptr& objPtr = object.ptr();
|
||||
const MWWorld::Ptr& actorPtr = actor.ptr();
|
||||
objPtr.getClass().activate(objPtr, actorPtr)->execute(actorPtr);
|
||||
},
|
||||
"_runStandardActivationAction");
|
||||
};
|
||||
api["_runStandardUseAction"] = [context](const GObject& object, const GObject& actor, bool force) {
|
||||
context.mLuaManager->addAction(
|
||||
[object, actor, force] {
|
||||
const MWWorld::Ptr& actorPtr = actor.ptr();
|
||||
const MWWorld::Ptr& objectPtr = object.ptr();
|
||||
if (actorPtr == MWBase::Environment::get().getWorld()->getPlayerPtr())
|
||||
MWBase::Environment::get().getWindowManager()->useItem(objectPtr, force);
|
||||
else
|
||||
{
|
||||
std::unique_ptr<MWWorld::Action> action = objectPtr.getClass().use(objectPtr, force);
|
||||
action->execute(actorPtr, true);
|
||||
}
|
||||
},
|
||||
"_runStandardUseAction");
|
||||
};
|
||||
|
||||
return LuaUtil::makeReadOnly(api);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#ifndef MWLUA_WORLDBINDINGS_H
|
||||
#define MWLUA_WORLDBINDINGS_H
|
||||
|
||||
#include <sol/forward.hpp>
|
||||
|
||||
#include "context.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
sol::table initWorldPackage(const Context&);
|
||||
}
|
||||
|
||||
#endif // MWLUA_WORLDBINDINGS_H
|
@ -0,0 +1,7 @@
|
||||
Package openmw.menu
|
||||
======================
|
||||
|
||||
.. include:: version.rst
|
||||
|
||||
.. raw:: html
|
||||
:file: generated_html/openmw_menu.html
|
@ -0,0 +1,115 @@
|
||||
local menu = require('openmw.menu')
|
||||
local ui = require('openmw.ui')
|
||||
local util = require('openmw.util')
|
||||
|
||||
local menuModeName = 'Lua[Menu]'
|
||||
|
||||
local function printHelp()
|
||||
local msg = [[
|
||||
This is the built-in Lua interpreter.
|
||||
help() - print this message
|
||||
exit() - exit Lua mode
|
||||
view(_G) - print content of the table `_G` (current environment)
|
||||
standard libraries (math, string, etc.) are loaded by default but not visible in `_G`
|
||||
view(menu, 2) - print table `menu` (i.e. `openmw.menu`) and its subtables (2 - traversal depth)]]
|
||||
ui.printToConsole(msg, ui.CONSOLE_COLOR.Info)
|
||||
end
|
||||
|
||||
local function printToConsole(...)
|
||||
local strs = {}
|
||||
for i = 1, select('#', ...) do
|
||||
strs[i] = tostring(select(i, ...))
|
||||
end
|
||||
return ui.printToConsole(table.concat(strs, '\t'), ui.CONSOLE_COLOR.Info)
|
||||
end
|
||||
|
||||
local function printRes(...)
|
||||
if select('#', ...) >= 0 then
|
||||
printToConsole(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function exitLuaMenuMode()
|
||||
ui.setConsoleMode('')
|
||||
ui.printToConsole('Lua mode OFF', ui.CONSOLE_COLOR.Success)
|
||||
end
|
||||
|
||||
local function enterLuaMenuMode()
|
||||
ui.printToConsole('Lua mode ON, use exit() to return, help() for more info', ui.CONSOLE_COLOR.Success)
|
||||
ui.printToConsole('Context: Menu', ui.CONSOLE_COLOR.Success)
|
||||
ui.setConsoleMode(menuModeName)
|
||||
end
|
||||
|
||||
local env = {
|
||||
I = require('openmw.interfaces'),
|
||||
menu = require('openmw.menu'),
|
||||
util = require('openmw.util'),
|
||||
core = require('openmw.core'),
|
||||
storage = require('openmw.storage'),
|
||||
vfs = require('openmw.vfs'),
|
||||
ambient = require('openmw.ambient'),
|
||||
async = require('openmw.async'),
|
||||
ui = require('openmw.ui'),
|
||||
input = require('openmw.input'),
|
||||
aux_util = require('openmw_aux.util'),
|
||||
view = require('openmw_aux.util').deepToString,
|
||||
print = printToConsole,
|
||||
exit = exitLuaMenuMode,
|
||||
help = printHelp,
|
||||
}
|
||||
env._G = env
|
||||
setmetatable(env, {__index = _G, __metatable = false})
|
||||
_G = nil
|
||||
|
||||
local function executeLuaCode(code)
|
||||
local fn
|
||||
local ok, err = pcall(function() fn = util.loadCode('return ' .. code, env) end)
|
||||
if ok then
|
||||
ok, err = pcall(function() printRes(fn()) end)
|
||||
else
|
||||
ok, err = pcall(function() util.loadCode(code, env)() end)
|
||||
end
|
||||
if not ok then
|
||||
ui.printToConsole(err, ui.CONSOLE_COLOR.Error)
|
||||
end
|
||||
end
|
||||
|
||||
local usageInfo = [[
|
||||
Usage: 'lua menu' or 'luam' - enter menu context
|
||||
Other contexts are available only when the game is started:
|
||||
'lua player' or 'luap' - enter player context
|
||||
'lua global' or 'luag' - enter global context
|
||||
'lua selected' or 'luas' - enter local context on the selected object]]
|
||||
|
||||
local function onConsoleCommand(mode, cmd)
|
||||
if mode == '' then
|
||||
cmd, arg = cmd:lower():match('(%w+) *(%w*)')
|
||||
if (cmd == 'lua' and arg == 'menu') or cmd == 'luam' then
|
||||
enterLuaMenuMode()
|
||||
elseif menu.getState() == menu.STATE.NoGame and (cmd == 'lua' or cmd == 'luap' or cmd == 'luas' or cmd == 'luag') then
|
||||
ui.printToConsole(usageInfo, ui.CONSOLE_COLOR.Info)
|
||||
end
|
||||
elseif mode == menuModeName then
|
||||
if cmd == 'exit()' then
|
||||
exitLuaMenuMode()
|
||||
else
|
||||
executeLuaCode(cmd)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function onStateChanged()
|
||||
local mode = ui.getConsoleMode()
|
||||
if menu.getState() ~= menu.STATE.Ended and mode ~= menuModeName then
|
||||
-- When a new game started or loaded reset console mode (except of `luam`) because
|
||||
-- other modes become invalid after restarting Lua scripts.
|
||||
ui.setConsoleMode('')
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
engineHandlers = {
|
||||
onConsoleCommand = onConsoleCommand,
|
||||
onStateChanged = onStateChanged,
|
||||
},
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
local core = require('openmw.core')
|
||||
local input = require('openmw.input')
|
||||
local storage = require('openmw.storage')
|
||||
local ui = require('openmw.ui')
|
||||
local util = require('openmw.util')
|
||||
local async = require('openmw.async')
|
||||
local I = require('openmw.interfaces')
|
||||
|
||||
local settingsGroup = 'SettingsOMWControls'
|
||||
|
||||
local function boolSetting(key, default)
|
||||
return {
|
||||
key = key,
|
||||
renderer = 'checkbox',
|
||||
name = key,
|
||||
description = key .. 'Description',
|
||||
default = default,
|
||||
}
|
||||
end
|
||||
|
||||
I.Settings.registerPage({
|
||||
key = 'OMWControls',
|
||||
l10n = 'OMWControls',
|
||||
name = 'ControlsPage',
|
||||
description = 'ControlsPageDescription',
|
||||
})
|
||||
|
||||
I.Settings.registerGroup({
|
||||
key = settingsGroup,
|
||||
page = 'OMWControls',
|
||||
l10n = 'OMWControls',
|
||||
name = 'MovementSettings',
|
||||
permanentStorage = true,
|
||||
settings = {
|
||||
boolSetting('alwaysRun', false),
|
||||
boolSetting('toggleSneak', false), -- TODO: consider removing this setting when we have the advanced binding UI
|
||||
boolSetting('smoothControllerMovement', true),
|
||||
},
|
||||
})
|
||||
|
||||
local interfaceL10n = core.l10n('interface')
|
||||
|
||||
local bindingSection = storage.playerSection('OMWInputBindings')
|
||||
|
||||
local recording = nil
|
||||
|
||||
|
||||
I.Settings.registerRenderer('inputBinding', function(id, set, arg)
|
||||
if type(id) ~= 'string' then error('inputBinding: must have a string default value') end
|
||||
if not arg then error('inputBinding: argument with "key" and "type" is required') end
|
||||
if not arg.type then error('inputBinding: type argument is required') end
|
||||
if not arg.key then error('inputBinding: key argument is required') end
|
||||
local info = input.actions[arg.key] or input.triggers[arg.key]
|
||||
if not info then return {} end
|
||||
|
||||
local l10n = core.l10n(info.key)
|
||||
|
||||
local name = {
|
||||
template = I.MWUI.templates.textNormal,
|
||||
props = {
|
||||
text = l10n(info.name),
|
||||
},
|
||||
}
|
||||
|
||||
local description = {
|
||||
template = I.MWUI.templates.textNormal,
|
||||
props = {
|
||||
text = l10n(info.description),
|
||||
},
|
||||
}
|
||||
|
||||
local binding = bindingSection:get(id)
|
||||
local label = interfaceL10n('None')
|
||||
if binding then label = input.getKeyName(binding.code) end
|
||||
if recording and recording.id == id then label = interfaceL10n('N/A') end
|
||||
|
||||
local recorder = {
|
||||
template = I.MWUI.templates.textNormal,
|
||||
props = {
|
||||
text = label,
|
||||
},
|
||||
events = {
|
||||
mouseClick = async:callback(function()
|
||||
if recording ~= nil then return end
|
||||
if binding ~= nil then bindingSection:set(id, nil) end
|
||||
recording = {
|
||||
id = id,
|
||||
arg = arg,
|
||||
refresh = function() set(id) end,
|
||||
}
|
||||
recording.refresh()
|
||||
end),
|
||||
},
|
||||
}
|
||||
|
||||
local row = {
|
||||
type = ui.TYPE.Flex,
|
||||
props = {
|
||||
horizontal = true,
|
||||
},
|
||||
content = ui.content {
|
||||
name,
|
||||
{ props = { size = util.vector2(10, 0) } },
|
||||
recorder,
|
||||
},
|
||||
}
|
||||
local column = {
|
||||
type = ui.TYPE.Flex,
|
||||
content = ui.content {
|
||||
row,
|
||||
description,
|
||||
},
|
||||
}
|
||||
|
||||
return column
|
||||
end)
|
||||
|
||||
return {
|
||||
engineHandlers = {
|
||||
onKeyPress = function(key)
|
||||
if recording == nil then return end
|
||||
local binding = {
|
||||
code = key.code,
|
||||
type = recording.arg.type,
|
||||
key = recording.arg.key,
|
||||
}
|
||||
if key.code == input.KEY.Escape then -- TODO: prevent settings modal from closing
|
||||
binding.code = nil
|
||||
end
|
||||
bindingSection:set(recording.id, binding)
|
||||
local refresh = recording.refresh
|
||||
recording = nil
|
||||
refresh()
|
||||
end,
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
---
|
||||
-- `openmw.menu` can be used only in menu scripts.
|
||||
-- @module menu
|
||||
-- @usage local menu = require('openmw.menu')
|
||||
|
||||
---
|
||||
-- @type STATE
|
||||
-- @field [parent=#STATE] NoGame
|
||||
-- @field [parent=#STATE] Running
|
||||
-- @field [parent=#STATE] Ended
|
||||
|
||||
---
|
||||
-- All possible game states returned by @{#menu.getState}
|
||||
-- @field [parent=#menu] #STATE STATE
|
||||
|
||||
---
|
||||
-- Current game state
|
||||
-- @function [parent=#menu] getState
|
||||
-- @return #STATE
|
||||
|
||||
---
|
||||
-- Start a new game
|
||||
-- @function [parent=#menu] newGame
|
||||
|
||||
---
|
||||
-- Load the game from a save slot
|
||||
-- @function [parent=#menu] loadGame
|
||||
-- @param #string directory name of the save directory (e. g. character)
|
||||
-- @param #string slotName name of the save slot
|
||||
|
||||
---
|
||||
-- Delete a saved game
|
||||
-- @function [parent=#menu] deleteGame
|
||||
-- @param #string directory name of the save directory (e. g. character)
|
||||
-- @param #string slotName name of the save slot
|
||||
|
||||
---
|
||||
-- Current save directory
|
||||
-- @function [parent=#menu] getCurrentSaveDir
|
||||
-- @return #string
|
||||
|
||||
---
|
||||
-- Save the game
|
||||
-- @function [parent=#menu] saveGame
|
||||
-- @param #string description human readable description of the save
|
||||
-- @param #string slotName name of the save slot
|
||||
|
||||
---
|
||||
-- @type SaveInfo
|
||||
-- @field #string description
|
||||
-- @field #string playerName
|
||||
-- @field #string playerLevel
|
||||
-- @field #number timePlayed Gameplay time for this saved game. Note: available even with [time played](../modding/settings/saves.html#timeplayed) turned off
|
||||
-- @field #number creationTime Time at which the game was saved, as a timestamp in seconds. Can be passed as the second argument to `os.data`.
|
||||
-- @field #list<#string> contentFiles
|
||||
|
||||
---
|
||||
-- List of all saves for the given directory
|
||||
-- @function [parent=#menu] getSaves
|
||||
-- @param #string directory name of the save directory (e. g. character)
|
||||
-- @return #list<#SaveInfo>
|
||||
|
||||
---
|
||||
-- List of all available saves, grouped by directory
|
||||
-- @function [parent=#menu] getAllSaves
|
||||
-- @return #map<#string, #list<#SaveInfo>>
|
||||
|
||||
---
|
||||
-- Exit the game
|
||||
-- @function [parent=#menu] quit
|
||||
|
||||
return nil
|
Loading…
Reference in New Issue