mirror of https://github.com/OpenMW/openmw.git
Saving/loading for Lua scripts (saves format is changed)
parent
914e604e06
commit
8c6d303730
@ -0,0 +1,63 @@
|
||||
#include "eventqueue.hpp"
|
||||
|
||||
#include <components/debug/debuglog.hpp>
|
||||
|
||||
#include <components/esm/esmreader.hpp>
|
||||
#include <components/esm/esmwriter.hpp>
|
||||
#include <components/esm/luascripts.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.eventName);
|
||||
dest.save(esm, true);
|
||||
if (!event.eventData.empty())
|
||||
saveLuaBinaryData(esm, event.eventData);
|
||||
}
|
||||
|
||||
void loadEvents(sol::state& 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)
|
||||
{
|
||||
ObjectId globalId;
|
||||
globalId.unset(); // Used as a marker of a global event.
|
||||
|
||||
for (const GlobalEvent& e : globalEvents)
|
||||
saveEvent(esm, globalId, e);
|
||||
for (const LocalEvent& e : localEvents)
|
||||
saveEvent(esm, e.dest, e);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue