diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index da14df0cd..febd582b3 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -400,13 +400,13 @@ PacketPreInit::PluginContainer Networking::getPluginListSample() while (true) { unsigned field = 0; - auto name = luaState.getEventCtrl().Call(id, field++); + auto name = luaState.getEventCtrl().Call(id, field++); if (name.empty()) break; PacketPreInit::HashList hashList; while (true) { - auto hash = luaState.getEventCtrl().Call(id, field++); + auto hash = luaState.getEventCtrl().Call(id, field++); if (hash.empty()) break; hashList.push_back((unsigned)stoul(hash)); diff --git a/apps/openmw-mp/Script/EventController.cpp b/apps/openmw-mp/Script/EventController.cpp index 308c84c7a..e01fdd9c1 100644 --- a/apps/openmw-mp/Script/EventController.cpp +++ b/apps/openmw-mp/Script/EventController.cpp @@ -13,7 +13,7 @@ void EventController::Init(LuaState &lua) eventsTable["register"] = [&lua](int event, sol::function func, sol::this_environment te) { sol::environment& env = te; - lua.getEventCtrl().registerEvent(event, env, func); + lua.getEventCtrl().registerEvent(event, env, func, false); }; eventsTable["stop"] = [&lua](int event) { lua.getEventCtrl().stop(event); @@ -112,11 +112,11 @@ EventController::EventController(LuaState *luaCtrl) } } -void EventController::registerEvent(int event, sol::environment &env, sol::function& func) +void EventController::registerEvent(int event, sol::environment &env, sol::function& func, bool needsOldState) { auto iter = events.find(event); if (iter != events.end()) - iter->second.push(env, func); + iter->second.push(env, func, needsOldState); } void EventController::stop(int event) @@ -146,12 +146,12 @@ void EventController::raiseEvent(Event id, sol::table data, const string &module if (!moduleName.empty()) { auto f = std::find_if(iter->second.begin(), iter->second.end(), [&moduleName](const auto &item){ - return item.first["ModuleName"]["name"] == moduleName; + return item.env["ModuleName"]["name"] == moduleName; }); if (f != iter->second.end()) - f->second.call(data); // call only specified mod + f->fn.call(data); // call only specified mod } - iter->second.call(CallbackCollection::type_tag(), data); // call all registered events with this id + iter->second.call(CallbackCollection::type_tag(), false, data); // call all registered events with this id } else cerr << "Event with id: " << id << " is not registered" << endl; diff --git a/apps/openmw-mp/Script/EventController.hpp b/apps/openmw-mp/Script/EventController.hpp index dfc6fdcdb..b94d10084 100644 --- a/apps/openmw-mp/Script/EventController.hpp +++ b/apps/openmw-mp/Script/EventController.hpp @@ -79,13 +79,21 @@ namespace CoreEvent class CallbackCollection // todo: add sort by dependencies { public: - typedef std::vector> Container; + struct CBType + { + sol::environment env; + sol::function fn; + bool needsOldState; + CBType(sol::environment _env, sol::function _fn, bool _needsOldState): env(_env), fn(_fn), needsOldState(_needsOldState) {} + }; + + typedef std::vector Container; typedef Container::iterator Iterator; typedef Container::const_iterator CIterator; template struct type_tag {}; - void push(sol::environment &env, sol::function &func) { functions.emplace_back(env, func); } + void push(sol::environment &env, sol::function &func, bool needsOldState) { functions.emplace_back(env, func, needsOldState); } CIterator begin() const { return functions.begin(); } CIterator end() const { return functions.end(); } @@ -93,6 +101,30 @@ public: bool isStoped() const {return _stop;} CIterator stopedAt() const { return lastCalled; } + template + void callWOld(const OldData &oldData, Args&&... args) + { + lastCalled = functions.end(); + _stop = false; + for (CIterator iter = functions.begin(); iter != functions.end(); ++iter) + { + if (!_stop) + { + if (!iter->needsOldState) + { + iter->fn.call(std::forward(args)...); + continue; + } + iter->fn.call(std::forward(args)..., oldData); + } + else + { + lastCalled = iter; + break; + } + } + } + template void call(type_tag, Args&&... args) { @@ -101,14 +133,13 @@ public: for (CIterator iter = functions.begin(); iter != functions.end(); ++iter) { if (!_stop) - iter->second.call(std::forward(args)...); + iter->fn.call(std::forward(args)...); else { lastCalled = iter; break; } } - } template @@ -121,7 +152,7 @@ public: for (CIterator iter = functions.begin(); iter != functions.end(); ++iter) { if (!_stop) - ret = iter->second.call(std::forward(args)...); + ret = iter->fn.call(std::forward(args)...); else { lastCalled = iter; @@ -145,16 +176,25 @@ public: explicit EventController(LuaState *luaCtrl); typedef std::unordered_map Container; - void registerEvent(int event, sol::environment &env, sol::function& func); + void registerEvent(int event, sol::environment &env, sol::function& func, bool needsOldState); CallbackCollection& GetEvents(Event event); Event createEvent(); void raiseEvent(Event id, sol::table data, const std::string &moduleName = ""); void stop(int event); - template + template R Call(Args&&... args) { - return events.at(event).call(CallbackCollection::type_tag{}, std::forward(args)...); + if(canProvideOldState) + events.at(event).callWOld(std::forward(args)...); + else + return events.at(event).call(CallbackCollection::type_tag{}, std::forward(args)...); + } + + template + R Call() + { + return events.at(event).call(CallbackCollection::type_tag{}); } private: Container events;