From be1ce81be744c1df03c7b00da0e2b239012a2aeb Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 6 Jan 2025 23:05:32 +0100 Subject: [PATCH 1/2] Write debug recast mesh before generating navmesh --- .../detournavigator/asyncnavmeshupdater.cpp | 60 +++++++++++-------- .../detournavigator/asyncnavmeshupdater.hpp | 2 - 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/components/detournavigator/asyncnavmeshupdater.cpp b/components/detournavigator/asyncnavmeshupdater.cpp index 24d7cc0d32..71c8bbc2d3 100644 --- a/components/detournavigator/asyncnavmeshupdater.cpp +++ b/components/detournavigator/asyncnavmeshupdater.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -74,6 +75,36 @@ namespace DetourNavigator { return job.mGeneratedNavMeshData != nullptr; } + + std::string makeRevision(const Version& version) + { + return Misc::StringUtils::format(".%zu.%zu", version.mGeneration, version.mRevision); + } + + void writeDebugRecastMesh( + const Settings& settings, const TilePosition& tilePosition, const RecastMesh& recastMesh) + { + if (!settings.mEnableWriteRecastMeshToFile) + return; + std::string revision; + if (settings.mEnableRecastMeshFileNameRevision) + revision = makeRevision(recastMesh.getVersion()); + writeToFile(recastMesh, + Misc::StringUtils::format( + "%s%d.%d.", settings.mRecastMeshPathPrefix, tilePosition.x(), tilePosition.y()), + revision, settings.mRecast); + } + + void writeDebugNavMesh( + const Settings& settings, const GuardedNavMeshCacheItem& navMeshCacheItem, const Version& version) + { + if (!settings.mEnableWriteNavMeshToFile) + return; + std::string revision; + if (settings.mEnableNavMeshFileNameRevision) + revision = makeRevision(version); + writeToFile(navMeshCacheItem.lockConst()->getImpl(), settings.mNavMeshPathPrefix, revision); + } } std::ostream& operator<<(std::ostream& stream, JobStatus value) @@ -512,6 +543,8 @@ namespace DetourNavigator return JobStatus::Done; } + writeDebugRecastMesh(mSettings, job.mChangedTile, *recastMesh); + NavMeshTilesCache::Value cachedNavMeshData = mNavMeshTilesCache.get(job.mAgentBounds, job.mChangedTile, *recastMesh); std::unique_ptr preparedNavMeshData; @@ -633,7 +666,7 @@ namespace DetourNavigator mPresentTiles.insert(std::make_tuple(job.mAgentBounds, job.mChangedTile)); } - writeDebugFiles(job, &recastMesh); + writeDebugNavMesh(mSettings, navMeshCacheItem, navMeshVersion); return isSuccess(status) ? JobStatus::Done : JobStatus::Fail; } @@ -688,31 +721,6 @@ namespace DetourNavigator return job; } - void AsyncNavMeshUpdater::writeDebugFiles(const Job& job, const RecastMesh* recastMesh) const - { - std::string revision; - std::string recastMeshRevision; - std::string navMeshRevision; - if ((mSettings.get().mEnableWriteNavMeshToFile || mSettings.get().mEnableWriteRecastMeshToFile) - && (mSettings.get().mEnableRecastMeshFileNameRevision || mSettings.get().mEnableNavMeshFileNameRevision)) - { - revision = "." - + std::to_string((std::chrono::steady_clock::now() - std::chrono::steady_clock::time_point()).count()); - if (mSettings.get().mEnableRecastMeshFileNameRevision) - recastMeshRevision = revision; - if (mSettings.get().mEnableNavMeshFileNameRevision) - navMeshRevision = std::move(revision); - } - if (recastMesh && mSettings.get().mEnableWriteRecastMeshToFile) - writeToFile(*recastMesh, - mSettings.get().mRecastMeshPathPrefix + std::to_string(job.mChangedTile.x()) + "_" - + std::to_string(job.mChangedTile.y()) + "_", - recastMeshRevision, mSettings.get().mRecast); - if (mSettings.get().mEnableWriteNavMeshToFile) - if (const auto shared = job.mNavMeshCacheItem.lock()) - writeToFile(shared->lockConst()->getImpl(), mSettings.get().mNavMeshPathPrefix, navMeshRevision); - } - bool AsyncNavMeshUpdater::lockTile( std::size_t jobId, const AgentBounds& agentBounds, const TilePosition& changedTile) { diff --git a/components/detournavigator/asyncnavmeshupdater.hpp b/components/detournavigator/asyncnavmeshupdater.hpp index a4c446afc9..7877ff8082 100644 --- a/components/detournavigator/asyncnavmeshupdater.hpp +++ b/components/detournavigator/asyncnavmeshupdater.hpp @@ -248,8 +248,6 @@ namespace DetourNavigator void postThreadJob(JobIt job, std::deque& queue); - void writeDebugFiles(const Job& job, const RecastMesh* recastMesh) const; - bool lockTile(std::size_t jobId, const AgentBounds& agentBounds, const TilePosition& changedTile); void unlockTile(std::size_t jobId, const AgentBounds& agentBounds, const TilePosition& changedTile); From 6464d9913467c9667929de203c8d4b5bdc37f701 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 6 Jan 2025 23:05:58 +0100 Subject: [PATCH 2/2] Throw system error on open file failure --- components/detournavigator/debug.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/detournavigator/debug.cpp b/components/detournavigator/debug.cpp index 5ce1464bdd..3a3430c9d3 100644 --- a/components/detournavigator/debug.cpp +++ b/components/detournavigator/debug.cpp @@ -1,5 +1,4 @@ #include "debug.hpp" -#include "exceptions.hpp" #include "recastmesh.hpp" #include "settings.hpp" #include "settingsutils.hpp" @@ -17,7 +16,7 @@ #include #include #include -#include +#include namespace DetourNavigator { @@ -224,7 +223,8 @@ namespace DetourNavigator const auto path = pathPrefix + "recastmesh" + revision + ".obj"; std::ofstream file(std::filesystem::path(path), std::ios::out); if (!file.is_open()) - throw NavigatorException("Open file failed: " + path); + throw std::system_error( + errno, std::generic_category(), "Failed to open file to write recast mesh: " + path); file.exceptions(std::ios::failbit | std::ios::badbit); file.precision(std::numeric_limits::max_exponent10); std::vector vertices = recastMesh.getMesh().getVertices(); @@ -271,7 +271,7 @@ namespace DetourNavigator const auto path = pathPrefix + "all_tiles_navmesh" + revision + ".bin"; std::ofstream file(std::filesystem::path(path), std::ios::out | std::ios::binary); if (!file.is_open()) - throw NavigatorException("Open file failed: " + path); + throw std::system_error(errno, std::generic_category(), "Failed to open file to write navmesh: " + path); file.exceptions(std::ios::failbit | std::ios::badbit); NavMeshSetHeader header;