From d1305bee6bbe542b9bdc16124d4ca623cc13ee44 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Tue, 12 Dec 2017 15:21:17 +0100 Subject: [PATCH] Cleanup --- include/openspace/scene/assetloader.h | 48 ++++++++++++++++++-------- include/openspace/scene/assetmanager.h | 2 +- src/scene/asset.cpp | 2 +- src/scene/assetloader.cpp | 16 ++++----- src/scene/assetmanager.cpp | 4 +-- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/include/openspace/scene/assetloader.h b/include/openspace/scene/assetloader.h index a494a82aa2..1f38d2579e 100644 --- a/include/openspace/scene/assetloader.h +++ b/include/openspace/scene/assetloader.h @@ -80,23 +80,28 @@ public: ~AssetLoader(); /** - * Add the asset as a request for the root asset - * The asset is loaded synchronously + * Add the asset as a request of the root asset */ std::shared_ptr add(const std::string& identifier); /** - * Remove the asset as a dependency on the root asset - * The asset is unloaded synchronously + * Remove the asset as a request of the root asset */ void remove(const std::string& identifier); + /** + * Enable the asset to be reused when the same path is required/requested again + */ void trackAsset(std::shared_ptr asset); + + /** + * Disable the asset from being reused when the same path is required/requested again + */ void untrackAsset(Asset* asset); /** - * Returns the asset identified by the identifier, - * if the asset is loaded. Otherwise return nullptr. + * Return the asset identified by the identifier, + * if the asset is tracked. Otherwise return nullptr. */ std::shared_ptr has(const std::string& identifier) const; @@ -115,20 +120,37 @@ public: */ const std::string& assetRootDirectory() const; + /** + * Load an asset + */ bool loadAsset(std::shared_ptr asset); + /** + * Call the onInitialize function specified in the asset file + */ void callOnInitialize(Asset* asset); + /** + * Call the onDeinitialize function specified in the asset file + */ void callOnDeinitialize(Asset* asset); - void callOnDependencyInitialize(Asset* asset, Asset* dependant); + /** + * Call the dependency.onInitialize function specified in the asset file + */ + void callOnDependencyInitialize(Asset* dependency, Asset* asset); - void callOnDependencyDeinitialize(Asset* asset, Asset* dependant); + /** + * Call the dependency.onDeinitialize function specified in the asset file + */ + void callOnDependencyDeinitialize(Asset* dependency, Asset* asset); + /** + * Generate the absolute path for an asset specified as `path` relative to `baseDirectory` + */ std::string generateAssetPath(const std::string& baseDirectory, const std::string& path) const; - /** * Add listener to asset state changes */ @@ -159,9 +181,6 @@ private: std::shared_ptr request(const std::string& path); void unrequest(const std::string& path); - /** - * Add the global assets table to the lua stack. - */ void setUpAssetLuaTable(Asset* asset); void tearDownAssetLuaTable(Asset* asset); @@ -193,12 +212,11 @@ private: friend int assetloader::syncedResource(lua_State* state); friend int assetloader::exportAsset(lua_State* state); + // Member variables std::shared_ptr _rootAsset; std::shared_ptr _currentAsset; - std::unordered_map> _loadedAssets; - + std::unordered_map> _trackedAssets; SynchronizationWatcher* _synchronizationWatcher; - std::string _assetRootDirectory; ghoul::lua::LuaState* _luaState; diff --git a/include/openspace/scene/assetmanager.h b/include/openspace/scene/assetmanager.h index fb546d6415..1497a32640 100644 --- a/include/openspace/scene/assetmanager.h +++ b/include/openspace/scene/assetmanager.h @@ -79,8 +79,8 @@ private: std::mutex _pendingInitializationsMutex; std::vector> _pendingInitializations; - std::unique_ptr _assetLoader; std::unique_ptr _synchronizationWatcher; + std::unique_ptr _assetLoader; }; } // namespace openspace diff --git a/src/scene/asset.cpp b/src/scene/asset.cpp index 07a319e5d9..d5e5559942 100644 --- a/src/scene/asset.cpp +++ b/src/scene/asset.cpp @@ -407,7 +407,7 @@ void Asset::initialize() { // 5. Call dependency initialization function of the child and this // if the requested child was initialized before this. for (auto& child : _requestedAssets) { - if (child->state() == State::Initialized) { + if (child->isInitialized()) { try { loader()->callOnDependencyInitialize(child.get(), this); } catch (const ghoul::lua::LuaRuntimeException& e) { diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 0fb55ba391..adf3eab90e 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -93,13 +93,13 @@ AssetLoader::~AssetLoader() { } void AssetLoader::trackAsset(std::shared_ptr asset) { - _loadedAssets.emplace(asset->id(), asset); + _trackedAssets.emplace(asset->id(), asset); } void AssetLoader::untrackAsset(Asset* asset) { - auto it = _loadedAssets.find(asset->id()); - if (it != _loadedAssets.end()) { - _loadedAssets.erase(it); + auto it = _trackedAssets.find(asset->id()); + if (it != _trackedAssets.end()) { + _trackedAssets.erase(it); } } @@ -252,9 +252,9 @@ std::shared_ptr AssetLoader::getAsset(std::string name) { std::string path = generateAssetPath(directory, name); // Check if asset is already loaded. - const auto it = _loadedAssets.find(path); + const auto it = _trackedAssets.find(path); - if (it != _loadedAssets.end()) { + if (it != _trackedAssets.end()) { std::shared_ptr a = it->second.lock(); if (a != nullptr) { return a; @@ -343,8 +343,8 @@ 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()) { + const auto it = _trackedAssets.find(path); + if (it == _trackedAssets.end()) { return nullptr; } return it->second.lock(); diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index 07a5774cab..341a83ba8e 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -41,8 +41,8 @@ AssetManager::AssetManager( std::unique_ptr loader, std::unique_ptr syncWatcher ) - : _assetLoader(std::move(loader)) - , _synchronizationWatcher(std::move(syncWatcher)) + : _synchronizationWatcher(std::move(syncWatcher)) + , _assetLoader(std::move(loader)) {} void AssetManager::initialize() {