diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index 2d74d604b2..9cc276248f 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -108,14 +108,16 @@ public: static scripting::LuaLibrary luaLibrary(); private: - void loadSingleAsset(const std::string& assetPath); + void loadAsset_init(const std::string assetName); + void loadAsset_postInit(const std::string assetName); void loadFonts(); void runGlobalCustomizationScripts(); void configureLogging(); std::string generateFilePath(std::string openspaceRelativePath); void resetPropertyChangeFlagsOfSubowners(openspace::properties::PropertyOwner* po); - void loadInitAssetSection(const std::string profileSectionName); + void loadInitAssetSection(std::string& initAssetOutput, + std::string profileSectionName); std::unique_ptr _scene; std::unique_ptr _assetManager; diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 3deb19b611..c1d37797f2 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -688,10 +688,10 @@ void OpenSpaceEngine::scheduleLoadSingleAsset(std::string assetPath) { _scheduledAssetPathToLoad = std::move(assetPath); } -void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { +void OpenSpaceEngine::loadAsset_init(const std::string assetName) { ZoneScoped - LTRACE("OpenSpaceEngine::loadSingleAsset(begin)"); + LTRACE("OpenSpaceEngine::loadAsset_init(begin)"); global::windowDelegate->setBarrier(false); global::windowDelegate->setSynchronization(false); @@ -700,7 +700,7 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { global::windowDelegate->setBarrier(true); }; - if (assetPath.empty()) { + if (assetName.empty()) { return; } if (_scene) { @@ -751,7 +751,7 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { } _assetManager->removeAll(); - _assetManager->add(assetPath); + _assetManager->add(assetName); _loadingScreen->setPhase(LoadingScreen::Phase::Construction); _loadingScreen->postMessage("Loading assets"); @@ -863,7 +863,16 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { _writeDocumentationTask = std::async(&OpenSpaceEngine::writeSceneDocumentation, this); - LTRACE("OpenSpaceEngine::loadSingleAsset(end)"); + LTRACE("OpenSpaceEngine::loadAsset_init(end)"); +} + +void OpenSpaceEngine::loadAsset_postInit(const std::string assetName) { + ZoneScoped + + LTRACE("OpenSpaceEngine::loadAsset_init(begin)"); + _assetManager->add(assetName); + _assetManager->update(); + LTRACE("OpenSpaceEngine::loadAsset_postInit(end)"); } void OpenSpaceEngine::deinitialize() { @@ -1098,23 +1107,37 @@ void OpenSpaceEngine::preSynchronization() { if (_hasScheduledAssetLoading) { LINFO(fmt::format("Loading asset: {}", absPath(_scheduledAssetPathToLoad))); global::profile->ignoreUpdates = true; - loadSingleAsset(_scheduledAssetPathToLoad); + loadAsset_init(_scheduledAssetPathToLoad); global::profile->ignoreUpdates = false; resetPropertyChangeFlagsOfSubowners(global::rootPropertyOwner); _hasScheduledAssetLoading = false; _scheduledAssetPathToLoad.clear(); } else if (!_hasInitializedProfile) { - loadInitAssetSection("_meta"); - loadInitAssetSection("_addedAssets"); - loadInitAssetSection("_modules"); - loadInitAssetSection("_actions"); - loadInitAssetSection("_keybinds"); - loadInitAssetSection("_time"); - loadInitAssetSection("_deltaTimes"); - loadInitAssetSection("_markNodes"); - loadInitAssetSection("_properties"); - loadInitAssetSection("_camera"); + std::string combinedAssetInitStr; + loadInitAssetSection(combinedAssetInitStr, "_meta"); + loadInitAssetSection(combinedAssetInitStr, "_addedAssets"); + combinedAssetInitStr += "asset.onInitialize(function()\n"; + loadInitAssetSection(combinedAssetInitStr, "_modules"); + loadInitAssetSection(combinedAssetInitStr, "_actions"); + loadInitAssetSection(combinedAssetInitStr, "_keybinds"); + loadInitAssetSection(combinedAssetInitStr, "_time"); + loadInitAssetSection(combinedAssetInitStr, "_deltaTimes"); + loadInitAssetSection(combinedAssetInitStr, "_markNodes"); + loadInitAssetSection(combinedAssetInitStr, "_properties"); + loadInitAssetSection(combinedAssetInitStr, "_camera"); + combinedAssetInitStr += "end)\n"; + + std::string outputScenePath = absPath("${TEMPORARY}").string() + + "/combinedInit.asset"; + std::ofstream combinedInitAsset(outputScenePath); + combinedInitAsset << combinedAssetInitStr; + + global::profile->ignoreUpdates = true; + loadAsset_init(outputScenePath); + global::profile->ignoreUpdates = false; + resetPropertyChangeFlagsOfSubowners(global::rootPropertyOwner); + _hasInitializedProfile = true; } @@ -1145,10 +1168,6 @@ void OpenSpaceEngine::preSynchronization() { ); } - if (!_hasInitializedProfile) { - loadInitAssetSection("_addedScripts"); - } - if (_scene) { Camera* camera = _scene->camera(); if (camera) { @@ -1168,23 +1187,39 @@ void OpenSpaceEngine::preSynchronization() { } if (!_hasInitializedProfile) { + std::string outputScenePath = absPath("${TEMPORARY}").string() + + "/combinedPostInit.asset"; + std::ofstream combinedPostInitAsset(outputScenePath); + std::string profilePostInitAsset = "asset.onInitialize(function()\n"; + profilePostInitAsset += fmt::format( + "{}_{}{}", + global::configuration->profileOutPrefixName, + "_addedScripts", + global::profile->assetFileExtension + ); + profilePostInitAsset += "end)\n"; + combinedPostInitAsset << profilePostInitAsset; + + loadAsset_postInit(outputScenePath); + _hasInitializedProfile = true; } LTRACE("OpenSpaceEngine::preSynchronization(end)"); } -void OpenSpaceEngine::loadInitAssetSection(const std::string profileSectionName) { - std::string assetFilename = fmt::format( +void OpenSpaceEngine::loadInitAssetSection(std::string& initAssetOutput, + std::string profileSectionName) +{ + std::string filename = fmt::format( "{}_{}{}", global::configuration->profileOutPrefixName, profileSectionName, global::profile->assetFileExtension ); + std::ifstream in(filename); + initAssetOutput.append(std::string(std::istreambuf_iterator(in), + std::istreambuf_iterator())); LINFO(fmt::format("Loading profile subsection {}", profileSectionName)); - global::profile->ignoreUpdates = true; - loadSingleAsset(assetFilename); - global::profile->ignoreUpdates = false; - resetPropertyChangeFlagsOfSubowners(global::rootPropertyOwner); } void OpenSpaceEngine::postSynchronizationPreDraw() { diff --git a/src/scene/profile.cpp b/src/scene/profile.cpp index daa5b6488b..c28cbce3f0 100644 --- a/src/scene/profile.cpp +++ b/src/scene/profile.cpp @@ -827,7 +827,7 @@ std::string convertToAsset_modules(const Profile& p) { std::string convertToAsset_actions(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; for (const Profile::Action& action : p.actions) { const std::string name = action.name.empty() ? action.identifier : action.name; output += fmt::format( @@ -839,7 +839,6 @@ std::string convertToAsset_actions(const Profile& p) { action.isLocal ? "true" : "false" ); } - output += "end)\n"; return output; } @@ -847,13 +846,12 @@ std::string convertToAsset_actions(const Profile& p) { std::string convertToAsset_keybinds(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; for (size_t i = 0; i < p.keybindings.size(); ++i) { const Profile::Keybinding& k = p.keybindings[i]; const std::string key = keyToString(k.key); output += fmt::format(" openspace.bindKey([[{}]], [[{}]])\n", key, k.action); } - output += "end)\n"; return output; } @@ -861,7 +859,7 @@ std::string convertToAsset_keybinds(const Profile& p) { std::string convertToAsset_time(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; switch (p.time->type) { case Profile::Time::Type::Absolute: output += fmt::format(" openspace.time.setTime(\"{}\")\n", p.time->value); @@ -876,7 +874,6 @@ std::string convertToAsset_time(const Profile& p) { default: throw ghoul::MissingCaseException(); } - output += "end)\n"; return output; } @@ -884,7 +881,7 @@ std::string convertToAsset_time(const Profile& p) { std::string convertToAsset_deltaTimes(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; { std::string times; for (double d : p.deltaTimes) { @@ -892,7 +889,6 @@ std::string convertToAsset_deltaTimes(const Profile& p) { } output += fmt::format(" openspace.time.setDeltaTimeSteps({{ {} }});\n", times); } - output += "end)\n"; return output; } @@ -900,7 +896,7 @@ std::string convertToAsset_deltaTimes(const Profile& p) { std::string convertToAsset_markNodes(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; { std::string nodes; for (const std::string& n : p.markNodes) { @@ -908,7 +904,6 @@ std::string convertToAsset_markNodes(const Profile& p) { } output += fmt::format(" openspace.markInterestingNodes({{ {} }});\n", nodes); } - output += "end)\n"; return output; } @@ -916,7 +911,7 @@ std::string convertToAsset_markNodes(const Profile& p) { std::string convertToAsset_properties(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; for (const Profile::Property& prop : p.properties) { switch (prop.setType) { case Profile::Property::SetType::SetPropertyValue: @@ -934,7 +929,6 @@ std::string convertToAsset_properties(const Profile& p) { throw ghoul::MissingCaseException(); } } - output += "end)\n"; return output; } @@ -942,7 +936,7 @@ std::string convertToAsset_properties(const Profile& p) { std::string convertToAsset_camera(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; if (p.camera.has_value()) { output += std::visit( overloaded { @@ -994,7 +988,6 @@ std::string convertToAsset_camera(const Profile& p) { *p.camera ); } - output += "end)\n"; return output; } @@ -1002,11 +995,10 @@ std::string convertToAsset_camera(const Profile& p) { std::string convertToAsset_addedScripts(const Profile& p) { ZoneScoped - std::string output = "asset.onInitialize(function()\n"; + std::string output; for (const std::string& a : p.additionalScripts) { output += fmt::format(" {}\n", a); } - output += "end)\n"; return output; }