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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add AssetDatabase for centralized asset management
esengine6b483ff4fc7a
diff --git a/src/esengine/editor/AssetDatabase.cpp b/src/esengine/editor/AssetDatabase.cppnew file mode 100644index 000000000..eaa6c3780--- /dev/null+++ b/src/esengine/editor/AssetDatabase.cpp@@ -0,0 +1,351 @@+#include "AssetDatabase.hpp"+#include "../platform/FileSystem.hpp"+#include "../core/Log.hpp"++#include <algorithm>+#include <random>+#include <sstream>+#include <iomanip>++namespace esengine::editor {++namespace {+ std::string getFileName(const std::string& path) {+ usize pos = path.find_last_of("/\\");+ if (pos != std::string::npos) {+ return path.substr(pos + 1);+ }+ return path;+ }++ std::string getFileExtension(const std::string& path) {+ usize pos = path.find_last_of('.');+ if (pos != std::string::npos) {+ return path.substr(pos);+ }+ return "";+ }++ std::string escapeJsonString(const std::string& str) {+ std::string result;+ result.reserve(str.size() + 16);+ for (char c : str) {+ switch (c) {+ case '"': result += "\\\""; break;+ case '\\': result += "\\\\"; break;+ case '\n': result += "\\n"; break;+ case '\r': result += "\\r"; break;+ case '\t': result += "\\t"; break;+ default: result += c; break;+ }+ }+ return result;+ }++ std::string unescapeJsonString(const std::string& str) {+ std::string result;+ result.reserve(str.size());+ for (usize i = 0; i < str.size(); ++i) {+ if (str[i] == '\\' && i + 1 < str.size()) {+ switch (str[i + 1]) {+ case '"': result += '"'; ++i; break;+ case '\\': result += '\\'; ++i; break;+ case 'n': result += '\n'; ++i; break;+ case 'r': result += '\r'; ++i; break;+ case 't': result += '\t'; ++i; break;+ default: result += str[i]; break;+ }+ } else {+ result += str[i];+ }+ }+ return result;+ }+} // namespace++AssetDatabase::AssetDatabase() = default;+AssetDatabase::~AssetDatabase() = default;++void AssetDatabase::setProjectPath(const std::string& path) {+ if (projectPath_ != path) {+ projectPath_ = path;+ loadDatabase();+ }+}++void AssetDatabase::scan() {+ if (projectPath_.empty()) {+ ES_LOG_WARN("AssetDatabase: Project path not set");+ return;+ }++ if (!FileSystem::directoryExists(projectPath_)) {+ ES_LOG_WARN("AssetDatabase: Project path does not exist: {}", projectPath_);+ return;+ }++ ES_LOG_INFO("AssetDatabase: Scanning {}", projectPath_);+ scanDirectory(projectPath_, true);+ saveDatabase();+ ES_LOG_INFO("AssetDatabase: Found {} assets", assetsByGUID_.size());+}++void AssetDatabase::refresh() {+ scan();+}++void AssetDatabase::scanDirectory(const std::string& directory, bool recursive) {+#ifdef ES_PLATFORM_WEB+ return;+#endif++ std::vector<std::string> entries = FileSystem::listDirectory(directory, false);++ for (const auto& entryPath : entries) {+ bool isDir = FileSystem::directoryExists(entryPath);++ auto existingIt = pathToGUID_.find(entryPath);+ if (existingIt != pathToGUID_.end()) {+ auto& existing = assetsByGUID_[existingIt->second];+ u64 currentModTime = FileSystem::getFileModificationTime(entryPath);+ if (existing.lastModified != currentModTime) {+ existing.lastModified = currentModTime;+ existing.fileSize = isDir ? 0 : FileSystem::getFileSize(entryPath);+ if (onAssetModified_) {+ onAssetModified_(existing);+ }+ }+ } else {+ AssetMetadata metadata;+ metadata.guid = generateGUID();+ metadata.path = entryPath;+ metadata.name = getFileName(entryPath);+ metadata.extension = getFileExtension(entryPath);+ metadata.isDirectory = isDir;+ metadata.type = isDir ? AssetType::Folder : detectAssetType(entryPath);+ metadata.fileSize = isDir ? 0 : FileSystem::getFileSize(entryPath);+ metadata.lastModified = FileSystem::getFileModificationTime(entryPath);++ assetsByGUID_[metadata.guid] = metadata;+ pathToGUID_[metadata.path] = metadata.guid;++ if (onAssetAdded_) {+ onAssetAdded_(metadata);+ }+ }++ if (isDir && recursive) {+ scanDirectory(entryPath, true);+ }+ }+}++const AssetMetadata* AssetDatabase::findByPath(const std::string& path) const {+ auto it = pathToGUID_.find(path);+ if (it != pathToGUID_.end()) {+ auto assetIt = assetsByGUID_.find(it->second);+ if (assetIt != assetsByGUID_.end()) {+ return &assetIt->second;+ }+ }+ return nullptr;+}++const AssetMetadata* AssetDatabase::findByGUID(const std::string& guid) const {+ auto it = assetsByGUID_.find(guid);+ if (it != assetsByGUID_.end()) {+ return &it->second;+ }+ return nullptr;+}++std::vector<const AssetMetadata*> AssetDatabase::getAssetsInDirectory(const std::string& directory) const {+ std::vector<const AssetMetadata*> result;++ std::string dirPath = directory;+ if (!dirPath.empty() && dirPath.back() != '/' && dirPath.back() != '\\') {+ dirPath += '/';+ }++ for (const auto& [guid, metadata] : assetsByGUID_) {+ if (metadata.path.size() > dirPath.size() &&+ metadata.path.compare(0, dirPath.size(), dirPath) == 0) {+ usize remaining = metadata.path.size() - dirPath.size();+ usize slashPos = metadata.path.find_first_of("/\\", dirPath.size());+ if (slashPos == std::string::npos || slashPos == metadata.path.size() - 1) {+ result.push_back(&metadata);+ }+ }+ }++ std::sort(result.begin(), result.end(),+ [](const AssetMetadata* a, const AssetMetadata* b) {+ if (a->isDirectory != b->isDirectory) {+ return a->isDirectory;+ }+ return a->name < b->name;+ });++ return result;+}++std::vector<std::string> AssetDatabase::getSubdirectories(const std::string& directory) const {+ std::vector<std::string> result;++ auto assets = getAssetsInDirectory(directory);+ for (const auto* asset : assets) {+ if (asset->isDirectory) {+ result.push_back(asset->path);+ }+ }++ return result;+}++std::string AssetDatabase::generateGUID() const {+ static std::random_device rd;+ static std::mt19937_64 gen(rd());+ static std::uniform_int_distribution<u64> dis;++ std::stringstream ss;+ ss << std::hex << std::setfill('0');++ u64 part1 = dis(gen);+ u64 part2 = dis(gen);++ ss << std::setw(8) << ((part1 >> 32) & 0xFFFFFFFF) << "-";+ ss << std::setw(4) << ((part1 >> 16) & 0xFFFF) << "-";+ ss << std::setw(4) << (part1 & 0xFFFF) << "-";+ ss << std::setw(4) << ((part2 >> 48) & 0xFFFF) << "-";+ ss << std::setw(12) << (part2 & 0xFFFFFFFFFFFF);++ return ss.str();+}++std::string AssetDatabase::getDatabasePath() const {+ return projectPath_ + "/.esengine/assets.db";+}++AssetType AssetDatabase::detectAssetType(const std::string& path) const {+ std::string ext = getFileExtension(path);+ std::transform(ext.begin(), ext.end(), ext.begin(),+ [](unsigned char c) { return static_cast<char>(std::tolower(c)); });+ return getAssetTypeFromExtension(ext);+}++void AssetDatabase::saveDatabase() {+#ifdef ES_PLATFORM_WEB+ return;+#endif++ std::string dbDir = projectPath_ + "/.esengine";+ if (!FileSystem::directoryExists(dbDir)) {+ FileSystem::createDirectory(dbDir);+ }++ std::stringstream ss;+ ss << "{\n \"version\": \"1.0\",\n \"assets\": [\n";++ bool first = true;+ for (const auto& [guid, metadata] : assetsByGUID_) {+ if (!first) ss << ",\n";+ first = false;++ ss << " {\n";+ ss << " \"guid\": \"" << escapeJsonString(metadata.guid) << "\",\n";+ ss << " \"path\": \"" << escapeJsonString(metadata.path) << "\",\n";+ ss << " \"name\": \"" << escapeJsonString(metadata.name) << "\",\n";+ ss << " \"type\": " << static_cast<int>(metadata.type) << ",\n";+ ss << " \"fileSize\": " << metadata.fileSize << ",\n";+ ss << " \"lastModified\": " << metadata.lastModified << ",\n";+ ss << " \"isDirectory\": " << (metadata.isDirectory ? "true" : "false") << "\n";+ ss << " }";+ }++ ss << "\n ]\n}\n";++ FileSystem::writeTextFile(getDatabasePath(), ss.str());+ ES_LOG_DEBUG("AssetDatabase: Saved to {}", getDatabasePath());+}++void AssetDatabase::loadDatabase() {+#ifdef ES_PLATFORM_WEB+ return;+#endif++ std::string dbPath = getDatabasePath();+ if (!FileSystem::fileExists(dbPath)) {+ return;+ }++ std::string content = FileSystem::readTextFile(dbPath);+ if (content.empty()) {+ return;+ }++ assetsByGUID_.clear();+ pathToGUID_.clear();++ usize pos = 0;+ while ((pos = content.find("\"guid\":", pos)) != std::string::npos) {+ AssetMetadata metadata;++ auto extractString = [&](const std::string& key) -> std::string {+ usize keyPos = content.find("\"" + key + "\":", pos);+ if (keyPos == std::string::npos) return "";+ usize valueStart = content.find("\"", keyPos + key.size() + 3);+ if (valueStart == std::string::npos) return "";+ usize valueEnd = content.find("\"", valueStart + 1);+ if (valueEnd == std::string::npos) return "";+ return unescapeJsonString(content.substr(valueStart + 1, valueEnd - valueStart - 1));+ };++ auto extractInt = [&](const std::string& key) -> u64 {+ usize keyPos = content.find("\"" + key + "\":", pos);+ if (keyPos == std::string::npos) return 0;+ usize valueStart = keyPos + key.size() + 3;+ while (valueStart < content.size() && !std::isdigit(content[valueStart])) {+ ++valueStart;+ }+ return std::stoull(content.substr(valueStart));+ };++ auto extractBool = [&](const std::string& key) -> bool {+ usize keyPos = content.find("\"" + key + "\":", pos);+ if (keyPos == std::string::npos) return false;+ return content.find("true", keyPos) < content.find("false", keyPos);+ };++ metadata.guid = extractString("guid");+ metadata.path = extractString("path");+ metadata.name = extractString("name");+ metadata.type = static_cast<AssetType>(extractInt("type"));+ metadata.fileSize = extractInt("fileSize");+ metadata.lastModified = extractInt("lastModified");+ metadata.isDirectory = extractBool("isDirectory");+ metadata.extension = getFileExtension(metadata.path);++ if (!metadata.guid.empty() && !metadata.path.empty()) {+ assetsByGUID_[metadata.guid] = metadata;+ pathToGUID_[metadata.path] = metadata.guid;+ }++ pos = content.find("}", pos) + 1;+ }++ ES_LOG_DEBUG("AssetDatabase: Loaded {} assets from {}", assetsByGUID_.size(), dbPath);+}++void AssetDatabase::setOnAssetAdded(std::function<void(const AssetMetadata&)> callback) {+ onAssetAdded_ = std::move(callback);+}++void AssetDatabase::setOnAssetRemoved(std::function<void(const std::string&)> callback) {+ onAssetRemoved_ = std::move(callback);+}++void AssetDatabase::setOnAssetModified(std::function<void(const AssetMetadata&)> callback) {+ onAssetModified_ = std::move(callback);+}++} // namespace esengine::editordiff --git a/src/esengine/editor/AssetDatabase.hpp b/src/esengine/editor/AssetDatabase.hppnew file mode 100644index 000000000..615327edc--- /dev/null+++ b/src/esengine/editor/AssetDatabase.hpp@@ -0,0 +1,65 @@+#pragma once++#include "../core/Types.hpp"+#include "panels/AssetBrowserTypes.hpp"++#include <string>+#include <vector>+#include <unordered_map>+#include <functional>++namespace esengine::editor {++struct AssetMetadata {+ std::string guid;+ std::string path;+ std::string name;+ std::string extension;+ AssetType type = AssetType::Unknown;+ usize fileSize = 0;+ u64 lastModified = 0;+ bool isDirectory = false;+};++class AssetDatabase {+public:+ AssetDatabase();+ ~AssetDatabase();++ void setProjectPath(const std::string& path);+ const std::string& getProjectPath() const { return projectPath_; }++ void scan();+ void refresh();++ const AssetMetadata* findByPath(const std::string& path) const;+ const AssetMetadata* findByGUID(const std::string& guid) const;++ std::vector<const AssetMetadata*> getAssetsInDirectory(const std::string& directory) const;+ std::vector<std::string> getSubdirectories(const std::string& directory) const;++ const std::unordered_map<std::string, AssetMetadata>& getAllAssets() const { return assetsByGUID_; }++ void saveDatabase();+ void loadDatabase();++ void setOnAssetAdded(std::function<void(const AssetMetadata&)> callback);+ void setOnAssetRemoved(std::function<void(const std::string&)> callback);+ void setOnAssetModified(std::function<void(const AssetMetadata&)> callback);++private:+ void scanDirectory(const std::string& directory, bool recursive);+ std::string generateGUID() const;+ std::string getDatabasePath() const;+ AssetType detectAssetType(const std::string& path) const;++ std::string projectPath_;+ std::unordered_map<std::string, AssetMetadata> assetsByGUID_;+ std::unordered_map<std::string, std::string> pathToGUID_;++ std::function<void(const AssetMetadata&)> onAssetAdded_;+ std::function<void(const std::string&)> onAssetRemoved_;+ std::function<void(const AssetMetadata&)> onAssetModified_;+};++} // namespace esengine::editordiff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txtindex 3065f5728..77806ad19 100644--- a/src/esengine/editor/CMakeLists.txt+++ b/src/esengine/editor/CMakeLists.txt@@ -3,6 +3,7 @@set(EDITOR_SOURCESmain.cppEditorApplication.cpp+ AssetDatabase.cpp# Property editorsproperty/PropertyEditor.cpp@@ -25,6 +26,7 @@ set(EDITOR_SOURCESset(EDITOR_HEADERSEditorApplication.hpp+ AssetDatabase.hpp# Core subsystemcore/EditorEvents.hppdiff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cppindex 9eb97210b..3e971be63 100644--- a/src/esengine/editor/EditorApplication.cpp+++ b/src/esengine/editor/EditorApplication.cpp@@ -97,6 +97,10 @@ void EditorApplication::onInit() {}});+ // Initialize asset database+ assetDatabase_.setProjectPath("assets");+ assetDatabase_.scan();+// Create demo scene for testingcreateDemoScene();@@ -312,7 +316,7 @@ void EditorApplication::setupEditorLayout() {ES_LOG_INFO("setupEditorLayout: Creating AssetBrowserPanel...");// Create Asset Browser panel (bottom)- auto assetBrowserPanel = makeUnique<AssetBrowserPanel>();+ auto assetBrowserPanel = makeUnique<AssetBrowserPanel>(assetDatabase_);assetBrowserPanel->setMinSize(glm::vec2(300.0f, 200.0f));ES_LOG_INFO("setupEditorLayout: Adding AssetBrowserPanel to dock...");dockArea_->addPanel(std::move(assetBrowserPanel), ui::DockDropZone::Bottom, nullptr, 0.25f);diff --git a/src/esengine/editor/EditorApplication.hpp b/src/esengine/editor/EditorApplication.hppindex 464797934..50df2a48c 100644--- a/src/esengine/editor/EditorApplication.hpp+++ b/src/esengine/editor/EditorApplication.hpp@@ -24,6 +24,7 @@#include "../ui/docking/DockArea.hpp"#include "command/CommandHistory.hpp"#include "core/Selection.hpp"+#include "AssetDatabase.hpp"namespace esengine {namespace editor {@@ -98,6 +99,12 @@ public:*/ecs::Registry& getEditorRegistry() { return Application::getRegistry(); }+ /**+ * @brief Get the asset database+ * @return Reference to the asset database+ */+ AssetDatabase& getAssetDatabase() { return assetDatabase_; }+protected:// =========================================================================// Application Lifecycle Overrides@@ -191,6 +198,7 @@ private:Dispatcher dispatcher_; ///< Central event busCommandHistory commandHistory_; ///< Undo/redo historyEntitySelection selection_; ///< Entity selection manager+ AssetDatabase assetDatabase_; ///< Asset databaseUnique<ui::UIContext> uiContext_; ///< UI system contextui::DockArea* dockArea_ = nullptr; ///< Main docking area (owned by UIContext)diff --git a/src/esengine/editor/panels/AssetBrowserPanel.cpp b/src/esengine/editor/panels/AssetBrowserPanel.cppindex 326a62f2d..2926470ba 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.cpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.cpp@@ -1,5 +1,6 @@#include "AssetBrowserPanel.hpp"#include "AssetGridItem.hpp"+#include "../AssetDatabase.hpp"#include "../../ui/UIContext.hpp"#include "../../ui/layout/StackLayout.hpp"#include "../../ui/widgets/Label.hpp"@@ -12,14 +13,18 @@namespace esengine::editor {-AssetBrowserPanel::AssetBrowserPanel()- : DockPanel(ui::WidgetId("asset_browser_panel"), "Assets") {+AssetBrowserPanel::AssetBrowserPanel(AssetDatabase& assetDB)+ : DockPanel(ui::WidgetId("asset_browser_panel"), "Assets"),+ assetDB_(assetDB) {setPanelType("AssetBrowser");setClosable(true);setMinSize(glm::vec2(400.0f, 250.0f));- rootPath_ = "assets";+ rootPath_ = assetDB_.getProjectPath();+ if (rootPath_.empty()) {+ rootPath_ = "assets";+ }currentPath_ = rootPath_;buildUI();diff --git a/src/esengine/editor/panels/AssetBrowserPanel.hpp b/src/esengine/editor/panels/AssetBrowserPanel.hppindex 9f8b02581..221d35d89 100644--- a/src/esengine/editor/panels/AssetBrowserPanel.hpp+++ b/src/esengine/editor/panels/AssetBrowserPanel.hpp@@ -13,9 +13,11 @@namespace esengine::editor {+class AssetDatabase;+class AssetBrowserPanel : public ui::DockPanel {public:- AssetBrowserPanel();+ explicit AssetBrowserPanel(AssetDatabase& assetDB);~AssetBrowserPanel() override;void refresh();@@ -65,6 +67,8 @@ private:std::string selectedAssetPath_;bool needsRebuild_ = false;+ AssetDatabase& assetDB_;+Connection folderSelectedConnection_;Connection searchChangedConnection_;};diff --git a/src/esengine/editor/panels/AssetBrowserTypes.hpp b/src/esengine/editor/panels/AssetBrowserTypes.hppindex 7c2034d56..49bcdca72 100644--- a/src/esengine/editor/panels/AssetBrowserTypes.hpp+++ b/src/esengine/editor/panels/AssetBrowserTypes.hpp@@ -2,6 +2,8 @@#include "../../core/Types.hpp"+#include <glm/glm.hpp>+#include <string>namespace esengine::editor {