mirror of https://github.com/OpenMW/openmw.git
Draft: add new type of Lua scripts - menu scripts
parent
d84caff94d
commit
1dd7a15255
@ -0,0 +1,136 @@
|
||||
#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 "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();
|
||||
};
|
||||
}
|
||||
|
||||
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["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;
|
||||
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,114 @@
|
||||
#include "menuscripts.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;
|
||||
sol::table contentFiles(lua, sol::create);
|
||||
for (size_t i = 0; i < slot.mProfile.mContentFiles.size(); ++i)
|
||||
contentFiles[i + 1] = slot.mProfile.mContentFiles[i];
|
||||
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,46 @@
|
||||
#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"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
sol::table initMenuPackage(const Context& context);
|
||||
|
||||
class MenuScripts : public LuaUtil::ScriptsContainer
|
||||
{
|
||||
public:
|
||||
MenuScripts(LuaUtil::LuaState* lua)
|
||||
: LuaUtil::ScriptsContainer(lua, "Menu")
|
||||
{
|
||||
registerEngineHandlers({ &mStateChanged, &mConsoleCommandHandlers, &mUiModeChanged });
|
||||
}
|
||||
|
||||
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:
|
||||
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,114 @@
|
||||
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'),
|
||||
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,
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue