diff --git a/ext/ghoul b/ext/ghoul index df1522a1fe..767fa3e42e 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit df1522a1fe6f02b4d03cd36478d9d36f4384e93e +Subproject commit 767fa3e42e1831ade7deb5b4b099c2c5a823c9f9 diff --git a/include/openspace/scene/assetloader.h b/include/openspace/scene/assetloader.h index e2da7ac1bc..29b114076c 100644 --- a/include/openspace/scene/assetloader.h +++ b/include/openspace/scene/assetloader.h @@ -55,9 +55,9 @@ int exportAsset(lua_State* state); } // namespace assetloader class Asset; +class AssetListener; class ResourceSynchronization; class SynchronizationWatcher; -class AssetListener; class AssetLoader { public: @@ -92,10 +92,11 @@ public: */ std::shared_ptr has(const std::string& identifier) const; - /** - * Return the root asset - */ - std::shared_ptr rootAsset() const; + /// Return the root asset + const Asset& rootAsset() const; + + /// Return the root asset + Asset& rootAsset(); /** * Return the asset root directory @@ -190,7 +191,7 @@ private: int syncedResourceLua(Asset* asset); int exportAssetLua(Asset* asset); - // Friend c closures (callable from lua, and maps to lua functions above) + // Friend C closures (callable from Lua, and maps to Lua functions above) friend int assetloader::onInitialize(lua_State* state); friend int assetloader::onDeinitialize(lua_State* state); friend int assetloader::onInitializeDependency(lua_State* state); diff --git a/include/openspace/scene/assetmanager.h b/include/openspace/scene/assetmanager.h index 91addbc4ef..8ed157d56d 100644 --- a/include/openspace/scene/assetmanager.h +++ b/include/openspace/scene/assetmanager.h @@ -27,9 +27,12 @@ #include +#include +#include +#include #include -#include #include +#include namespace openspace { @@ -46,11 +49,9 @@ class SynchronizationWatcher; * synchronized. Also, setting a target state of an asset to Unloaded will only unload an * asset from the system if it is not a dependency of a loaded asset. */ - class AssetManager : AssetListener { public: - AssetManager(std::unique_ptr loader, - std::unique_ptr syncWatcher); + AssetManager(ghoul::lua::LuaState* state, std::string assetRootDirectory); virtual ~AssetManager() = default; @@ -59,11 +60,12 @@ public: void add(const std::string& path); void remove(const std::string& path); void removeAll(); - std::shared_ptr rootAsset(); + const Asset& rootAsset() const; + Asset& rootAsset(); - void assetStateChanged(Asset* asset, Asset::State state); - void assetRequested(Asset* parent, std::shared_ptr child); - void assetUnrequested(Asset* parent, std::shared_ptr child); + void assetStateChanged(Asset* asset, Asset::State state) override; + void assetRequested(Asset* parent, std::shared_ptr child) override; + void assetUnrequested(Asset* parent, std::shared_ptr child) override; bool update(); scripting::LuaLibrary luaLibrary(); @@ -73,8 +75,8 @@ private: std::mutex _pendingInitializationsMutex; std::vector> _pendingInitializations; - std::unique_ptr _synchronizationWatcher; - std::unique_ptr _assetLoader; + SynchronizationWatcher _synchronizationWatcher; + AssetLoader _assetLoader; }; } // namespace openspace diff --git a/modules/imgui/src/guiassetcomponent.cpp b/modules/imgui/src/guiassetcomponent.cpp index 35d8a60317..9e9be668e7 100644 --- a/modules/imgui/src/guiassetcomponent.cpp +++ b/modules/imgui/src/guiassetcomponent.cpp @@ -79,7 +79,7 @@ void GuiAssetComponent::render() { std::string rootPath; - for (const std::shared_ptr& a : assetManager.rootAsset()->childAssets()) { + for (const std::shared_ptr& a : assetManager.rootAsset().childAssets()) { renderTree(*a, rootPath); } diff --git a/modules/sync/tasks/syncassettask.cpp b/modules/sync/tasks/syncassettask.cpp index 96ef7bc3a1..2822b09651 100644 --- a/modules/sync/tasks/syncassettask.cpp +++ b/modules/sync/tasks/syncassettask.cpp @@ -110,10 +110,10 @@ void SyncAssetTask::perform(const Task::ProgressCallback& progressCallback) { AssetLoader loader(&luaState, &watcher, "${ASSETS}"); loader.add(_asset); - loader.rootAsset()->startSynchronizations(); + loader.rootAsset().startSynchronizations(); std::vector> allAssets = - loader.rootAsset()->subTreeAssets(); + loader.rootAsset().subTreeAssets(); while (true) { bool inProgress = false; @@ -126,7 +126,7 @@ void SyncAssetTask::perform(const Task::ProgressCallback& progressCallback) { inProgress = true; } } - progressCallback(loader.rootAsset()->requestedSynchronizationProgress()); + progressCallback(loader.rootAsset().requestedSynchronizationProgress()); std::this_thread::sleep_for(ProgressPollInterval); watcher.notify(); if (!inProgress) { diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 66c45fec15..42c7a7e5e8 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -341,17 +341,9 @@ void OpenSpaceEngine::initialize() { } // Set up asset loader - std::unique_ptr w = - std::make_unique(); - SynchronizationWatcher* rawWatcher = w.get(); - global::openSpaceEngine._assetManager = std::make_unique( - std::make_unique( - global::scriptEngine.luaState(), - rawWatcher, - FileSys.absPath("${ASSETS}") - ), - std::move(w) + global::scriptEngine.luaState(), + FileSys.absPath("${ASSETS}") ); global::scriptEngine.addLibrary(global::openSpaceEngine._assetManager->luaLibrary()); @@ -760,7 +752,7 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { _loadingScreen->postMessage("Synchronizing assets"); std::vector> allAssets = - _assetManager->rootAsset()->subTreeAssets(); + _assetManager->rootAsset().subTreeAssets(); std::unordered_set resourceSyncs; for (const std::shared_ptr& a : allAssets) { diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 66c2d6fde1..b35aacad8b 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -107,7 +107,7 @@ AssetLoader::AssetLoader(ghoul::lua::LuaState* luaState, { setCurrentAsset(_rootAsset.get()); - // Create _assets table. + // Create _assets table lua_newtable(*_luaState); _assetsTableRef = luaL_ref(*_luaState, LUA_REGISTRYINDEX); } @@ -397,16 +397,16 @@ std::string AssetLoader::generateAssetPath(const std::string& baseDirectory, fullAssetPath, fullScenePath, prefix + assetPath, fullAssetPath )); - return ghoul::filesystem::File(FileSys.absPath(fullAssetPath)); + return FileSys.absPath(fullAssetPath); } if (fullScenePathExists) { - return ghoul::filesystem::File(FileSys.absPath(fullScenePath)); + return FileSys.absPath(fullScenePath); } // We don't check whether the file exists here as the error will be more // comprehensively logged by Lua either way - return ghoul::filesystem::File(FileSys.absPath(fullAssetPath)); + return FileSys.absPath(fullAssetPath); } std::shared_ptr AssetLoader::getAsset(const std::string& name) { @@ -523,8 +523,12 @@ std::shared_ptr AssetLoader::has(const std::string& identifier) const { return it->second.lock(); } -std::shared_ptr AssetLoader::rootAsset() const { - return _rootAsset; +const Asset& AssetLoader::rootAsset() const { + return *_rootAsset; +} + +Asset& AssetLoader::rootAsset() { + return *_rootAsset; } const std::string& AssetLoader::assetRootDirectory() const { diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index 18d9089a2f..a1540deebe 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -36,23 +36,19 @@ namespace openspace { -AssetManager::AssetManager(std::unique_ptr loader, - std::unique_ptr syncWatcher) - : _synchronizationWatcher(std::move(syncWatcher)) - , _assetLoader(std::move(loader)) +AssetManager::AssetManager(ghoul::lua::LuaState* state, std::string assetRootDirectory) + : _assetLoader(state, &_synchronizationWatcher, std::move(assetRootDirectory)) {} void AssetManager::initialize() { - _assetLoader->addAssetListener(this); - std::shared_ptr rootAsset = _assetLoader->rootAsset(); - rootAsset->initialize(); + _assetLoader.addAssetListener(this); + _assetLoader.rootAsset().initialize(); } void AssetManager::deinitialize() { - _assetLoader->rootAsset()->deinitialize(); - _assetLoader->rootAsset()->unload(); - _assetLoader->removeAssetListener(this); - _assetLoader = nullptr; + _assetLoader.rootAsset().deinitialize(); + _assetLoader.rootAsset().unload(); + _assetLoader.removeAssetListener(this); } bool AssetManager::update() { @@ -63,21 +59,21 @@ bool AssetManager::update() { const std::string& path = c.first; const bool add = c.second; if (add) { - _assetLoader->add(path); + _assetLoader.add(path); } } // Remove assets for (const std::pair& c : _pendingStateChangeCommands) { const std::string& path = c.first; const bool remove = !c.second; - if (remove && _assetLoader->has(path)) { - _assetLoader->remove(path); + if (remove && _assetLoader.has(path)) { + _assetLoader.remove(path); } } _pendingStateChangeCommands.clear(); // Change state based on synchronizations - _synchronizationWatcher->notify(); + _synchronizationWatcher.notify(); return false; } @@ -110,15 +106,19 @@ void AssetManager::removeAll() { _pendingStateChangeCommands.clear(); std::vector> allAssets = - _assetLoader->rootAsset()->requestedAssets(); + _assetLoader.rootAsset().requestedAssets(); for (const std::shared_ptr& a : allAssets) { _pendingStateChangeCommands[a->assetFilePath()] = false; } } -std::shared_ptr AssetManager::rootAsset() { - return _assetLoader->rootAsset(); +const Asset& AssetManager::rootAsset() const { + return _assetLoader.rootAsset(); +} + +Asset& AssetManager::rootAsset() { + return _assetLoader.rootAsset(); } scripting::LuaLibrary AssetManager::luaLibrary() { @@ -144,4 +144,4 @@ scripting::LuaLibrary AssetManager::luaLibrary() { }; } -} +} // namespace openspace diff --git a/src/scene/scenelicensewriter.cpp b/src/scene/scenelicensewriter.cpp index de546670bf..90319fe9d6 100644 --- a/src/scene/scenelicensewriter.cpp +++ b/src/scene/scenelicensewriter.cpp @@ -49,7 +49,7 @@ std::string SceneLicenseWriter::generateJson() const { json << "["; std::vector> assets = - global::openSpaceEngine.assetManager().rootAsset()->subTreeAssets(); + global::openSpaceEngine.assetManager().rootAsset().subTreeAssets(); for (const std::shared_ptr& asset : assets) { std::optional meta = asset->metaInformation(); diff --git a/src/util/synchronizationwatcher.cpp b/src/util/synchronizationwatcher.cpp index 86db87b078..2fe4377611 100644 --- a/src/util/synchronizationwatcher.cpp +++ b/src/util/synchronizationwatcher.cpp @@ -32,7 +32,7 @@ SynchronizationWatcher::WatchHandle SynchronizationWatcher::watchSynchronization std::shared_ptr synchronization, ResourceSynchronization::StateChangeCallback callback) { - std::lock_guard guard(_mutex); + std::lock_guard guard(_mutex); WatchHandle watchHandle = nextWatchHandle++; @@ -51,7 +51,7 @@ SynchronizationWatcher::WatchHandle SynchronizationWatcher::watchSynchronization } void SynchronizationWatcher::unwatchSynchronization(WatchHandle watchHandle) { - std::lock_guard guard(_mutex); + std::lock_guard guard(_mutex); const auto it = _watchedSyncs.find(watchHandle); if (it == _watchedSyncs.end()) { @@ -79,7 +79,7 @@ void SynchronizationWatcher::unwatchSynchronization(WatchHandle watchHandle) { void SynchronizationWatcher::notify() { std::vector notifications; { - std::lock_guard guard(_mutex); + std::lock_guard guard(_mutex); notifications = _pendingNotifications; _pendingNotifications.clear(); }