Turn menu.saveGame into a delayed action

This commit is contained in:
Evil Eye 2025-09-15 20:17:08 +02:00
parent fbb726cee0
commit 32f54eb555
4 changed files with 25 additions and 9 deletions

View File

@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...")
set(OPENMW_VERSION_MAJOR 0) set(OPENMW_VERSION_MAJOR 0)
set(OPENMW_VERSION_MINOR 50) set(OPENMW_VERSION_MINOR 50)
set(OPENMW_VERSION_RELEASE 0) set(OPENMW_VERSION_RELEASE 0)
set(OPENMW_LUA_API_REVISION 95) set(OPENMW_LUA_API_REVISION 96)
set(OPENMW_POSTPROCESSING_API_REVISION 3) set(OPENMW_POSTPROCESSING_API_REVISION 3)
set(OPENMW_VERSION_COMMITHASH "") set(OPENMW_VERSION_COMMITHASH "")

View File

@ -316,6 +316,8 @@ namespace MWLua
void LuaManager::applyDelayedActions() void LuaManager::applyDelayedActions()
{ {
if (mApplyingDelayedActions)
return;
mApplyingDelayedActions = true; mApplyingDelayedActions = true;
for (DelayedAction& action : mActionQueue) for (DelayedAction& action : mActionQueue)
action.apply(); action.apply();
@ -324,6 +326,9 @@ namespace MWLua
if (mTeleportPlayerAction) if (mTeleportPlayerAction)
mTeleportPlayerAction->apply(); mTeleportPlayerAction->apply();
mTeleportPlayerAction.reset(); mTeleportPlayerAction.reset();
for (DelayedAction& action : mSaveActionQueue)
action.apply();
mSaveActionQueue.clear();
mApplyingDelayedActions = false; mApplyingDelayedActions = false;
} }
@ -824,6 +829,11 @@ namespace MWLua
mTeleportPlayerAction = DelayedAction(&mLua, std::move(action), "TeleportPlayer"); mTeleportPlayerAction = DelayedAction(&mLua, std::move(action), "TeleportPlayer");
} }
void LuaManager::addSaveGameAction(std::function<void()> action)
{
mSaveActionQueue.emplace_back(&mLua, std::move(action), "SaveGame");
}
void LuaManager::reportStats(unsigned int frameNumber, osg::Stats& stats) const void LuaManager::reportStats(unsigned int frameNumber, osg::Stats& stats) const
{ {
stats.setAttribute(frameNumber, "Lua UsedMemory", mLua.getTotalMemoryUsage()); stats.setAttribute(frameNumber, "Lua UsedMemory", mLua.getTotalMemoryUsage());

View File

@ -125,8 +125,9 @@ namespace MWLua
// Some changes to the game world can not be done from the scripting thread (because it runs in parallel with // 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. // OSG Cull), so we need to queue it and apply from the main thread.
void addAction(std::function<void()> action, std::string_view name = ""); void addAction(std::function<void()> action, std::string_view name = {});
void addTeleportPlayerAction(std::function<void()> action); void addTeleportPlayerAction(std::function<void()> action);
void addSaveGameAction(std::function<void()> action);
// Saving // Saving
void write(ESM::ESMWriter& writer, Loading::Listener& progress) override; void write(ESM::ESMWriter& writer, Loading::Listener& progress) override;
@ -234,6 +235,7 @@ namespace MWLua
}; };
std::vector<DelayedAction> mActionQueue; std::vector<DelayedAction> mActionQueue;
std::optional<DelayedAction> mTeleportPlayerAction; std::optional<DelayedAction> mTeleportPlayerAction;
std::vector<DelayedAction> mSaveActionQueue;
std::vector<std::pair<std::string, MWGui::ShowInDialogueMode>> mUIMessages; std::vector<std::pair<std::string, MWGui::ShowInDialogueMode>> mUIMessages;
std::vector<std::pair<std::string, Misc::Color>> mInGameConsoleMessages; std::vector<std::pair<std::string, Misc::Color>> mInGameConsoleMessages;
std::optional<ObjectId> mDelayedUiModeChangedArg; std::optional<ObjectId> mDelayedUiModeChangedArg;

View File

@ -8,6 +8,7 @@
#include "../mwstate/character.hpp" #include "../mwstate/character.hpp"
#include "context.hpp" #include "context.hpp"
#include "luamanagerimp.hpp"
namespace MWLua namespace MWLua
{ {
@ -72,13 +73,16 @@ namespace MWLua
return sol::nullopt; return sol::nullopt;
}; };
api["saveGame"] = [](std::string_view description, sol::optional<std::string_view> slotName) { api["saveGame"] = [context](std::string description, sol::optional<std::string> slotName) {
MWBase::StateManager* manager = MWBase::Environment::get().getStateManager(); context.mLuaManager->addSaveGameAction(
const MWState::Character* character = manager->getCurrentCharacter(); [description = std::move(description), slotName = std::move(slotName)]() {
const MWState::Slot* slot = nullptr; MWBase::StateManager* manager = MWBase::Environment::get().getStateManager();
if (slotName) const MWState::Character* character = manager->getCurrentCharacter();
slot = findSlot(character, *slotName); const MWState::Slot* slot = nullptr;
manager->saveGame(description, slot); if (slotName)
slot = findSlot(character, *slotName);
manager->saveGame(description, slot);
});
}; };
auto getSaves = [](sol::state_view currentState, const MWState::Character& character) { auto getSaves = [](sol::state_view currentState, const MWState::Character& character) {