From 375af6829c2483e837870ca33b3ce3e8981005b4 Mon Sep 17 00:00:00 2001
From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com>
Date: Wed, 21 May 2025 21:31:47 -0500
Subject: [PATCH] Fixed Crash By Adding Lock

---
 src/core/systems/AssetManager.cpp  |  5 +++--
 src/core/systems/AssetManager.h    |  5 ++++-
 src/editor/Windows/FileBrowser.cpp | 12 ++++++++++--
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/core/systems/AssetManager.cpp b/src/core/systems/AssetManager.cpp
index 1fa0387..ccbcbd7 100644
--- a/src/core/systems/AssetManager.cpp
+++ b/src/core/systems/AssetManager.cpp
@@ -18,6 +18,7 @@ namespace OX
     // Map from actual file path to virtual ID
     std::unordered_map<std::string, std::string> AssetManager::s_PathToID;
     std::mutex AssetManager::s_AssetMutex;
+    std::mutex AssetManager::s_TreeMutex;
 
     std::shared_ptr<ResourceTreeNode> AssetManager::s_FileTree = std::make_shared<ResourceTreeNode>();
 
@@ -96,7 +97,7 @@ namespace OX
                             {
                                 std::lock_guard<std::mutex> lock(s_AssetMutex);
                                 // now fill in the real metadata
-                                s_MetadataMap[resPath] = { type, path.string() };
+                                s_MetadataMap[resPath] = {type, path.string()};
                             }
                             std::lock_guard<std::mutex> qlock(s_QueueMutex);
                             s_TextureQueue.push({resPath, w, h, ch, data});
@@ -138,7 +139,6 @@ namespace OX
             // Also map original file path to this ID
             auto meta = s_MetadataMap[pending.id];
             s_PathToID[meta.absolutePath] = pending.id;
-
         }
     }
 
@@ -224,6 +224,7 @@ namespace OX
             pos = next + 1;
         }
         if (pos < resPath.size()) parts.push_back(resPath.substr(pos));
+        std::lock_guard<std::mutex> lock(AssetManager::s_TreeMutex);
 
         auto current = s_FileTree;
         for (size_t i = 1; i < parts.size(); ++i) {
diff --git a/src/core/systems/AssetManager.h b/src/core/systems/AssetManager.h
index c9daae5..d0759b0 100644
--- a/src/core/systems/AssetManager.h
+++ b/src/core/systems/AssetManager.h
@@ -37,6 +37,9 @@ namespace OX {
         static std::shared_ptr<ResourceTreeNode> GetFileTree();
         static void SaveAssetPack(const std::string& outputPath);
 
+        static std::mutex                 s_TreeMutex;
+
+
     private:
         struct AssetMetadata {
             std::string type;
@@ -69,6 +72,6 @@ namespace OX {
 
         // texture upload queue
         static std::queue<PendingTexture> s_TextureQueue;
-        static std::mutex                s_QueueMutex;
+        static std::mutex                 s_QueueMutex;
     };
 }
diff --git a/src/editor/Windows/FileBrowser.cpp b/src/editor/Windows/FileBrowser.cpp
index 6218871..38540a6 100644
--- a/src/editor/Windows/FileBrowser.cpp
+++ b/src/editor/Windows/FileBrowser.cpp
@@ -93,9 +93,17 @@ namespace OX
         ImGui::BeginChild("GridRegion", ImVec2(0, 0), false, ImGuiWindowFlags_AlwaysUseWindowPadding);
         ImGui::Columns(cols, nullptr, false);
 
-        for (auto &c: node->children) {
-            if (!_filter.empty() && !PassesFilter(c->name)) continue;
+        std::vector<std::shared_ptr<ResourceTreeNode> > children;
+
+        {
+            std::lock_guard<std::mutex> lock(AssetManager::s_TreeMutex); // assume you add this mutex
+            children = node->children;
+        }
+        for (auto& c : children) {
             if (!c) return;
+
+            if (!_filter.empty() && !PassesFilter(c->name)) continue;
+
             ImGui::PushID(c->path.c_str());
             ImGui::BeginGroup();