#ifndef MWLUA_RECORDSTORE_H #define MWLUA_RECORDSTORE_H #include #include #include #include #include "apps/openmw/mwbase/environment.hpp" #include "apps/openmw/mwbase/world.hpp" #include "apps/openmw/mwworld/esmstore.hpp" #include "apps/openmw/mwworld/store.hpp" #include "context.hpp" #include "object.hpp" namespace sol { // Ensure sol does not try to create the automatic Container or usertype bindings for Store. // They include write operations and we want the store to be read-only. template struct is_automagical> : std::false_type { }; } namespace MWLua { template void addRecordFunctionBinding( sol::table& table, const Context& context, const std::string& recordName = std::string(T::getRecordType())) { const MWWorld::Store& store = MWBase::Environment::get().getESMStore()->get(); table["record"] = sol::overload([](const Object& obj) -> const T* { return obj.ptr().get()->mBase; }, [&store](std::string_view id) -> const T* { return store.search(ESM::RefId::deserializeText(id)); }); // Define a custom user type for the store. // Provide the interface of a read-only array. using StoreT = MWWorld::Store; sol::state_view& lua = context.mLua->sol(); sol::usertype storeT = lua.new_usertype(recordName + "WorldStore"); storeT[sol::meta_function::to_string] = [recordName](const StoreT& store) { return "{" + std::to_string(store.getSize()) + " " + recordName + " records}"; }; storeT[sol::meta_function::length] = [](const StoreT& store) { return store.getSize(); }; storeT[sol::meta_function::index] = sol::overload( [](const StoreT& store, size_t index) -> const T* { if (index == 0 || index > store.getSize()) return nullptr; return store.at(LuaUtil::fromLuaIndex(index)); }, [](const StoreT& store, std::string_view id) -> const T* { return store.search(ESM::RefId::deserializeText(id)); }); storeT[sol::meta_function::ipairs] = lua["ipairsForArray"].template get(); storeT[sol::meta_function::pairs] = lua["ipairsForArray"].template get(); // Provide access to the store. table["records"] = &store; } } #endif // MWLUA_RECORDSTORE_H