diff --git a/include/openspace/scene/asset.h b/include/openspace/scene/asset.h index a12bfd2fad..a451adf05a 100644 --- a/include/openspace/scene/asset.h +++ b/include/openspace/scene/asset.h @@ -25,6 +25,8 @@ #ifndef __OPENSPACE_CORE___ASSET___H__ #define __OPENSPACE_CORE___ASSET___H__ +#include + #include #include #include @@ -41,8 +43,6 @@ public: enum class ReadyState : unsigned int { Loaded, - Synchronizing, - Synchronized, Initialized }; @@ -65,8 +65,9 @@ public: std::string syncDirectory() const; ReadyState readyState() const; - void synchronize(); - void synchronizeEnabledRecursive(); + void addSynchronization(std::shared_ptr synchronization); + std::vector> synchronizations(); + std::vector> getSynchronizationsRecursive(); bool isInitReady() const; void initialize(); @@ -90,17 +91,12 @@ public: void dependantDidInitialize(Asset* dependant); void dependantWillDeinitialize(Asset* dependant); - //void optionalDidInitialize(Asset* optional); - //void optionalWillDeinitialize(Asset* optional); - - //bool shouldSynchronize(); - //bool shouldInitialize(); - std::string resolveLocalResource(std::string resourceName); std::string resolveSyncedResource(std::string resourceName); private: ReadyState _readyState; AssetLoader* _loader; + std::vector> _synchronizations; // The name of the asset std::string _assetName; diff --git a/include/openspace/scene/assetloader.h b/include/openspace/scene/assetloader.h index c59ceb4ddb..71a5778e74 100644 --- a/include/openspace/scene/assetloader.h +++ b/include/openspace/scene/assetloader.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include @@ -64,7 +64,6 @@ public: */ AssetLoader( ghoul::lua::LuaState& luaState, - ResourceSynchronizer& resourceSynchronizer, std::string assetRoot, std::string syncRoot ); @@ -74,32 +73,19 @@ public: */ ~AssetLoader(); - /** - * Start synchronization recursively - */ - void synchronizeEnabledAssets(); - /** * Load single asset. * - Import one asset * - Unimport all other assets */ - void loadSingleAsset(const std::string& identifier); - - /** - * Synchronize, mark as synchronized, - * initialize and deinitialize dependencies - * based on state of the assets. - * Update asset ready states. - */ - void update(); + Asset* loadSingleAsset(const std::string& identifier); /** * Import an asset: * Add the asset as an optional on the root asset * The asset is imported synchronously */ - void importAsset(const std::string& identifier); + Asset* importAsset(const std::string& identifier); /** * Unimport an asset: @@ -136,8 +122,6 @@ public: void callOnDependantDeinitialize(Asset* asset, Asset* dependant); - //void synchronizeResource(const ghoul::Dictionary& d, std::function onFinish); - std::string generateAssetPath(const std::string& baseDirectory, const std::string& path) const; private: @@ -162,7 +146,6 @@ private: int importOptionalDependencyLua(Asset* asset); int resolveLocalResourceLua(Asset* asset); int resolveSyncedResourceLua(Asset* asset); - int onFinishSynchronizationLua(Asset* asset); int exportAssetLua(Asset* asset); // Friend c closures (callable from lua, and maps to lua functions above) @@ -181,7 +164,7 @@ private: std::map> _importedAssets; std::vector _assetStack; - ResourceSynchronizer* _resourceSynchronizer; + AssetSynchronizer* _assetSynchronizer; std::string _assetRootDirectory; std::string _syncRootDirectory; ghoul::lua::LuaState* _luaState; diff --git a/include/openspace/scene/assetsynchronizer.h b/include/openspace/scene/assetsynchronizer.h new file mode 100644 index 0000000000..fc000d68a1 --- /dev/null +++ b/include/openspace/scene/assetsynchronizer.h @@ -0,0 +1,77 @@ +/***************************************************************************************** + * * + * 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___ASSETSYNCHRONIZER___H__ +#define __OPENSPACE_CORE___ASSETSYNCHRONIZER___H__ + +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include +#include + +namespace openspace { + +class AssetSynchronizer : public ResourceSyncClient { +public: + AssetSynchronizer(ResourceSynchronizer* resourceSynchronizer); + void addAsset(std::shared_ptr asset); + void removeAsset(Asset* asset); + void syncAsset(Asset* asset); + + std::vector getRecentlySynchronizedAssets(); +private: + enum class SynchronizationState : int { + Added, + Synchronizing, + RecentlySynchronized, + Synchronized + }; + + struct AssetSynchronization { + std::shared_ptr asset; + SynchronizationState state; + }; + + ResourceSynchronizer* _resourceSynchronizer; + std::vector _synchronizations; + + std::map _resourceToAssetMap; +}; + + + + +} // namespace openspace + +#endif // __OPENSPACE_CORE___ASSETSYNCHRONIZER___H__ diff --git a/include/openspace/util/resourcesynchronization.h b/include/openspace/util/resourcesynchronization.h index ff24b87dd9..01a0260f78 100644 --- a/include/openspace/util/resourcesynchronization.h +++ b/include/openspace/util/resourcesynchronization.h @@ -32,16 +32,46 @@ namespace openspace { +class ResourceSynchronization; + +class SynchronizationProduct { + +}; + +class SynchronizationJob : public Job { +public: + SynchronizationJob(std::shared_ptr synchronization); + void execute() = 0; +protected: + void resolve(); + void updateProgress(float t); +private: + std::shared_ptr _synchronization; +}; class ResourceSynchronization { public: - static std::unique_ptr createFromDictionary(const ghoul::Dictionary& dictionary); + ResourceSynchronization(); + static std::unique_ptr createFromDictionary( + const ghoul::Dictionary& dictionary); - ResourceSynchronization() = default; - virtual bool needsSync() = 0; + virtual std::shared_ptr job(); + void wait(); + bool isResolved(); + void resolve(); + float progress(); + void updateProgress(float t); +private: + std::atomic _started; + std::atomic _resolved; + std::atomic _progress; }; + + + + } // namespace openspace #endif // __OPENSPACE_CORE___RESOURCESYNCHRONIZATION___H__ diff --git a/include/openspace/util/resourcesynchronizer.h b/include/openspace/util/resourcesynchronizer.h index 913fd6561a..593a851c57 100644 --- a/include/openspace/util/resourcesynchronizer.h +++ b/include/openspace/util/resourcesynchronizer.h @@ -30,8 +30,15 @@ namespace openspace { -class ResourceSynchronizer { +class ResourceSyncClient {}; +class ResourceSynchronizer { +public: + void enqueueSynchronization(std::shared_ptr client); + std::vector> getFinishedSynchronizations(ResourceSyncClient* client); +private: + std::map, + std::shared_ptr> _origin; }; diff --git a/modules/sync/syncs/httpsynchronization.cpp b/modules/sync/syncs/httpsynchronization.cpp index 75f08bb9f1..e093185d88 100644 --- a/modules/sync/syncs/httpsynchronization.cpp +++ b/modules/sync/syncs/httpsynchronization.cpp @@ -35,12 +35,11 @@ HttpSynchronization::HttpSynchronization(const ghoul::Dictionary& dict) } -bool HttpSynchronization::needsSync() { - return true; -} - documentation::Documentation HttpSynchronization::Documentation() { return {}; } + + + } // namespace openspace diff --git a/modules/sync/syncs/httpsynchronization.h b/modules/sync/syncs/httpsynchronization.h index dd7e289f67..27c43d603a 100644 --- a/modules/sync/syncs/httpsynchronization.h +++ b/modules/sync/syncs/httpsynchronization.h @@ -35,11 +35,15 @@ namespace openspace { class HttpSynchronization : public ResourceSynchronization { public: HttpSynchronization(const ghoul::Dictionary& dict); - bool needsSync() override; - void start(); static documentation::Documentation Documentation(); }; +class HttpSynchronizationJob : public SynchronizationJob { + HttpSynchronizationJob(std::shared_ptr synchronization); + void execute() override; +}; + + } // namespace openspace #endif // __OPENSPACE_MODULE_SYNC___HTTPSYNCHRONIZATION___H__ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ade242ea74..58eaf3bc62 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -129,6 +129,7 @@ set(OPENSPACE_SOURCE ${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/assetsynchronizer.cpp ${OPENSPACE_BASE_DIR}/src/scene/translation.cpp ${OPENSPACE_BASE_DIR}/src/scene/rotation.cpp ${OPENSPACE_BASE_DIR}/src/scene/scale.cpp @@ -154,6 +155,7 @@ set(OPENSPACE_SOURCE ${OPENSPACE_BASE_DIR}/src/util/powerscaledscalar.cpp ${OPENSPACE_BASE_DIR}/src/util/powerscaledsphere.cpp ${OPENSPACE_BASE_DIR}/src/util/progressbar.cpp + ${OPENSPACE_BASE_DIR}/src/util/resourcesynchronization.cpp ${OPENSPACE_BASE_DIR}/src/util/resourcesynchronizer.cpp ${OPENSPACE_BASE_DIR}/src/util/screenlog.cpp ${OPENSPACE_BASE_DIR}/src/util/spicemanager.cpp @@ -287,6 +289,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/assetsynchronizer.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/translation.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/rotation.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/scale.h diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 565d6e7ed5..94e6632de3 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -412,7 +412,7 @@ void OpenSpaceEngine::create(int argc, char** argv, // Set up asset loader _engine->_assetLoader = std::make_unique( - *OsEng.scriptEngine().luaState(), OsEng.resourceSynchronizer(), "${ASSETS}", "${SYNC}"); + *OsEng.scriptEngine().luaState(), "${ASSETS}", "${SYNC}"); //_engine->_globalPropertyNamespace->addPropertySubOwner(_engine->_assetLoader->rootAsset()); } @@ -614,8 +614,6 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { } } - _assetLoader->synchronizeEnabledAssets(); - _renderEngine->setGlobalBlackOutFactor(0.0); _renderEngine->startFading(1, 3.0); diff --git a/src/scene/asset.cpp b/src/scene/asset.cpp index 79660d68f2..f26d7b0344 100644 --- a/src/scene/asset.cpp +++ b/src/scene/asset.cpp @@ -74,11 +74,17 @@ Asset::ReadyState Asset::readyState() const { return _readyState; } +void Asset::addSynchronization(std::shared_ptr synchronization) { + _synchronizations.push_back(synchronization); +} + bool Asset::isInitReady() const { - // An asset is ready for initialization if it is synchronized + // An asset is ready for initialization if all synchronizations are resolved // and all its required dependencies are ready for initialization. - if (_readyState != Asset::ReadyState::Synchronized) { - return false; + for (const std::shared_ptr& sync : _synchronizations) { + if (!sync->isResolved()) { + return false; + } } for (const auto& dependency : _requiredDependencies) { if (!dependency->isInitReady()) { @@ -88,34 +94,6 @@ bool Asset::isInitReady() const { return true; } - -void Asset::synchronizeEnabledRecursive() { - synchronize(); - for (Asset* a : _requiredDependencies) { - a->synchronizeEnabledRecursive(); - } - for (Optional& o : _optionalDependencies) { - if (o.second) { - o.first->synchronizeEnabledRecursive(); - } - } -} - -void Asset::synchronize() { - if (_readyState != Asset::ReadyState::Loaded) { - // Already synchronized or currently synchronizing - return; - } - - LDEBUG("Synchronizing asset " << id()); - _readyState = Asset::ReadyState::Synchronizing; - - // Synchronize dependencies - for (auto& dependency : _requiredDependencies) { - dependency->synchronize(); - } -} - void Asset::initialize() { LDEBUG("Initializing asset " << id()); if (_readyState == Asset::ReadyState::Initialized) { @@ -156,7 +134,7 @@ void Asset::deinitialize() { // Call onDeinitialize in Lua loader()->callOnDeinitialize(this); - _readyState = Asset::ReadyState::Synchronized; + _readyState = Asset::ReadyState::Loaded; // Make sure no dependencies are left dangling for (auto& dependency : _requiredDependencies) { @@ -173,7 +151,7 @@ std::string Asset::resolveSyncedResource(std::string resourceName) { } std::string Asset::id() const { - return _assetPath.has_value() ? _assetPath.value() : ""; + return _assetPath.has_value() ? _assetPath.value() : "$root"; } std::string Asset::assetFilePath() const { @@ -262,11 +240,11 @@ void Asset::removeRequiredDependency(const std::string& assetId) { } void Asset::dependantDidInitialize(Asset* dependant) { - //loader()->callOnDependantInitialize(this, dependant); + loader()->callOnDependantInitialize(this, dependant); } void Asset::dependantWillDeinitialize(Asset* dependant) { - //loader()->callOnDependantDeinitialize(this, dependant); + loader()->callOnDependantDeinitialize(this, dependant); } bool Asset::hasDependants() const { diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 3173a1a0c5..18a4a71d06 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -73,7 +73,6 @@ namespace openspace { AssetLoader::AssetLoader( ghoul::lua::LuaState& luaState, - ResourceSynchronizer& resourceSynchronizer, std::string assetRootDirectory, std::string syncRootDirectory ) @@ -81,7 +80,6 @@ AssetLoader::AssetLoader( , _rootAsset(std::make_unique(this)) , _assetRootDirectory(assetRootDirectory) , _syncRootDirectory(std::move(syncRootDirectory)) - , _resourceSynchronizer(&resourceSynchronizer) { pushAsset(_rootAsset.get()); @@ -109,6 +107,7 @@ Asset* AssetLoader::loadAsset(std::string path) { ghoul::lua::runScriptFile(*_luaState, path); _importedAssets.emplace(asset->id(), std::move(asset)); + return rawAsset; } @@ -141,8 +140,6 @@ Asset* AssetLoader::getAsset(std::string name) { int AssetLoader::onInitializeLua(Asset* asset) { int nArguments = lua_gettop(*_luaState); SCRIPT_CHECK_ARGUMENTS("onInitialize", *_luaState, 1, nArguments); - - lua_pushvalue(*_luaState, 1); int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onInitializationFunctionRefs[asset].push_back(referenceIndex); return 0; @@ -151,8 +148,6 @@ int AssetLoader::onInitializeLua(Asset* asset) { int AssetLoader::onDeinitializeLua(Asset* asset) { int nArguments = lua_gettop(*_luaState); SCRIPT_CHECK_ARGUMENTS("onDeinitialize", *_luaState, 1, nArguments); - - lua_pushvalue(*_luaState, 1); int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onDeinitializationFunctionRefs[asset].push_back(referenceIndex); return 0; @@ -161,8 +156,6 @@ int AssetLoader::onDeinitializeLua(Asset* asset) { int AssetLoader::onInitializeDependencyLua(Asset* dependant, Asset* dependency) { int nArguments = lua_gettop(*_luaState); SCRIPT_CHECK_ARGUMENTS("onInitializeDependency", *_luaState, 1, nArguments); - - lua_pushvalue(*_luaState, 1); int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onDependencyInitializationFunctionRefs[dependant][dependency].push_back(referenceIndex); return 0; @@ -171,14 +164,19 @@ int AssetLoader::onInitializeDependencyLua(Asset* dependant, Asset* dependency) int AssetLoader::onDeinitializeDependencyLua(Asset* dependant, Asset* dependency) { int nArguments = lua_gettop(*_luaState); SCRIPT_CHECK_ARGUMENTS("onDeinitializeDependency", *_luaState, 1, nArguments); - - lua_pushvalue(*_luaState, 1); int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onDependencyDeinitializationFunctionRefs[dependant][dependency].push_back(referenceIndex); return 0; } -int AssetLoader::addSynchronizationLua(Asset * asset) { +int AssetLoader::addSynchronizationLua(Asset* asset) { + int nArguments = lua_gettop(*_luaState); + SCRIPT_CHECK_ARGUMENTS("addSynchronization", *_luaState, 1, nArguments); + + ghoul::Dictionary d; + ghoul::lua::luaDictionaryFromState(*_luaState, d); + asset->addSynchronization(ResourceSynchronization::createFromDictionary(d)); + return 0; } @@ -204,11 +202,7 @@ ghoul::filesystem::Directory AssetLoader::currentDirectory() { } } -void AssetLoader::synchronizeEnabledAssets() { - _rootAsset->synchronizeEnabledRecursive(); -} - -void AssetLoader::loadSingleAsset(const std::string& identifier) { +Asset* AssetLoader::loadSingleAsset(const std::string& identifier) { Asset* imported = importOptionalDependency(identifier, true); std::vector optionals = _rootAsset->optionalAssets(); @@ -218,15 +212,12 @@ void AssetLoader::loadSingleAsset(const std::string& identifier) { _rootAsset->removeOptionalDependency(optional); } } + return imported; } -void AssetLoader::update() { - // 1. synchronize assets -} - -void AssetLoader::importAsset(const std::string & identifier) { +Asset* AssetLoader::importAsset(const std::string & identifier) { ghoul_assert(_assetStack.size() == 1, "Can only import an asset from the root asset"); - importOptionalDependency(identifier); + return importOptionalDependency(identifier); } @@ -252,102 +243,42 @@ const std::string& AssetLoader::syncRootDirectory() { void AssetLoader::callOnInitialize(Asset* asset) { for (int init : _onInitializationFunctionRefs[asset]) { - lua_pcall(*_luaState, init, 0, 0); + lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, init); + if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { + throw ghoul::lua::LuaRuntimeException(luaL_checkstring(*_luaState, -1)); + } } } void AssetLoader::callOnDeinitialize(Asset * asset) { std::vector& funs = _onDeinitializationFunctionRefs[asset]; for (auto it = funs.rbegin(); it != funs.rend(); it++) { - lua_pcall(*_luaState, *it, 0, 0); + lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, *it); + if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { + throw ghoul::lua::LuaRuntimeException(luaL_checkstring(*_luaState, -1)); + } } } -/* -void AssetLoader::callOnSynchronize(Asset* asset) { - if (asset == _rootAsset.get()) { - return; - } - lua_getglobal(*_luaState, AssetsTableName); - lua_getfield(*_luaState, -1, asset->id().c_str()); - lua_getfield(*_luaState, -1, OnSynchronizeFunctionName); - - // onSynchronize(function onFinish) - - lua_pushlightuserdata(*_luaState, asset); - lua_pushcclosure(*_luaState, &assetloader::onFinishSynchronization, 1); - - const int status = lua_pcall(*_luaState, 1, 0, 0); - if (status != LUA_OK) { - throw ghoul::lua::LuaExecutionException(lua_tostring(*_luaState, -1)); - } -}*/ - -/* -void AssetLoader::callOnInitialize(Asset * asset) { - if (asset == _rootAsset.get()) { - return; - } - lua_getglobal(*_luaState, AssetsTableName); - lua_getfield(*_luaState, -1, asset->id().c_str()); - lua_getfield(*_luaState, -1, OnInitializeFunctionName); - const int status = lua_pcall(*_luaState, 0, 0, 0); - if (status != LUA_OK) { - throw ghoul::lua::LuaExecutionException(lua_tostring(*_luaState, -1)); - } -} - -void AssetLoader::callOnDeinitialize(Asset* asset) { - if (asset == _rootAsset.get()) { - return; - } - lua_getglobal(*_luaState, AssetsTableName); - lua_getfield(*_luaState, -1, asset->id().c_str()); - lua_getfield(*_luaState, -1, OnDeinitializeFunctionName); - const int status = lua_pcall(*_luaState, 0, 0, 0); - if (status != LUA_OK) { - throw ghoul::lua::LuaExecutionException(lua_tostring(*_luaState, -1)); - } -}*/ - -/* void AssetLoader::callOnDependantInitialize(Asset* asset, Asset* dependant) { - if (asset == _rootAsset.get()) { - return; - } - lua_getglobal(*_luaState, AssetsTableName); - lua_getfield(*_luaState, -1, asset->id().c_str()); - lua_getfield(*_luaState, -1, DependantsTableName); - lua_getfield(*_luaState, -1, dependant->id().c_str()); - lua_getfield(*_luaState, -1, OnInitializeFunctionName); - const int status = lua_pcall(*_luaState, 0, 0, 0); - if (status != LUA_OK) { - throw ghoul::lua::LuaLoadingException(lua_tostring(*_luaState, -1)); + for (int init : _onDependencyInitializationFunctionRefs[dependant][asset]) { + lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, init); + if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { + throw ghoul::lua::LuaRuntimeException(luaL_checkstring(*_luaState, -1)); + } } } void AssetLoader::callOnDependantDeinitialize(Asset* asset, Asset* dependant) { - if (asset == _rootAsset.get()) { - return; - } - - lua_getglobal(*_luaState, AssetsTableName); - lua_getfield(*_luaState, -1, asset->id().c_str()); - lua_getfield(*_luaState, -1, DependantsTableName); - lua_getfield(*_luaState, -1, dependant->id().c_str()); - lua_getfield(*_luaState, -1, OnDeinitializeFunctionName); - const int status = lua_pcall(*_luaState, 0, 0, 0); - if (status != LUA_OK) { - throw ghoul::lua::LuaLoadingException(lua_tostring(*_luaState, -1)); + std::vector& funs = _onDependencyDeinitializationFunctionRefs[dependant][asset]; + for (auto it = funs.rbegin(); it != funs.rend(); it++) { + lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, *it); + if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { + throw ghoul::lua::LuaRuntimeException(luaL_checkstring(*_luaState, -1)); + } } } -void AssetLoader::synchronizeResource(const ghoul::Dictionary & d, std::function onFinish) { - LINFO("DUMMY SYNC RESOURCE CALL"); - onFinish(true); -} -*/ - int AssetLoader::resolveLocalResourceLua(Asset* asset) { int nArguments = lua_gettop(*_luaState); if (nArguments != 1) { @@ -374,11 +305,6 @@ int AssetLoader::resolveSyncedResourceLua(Asset* asset) { return 1; } -int AssetLoader::onFinishSynchronizationLua(Asset* asset) { - LINFO("Finished syncing resource!" << asset->id()); - return 0; -} - void AssetLoader::pushAsset(Asset* asset) { _assetStack.push_back(asset); diff --git a/src/scene/assetloader_lua.inl b/src/scene/assetloader_lua.inl index 094d58b0d4..99ba375533 100644 --- a/src/scene/assetloader_lua.inl +++ b/src/scene/assetloader_lua.inl @@ -51,34 +51,6 @@ int unimportAsset(lua_State* state) { return 0; } -int addSynchronization(lua_State* state) { - /* - AssetLoader *assetLoader = - reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); - - int nArguments = lua_gettop(state); - SCRIPT_CHECK_ARGUMENTS("addSynchronization", state, 2, nArguments); - - int referenceIndex = luaL_ref(state, LUA_REGISTRYINDEX); - - ghoul::Dictionary d; - try { - ghoul::lua::luaDictionaryFromState(state, d); - } - catch (const ghoul::lua::LuaFormatException& e) { - LERRORC("addSynchronization", e.what()); - return luaL_error(state, "Error loading dictionary from lua state"); - } - - assetLoader->synchronizeResource(d, [state, referenceIndex](bool success) { - lua_rawgeti(state, LUA_REGISTRYINDEX, referenceIndex); - lua_pushboolean(state, success ? 1 : 0); - lua_pcall(state, 1, 0, 0); - luaL_unref(state, LUA_REGISTRYINDEX, referenceIndex); - });*/ - return 0; -} - } // namespace luascriptfunctions namespace assetloader { diff --git a/src/scene/assetsynchronizer.cpp b/src/scene/assetsynchronizer.cpp new file mode 100644 index 0000000000..8ad8e3e25a --- /dev/null +++ b/src/scene/assetsynchronizer.cpp @@ -0,0 +1,71 @@ +/***************************************************************************************** + * * + * 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 + +namespace { + const char* _loggerCat = "AssetSynchronizer"; +} + +namespace openspace { +AssetSynchronizer::AssetSynchronizer(ResourceSynchronizer* resourceSynchronizer) { + _resourceSynchronizer = resourceSynchronizer; +} + +void AssetSynchronizer::addAsset(std::shared_ptr asset) { + _synchronizations.push_back({ asset, SynchronizationState::Added }); + std::vector> syncs = asset->synchronizations(); + for (const auto& s : syncs) { + if (!s->isResolved()) { + return; + } + } + _synchronizations.back().state = SynchronizationState::Synchronized; +} + +void AssetSynchronizer::removeAsset(Asset* asset) { + std::remove_if( + _synchronizations.begin(), + _synchronizations.end(), + [asset](const AssetSynchronization& a) { + return a.asset.get() == asset; + } + ); +} + +void AssetSynchronizer::syncAsset(Asset* asset) { + const auto& sync = std::find_if( + _synchronizations.begin(), + _synchronizations.end(), + [asset](const AssetSynchronization& a) { + return a.asset.get() == asset; + } + ); + // todo... +} + +} diff --git a/src/util/resourcesynchronization.cpp b/src/util/resourcesynchronization.cpp new file mode 100644 index 0000000000..76ed71c19f --- /dev/null +++ b/src/util/resourcesynchronization.cpp @@ -0,0 +1,74 @@ +#include "..\..\include\openspace\util\resourcesynchronization.h" +/***************************************************************************************** + * * + * 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 { + +ResourceSynchronization::ResourceSynchronization() {} + +std::unique_ptr ResourceSynchronization::createFromDictionary( + const ghoul::Dictionary & dictionary) +{ + return std::unique_ptr(); +} + +std::shared_ptr ResourceSynchronization::job() +{ + return std::shared_ptr(); +} + +void ResourceSynchronization::wait() { +} + +bool ResourceSynchronization::isResolved() { + return _resolved; +} + +void ResourceSynchronization::resolve() { + _resolved = true; +} + +float ResourceSynchronization::progress() { + return _progress; +} + +void ResourceSynchronization::updateProgress(float t) { + _progress = std::min(1.0f, std::max(t, 0.0f)); +} + +SynchronizationJob::SynchronizationJob( + std::shared_ptr synchronization) +{ + _synchronization = synchronization; +} + +void SynchronizationJob::resolve() { + _synchronization->resolve(); +} + +void SynchronizationJob::updateProgress(float t) { + _synchronization->updateProgress(t); +} + +} diff --git a/tests/AssetLoaderTest/initialization.asset b/tests/AssetLoaderTest/initialization.asset index e5e8e20c72..186dee561b 100644 --- a/tests/AssetLoaderTest/initialization.asset +++ b/tests/AssetLoaderTest/initialization.asset @@ -1,3 +1,3 @@ -asset.onInitialize(function () - passTest() +asset.onInitialize(function () + passTest() end) \ No newline at end of file diff --git a/tests/test_assetloader.inl b/tests/test_assetloader.inl index e7fc0e4a13..1c8f78c8bd 100644 --- a/tests/test_assetloader.inl +++ b/tests/test_assetloader.inl @@ -63,10 +63,8 @@ protected: virtual void SetUp() { ghoul::lua::LuaState* state = OsEng.scriptEngine().luaState(); OsEng.scriptEngine().initialize(); - _resourceSynchronizer = std::make_unique(); _assetLoader = std::make_unique( *state, - *_resourceSynchronizer.get(), "${TESTDIR}/AssetLoaderTest/", "${TEMPORARY}/resources/"); @@ -133,10 +131,9 @@ TEST_F(AssetLoaderTest, DependencyFuncitons) { } TEST_F(AssetLoaderTest, AssetInitialization) { - LINFOC("test", "WHAT?"); try { - _assetLoader->loadSingleAsset("initialization"); - _assetLoader->rootAsset()->initialize(); + openspace::Asset* asset = _assetLoader->loadSingleAsset("initialization"); + asset->initialize(); EXPECT_TRUE(passed()); } catch (const std::exception& e) {