diff --git a/assets/assethelper/assethelper.asset b/assets/assethelper/assethelper.asset new file mode 100644 index 0000000000..f75d40e263 --- /dev/null +++ b/assets/assethelper/assethelper.asset @@ -0,0 +1,38 @@ +asset.registerSceneGraphNodes = function (sceneAsset, nodes) + local oldInit = sceneAsset.onInitialize; + sceneAsset.onInitialize = function () + for i, node in ipairs(nodes) do + openspace.addSceneGraphNode(node) + end + oldInit() + end + + local oldDeinit = sceneAsset.onDeinitialize; + sceneAsset.onDeinitialize = function () + for i = #nodes, 1, -1 do + node = nodes[i] + openspace.removeSceneGraphNode(node.Name) + end + oldDeinit() + end +end + +asset.registerSpiceKernels = function (spiceAsset, kernels) + local oldInit = spiceAsset.onInitialize; + spiceAsset.onInitialize = function () + print "loading spice kernels...." + for i, kernel in ipairs(kernels) do + openspace.spice.loadKernel(kernel) + end + oldInit() + end + + local oldDeinit = spiceAsset.onDeinitialize; + spiceAsset.onDeinitialize = function () + for i = #kernels, 1, -1 do + kernel = kernels[i] + openspace.spice.unloadKernel(kernel) + end + oldDeinit() + end +end \ No newline at end of file diff --git a/assets/earth/earth.asset b/assets/planets/earth/earth.asset similarity index 98% rename from assets/earth/earth.asset rename to assets/planets/earth/earth.asset index ea8f917f0a..26b672937c 100644 --- a/assets/earth/earth.asset +++ b/assets/planets/earth/earth.asset @@ -1,5 +1,6 @@ local Sun = asset.import('sun'); local AssetHelper = asset.import('assethelper') +local SpiceBase = asset.import('spice/base') asset.EarthBarycenter = { Name = "EarthBarycenter", diff --git a/assets/scenes/default/default.asset b/assets/scenes/default/default.asset new file mode 100644 index 0000000000..328add0d47 --- /dev/null +++ b/assets/scenes/default/default.asset @@ -0,0 +1,32 @@ +local {Sun, SunToggle} = asset.importToggle('sun', true) +local {Earth, EarthToggle} = asset.importToggle('planets/earth', false) +local {MilkyWay, MilkyWayToggle} = asset.importToggle('milkyway', false) + +SunToggle.onInitialize = function () + openspace.setPropertyValue("SunMarker.renderable.enabled", false) +end + +EarthToggle.onInitialize = function () + openspace.setPropertyValue("EarthMarker.renderable.enabled", false) + openspace.setPropertyValue("Constellation Bounds.renderable.enabled", false) +end + +MilkyWayToggle.onInitialize = function () + openspace.setPropertyValue("MilkyWay.renderable.transparency", 0.55) + openspace.setPropertyValue("MilkyWay.renderable.segments", 50) +end + +asset.onInitialize = function () + openspace.time.setTime(openspace.time.currentWallTime()) + dofile(openspace.absPath('${SCRIPTS}/bind_common_keys.lua')) + openspace.resetCameraDirection() + openspace.addVirtualProperty("BoolProperty", "Show Trails", "*Trail.renderable.enabled", true, nil, nil) +end + +asset.onDeinitialize = function () + openspace.removeVirtualProperty("BoolProperty", "Show Trails", "*Trail.renderable.enabled", true, nil, nil) +end + + + + diff --git a/assets/spice/base/base.asset b/assets/spice/base/base.asset new file mode 100644 index 0000000000..71837a1ddb --- /dev/null +++ b/assets/spice/base/base.asset @@ -0,0 +1,7 @@ +local AssetHelper = asset.import("assethelper") +local kernels = { + asset.syncedResource("naif0012.tls"), + asset.syncedResource("pck00010.tpc") +} + +AssetHelper.registerSpiceKernels(asset, kernels) \ No newline at end of file diff --git a/assets/sun/sun.asset b/assets/sun/sun.asset index 0b47186acf..1e340a0f92 100644 --- a/assets/sun/sun.asset +++ b/assets/sun/sun.asset @@ -1,5 +1,5 @@ --local Base = asset.import('base') ---BaseSpiceKernels = import('base-spice-kernels') +local SpiceBase = asset.import('spice/base') local AssetHelper = asset.import('assethelper') asset.SolarSystem = { diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index 64518e4a79..7377b6df26 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -110,8 +110,6 @@ public: void writeDocumentation(); void toggleShutdownMode(); - - void runPostInitializationScripts(const std::string& sceneDescription); // Guaranteed to return a valid pointer ConfigurationManager& configurationManager(); @@ -179,7 +177,6 @@ private: void loadScene(const std::string& scenePath); void gatherCommandlineArguments(); void loadFonts(); - void runPreInitializationScripts(const std::string& sceneDescription); void configureLogging(); // Components diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index cc457a3fd1..a66fd1a819 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -92,9 +92,6 @@ namespace { const char* SgctDefaultConfigFile = "${SGCT}/single.xml"; const char* SgctConfigArgumentCommand = "-config"; - - const char* PreInitializeFunction = "preInitialization"; - const char* PostInitializationFunction = "postInitialization"; const int CacheVersion = 1; const int DownloadVersion = 1; @@ -123,7 +120,7 @@ OpenSpaceEngine* OpenSpaceEngine::_engine = nullptr; OpenSpaceEngine::OpenSpaceEngine(std::string programName, std::unique_ptr windowWrapper) : _configurationManager(new ConfigurationManager) - , _scene(new Scene) + , _scene(nullptr) , _downloadManager(nullptr) , _console(new LuaConsole) , _moduleEngine(new ModuleEngine) @@ -385,11 +382,13 @@ void OpenSpaceEngine::create(int argc, char** argv, } void OpenSpaceEngine::destroy() { + LTRACE("OpenSpaceEngine::destroy(begin)"); if (_engine->parallelConnection().status() != ParallelConnection::Status::Disconnected) { _engine->parallelConnection().signalDisconnect(); } - LTRACE("OpenSpaceEngine::destroy(begin)"); + _engine->_scene = nullptr; + for (const auto& func : _engine->_moduleCallbacks.deinitializeGL) { func(); } @@ -405,7 +404,6 @@ void OpenSpaceEngine::destroy() { _engine->_console->deinitialize(); _engine->_scriptEngine->deinitialize(); - _engine->_scene = nullptr; delete _engine; FactoryManager::deinitialize(); @@ -558,14 +556,6 @@ void OpenSpaceEngine::loadScene(const std::string& scenePath) { if (scenePath != "") { - // Run start up scripts - try { - runPreInitializationScripts(scenePath); - } - catch (const ghoul::RuntimeError& e) { - LERRORC(e.component, e.message); - } - // Load the scene try { if (_scene) { _syncEngine->removeSyncables(_timeManager->getSyncables()); @@ -612,11 +602,6 @@ void OpenSpaceEngine::loadScene(const std::string& scenePath) { if (_scene) { _renderEngine->setCamera(_scene->camera()); _interactionHandler->setCamera(_scene->camera()); - try { - runPostInitializationScripts(scenePath); - } catch (const ghoul::RuntimeError& e) { - LFATALC(e.component, e.message); - } // Write keyboard documentation. if (configurationManager().hasKey(ConfigurationManager::KeyKeyboardShortcuts)) { @@ -709,69 +694,6 @@ void OpenSpaceEngine::gatherCommandlineArguments() { )); } -void OpenSpaceEngine::runPreInitializationScripts(const std::string& sceneDescription) { - // @CLEANUP: Move this into the scene loading? ---abock - LINFO("Running Initialization scripts"); - - ghoul::lua::LuaState state; - OsEng.scriptEngine().initializeLuaState(state); - - // First execute the script to get all global variables - ghoul::lua::runScriptFile(state, absPath(sceneDescription)); - - // Get the preinitialize function - lua_getglobal(state, PreInitializeFunction); - bool isFunction = lua_isfunction(state, -1); - if (!isFunction) { - LERROR( - "Error executing startup script '" << sceneDescription << "'. Scene '" << - sceneDescription << "' does not have a function '" << - PreInitializeFunction << "'" - ); - return; - } - - // And execute the preinitialize function - int success = lua_pcall(state, 0, 0, 0); - if (success != 0) { - LERROR( - "Error executing '" << PreInitializeFunction << "': " << - lua_tostring(state, -1) - ); - } -} - -void OpenSpaceEngine::runPostInitializationScripts(const std::string& sceneDescription) { - // @CLEANUP: Move this into the scene loading? ---abock - LINFO("Running Setup scripts"); - ghoul::lua::LuaState state; - OsEng.scriptEngine().initializeLuaState(state); - - // First execute the script to get all global variables - ghoul::lua::runScriptFile(state, absPath(sceneDescription)); - - // Get the preinitialize function - lua_getglobal(state, PostInitializationFunction); - bool isFunction = lua_isfunction(state, -1); - if (!isFunction) { - LERROR( - "Error executing startup script '" << sceneDescription << "'. Scene '" << - sceneDescription << "' does not have a function '" << - PostInitializationFunction << "'" - ); - return; - } - - // And execute the preinitialize function - int success = lua_pcall(state, 0, 0, 0); - if (success != 0) { - LERROR( - "Error executing '" << PostInitializationFunction << "': " << - lua_tostring(state, -1) - ); - } -} - void OpenSpaceEngine::loadFonts() { ghoul::Dictionary fonts; configurationManager().getValue(ConfigurationManager::KeyFonts, fonts); diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 7282425a70..1321f9dab0 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -121,6 +121,7 @@ AssetLoader::Asset* AssetLoader::importAsset(const std::string& name) { } catch (const ghoul::lua::LuaRuntimeException& e) { LERROR(e.message << ": " << e.component); } + LDEBUG("Loaded asset " << asset->id()); _importedAssets.emplace(id, std::move(newAsset)); return _importedAssets[id].get(); @@ -296,11 +297,16 @@ void AssetLoader::Asset::initialize() { // Call onInitialize if (_hasLuaTable) { - ghoul::lua::LuaState* state = loader()->luaState(); - lua_getglobal(*state, AssetsTableName); - lua_getfield(*state, -1, id().c_str()); - lua_getfield(*state, -1, OnInitializeFunctionName); - lua_call(*state, 0, 0); + try { + ghoul::lua::LuaState* state = loader()->luaState(); + lua_getglobal(*state, AssetsTableName); + lua_getfield(*state, -1, id().c_str()); + lua_getfield(*state, -1, OnInitializeFunctionName); + lua_call(*state, 0, 0); + } catch (const ghoul::lua::LuaRuntimeException& e) { + LERROR(e.message << ": " << e.component); + return; + } } _initialized = true; } @@ -313,11 +319,17 @@ void AssetLoader::Asset::deinitialize() { // Call onDeinitialize _initialized = false; if (_hasLuaTable) { - ghoul::lua::LuaState* state = loader()->luaState(); - lua_getglobal(*state, AssetsTableName); - lua_getfield(*state, -1, id().c_str()); - lua_getfield(*state, -1, OnDeinitializeFunctionName); - lua_call(*state, 0, 0); + try { + ghoul::lua::LuaState* state = loader()->luaState(); + lua_getglobal(*state, AssetsTableName); + lua_getfield(*state, -1, id().c_str()); + lua_getfield(*state, -1, OnDeinitializeFunctionName); + lua_call(*state, 0, 0); + } + catch (const ghoul::lua::LuaRuntimeException& e) { + LERROR(e.message << ": " << e.component); + return; + } } // Also deinitialize any dangling dependencies