1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-31 06:32:39 +00:00
OpenMW/apps/openmw/mwlua/luamanagerimp.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

206 lines
8.4 KiB
C++
Raw Normal View History

2020-12-18 23:21:10 +01:00
#ifndef MWLUA_LUAMANAGERIMP_H
#define MWLUA_LUAMANAGERIMP_H
#include <map>
#include <set>
2021-12-14 00:39:01 +01:00
#include <components/lua/luastate.hpp>
#include <components/lua/storage.hpp>
2020-12-18 23:21:10 +01:00
#include <components/lua_ui/resources.hpp>
2022-04-09 23:07:57 +02:00
#include <components/misc/color.hpp>
#include <filesystem>
2022-04-09 23:07:57 +02:00
2020-12-18 23:21:10 +01:00
#include "../mwbase/luamanager.hpp"
#include "engineevents.hpp"
2020-12-18 23:21:10 +01:00
#include "globalscripts.hpp"
2022-08-17 19:09:38 +02:00
#include "localscripts.hpp"
#include "luaevents.hpp"
2020-12-18 23:21:10 +01:00
#include "object.hpp"
#include "worldview.hpp"
namespace MWLua
{
// \brief LuaManager is the central interface through which the engine invokes lua scripts.
//
// This class implements the interface defined in MWBase::LuaManager.
// In addition to the interface, this class exposes lower level interaction between the engine
// and the lua world.
2020-12-18 23:21:10 +01:00
class LuaManager : public MWBase::LuaManager
{
public:
LuaManager(const VFS::Manager* vfs, const std::filesystem::path& libsDir);
LuaManager(const LuaManager&) = delete;
LuaManager(LuaManager&&) = delete;
2020-12-18 23:21:10 +01:00
// Called by engine.cpp when the environment is fully initialized.
2020-12-18 23:21:10 +01:00
void init();
void loadPermanentStorage(const std::filesystem::path& userConfigPath);
void savePermanentStorage(const std::filesystem::path& userConfigPath);
2021-12-14 00:39:01 +01:00
// \brief Executes lua handlers. Defaults to running in parallel with OSG Cull.
//
// The OSG Cull is expensive enough that we have "free" time to
// execute Lua by running it in parallel. The Cull also does
// not modify the game state, meaning we can safely read state from Lua
// despite the concurrency. Only modifying the parts of the game state
// that affect the scene graph is forbidden. Such modifications must
// be queued for execution in synchronizedUpdate().
// The parallelism can be turned off in the settings.
void update();
2020-12-18 23:21:10 +01:00
// \brief Executes latency-critical and scene graph related Lua logic.
//
// Called by engine.cpp from the main thread between InputManager and MechanicsManager updates.
// Can use the scene graph and applies the actions queued during update()
void synchronizedUpdate();
2020-12-18 23:21:10 +01:00
// Available everywhere through the MWBase::LuaManager interface.
// LuaManager queues these events and propagates to scripts on the next `update` call.
void newGameStarted() override;
void gameLoaded() override;
2020-12-18 23:21:10 +01:00
void objectAddedToScene(const MWWorld::Ptr& ptr) override;
void objectRemovedFromScene(const MWWorld::Ptr& ptr) override;
2021-06-26 23:10:24 +02:00
void inputEvent(const InputEvent& event) override { mInputEvents.push_back(event); }
void itemConsumed(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) override
{
mEngineEvents.addToQueue(EngineEvents::OnConsume{ getId(actor), getId(consumable) });
}
void objectActivated(const MWWorld::Ptr& object, const MWWorld::Ptr& actor) override
{
mEngineEvents.addToQueue(EngineEvents::OnActivate{ getId(actor), getId(object) });
}
2020-12-18 23:21:10 +01:00
MWBase::LuaManager::ActorControls* getActorControls(const MWWorld::Ptr&) const override;
2020-12-18 23:21:10 +01:00
void clear() override; // should be called before loading game or starting a new game to reset internal state.
void setupPlayer(const MWWorld::Ptr& ptr) override; // Should be called once after each "clear".
// Used only in Lua bindings
void addCustomLocalScript(const MWWorld::Ptr&, int scriptId, std::string_view initData);
2021-01-29 01:54:54 +01:00
void addUIMessage(std::string_view message) { mUIMessages.emplace_back(message); }
2022-04-09 23:07:57 +02:00
void addInGameConsoleMessage(const std::string& msg, const Misc::Color& color)
{
mInGameConsoleMessages.push_back({ msg, color });
}
2022-09-22 21:26:05 +03:00
2022-04-11 01:04:55 +02:00
// Some changes to the game world can not be done from the scripting thread (because it runs in parallel with
// OSG Cull), so we need to queue it and apply from the main thread. All such changes should be implemented as
// classes inherited from MWLua::Action.
class Action
{
public:
Action(LuaUtil::LuaState* state);
virtual ~Action() {}
void safeApply() const;
virtual void apply() const = 0;
2022-04-11 01:04:55 +02:00
virtual std::string toString() const = 0;
private:
std::string mCallerTraceback;
};
void addAction(std::function<void()> action, std::string_view name = "");
void addAction(std::unique_ptr<Action>&& action) { mActionQueue.push_back(std::move(action)); }
void addTeleportPlayerAction(std::function<void()> action);
2020-12-18 23:21:10 +01:00
// Saving
void write(ESM::ESMWriter& writer, Loading::Listener& progress) override;
void saveLocalScripts(const MWWorld::Ptr& ptr, ESM::LuaScripts& data) override;
// Loading from a save
void readRecord(ESM::ESMReader& reader, uint32_t type) override;
void loadLocalScripts(const MWWorld::Ptr& ptr, const ESM::LuaScripts& data) override;
void setContentFileMapping(const std::map<int, int>& mapping) override { mContentFileMapping = mapping; }
2021-04-12 03:15:17 +02:00
// Drops script cache and reloads all scripts. Calls `onSave` and `onLoad` for every script.
void reloadAllScripts() override;
2022-04-09 23:07:57 +02:00
void handleConsoleCommand(
const std::string& consoleMode, const std::string& command, const MWWorld::Ptr& selectedPtr) override;
2021-08-27 09:26:38 +02:00
// Used to call Lua callbacks from C++
void queueCallback(LuaUtil::Callback callback, sol::main_object arg)
{
mQueuedCallbacks.push_back({ std::move(callback), std::move(arg) });
}
2021-08-27 09:26:38 +02:00
// Wraps Lua callback into an std::function.
// NOTE: Resulted function is not thread safe. Can not be used while LuaManager::update() or
// any other Lua-related function is running.
template <class Arg>
std::function<void(Arg)> wrapLuaCallback(const LuaUtil::Callback& c)
2021-08-27 09:26:38 +02:00
{
return
[this, c](Arg arg) { this->queueCallback(c, sol::main_object(this->mLua.sol(), sol::in_place, arg)); };
2021-08-27 09:26:38 +02:00
}
2022-02-26 12:10:55 +01:00
LuaUi::ResourceManager* uiResourceManager() { return &mUiResourceManager; }
2022-03-18 00:27:16 +01:00
bool isProcessingInputEvents() const { return mProcessingInputEvents; }
2022-11-26 21:25:18 +01:00
void reportStats(unsigned int frameNumber, osg::Stats& stats) const;
std::string formatResourceUsageStats() const override;
2022-09-30 01:46:43 +02:00
2020-12-18 23:21:10 +01:00
private:
void initConfiguration();
2022-05-20 21:47:13 +02:00
LocalScripts* createLocalScripts(const MWWorld::Ptr& ptr,
std::optional<LuaUtil::ScriptIdsWithInitializationData> autoStartConf = std::nullopt);
bool mInitialized = false;
bool mGlobalScriptsStarted = false;
2022-03-18 00:27:16 +01:00
bool mProcessingInputEvents = false;
LuaUtil::ScriptsConfiguration mConfiguration;
2020-12-18 23:21:10 +01:00
LuaUtil::LuaState mLua;
2022-02-26 12:10:55 +01:00
LuaUi::ResourceManager mUiResourceManager;
2020-12-18 23:21:10 +01:00
sol::table mNearbyPackage;
2021-01-29 01:54:54 +01:00
sol::table mUserInterfacePackage;
sol::table mCameraPackage;
2021-06-26 23:10:24 +02:00
sol::table mInputPackage;
2021-12-14 00:39:01 +01:00
sol::table mLocalStoragePackage;
sol::table mPlayerStoragePackage;
2022-05-13 18:58:00 -07:00
sol::table mPostprocessingPackage;
sol::table mDebugPackage;
2020-12-18 23:21:10 +01:00
GlobalScripts mGlobalScripts{ &mLua };
std::set<LocalScripts*> mActiveLocalScripts;
WorldView mWorldView;
MWWorld::Ptr mPlayer;
2023-03-25 18:14:33 +01:00
LuaEvents mLuaEvents{ mGlobalScripts };
EngineEvents mEngineEvents{ mGlobalScripts };
std::vector<MWBase::LuaManager::InputEvent> mInputEvents;
2020-12-18 23:21:10 +01:00
std::unique_ptr<LuaUtil::UserdataSerializer> mGlobalSerializer;
std::unique_ptr<LuaUtil::UserdataSerializer> mLocalSerializer;
std::map<int, int> mContentFileMapping;
std::unique_ptr<LuaUtil::UserdataSerializer> mGlobalLoader;
std::unique_ptr<LuaUtil::UserdataSerializer> mLocalLoader;
2021-08-27 09:26:38 +02:00
struct CallbackWithData
{
LuaUtil::Callback mCallback;
sol::main_object mArg;
2021-08-27 09:26:38 +02:00
};
std::vector<CallbackWithData> mQueuedCallbacks;
2021-01-29 01:54:54 +01:00
// Queued actions that should be done in main thread. Processed by applyQueuedChanges().
2021-03-28 17:44:08 +02:00
std::vector<std::unique_ptr<Action>> mActionQueue;
2022-04-11 01:04:55 +02:00
std::unique_ptr<Action> mTeleportPlayerAction;
2021-01-29 01:54:54 +01:00
std::vector<std::string> mUIMessages;
2022-04-09 23:07:57 +02:00
std::vector<std::pair<std::string, Misc::Color>> mInGameConsoleMessages;
2021-12-14 00:39:01 +01:00
LuaUtil::LuaStorage mGlobalStorage{ mLua.sol() };
LuaUtil::LuaStorage mPlayerStorage{ mLua.sol() };
2020-12-18 23:21:10 +01:00
};
}
#endif // MWLUA_LUAMANAGERIMP_H