diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index ebf0f3b3ea..77a2b245c5 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -41,8 +41,7 @@ namespace ghoul::fontrendering { class FontManager; } namespace openspace { -class AssetLoader; -class AssetSynchronizer; +class AssetManager; class ConfigurationManager; class DownloadManager; class GUI; @@ -100,17 +99,14 @@ public: void decode(); void scheduleLoadSingleAsset(std::string assetPath); - - void enableBarrier(); - void disableBarrier(); - - + void writeDocumentation(); void toggleShutdownMode(); // Guaranteed to return a valid pointer ConfigurationManager& configurationManager(); LuaConsole& console(); + AssetManager& assetManager(); DownloadManager& downloadManager(); ModuleEngine& moduleEngine(); NetworkEngine& networkEngine(); @@ -119,9 +115,7 @@ public: SettingsEngine& settingsEngine(); TimeManager& timeManager(); WindowWrapper& windowWrapper(); - AssetLoader& assetLoader(); ResourceSynchronizer& resourceSynchronizer(); - AssetSynchronizer& assetSynchronizer(); ghoul::fontrendering::FontManager& fontManager(); interaction::NavigationHandler& navigationHandler(); interaction::KeyBindingManager& keyBindingManager(); @@ -185,12 +179,13 @@ private: // Components std::unique_ptr _configurationManager; std::unique_ptr _scene; - std::unique_ptr _assetLoader; + std::unique_ptr _assetManager; std::unique_ptr _downloadManager; std::unique_ptr _console; std::unique_ptr _moduleEngine; std::unique_ptr _networkEngine; std::unique_ptr _parallelConnection; + std::unique_ptr _resourceSynchronizer; std::unique_ptr _renderEngine; std::unique_ptr _settingsEngine; std::unique_ptr _syncEngine; @@ -200,8 +195,7 @@ private: std::unique_ptr _fontManager; std::unique_ptr _navigationHandler; std::unique_ptr _keyBindingManager; - std::unique_ptr _resourceSynchronizer; - std::unique_ptr _assetSynchronizer; + std::unique_ptr _scriptEngine; std::unique_ptr _scriptScheduler; std::unique_ptr _virtualPropertyManager; diff --git a/include/openspace/scene/assetloader.h b/include/openspace/scene/assetloader.h index 9a7af41e15..0e47502bc5 100644 --- a/include/openspace/scene/assetloader.h +++ b/include/openspace/scene/assetloader.h @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * @@ -85,19 +85,14 @@ public: * Add the asset as an optional on the root asset * The asset is imported synchronously */ - std::shared_ptr importAsset(const std::string& identifier); + std::shared_ptr loadAsset(const std::string& identifier); /** * Unimport an asset: * Remove the asset as an optional on the root asset * The asset is unimported synchronously */ - void unimportAsset(const std::string& identifier); - - /** - * Return the lua library of the asset loader - */ - scripting::LuaLibrary luaLibrary(); + void unloadAsset(const std::string& identifier); /** * Return the lua state @@ -132,8 +127,8 @@ public: private: std::shared_ptr importRequiredDependency(const std::string& identifier); std::shared_ptr importOptionalDependency(const std::string& identifier, bool enabled = true); - std::shared_ptr loadAsset(std::string name); - std::shared_ptr getAsset(std::string name); + std::shared_ptr importAsset(std::string path); + std::shared_ptr getAsset(std::string path); ghoul::filesystem::Directory currentDirectory(); void pushAsset(std::shared_ptr asset); diff --git a/include/openspace/scene/assetmanager.h b/include/openspace/scene/assetmanager.h new file mode 100644 index 0000000000..68dcfd4722 --- /dev/null +++ b/include/openspace/scene/assetmanager.h @@ -0,0 +1,75 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2017 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#ifndef __OPENSPACE_CORE___ASSETMANAGER___H__ +#define __OPENSPACE_CORE___ASSETMANAGER___H__ + +#include + +#include +#include + +#include + +#include +#include +#include +#include + +namespace openspace { + +class Asset; + +class AssetManager { +public: + AssetManager( + std::unique_ptr loader, + std::unique_ptr synchronizer + ); + + enum class AssetState : int { + Unloaded = 0, + Loaded = 1, + Synchronized = 2, + Initialized = 3 + }; + + void update(); + void setTargetAssetState(const std::string& path, AssetState targetState); + void clearAllTargetAssets(); + std::vector> allAssets(); + scripting::LuaLibrary luaLibrary(); + +private: + + std::unordered_map _pendingStateChangeCommands; + std::unordered_set _synchronizingAssets; + bool _shouldClearAssets; + std::unique_ptr _assetLoader; + std::unique_ptr _assetSynchronizer; +}; + +} // namespace openspace + +#endif // __OPENSPACE_CORE___ASSETMANAGER___H__ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 065989adc1..81347b3a74 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -127,8 +127,10 @@ set(OPENSPACE_SOURCE ${OPENSPACE_BASE_DIR}/src/rendering/screenspacerenderable.cpp ${OPENSPACE_BASE_DIR}/src/rendering/transferfunction.cpp ${OPENSPACE_BASE_DIR}/src/scene/asset.cpp - ${OPENSPACE_BASE_DIR}/src/scene/assetloader_lua.inl ${OPENSPACE_BASE_DIR}/src/scene/assetloader.cpp + ${OPENSPACE_BASE_DIR}/src/scene/assetloader_lua.inl + ${OPENSPACE_BASE_DIR}/src/scene/assetmanager.cpp + ${OPENSPACE_BASE_DIR}/src/scene/assetmanager_lua.inl ${OPENSPACE_BASE_DIR}/src/scene/assetsynchronizer.cpp ${OPENSPACE_BASE_DIR}/src/scene/translation.cpp ${OPENSPACE_BASE_DIR}/src/scene/rotation.cpp @@ -291,6 +293,7 @@ set(OPENSPACE_HEADER ${OPENSPACE_BASE_DIR}/include/openspace/rendering/transferfunction.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/asset.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/assetloader.h + ${OPENSPACE_BASE_DIR}/include/openspace/scene/assetmanager.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/assetsynchronizer.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/translation.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/rotation.h diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 67c18fd629..a6a9042ea2 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -46,8 +46,8 @@ #include #include +#include #include -#include #include #include #include @@ -148,7 +148,6 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName, , _parallelConnection(new ParallelConnection) , _renderEngine(new RenderEngine) , _resourceSynchronizer(new ResourceSynchronizer) - , _assetSynchronizer(new AssetSynchronizer(_resourceSynchronizer.get())) , _settingsEngine(new SettingsEngine) , _syncEngine(std::make_unique(4096)) , _timeManager(new TimeManager) @@ -405,8 +404,10 @@ void OpenSpaceEngine::create(int argc, char** argv, sgctArguments.insert(sgctArguments.begin() + 2, absPath(sgctConfigurationPath)); // Set up asset loader - _engine->_assetLoader = std::make_unique( - *OsEng.scriptEngine().luaState(), "${ASSETS}", "${SYNC}"); + _engine->_assetManager = std::make_unique( + std::make_unique(*OsEng.scriptEngine().luaState(), "${ASSETS}", "${SYNC}"), + std::make_unique(*OsEng._resourceSynchronizer.get()) + ); //_engine->_globalPropertyNamespace->addPropertySubOwner(_engine->_assetLoader->rootAsset()); } @@ -511,7 +512,7 @@ void OpenSpaceEngine::initialize() { // Register Lua script functions LDEBUG("Registering Lua libraries"); registerCoreClasses(*_scriptEngine); - _scriptEngine->addLibrary(_engine->_assetLoader->luaLibrary()); + _scriptEngine->addLibrary(_engine->_assetManager->luaLibrary()); for (OpenSpaceModule* module : _moduleEngine->modules()) { _scriptEngine->addLibrary(module->luaLibrary()); @@ -589,14 +590,8 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { _scene = std::make_unique(); _renderEngine->setScene(_scene.get()); - std::shared_ptr asset = _assetLoader->loadSingleAsset(assetPath); - std::vector> assets = asset->allActiveAssets(); - for (const auto& asset : assets) { - _assetSynchronizer->addAsset(asset); - } - LINFO("SYNCS"); - - asset->initialize(); + _assetManager->clearAllTargetAssets(); + _assetManager->setTargetAssetState(assetPath, AssetManager::AssetState::Initialized); } catch (const ghoul::FileNotFoundError& e) { LERRORC(e.component, e.message); @@ -1111,6 +1106,7 @@ void OpenSpaceEngine::postSynchronizationPreDraw() { _shutdown.timer -= static_cast(_windowWrapper->averageDeltaTime()); } + _assetManager->update(); _renderEngine->updateScene(); _renderEngine->updateFade(); _renderEngine->updateRenderer(); @@ -1471,9 +1467,9 @@ WindowWrapper& OpenSpaceEngine::windowWrapper() { return *_windowWrapper; } -AssetLoader & OpenSpaceEngine::assetLoader() { - ghoul_assert(_assetLoader, "Asset loader must not be nullptr"); - return *_assetLoader; +AssetManager& OpenSpaceEngine::assetManager() { + ghoul_assert(_assetManager, "Asset Manager must not be nullptr"); + return *_assetManager; } ResourceSynchronizer & OpenSpaceEngine::resourceSynchronizer() { @@ -1481,11 +1477,6 @@ ResourceSynchronizer & OpenSpaceEngine::resourceSynchronizer() { return *_resourceSynchronizer; } -AssetSynchronizer & OpenSpaceEngine::assetSynchronizer() { - ghoul_assert(_assetSynchronizer, "Asset Synchronizer must not be nullptr"); - return *_assetSynchronizer; -} - ghoul::fontrendering::FontManager& OpenSpaceEngine::fontManager() { ghoul_assert(_fontManager, "Font Manager must not be nullptr"); return *_fontManager; diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 8a337db349..1a341de540 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * @@ -91,7 +91,7 @@ AssetLoader::AssetLoader( AssetLoader::~AssetLoader() { } -std::shared_ptr AssetLoader::loadAsset(std::string path) { +std::shared_ptr AssetLoader::importAsset(std::string path) { std::shared_ptr asset = std::make_shared(this, path); pushAsset(asset); @@ -213,16 +213,15 @@ std::shared_ptr AssetLoader::loadSingleAsset(const std::string& identifie return imported; } -std::shared_ptr AssetLoader::importAsset(const std::string & identifier) { - ghoul_assert(_assetStack.size() == 1, "Can only import an asset from the root asset"); +std::shared_ptr AssetLoader::loadAsset(const std::string & identifier) { + ghoul_assert(_assetStack.size() == 1, "Can only load an asset from the root asset"); return importOptionalDependency(identifier); } -void AssetLoader::unimportAsset(const std::string & identifier) { - ghoul_assert(_assetStack.size() == 1, "Can only unimport an asset from the root asset"); - - ghoul::filesystem::Directory directory = currentDirectory(); +void AssetLoader::unloadAsset(const std::string & identifier) { + ghoul_assert(_assetStack.size() == 1, "Can only unload an asset from the root asset"); + // TODO: Implement this //_rootAsset->removeOptional(id); } @@ -584,27 +583,4 @@ void AssetLoader::addLuaDependencyTable(Asset* dependant, Asset* dependency) { lua_setfield(*_luaState, dependantsTableIndex, dependantId.c_str()); } - -scripting::LuaLibrary AssetLoader::luaLibrary() { - return { - "", - { - { - "importAsset", - &luascriptfunctions::importAsset, - {this}, - "string", - "" - }, - { - "unimportAsset", - &luascriptfunctions::unimportAsset, - {this}, - "string", - "" - } - } - }; -} - } diff --git a/src/scene/assetloader_lua.inl b/src/scene/assetloader_lua.inl index 99ba375533..8b4b1bf43e 100644 --- a/src/scene/assetloader_lua.inl +++ b/src/scene/assetloader_lua.inl @@ -22,38 +22,7 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -namespace openspace { -namespace luascriptfunctions { - -int importAsset(lua_State* state) { - AssetLoader *assetLoader = - reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); - - int nArguments = lua_gettop(state); - SCRIPT_CHECK_ARGUMENTS("importAsset", state, 1, nArguments); - - std::string assetName = luaL_checkstring(state, -1); - - assetLoader->importAsset(assetName); - return 0; -} - -int unimportAsset(lua_State* state) { - AssetLoader *assetLoader = - reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); - - int nArguments = lua_gettop(state); - SCRIPT_CHECK_ARGUMENTS("unimportAsset", state, 1, nArguments); - - std::string assetName = luaL_checkstring(state, -1); - - assetLoader->unimportAsset(assetName); - return 0; -} - -} // namespace luascriptfunctions - -namespace assetloader { +namespace openspace::assetloader { /** * Adds a Lua function to be called upon asset initialization @@ -147,8 +116,4 @@ int exportAsset(lua_State* state) { return asset->loader()->exportAssetLua(asset); } -} // namespace assetloader - - - -} +} // namespace openspace::assetloader diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp new file mode 100644 index 0000000000..a83e740466 --- /dev/null +++ b/src/scene/assetmanager.cpp @@ -0,0 +1,118 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2017 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#include + +#include + +#include +#include + +#include "assetmanager_lua.inl" + +namespace { + const char* _loggerCat = "AssetManager"; +} + +namespace openspace { +AssetManager::AssetManager(std::unique_ptr loader, + std::unique_ptr synchronizer) +{ + +} + +void AssetManager::update() { + // 1. Check clear flag. + + for (const auto& c : _pendingStateChangeCommands) { + const std::string& path = c.first; + const AssetState targetState = c.second; + + AssetState currentState = AssetState::Unloaded; + + + + switch (currentState) { + case AssetState::Unloaded: + if (_assetLoader->rootAsset) + _assetLoader->unloadAsset(path); + break; + case AssetState::Loaded: + _assetLoader->loadAsset(path); + break; + case AssetState::Synchronized: + //_assetSynchronizer->addAsset(); + break; + case AssetState::Initialized: + + break; + } + } + +// startSynchronizations(); +// handleFinishedSynchronizations(); +// handleLoading(); +// handleInitialization(); +} + +void AssetManager::setTargetAssetState(const std::string& path, AssetState targetState) { + ghoul::filesystem::File file(absPath(path)); + std::string normalizedPath = file.path(); + _pendingStateChangeCommands[normalizedPath] = targetState; +} + +void AssetManager::clearAllTargetAssets() { + _pendingStateChangeCommands.clear(); + _shouldClearAssets = true; +} + +std::vector> AssetManager::allAssets() +{ + return std::vector>(); +} + +scripting::LuaLibrary AssetManager::luaLibrary() { + return { + "", + { + { + "importAsset", + &luascriptfunctions::importAsset, + {this}, + "string", + "" + }, + { + "unimportAsset", + &luascriptfunctions::unimportAsset, + {this}, + "string", + "" + } + } + }; +} + + +} diff --git a/src/scene/assetmanager_lua.inl b/src/scene/assetmanager_lua.inl new file mode 100644 index 0000000000..f103f8f12a --- /dev/null +++ b/src/scene/assetmanager_lua.inl @@ -0,0 +1,53 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2017 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace::luascriptfunctions { + +int importAsset(lua_State* state) { + AssetManager *assetManager = + reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); + + int nArguments = lua_gettop(state); + SCRIPT_CHECK_ARGUMENTS("importAsset", state, 1, nArguments); + + std::string assetName = luaL_checkstring(state, -1); + + assetManager->setTargetAssetState(assetName, AssetManager::AssetState::Initialized); + return 0; +} + +int unimportAsset(lua_State* state) { + AssetManager *assetManager = + reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); + + int nArguments = lua_gettop(state); + SCRIPT_CHECK_ARGUMENTS("unimportAsset", state, 1, nArguments); + + std::string assetName = luaL_checkstring(state, -1); + + assetManager->setTargetAssetState(assetName, AssetManager::AssetState::Unloaded); + return 0; +} + +} // namespace openspace::luascriptfunctions