diff --git a/include/openspace/scene/asset.h b/include/openspace/scene/asset.h index 777e8ce0b5..65d0262d82 100644 --- a/include/openspace/scene/asset.h +++ b/include/openspace/scene/asset.h @@ -120,8 +120,6 @@ public: std::string resolveLocalResource(std::string resourceName); private: - void handleRequests(); - std::atomic _state; AssetLoader* _loader; std::vector> _synchronizations; diff --git a/include/openspace/scene/assetloader.h b/include/openspace/scene/assetloader.h index a4b39c0155..52d1dea331 100644 --- a/include/openspace/scene/assetloader.h +++ b/include/openspace/scene/assetloader.h @@ -141,7 +141,7 @@ private: std::shared_ptr loadAsset(std::string path); std::shared_ptr getAsset(std::string path); - ghoul::filesystem::Directory currentDirectory(); + ghoul::filesystem::Directory currentDirectory() const; void pushAsset(std::shared_ptr asset); void popAsset(); diff --git a/src/scene/asset.cpp b/src/scene/asset.cpp index 9072ef4e79..bb55cdf8da 100644 --- a/src/scene/asset.cpp +++ b/src/scene/asset.cpp @@ -65,14 +65,6 @@ std::string Asset::resolveLocalResource(std::string resourceName) { resourceName; } -void Asset::handleRequests() { - State currentState = state(); - - if (currentState == State::Initialized) { - // ... - } -} - Asset::State Asset::state() const { return _state; } @@ -361,7 +353,7 @@ void Asset::initialize() { // 6. Ask requested children to initialize if they are not already initialized. // Initialization may not happen immediately. for (auto& child : _requestedAssets) { - child->handleRequests(); + //child->handleRequests(); } // 7. Call dependency initialization function of this and the parent @@ -474,22 +466,34 @@ void Asset::request(std::shared_ptr child) { _requestedAssets.push_back(child); child->_requestingAssets.push_back(shared_from_this()); - child->handleRequests(); + // TODO: update real state! } void Asset::unrequest(std::shared_ptr child) { - auto it = std::find(_requestedAssets.begin(), + auto childIt = std::find( + _requestedAssets.begin(), _requestedAssets.end(), child); - if (it != _requestedAssets.end()) { - // Do nothing if the request already exists. + auto parentIt = std::find_if( + child->_requestingAssets.begin(), + child->_requestingAssets.end(), + [this](std::weak_ptr a) { + return a.lock().get() == this; + } + ); + + if (childIt == _requestedAssets.end() || + parentIt == child->_requestingAssets.end()) + { + // Do nothing if the request node not exist. return; } - _requestedAssets.push_back(child); - child->_requestingAssets.push_back(shared_from_this()); - child->handleRequests(); + _requestedAssets.erase(childIt); + child->_requestingAssets.erase(parentIt); + + // TODO: update real state! } bool Asset::requests(const Asset* asset) const { diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index a615c10a20..0c0acafa2f 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -202,7 +202,22 @@ std::shared_ptr AssetLoader::request(const std::string& name) { return nullptr; } -ghoul::filesystem::Directory AssetLoader::currentDirectory() { +void AssetLoader::unrequest(const std::string& name) { + std::shared_ptr asset = has(name); + if (asset) { + try { + std::shared_ptr parent = _assetStack.back(); + parent->unrequest(asset); + } + catch (ghoul::RuntimeError& e) { + LERROR("Failed to unload " << name << ". " << e.component << " :" << e.message); + } + } + +} + + +ghoul::filesystem::Directory AssetLoader::currentDirectory() const { if (_assetStack.back()->hasAssetFile()) { return _assetStack.back()->assetDirectory(); } else { @@ -216,8 +231,9 @@ std::shared_ptr AssetLoader::add(const std::string& identifier) { } -void AssetLoader::remove(const std::string & identifier) { +void AssetLoader::remove(const std::string& identifier) { ghoul_assert(_assetStack.size() == 1, "Can only unload an asset from the root asset"); + unrequest(identifier); // TODO: Implement this //_rootAsset->removeDependency(id); } @@ -228,8 +244,11 @@ void AssetLoader::remove(const Asset* asset) { //_rootAsset->removeDependency(id); } -std::shared_ptr AssetLoader::has(const std::string& identifier) const { - const auto it = _loadedAssets.find(identifier); +std::shared_ptr AssetLoader::has(const std::string& name) const { + ghoul::filesystem::Directory directory = currentDirectory(); + std::string path = generateAssetPath(directory, name); + + const auto it = _loadedAssets.find(path); if (it == _loadedAssets.end()) { return nullptr; } diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index b954ca7178..d5b138d228 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -75,7 +75,7 @@ bool AssetManager::update() { for (const auto& c : _pendingStateChangeCommands) { const std::string& path = c.first; const bool add = c.second; - if (add && !_assetLoader->has(path)) { + if (add) { std::shared_ptr asset = tryAddAsset(path); } } @@ -83,7 +83,7 @@ bool AssetManager::update() { // Remove assets for (const auto& c : _pendingStateChangeCommands) { const std::string& path = c.first; - const bool remove = c.second; + const bool remove = !c.second; if (remove && _assetLoader->has(path)) { tryRemoveAsset(path); }