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.

fix: Add missing includes for ScriptSystem compilation

esengine7864e5df3764
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 85f5f65f1..ee25ff266 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -134,11 +134,13 @@ set(ESENGINE_HEADERS
if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
list(APPEND ESENGINE_SOURCES
src/esengine/platform/web/WebPlatform.cpp
+ src/esengine/platform/web/WebFileSystem.cpp
)
else()
# Native platform (GLFW + OpenGL)
list(APPEND ESENGINE_SOURCES
src/esengine/platform/native/NativePlatform.cpp
+ src/esengine/platform/native/NativeFileSystem.cpp
# Scripting (Native only - Web uses Emscripten bindings)
src/esengine/scripting/ScriptContext.cpp
src/esengine/scripting/ScriptValue.cpp
@@ -157,6 +159,11 @@ else()
endif()
+# Platform-independent headers
+list(APPEND ESENGINE_HEADERS
+ src/esengine/platform/FileSystem.hpp
+)
+
add_library(esengine STATIC ${ESENGINE_SOURCES} ${ESENGINE_HEADERS})
target_include_directories(esengine
diff --git a/src/esengine/ecs/System.hpp b/src/esengine/ecs/System.hpp
index f60c73291..1c9625326 100644
--- a/src/esengine/ecs/System.hpp
+++ b/src/esengine/ecs/System.hpp
@@ -19,6 +19,9 @@
// Project includes
#include "../core/Types.hpp"
+// Standard library
+#include <algorithm>
+
namespace esengine::ecs {
// Forward declaration
diff --git a/src/esengine/ecs/systems/ScriptSystem.cpp b/src/esengine/ecs/systems/ScriptSystem.cpp
index 73e232c56..f78315a51 100644
--- a/src/esengine/ecs/systems/ScriptSystem.cpp
+++ b/src/esengine/ecs/systems/ScriptSystem.cpp
@@ -10,6 +10,7 @@
*/
#include "ScriptSystem.hpp"
+#include "../Registry.hpp"
#include "../../core/Log.hpp"
#include "../../scripting/ScriptValue.hpp"
diff --git a/src/esengine/ecs/systems/ScriptSystem.hpp b/src/esengine/ecs/systems/ScriptSystem.hpp
index c641654f8..858ac2232 100644
--- a/src/esengine/ecs/systems/ScriptSystem.hpp
+++ b/src/esengine/ecs/systems/ScriptSystem.hpp
@@ -15,6 +15,10 @@
#include "../components/Script.hpp"
#include "../../scripting/ScriptContext.hpp"
+namespace esengine {
+class ScriptValue; // Forward declaration
+}
+
namespace esengine::ecs {
/**
diff --git a/src/esengine/platform/FileSystem.hpp b/src/esengine/platform/FileSystem.hpp
new file mode 100644
index 000000000..272476169
--- /dev/null
+++ b/src/esengine/platform/FileSystem.hpp
@@ -0,0 +1,165 @@
+/**
+ * @file FileSystem.hpp
+ * @brief Cross-platform file system abstraction with hot reload support
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#pragma once
+
+#include "../core/Types.hpp"
+#include <string>
+#include <vector>
+#include <functional>
+
+namespace esengine {
+
+/**
+ * @brief Platform-independent file system interface
+ *
+ * @details Provides file I/O operations and file monitoring for hot reload.
+ * - Native platforms use standard C++ filesystem and OS-specific file watchers
+ * - Web platforms use Emscripten virtual filesystem with preloaded assets
+ */
+class FileSystem {
+public:
+ /**
+ * @brief Initialize the file system
+ * @note Must be called before any file operations
+ */
+ static void init();
+
+ /**
+ * @brief Shutdown the file system and clean up watchers
+ */
+ static void shutdown();
+
+ /**
+ * @brief Update file watchers (call once per frame)
+ * @note Required for polling-based file monitoring
+ */
+ static void update();
+
+ // =========================================================================
+ // File Operations
+ // =========================================================================
+
+ /**
+ * @brief Check if a file exists
+ * @param path File path (relative or absolute)
+ * @return true if file exists and is readable
+ */
+ static bool fileExists(const std::string& path);
+
+ /**
+ * @brief Read entire file as text
+ * @param path File path
+ * @return File contents as string, empty string on error
+ */
+ static std::string readTextFile(const std::string& path);
+
+ /**
+ * @brief Read entire file as binary data
+ * @param path File path
+ * @return File contents as byte vector, empty on error
+ */
+ static std::vector<u8> readBinaryFile(const std::string& path);
+
+ /**
+ * @brief Write text to file
+ * @param path File path
+ * @param content Text content to write
+ * @return true if write succeeded
+ */
+ static bool writeTextFile(const std::string& path, const std::string& content);
+
+ /**
+ * @brief Write binary data to file
+ * @param path File path
+ * @param data Binary data to write
+ * @return true if write succeeded
+ */
+ static bool writeBinaryFile(const std::string& path, const std::vector<u8>& data);
+
+ /**
+ * @brief Get file size in bytes
+ * @param path File path
+ * @return File size, 0 on error
+ */
+ static usize getFileSize(const std::string& path);
+
+ /**
+ * @brief Get last modification time (Unix timestamp)
+ * @param path File path
+ * @return Modification time in seconds since epoch, 0 on error
+ */
+ static u64 getFileModificationTime(const std::string& path);
+
+ // =========================================================================
+ // Directory Operations
+ // =========================================================================
+
+ /**
+ * @brief Check if a directory exists
+ * @param path Directory path
+ * @return true if directory exists
+ */
+ static bool directoryExists(const std::string& path);
+
+ /**
+ * @brief List all files in a directory
+ * @param path Directory path
+ * @param recursive If true, list files in subdirectories
+ * @return Vector of file paths (relative to input path)
+ */
+ static std::vector<std::string> listDirectory(const std::string& path, bool recursive = false);
+
+ /**
+ * @brief Create a directory (and parent directories if needed)
+ * @param path Directory path
+ * @return true if creation succeeded or directory already exists
+ */
+ static bool createDirectory(const std::string& path);
+
+ // =========================================================================
+ // File Monitoring (Hot Reload)
+ // =========================================================================
+
+ /**
+ * @brief Watch a file for changes
+ * @param path File path to watch
+ * @param callback Function called when file changes (receives file path)
+ *
+ * @details Implementation varies by platform:
+ * - Windows: Uses ReadDirectoryChangesW (native event-based)
+ * - Linux: Uses inotify (native event-based)
+ * - macOS: Uses FSEvents (native event-based)
+ * - Fallback: Polling via update() (checks mtime every frame)
+ *
+ * @note Callback is called on the main thread during update()
+ */
+ static void watchFile(const std::string& path,
+ std::function<void(const std::string&)> callback);
+
+ /**
+ * @brief Stop watching a file
+ * @param path File path to unwatch
+ */
+ static void unwatchFile(const std::string& path);
+
+ /**
+ * @brief Check if a file is being watched
+ * @param path File path to check
+ * @return true if file is being watched
+ */
+ static bool isWatching(const std::string& path);
+
+private:
+ FileSystem() = delete; // Static class, no instances
+};
+
+} // namespace esengine
diff --git a/src/esengine/platform/native/NativeFileSystem.cpp b/src/esengine/platform/native/NativeFileSystem.cpp
new file mode 100644
index 000000000..158ecabd8
--- /dev/null
+++ b/src/esengine/platform/native/NativeFileSystem.cpp
@@ -0,0 +1,273 @@
+/**
+ * @file NativeFileSystem.cpp
+ * @brief Native platform file system implementation
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "../FileSystem.hpp"
+#include "../../core/Log.hpp"
+
+#include <fstream>
+#include <sstream>
+#include <filesystem>
+#include <unordered_map>
+#include <chrono>
+
+namespace fs = std::filesystem;
+
+namespace esengine {
+
+// =============================================================================
+// File Watcher Data
+// =============================================================================
+
+struct WatchedFile {
+ std::string path;
+ u64 lastModTime;
+ std::function<void(const std::string&)> callback;
+};
+
+static bool s_initialized = false;
+static std::unordered_map<std::string, WatchedFile> s_watchedFiles;
+
+// =============================================================================
+// Initialization
+// =============================================================================
+
+void FileSystem::init() {
+ if (s_initialized) {
+ ES_LOG_WARN("FileSystem already initialized");
+ return;
+ }
+
+ s_watchedFiles.clear();
+ s_initialized = true;
+
+ ES_LOG_INFO("FileSystem initialized (polling-based file watcher)");
+}
+
+void FileSystem::shutdown() {
+ if (!s_initialized) {
+ return;
+ }
+
+ s_watchedFiles.clear();
+ s_initialized = false;
+
+ ES_LOG_INFO("FileSystem shutdown");
+}
+
+void FileSystem::update() {
+ if (!s_initialized) {
+ return;
+ }
+
+ // Poll all watched files for modifications
+ for (auto& [path, watched] : s_watchedFiles) {
+ u64 currentModTime = getFileModificationTime(watched.path);
+
+ if (currentModTime > watched.lastModTime) {
+ ES_LOG_INFO("File changed: {}", watched.path);
+ watched.lastModTime = currentModTime;
+
+ // Call callback
+ if (watched.callback) {
+ watched.callback(watched.path);
+ }
+ }
+ }
+}
+
+// =============================================================================
+// File Operations
+// =============================================================================
+
+bool FileSystem::fileExists(const std::string& path) {
+ try {
+ return fs::exists(path) && fs::is_regular_file(path);
+ } catch (const fs::filesystem_error& e) {
+ ES_LOG_ERROR("fileExists error: {}", e.what());
+ return false;
+ }
+}
+
+std::string FileSystem::readTextFile(const std::string& path) {
+ std::ifstream file(path, std::ios::in);
+ if (!file.is_open()) {
+ ES_LOG_ERROR("Failed to open file: {}", path);
+ return "";
+ }
+
+ std::stringstream buffer;
+ buffer << file.rdbuf();
+ file.close();
+
+ return buffer.str();
+}
+
+std::vector<u8> FileSystem::readBinaryFile(const std::string& path) {
+ std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
+ if (!file.is_open()) {
+ ES_LOG_ERROR("Failed to open file: {}", path);
+ return {};
+ }
+
+ std::streamsize size = file.tellg();
+ file.seekg(0, std::ios::beg);
+
+ std::vector<u8> buffer(static_cast<usize>(size));
+ if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
+ ES_LOG_ERROR("Failed to read file: {}", path);
+ return {};
+ }
+
+ file.close();
+ return buffer;
+}
+
+bool FileSystem::writeTextFile(const std::string& path, const std::string& content) {
+ std::ofstream file(path, std::ios::out | std::ios::trunc);
+ if (!file.is_open()) {
+ ES_LOG_ERROR("Failed to open file for writing: {}", path);
+ return false;
+ }
+
+ file << content;
+ file.close();
+
+ return true;
+}
+
+bool FileSystem::writeBinaryFile(const std::string& path, const std::vector<u8>& data) {
+ std::ofstream file(path, std::ios::out | std::ios::binary | std::ios::trunc);
+ if (!file.is_open()) {
+ ES_LOG_ERROR("Failed to open file for writing: {}", path);
+ return false;
+ }
+
+ file.write(reinterpret_cast<const char*>(data.data()),
+ static_cast<std::streamsize>(data.size()));
+ file.close();
+
+ return true;
+}
+
+usize FileSystem::getFileSize(const std::string& path) {
+ try {
+ return static_cast<usize>(fs::file_size(path));
+ } catch (const fs::filesystem_error& e) {
+ ES_LOG_ERROR("getFileSize error: {}", e.what());
+ return 0;
+ }
+}
+
+u64 FileSystem::getFileModificationTime(const std::string& path) {
+ try {
+ auto ftime = fs::last_write_time(path);
+ auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
+ ftime - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
+ );
+ return static_cast<u64>(std::chrono::system_clock::to_time_t(sctp));
+ } catch (const fs::filesystem_error& e) {
+ ES_LOG_ERROR("getFileModificationTime error: {}", e.what());
+ return 0;
+ }
+}
+
+// =============================================================================
+// Directory Operations
+// =============================================================================
+
+bool FileSystem::directoryExists(const std::string& path) {
+ try {
+ return fs::exists(path) && fs::is_directory(path);
+ } catch (const fs::filesystem_error& e) {
+ ES_LOG_ERROR("directoryExists error: {}", e.what());
+ return false;
+ }
+}
+
+std::vector<std::string> FileSystem::listDirectory(const std::string& path, bool recursive) {
+ std::vector<std::string> files;
+
+ try {
+ if (recursive) {
+ for (const auto& entry : fs::recursive_directory_iterator(path)) {
+ if (entry.is_regular_file()) {
+ files.push_back(entry.path().string());
+ }
+ }
+ } else {
+ for (const auto& entry : fs::directory_iterator(path)) {
+ if (entry.is_regular_file()) {
+ files.push_back(entry.path().string());
+ }
+ }
+ }
+ } catch (const fs::filesystem_error& e) {
+ ES_LOG_ERROR("listDirectory error: {}", e.what());
+ }
+
+ return files;
+}
+
+bool FileSystem::createDirectory(const std::string& path) {
+ try {
+ return fs::create_directories(path);
+ } catch (const fs::filesystem_error& e) {
+ ES_LOG_ERROR("createDirectory error: {}", e.what());
+ return false;
+ }
+}
+
+// =============================================================================
+// File Monitoring (Hot Reload)
+// =============================================================================
+
+void FileSystem::watchFile(const std::string& path,
+ std::function<void(const std::string&)> callback) {
+ if (!s_initialized) {
+ ES_LOG_ERROR("FileSystem not initialized");
+ return;
+ }
+
+ if (!fileExists(path)) {
+ ES_LOG_WARN("Cannot watch non-existent file: {}", path);
+ return;
+ }
+
+ // Get initial modification time
+ u64 modTime = getFileModificationTime(path);
+
+ // Add to watch list
+ s_watchedFiles[path] = WatchedFile{path, modTime, callback};
+
+ ES_LOG_INFO("Watching file: {}", path);
+}
+
+void FileSystem::unwatchFile(const std::string& path) {
+ if (!s_initialized) {
+ return;
+ }
+
+ auto it = s_watchedFiles.find(path);
+ if (it != s_watchedFiles.end()) {
+ s_watchedFiles.erase(it);
+ ES_LOG_INFO("Stopped watching file: {}", path);
+ }
+}
+
+bool FileSystem::isWatching(const std::string& path) {
+ if (!s_initialized) {
+ return false;
+ }
+
+ return s_watchedFiles.find(path) != s_watchedFiles.end();
+}
+
+} // namespace esengine
diff --git a/src/esengine/platform/web/WebFileSystem.cpp b/src/esengine/platform/web/WebFileSystem.cpp
new file mode 100644
index 000000000..b6d4cab64
--- /dev/null
+++ b/src/esengine/platform/web/WebFileSystem.cpp
@@ -0,0 +1,157 @@
+/**
+ * @file WebFileSystem.cpp
+ * @brief Web platform file system implementation (Emscripten)
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "../FileSystem.hpp"
+#include "../../core/Log.hpp"
+
+#ifdef ES_PLATFORM_WEB
+ #include <emscripten.h>
+#endif
+
+#include <fstream>
+#include <sstream>
+
+namespace esengine {
+
+// =============================================================================
+// Initialization
+// =============================================================================
+
+void FileSystem::init() {
+ ES_LOG_INFO("FileSystem initialized (Web platform - read-only preloaded assets)");
+}
+
+void FileSystem::shutdown() {
+ ES_LOG_INFO("FileSystem shutdown");
+}
+
+void FileSystem::update() {
+ // No file watching support on web
+}
+
+// =============================================================================
+// File Operations
+// =============================================================================
+
+bool FileSystem::fileExists(const std::string& path) {
+ std::ifstream file(path);
+ return file.good();
+}
+
+std::string FileSystem::readTextFile(const std::string& path) {
+ std::ifstream file(path, std::ios::in);
+ if (!file.is_open()) {
+ ES_LOG_ERROR("Failed to open file: {}", path);
+ return "";
+ }
+
+ std::stringstream buffer;
+ buffer << file.rdbuf();
+ file.close();
+
+ return buffer.str();
+}
+
+std::vector<u8> FileSystem::readBinaryFile(const std::string& path) {
+ std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
+ if (!file.is_open()) {
+ ES_LOG_ERROR("Failed to open file: {}", path);
+ return {};
+ }
+
+ std::streamsize size = file.tellg();
+ file.seekg(0, std::ios::beg);
+
+ std::vector<u8> buffer(static_cast<usize>(size));
+ if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
+ ES_LOG_ERROR("Failed to read file: {}", path);
+ return {};
+ }
+
+ file.close();
+ return buffer;
+}
+
+bool FileSystem::writeTextFile(const std::string& path, const std::string& content) {
+ (void)path;
+ (void)content;
+ ES_LOG_WARN("File writing not supported on web platform");
+ return false;
+}
+
+bool FileSystem::writeBinaryFile(const std::string& path, const std::vector<u8>& data) {
+ (void)path;
+ (void)data;
+ ES_LOG_WARN("File writing not supported on web platform");
+ return false;
+}
+
+usize FileSystem::getFileSize(const std::string& path) {
+ std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
+ if (!file.is_open()) {
+ return 0;
+ }
+
+ usize size = static_cast<usize>(file.tellg());
+ file.close();
+ return size;
+}
+
+u64 FileSystem::getFileModificationTime(const std::string& path) {
+ (void)path;
+ // No modification time support on web
+ return 0;
+}
+
+// =============================================================================
+// Directory Operations
+// =============================================================================
+
+bool FileSystem::directoryExists(const std::string& path) {
+ (void)path;
+ // No directory API on web
+ return false;
+}
+
+std::vector<std::string> FileSystem::listDirectory(const std::string& path, bool recursive) {
+ (void)path;
+ (void)recursive;
+ ES_LOG_WARN("Directory listing not supported on web platform");
+ return {};
+}
+
+bool FileSystem::createDirectory(const std::string& path) {
+ (void)path;
+ ES_LOG_WARN("Directory creation not supported on web platform");
+ return false;
+}
+
+// =============================================================================
+// File Monitoring (Hot Reload)
+// =============================================================================
+
+void FileSystem::watchFile(const std::string& path,
+ std::function<void(const std::string&)> callback) {
+ (void)path;
+ (void)callback;
+ ES_LOG_WARN("File watching not supported on web platform");
+}
+
+void FileSystem::unwatchFile(const std::string& path) {
+ (void)path;
+}
+
+bool FileSystem::isWatching(const std::string& path) {
+ (void)path;
+ return false;
+}
+
+} // namespace esengine
diff --git a/src/esengine/scripting/ScriptContext.cpp b/src/esengine/scripting/ScriptContext.cpp
index d627b52dc..1f510a3f7 100644
--- a/src/esengine/scripting/ScriptContext.cpp
+++ b/src/esengine/scripting/ScriptContext.cpp
@@ -11,9 +11,7 @@
#include "ScriptContext.hpp"
#include "../core/Log.hpp"
-
-#include <fstream>
-#include <sstream>
+#include "../platform/FileSystem.hpp"
namespace esengine {
@@ -125,21 +123,11 @@ bool ScriptContext::evalFile(const std::string& path) {
return false;
}
- // Read file contents
- std::ifstream file(path, std::ios::in | std::ios::binary);
- if (!file.is_open()) {
- lastError_ = "Failed to open script file: " + path;
- ES_LOG_ERROR("{}", lastError_);
- return false;
- }
-
- std::stringstream buffer;
- buffer << file.rdbuf();
- std::string code = buffer.str();
-
+ // Read file contents using FileSystem
+ std::string code = FileSystem::readTextFile(path);
if (code.empty()) {
- lastError_ = "Script file is empty: " + path;
- ES_LOG_WARN("{}", lastError_);
+ lastError_ = "Failed to read script file or file is empty: " + path;
+ ES_LOG_ERROR("{}", lastError_);
return false;
}