mirror of https://github.com/OpenMW/openmw.git
Refactor mwlua/eventqueue and extract some code out of LuaManager
parent
4fd07cb58d
commit
7ef759c78b
@ -1,64 +0,0 @@
|
||||
#include "eventqueue.hpp"
|
||||
|
||||
#include <components/debug/debuglog.hpp>
|
||||
|
||||
#include <components/esm/luascripts.hpp>
|
||||
#include <components/esm3/esmreader.hpp>
|
||||
#include <components/esm3/esmwriter.hpp>
|
||||
|
||||
#include <components/lua/serialization.hpp>
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
template <typename Event>
|
||||
void saveEvent(ESM::ESMWriter& esm, const ObjectId& dest, const Event& event)
|
||||
{
|
||||
esm.writeHNString("LUAE", event.mEventName);
|
||||
dest.save(esm, true);
|
||||
if (!event.mEventData.empty())
|
||||
saveLuaBinaryData(esm, event.mEventData);
|
||||
}
|
||||
|
||||
void loadEvents(sol::state_view& lua, ESM::ESMReader& esm, GlobalEventQueue& globalEvents,
|
||||
LocalEventQueue& localEvents, const std::map<int, int>& contentFileMapping,
|
||||
const LuaUtil::UserdataSerializer* serializer)
|
||||
{
|
||||
while (esm.isNextSub("LUAE"))
|
||||
{
|
||||
std::string name = esm.getHString();
|
||||
ObjectId dest;
|
||||
dest.load(esm, true);
|
||||
std::string data = loadLuaBinaryData(esm);
|
||||
try
|
||||
{
|
||||
data = LuaUtil::serialize(LuaUtil::deserialize(lua, data, serializer), serializer);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
Log(Debug::Error) << "loadEvent: invalid event data: " << e.what();
|
||||
}
|
||||
if (dest.isSet())
|
||||
{
|
||||
auto it = contentFileMapping.find(dest.mContentFile);
|
||||
if (it != contentFileMapping.end())
|
||||
dest.mContentFile = it->second;
|
||||
localEvents.push_back({ dest, std::move(name), std::move(data) });
|
||||
}
|
||||
else
|
||||
globalEvents.push_back({ std::move(name), std::move(data) });
|
||||
}
|
||||
}
|
||||
|
||||
void saveEvents(ESM::ESMWriter& esm, const GlobalEventQueue& globalEvents, const LocalEventQueue& localEvents)
|
||||
{
|
||||
// Used as a marker of a global event.
|
||||
constexpr ObjectId globalId;
|
||||
|
||||
for (const GlobalEvent& e : globalEvents)
|
||||
saveEvent(esm, globalId, e);
|
||||
for (const LocalEvent& e : localEvents)
|
||||
saveEvent(esm, e.mDest, e);
|
||||
}
|
||||
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
#ifndef MWLUA_EVENTQUEUE_H
|
||||
#define MWLUA_EVENTQUEUE_H
|
||||
|
||||
#include "object.hpp"
|
||||
|
||||
namespace ESM
|
||||
{
|
||||
class ESMReader;
|
||||
class ESMWriter;
|
||||
}
|
||||
|
||||
namespace LuaUtil
|
||||
{
|
||||
class UserdataSerializer;
|
||||
}
|
||||
|
||||
namespace sol
|
||||
{
|
||||
class state;
|
||||
}
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
struct GlobalEvent
|
||||
{
|
||||
std::string mEventName;
|
||||
std::string mEventData;
|
||||
};
|
||||
struct LocalEvent
|
||||
{
|
||||
ObjectId mDest;
|
||||
std::string mEventName;
|
||||
std::string mEventData;
|
||||
};
|
||||
using GlobalEventQueue = std::vector<GlobalEvent>;
|
||||
using LocalEventQueue = std::vector<LocalEvent>;
|
||||
|
||||
void loadEvents(sol::state_view& lua, ESM::ESMReader& esm, GlobalEventQueue&, LocalEventQueue&,
|
||||
const std::map<int, int>& contentFileMapping, const LuaUtil::UserdataSerializer* serializer);
|
||||
void saveEvents(ESM::ESMWriter& esm, const GlobalEventQueue&, const LocalEventQueue&);
|
||||
}
|
||||
|
||||
#endif // MWLUA_EVENTQUEUE_H
|
@ -0,0 +1,108 @@
|
||||
#include "luaevents.hpp"
|
||||
|
||||
#include <components/debug/debuglog.hpp>
|
||||
|
||||
#include <components/esm/luascripts.hpp>
|
||||
#include <components/esm3/esmreader.hpp>
|
||||
#include <components/esm3/esmwriter.hpp>
|
||||
|
||||
#include <components/lua/serialization.hpp>
|
||||
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwworld/worldmodel.hpp"
|
||||
|
||||
#include "globalscripts.hpp"
|
||||
#include "localscripts.hpp"
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
void LuaEvents::clear()
|
||||
{
|
||||
mGlobalEventBatch.clear();
|
||||
mLocalEventBatch.clear();
|
||||
mNewGlobalEventBatch.clear();
|
||||
mNewLocalEventBatch.clear();
|
||||
}
|
||||
|
||||
void LuaEvents::finalizeEventBatch()
|
||||
{
|
||||
mNewGlobalEventBatch.swap(mGlobalEventBatch);
|
||||
mNewLocalEventBatch.swap(mLocalEventBatch);
|
||||
mNewGlobalEventBatch.clear();
|
||||
mNewLocalEventBatch.clear();
|
||||
}
|
||||
|
||||
void LuaEvents::callEventHandlers()
|
||||
{
|
||||
for (Global& e : mGlobalEventBatch)
|
||||
mGlobalScripts->receiveEvent(e.mEventName, e.mEventData);
|
||||
mGlobalEventBatch.clear();
|
||||
for (Local& e : mLocalEventBatch)
|
||||
{
|
||||
MWWorld::Ptr ptr = MWBase::Environment::get().getWorldModel()->getPtr(e.mDest);
|
||||
LocalScripts* scripts = ptr.isEmpty() ? nullptr : ptr.getRefData().getLuaScripts();
|
||||
if (scripts)
|
||||
scripts->receiveEvent(e.mEventName, e.mEventData);
|
||||
else
|
||||
Log(Debug::Debug) << "Ignored event " << e.mEventName << " to L" << e.mDest.toString()
|
||||
<< ". Object not found or has no attached scripts";
|
||||
}
|
||||
mLocalEventBatch.clear();
|
||||
}
|
||||
|
||||
template <typename Event>
|
||||
static void saveEvent(ESM::ESMWriter& esm, const ESM::RefNum& dest, const Event& event)
|
||||
{
|
||||
esm.writeHNString("LUAE", event.mEventName);
|
||||
dest.save(esm, true);
|
||||
if (!event.mEventData.empty())
|
||||
saveLuaBinaryData(esm, event.mEventData);
|
||||
}
|
||||
|
||||
void LuaEvents::load(lua_State* lua, ESM::ESMReader& esm, const std::map<int, int>& contentFileMapping,
|
||||
const LuaUtil::UserdataSerializer* serializer)
|
||||
{
|
||||
clear();
|
||||
while (esm.isNextSub("LUAE"))
|
||||
{
|
||||
std::string name = esm.getHString();
|
||||
ESM::RefNum dest;
|
||||
dest.load(esm, true);
|
||||
std::string data = loadLuaBinaryData(esm);
|
||||
try
|
||||
{
|
||||
data = LuaUtil::serialize(LuaUtil::deserialize(lua, data, serializer), serializer);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
Log(Debug::Error) << "loadEvent: invalid event data: " << e.what();
|
||||
}
|
||||
if (dest.isSet())
|
||||
{
|
||||
auto it = contentFileMapping.find(dest.mContentFile);
|
||||
if (it != contentFileMapping.end())
|
||||
dest.mContentFile = it->second;
|
||||
mLocalEventBatch.push_back({ dest, std::move(name), std::move(data) });
|
||||
}
|
||||
else
|
||||
mGlobalEventBatch.push_back({ std::move(name), std::move(data) });
|
||||
}
|
||||
}
|
||||
|
||||
void LuaEvents::save(ESM::ESMWriter& esm) const
|
||||
{
|
||||
// Used as a marker of a global event.
|
||||
constexpr ESM::RefNum globalId;
|
||||
|
||||
for (const Global& e : mGlobalEventBatch)
|
||||
saveEvent(esm, globalId, e);
|
||||
for (const Global& e : mNewGlobalEventBatch)
|
||||
saveEvent(esm, globalId, e);
|
||||
for (const Local& e : mLocalEventBatch)
|
||||
saveEvent(esm, e.mDest, e);
|
||||
for (const Local& e : mNewLocalEventBatch)
|
||||
saveEvent(esm, e.mDest, e);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
#ifndef MWLUA_LUAEVENTS_H
|
||||
#define MWLUA_LUAEVENTS_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include <components/esm3/cellref.hpp> // defines RefNum that is used as a unique id
|
||||
|
||||
struct lua_State;
|
||||
|
||||
namespace ESM
|
||||
{
|
||||
class ESMReader;
|
||||
class ESMWriter;
|
||||
}
|
||||
|
||||
namespace LuaUtil
|
||||
{
|
||||
class UserdataSerializer;
|
||||
}
|
||||
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
class GlobalScripts;
|
||||
|
||||
class LuaEvents
|
||||
{
|
||||
public:
|
||||
explicit LuaEvents(GlobalScripts* globalScripts)
|
||||
: mGlobalScripts(globalScripts)
|
||||
{
|
||||
}
|
||||
|
||||
struct Global
|
||||
{
|
||||
std::string mEventName;
|
||||
std::string mEventData;
|
||||
};
|
||||
struct Local
|
||||
{
|
||||
ESM::RefNum mDest;
|
||||
std::string mEventName;
|
||||
std::string mEventData;
|
||||
};
|
||||
|
||||
void addGlobalEvent(Global event) { mNewGlobalEventBatch.push_back(std::move(event)); }
|
||||
void addLocalEvent(Local event) { mNewLocalEventBatch.push_back(std::move(event)); }
|
||||
|
||||
void clear();
|
||||
void finalizeEventBatch();
|
||||
void callEventHandlers();
|
||||
|
||||
void load(lua_State* lua, ESM::ESMReader& esm, const std::map<int, int>& contentFileMapping,
|
||||
const LuaUtil::UserdataSerializer* serializer);
|
||||
void save(ESM::ESMWriter& esm) const;
|
||||
|
||||
private:
|
||||
GlobalScripts* mGlobalScripts;
|
||||
std::vector<Global> mNewGlobalEventBatch;
|
||||
std::vector<Local> mNewLocalEventBatch;
|
||||
std::vector<Global> mGlobalEventBatch;
|
||||
std::vector<Local> mLocalEventBatch;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MWLUA_LUAEVENTS_H
|
Loading…
Reference in New Issue