Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: Add PathResolver for cross-platform asset path handling

esenginec48e8bd4dd01
diff --git a/CMakeLists.txt b/CMakeLists.txt
index eda673a89..df8b2f7ef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,6 +37,7 @@ set(ESENGINE_SOURCES
src/esengine/renderer/Framebuffer.cpp
src/esengine/renderer/stb_image_impl.cpp
src/esengine/platform/input/Input.cpp
+ src/esengine/platform/PathResolver.cpp
# UI System
src/esengine/ui/UIContext.cpp
src/esengine/ui/rendering/UIBatchRenderer.cpp
@@ -256,6 +257,8 @@ else()
# Native platform - link GLFW, GLAD, OpenGL, QuickJS, and FreeType
find_package(OpenGL REQUIRED)
target_compile_definitions(esengine PUBLIC ES_PLATFORM_NATIVE)
+ # Development mode: use source directory for editor assets
+ target_compile_definitions(esengine PUBLIC ES_SOURCE_DIR="${CMAKE_SOURCE_DIR}")
target_link_libraries(esengine PUBLIC glfw glad OpenGL::GL quickjs freetype)
target_include_directories(esengine PUBLIC
${CMAKE_SOURCE_DIR}/third_party/glad/include
diff --git a/src/esengine/core/Application.cpp b/src/esengine/core/Application.cpp
index 7fe959895..85dc85acc 100644
--- a/src/esengine/core/Application.cpp
+++ b/src/esengine/core/Application.cpp
@@ -13,6 +13,7 @@
#include "Application.hpp"
#include "Log.hpp"
#include "../platform/input/Input.hpp"
+#include "../platform/FileSystem.hpp"
#ifdef ES_PLATFORM_WEB
#include <emscripten.h>
@@ -70,6 +71,9 @@ void Application::init() {
Log::init();
ES_LOG_INFO("Initializing ESEngine Application: {}", config_.title);
+ // Initialize file system (and PathResolver)
+ FileSystem::init();
+
// Create and initialize platform
platform_ = Platform::create();
if (!platform_->initialize(config_.width, config_.height)) {
@@ -185,6 +189,9 @@ void Application::shutdown() {
// Shutdown platform
platform_->shutdown();
+ // Shutdown file system
+ FileSystem::shutdown();
+
Log::shutdown();
}
diff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cpp
index 24e776b70..25ec99270 100644
--- a/src/esengine/editor/EditorApplication.cpp
+++ b/src/esengine/editor/EditorApplication.cpp
@@ -14,6 +14,7 @@
#include "../renderer/RenderCommand.hpp"
#include "../math/Math.hpp"
#include "../platform/input/Input.hpp"
+#include "../platform/PathResolver.hpp"
#include "../ui/widgets/Panel.hpp"
#include "../ui/widgets/Label.hpp"
#include "../ui/widgets/Button.hpp"
@@ -76,7 +77,6 @@ void EditorApplication::onInit() {
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
#endif
- "assets/fonts/default.ttf",
nullptr
};
@@ -88,11 +88,21 @@ void EditorApplication::onInit() {
break;
}
}
+
+ if (!fontLoaded) {
+ std::string fallbackFont = PathResolver::editorPath("assets/fonts/default.ttf");
+ if (uiContext_->loadFont("default", fallbackFont, 48.0f, 8.0f)) {
+ ES_LOG_INFO("Loaded SDF font: {}", fallbackFont);
+ fontLoaded = true;
+ }
+ }
+
if (!fontLoaded) {
ES_LOG_WARN("No font loaded, text will not render");
}
- if (!uiContext_->loadFont("icons", "assets/fonts/lucide.ttf", 48.0f, 4.0f)) {
+ std::string iconFontPath = PathResolver::editorPath("assets/fonts/lucide.ttf");
+ if (!uiContext_->loadFont("icons", iconFontPath, 48.0f, 4.0f)) {
ES_LOG_WARN("Icon font not loaded, icons will not render");
}
@@ -114,7 +124,7 @@ void EditorApplication::onInit() {
assetDatabase_.setOnAssetAdded([this](const AssetMetadata& asset) {
thumbnailGenerator_.generateThumbnail(asset.guid, asset.path, asset.type);
});
- assetDatabase_.setProjectPath("assets");
+ assetDatabase_.setProjectPath(PathResolver::projectPath("assets"));
assetDatabase_.scan();
// Create demo scene for testing
diff --git a/src/esengine/platform/PathResolver.cpp b/src/esengine/platform/PathResolver.cpp
new file mode 100644
index 000000000..8d56609d8
--- /dev/null
+++ b/src/esengine/platform/PathResolver.cpp
@@ -0,0 +1,195 @@
+/**
+ * @file PathResolver.cpp
+ * @brief Cross-platform path resolution implementation
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "PathResolver.hpp"
+#include "../core/Log.hpp"
+
+#ifdef ES_PLATFORM_WINDOWS
+#include <windows.h>
+#elif defined(ES_PLATFORM_LINUX)
+#include <unistd.h>
+#include <linux/limits.h>
+#elif defined(ES_PLATFORM_MACOS)
+#include <mach-o/dyld.h>
+#include <limits.h>
+#endif
+
+#include <algorithm>
+
+namespace esengine {
+
+std::string PathResolver::editorRoot_;
+std::string PathResolver::projectRoot_;
+bool PathResolver::initialized_ = false;
+
+void PathResolver::init() {
+ if (initialized_) return;
+
+#ifdef ES_SOURCE_DIR
+ // Development mode: use source directory for editor assets
+ editorRoot_ = ES_SOURCE_DIR;
+ projectRoot_ = ES_SOURCE_DIR;
+ ES_LOG_INFO("PathResolver initialized (dev mode) - Editor root: {}", editorRoot_);
+#else
+ // Release mode: use executable directory
+ editorRoot_ = getExecutableDirectory();
+ projectRoot_ = editorRoot_;
+ ES_LOG_INFO("PathResolver initialized - Editor root: {}", editorRoot_);
+#endif
+
+ initialized_ = true;
+}
+
+void PathResolver::shutdown() {
+ if (!initialized_) return;
+
+ editorRoot_.clear();
+ projectRoot_.clear();
+ initialized_ = false;
+}
+
+void PathResolver::setProjectRoot(const std::string& path) {
+ projectRoot_ = normalizePath(path);
+ ES_LOG_INFO("Project root set to: {}", projectRoot_);
+}
+
+const std::string& PathResolver::getProjectRoot() {
+ return projectRoot_;
+}
+
+const std::string& PathResolver::getEditorRoot() {
+ return editorRoot_;
+}
+
+std::string PathResolver::resolve(const std::string& path, PathType type) {
+ if (type == PathType::Absolute || isAbsolutePath(path)) {
+ return normalizePath(path);
+ }
+
+ std::string basePath;
+ switch (type) {
+ case PathType::Editor:
+ basePath = editorRoot_;
+ break;
+ case PathType::Project:
+ basePath = projectRoot_;
+ break;
+ default:
+ basePath = editorRoot_;
+ break;
+ }
+
+ if (basePath.empty()) {
+ return normalizePath(path);
+ }
+
+ return normalizePath(basePath + "/" + path);
+}
+
+std::string PathResolver::editorPath(const std::string& relativePath) {
+ return resolve(relativePath, PathType::Editor);
+}
+
+std::string PathResolver::projectPath(const std::string& relativePath) {
+ return resolve(relativePath, PathType::Project);
+}
+
+bool PathResolver::isAbsolutePath(const std::string& path) {
+ if (path.empty()) return false;
+
+#ifdef ES_PLATFORM_WINDOWS
+ if (path.length() >= 2 && path[1] == ':') {
+ return true;
+ }
+ if (path.length() >= 2 && (path[0] == '\\' && path[1] == '\\')) {
+ return true;
+ }
+#else
+ if (path[0] == '/') {
+ return true;
+ }
+#endif
+
+ return false;
+}
+
+std::string PathResolver::getExecutableDirectory() {
+#ifdef ES_PLATFORM_WINDOWS
+ char buffer[MAX_PATH];
+ DWORD length = GetModuleFileNameA(nullptr, buffer, MAX_PATH);
+ if (length > 0 && length < MAX_PATH) {
+ std::string path(buffer);
+ auto pos = path.find_last_of("\\/");
+ if (pos != std::string::npos) {
+ return path.substr(0, pos);
+ }
+ }
+ return ".";
+
+#elif defined(ES_PLATFORM_LINUX)
+ char buffer[PATH_MAX];
+ ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
+ if (len != -1) {
+ buffer[len] = '\0';
+ std::string path(buffer);
+ auto pos = path.find_last_of('/');
+ if (pos != std::string::npos) {
+ return path.substr(0, pos);
+ }
+ }
+ return ".";
+
+#elif defined(ES_PLATFORM_MACOS)
+ char buffer[PATH_MAX];
+ uint32_t size = sizeof(buffer);
+ if (_NSGetExecutablePath(buffer, &size) == 0) {
+ char realPath[PATH_MAX];
+ if (realpath(buffer, realPath)) {
+ std::string path(realPath);
+ auto pos = path.find_last_of('/');
+ if (pos != std::string::npos) {
+ return path.substr(0, pos);
+ }
+ }
+ }
+ return ".";
+
+#elif defined(ES_PLATFORM_WEB)
+ return "";
+
+#else
+ return ".";
+#endif
+}
+
+std::string PathResolver::normalizePath(const std::string& path) {
+ if (path.empty()) return path;
+
+ std::string result = path;
+
+#ifdef ES_PLATFORM_WINDOWS
+ std::replace(result.begin(), result.end(), '/', '\\');
+
+ while (result.length() > 1 && result.back() == '\\') {
+ result.pop_back();
+ }
+#else
+ std::replace(result.begin(), result.end(), '\\', '/');
+
+ while (result.length() > 1 && result.back() == '/') {
+ result.pop_back();
+ }
+#endif
+
+ return result;
+}
+
+} // namespace esengine
diff --git a/src/esengine/platform/PathResolver.hpp b/src/esengine/platform/PathResolver.hpp
new file mode 100644
index 000000000..52d19e5b3
--- /dev/null
+++ b/src/esengine/platform/PathResolver.hpp
@@ -0,0 +1,60 @@
+/**
+ * @file PathResolver.hpp
+ * @brief Cross-platform path resolution for editor and project resources
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#pragma once
+
+#include "../core/Types.hpp"
+#include <string>
+
+namespace esengine {
+
+enum class PathType {
+ Editor, // Relative to executable directory
+ Project, // Relative to project directory
+ Absolute // Use path as-is
+};
+
+/**
+ * @brief Cross-platform path resolver for editor and project resources
+ *
+ * @details Distinguishes between:
+ * - Editor assets: fonts, themes, built-in resources (relative to exe)
+ * - Project assets: user scenes, textures, scripts (relative to project root)
+ */
+class PathResolver {
+public:
+ static void init();
+ static void shutdown();
+
+ static void setProjectRoot(const std::string& path);
+ [[nodiscard]] static const std::string& getProjectRoot();
+
+ [[nodiscard]] static const std::string& getEditorRoot();
+
+ [[nodiscard]] static std::string resolve(const std::string& path, PathType type);
+
+ [[nodiscard]] static std::string editorPath(const std::string& relativePath);
+ [[nodiscard]] static std::string projectPath(const std::string& relativePath);
+
+ [[nodiscard]] static bool isAbsolutePath(const std::string& path);
+
+private:
+ PathResolver() = delete;
+
+ static std::string getExecutableDirectory();
+ static std::string normalizePath(const std::string& path);
+
+ static std::string editorRoot_;
+ static std::string projectRoot_;
+ static bool initialized_;
+};
+
+} // namespace esengine
diff --git a/src/esengine/platform/native/NativeFileSystem.cpp b/src/esengine/platform/native/NativeFileSystem.cpp
index eeaf33574..38e7d02ec 100644
--- a/src/esengine/platform/native/NativeFileSystem.cpp
+++ b/src/esengine/platform/native/NativeFileSystem.cpp
@@ -10,6 +10,7 @@
*/
#include "../FileSystem.hpp"
+#include "../PathResolver.hpp"
#include "../../core/Log.hpp"
#include <fstream>
@@ -45,6 +46,8 @@ void FileSystem::init() {
return;
}
+ PathResolver::init();
+
s_watchedFiles.clear();
s_initialized = true;
@@ -59,6 +62,8 @@ void FileSystem::shutdown() {
s_watchedFiles.clear();
s_initialized = false;
+ PathResolver::shutdown();
+
ES_LOG_INFO("FileSystem shutdown");
}
diff --git a/src/esengine/platform/web/WebFileSystem.cpp b/src/esengine/platform/web/WebFileSystem.cpp
index b6d4cab64..2b20627a1 100644
--- a/src/esengine/platform/web/WebFileSystem.cpp
+++ b/src/esengine/platform/web/WebFileSystem.cpp
@@ -10,6 +10,7 @@
*/
#include "../FileSystem.hpp"
+#include "../PathResolver.hpp"
#include "../../core/Log.hpp"
#ifdef ES_PLATFORM_WEB
@@ -26,10 +27,12 @@ namespace esengine {
// =============================================================================
void FileSystem::init() {
+ PathResolver::init();
ES_LOG_INFO("FileSystem initialized (Web platform - read-only preloaded assets)");
}
void FileSystem::shutdown() {
+ PathResolver::shutdown();
ES_LOG_INFO("FileSystem shutdown");
}