From a89bd0e40e525d14efa574f1ad361c09e5d87b63 Mon Sep 17 00:00:00 2001 From: Diject Date: Tue, 30 Dec 2025 21:21:27 +0300 Subject: [PATCH] Add Lua API for map overwrite flag and existing map IDs --- apps/openmw/mwbase/world.hpp | 3 ++ apps/openmw/mwlua/worldbindings.cpp | 45 +++++++++++++++++++++++++++++ apps/openmw/mwworld/worldimp.hpp | 2 +- files/lua_api/openmw/world.lua | 42 +++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwbase/world.hpp b/apps/openmw/mwbase/world.hpp index 01be0f5ba9..531921f1c8 100644 --- a/apps/openmw/mwbase/world.hpp +++ b/apps/openmw/mwbase/world.hpp @@ -613,6 +613,9 @@ namespace MWBase virtual std::string getLocalMapOutputPath() const = 0; ///< Get the local map output path from options or default + virtual bool getOverwriteMaps() const = 0; + ///< Get the overwrite maps flag + virtual void extractWorldMap() = 0; ///< Extract world map using path from options or default diff --git a/apps/openmw/mwlua/worldbindings.cpp b/apps/openmw/mwlua/worldbindings.cpp index 978842b4e0..4c577b1a69 100644 --- a/apps/openmw/mwlua/worldbindings.cpp +++ b/apps/openmw/mwlua/worldbindings.cpp @@ -1,5 +1,7 @@ #include "worldbindings.hpp" +#include + #include #include #include @@ -301,6 +303,49 @@ namespace MWLua return MWBase::Environment::get().getWorld()->isMapExtractionActive(); }; + api["getOverwriteFlag"] = [lua = context.mLua]() -> bool { + checkGameInitialized(lua); + return MWBase::Environment::get().getWorld()->getOverwriteMaps(); + }; + + api["getExistingLocalMapIds"] = [lua = context.mLua](sol::this_state luaState) -> sol::table { + checkGameInitialized(lua); + sol::state_view state(luaState); + sol::table result = state.create_table(); + + std::string localMapPath = MWBase::Environment::get().getWorld()->getLocalMapOutputPath(); + std::filesystem::path dir(localMapPath); + + if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir)) + return result; + + // Use set to store unique filenames (without extension) + std::set uniqueNames; + + for (const auto& entry : std::filesystem::directory_iterator(dir)) + { + if (entry.is_regular_file()) + { + std::string ext = entry.path().extension().string(); + // Check for .yaml, .png, or .tga extensions + if (ext == ".yaml" || ext == ".png" || ext == ".tga") + { + std::string filename = entry.path().stem().string(); + uniqueNames.insert(filename); + } + } + } + + // Convert set to lua table + int index = 1; + for (const auto& name : uniqueNames) + { + result[index++] = name; + } + + return result; + }; + return LuaUtil::makeReadOnly(api); } } diff --git a/apps/openmw/mwworld/worldimp.hpp b/apps/openmw/mwworld/worldimp.hpp index cf75443d9e..aefd0532ab 100644 --- a/apps/openmw/mwworld/worldimp.hpp +++ b/apps/openmw/mwworld/worldimp.hpp @@ -689,7 +689,7 @@ namespace MWWorld std::string getWorldMapOutputPath() const override { return mWorldMapOutputPath; } std::string getLocalMapOutputPath() const override { return mLocalMapOutputPath; } - bool getOverwriteMaps() const { return mOverwriteMaps; } + bool getOverwriteMaps() const override { return mOverwriteMaps; } void extractWorldMap() override; void extractLocalMaps() override; diff --git a/files/lua_api/openmw/world.lua b/files/lua_api/openmw/world.lua index 84edc95874..22a2a5f676 100644 --- a/files/lua_api/openmw/world.lua +++ b/files/lua_api/openmw/world.lua @@ -265,5 +265,47 @@ -- print("Map extraction already in progress") -- end +--- +-- Get the overwrite maps flag from command line options. +-- Returns true if the --overwrite-maps option was specified, false otherwise. +-- This flag determines whether existing map files should be overwritten during extraction. +-- @function [parent=#world] getOverwriteFlag +-- @return #boolean true if overwrite is enabled, false otherwise +-- @usage +-- if world.getOverwriteFlag() then +-- print("Will overwrite existing maps") +-- else +-- print("Will skip existing maps") +-- end + +--- +-- Get list of existing local map IDs (filenames without extension). +-- Returns a table containing unique names of all files in the local map output directory +-- with .yaml, .png, or .tga extensions, without the extension itself. +-- Each filename corresponds to a cell ID that has been extracted. +-- This can be used to check which maps have already been generated. +-- The list contains unique names - if a cell has multiple file types (e.g., both .yaml and .png), +-- the name will appear only once in the list. +-- @function [parent=#world] getExistingLocalMapIds +-- @return #table Array of strings containing unique local map IDs (cell names without extension) +-- @usage +-- local existingMaps = world.getExistingLocalMapIds() +-- for _, mapId in ipairs(existingMaps) do +-- print("Found existing map: " .. mapId) +-- end +-- +-- -- Check if a specific map exists +-- local targetCell = "Balmora" +-- local exists = false +-- for _, mapId in ipairs(existingMaps) do +-- if mapId == targetCell then +-- exists = true +-- break +-- end +-- end +-- if not exists then +-- print("Map for " .. targetCell .. " not found, need to extract") +-- end + return nil