Use unique_ptr to handle lua state lifetime

This commit is contained in:
elsid 2025-04-18 13:32:11 +02:00
parent 87d77a6882
commit f80283422f
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
5 changed files with 48 additions and 42 deletions

View file

@ -1,8 +1,9 @@
#include "luascripts.hpp"
#include "components/esm3/esmreader.hpp"
#include "components/esm3/esmwriter.hpp"
#include <components/esm3/esmreader.hpp>
#include <components/esm3/esmwriter.hpp>
#include <components/lua/luastateptr.hpp>
#include <components/lua/serialization.hpp>
// List of all records, that are related to Lua.
@ -102,13 +103,16 @@ void ESM::LuaScriptsCfg::adjustRefNums(const ESMReader& esm)
throw std::runtime_error("Incorrect contentFile index");
};
lua_State* L = luaL_newstate();
LuaUtil::LuaStatePtr state(luaL_newstate());
if (state == nullptr)
throw std::runtime_error("Failed to create Lua runtime");
LuaUtil::BasicSerializer serializer(adjustRefNumFn);
auto adjustLuaData = [&](std::string& data) {
if (data.empty())
return;
sol::object luaData = LuaUtil::deserialize(L, data, &serializer);
sol::object luaData = LuaUtil::deserialize(state.get(), data, &serializer);
data = LuaUtil::serialize(luaData, &serializer);
};
@ -123,7 +127,6 @@ void ESM::LuaScriptsCfg::adjustRefNums(const ESMReader& esm)
refCfg.mRefnumContentFile = adjustRefNumFn(refCfg.mRefnumContentFile);
}
}
lua_close(L);
}
void ESM::LuaScriptsCfg::save(ESMWriter& esm) const