mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-27 06:29:32 -06:00
Move more scene loading logic to assets
This commit is contained in:
38
assets/assethelper/assethelper.asset
Normal file
38
assets/assethelper/assethelper.asset
Normal file
@@ -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
|
||||
@@ -1,5 +1,6 @@
|
||||
local Sun = asset.import('sun');
|
||||
local AssetHelper = asset.import('assethelper')
|
||||
local SpiceBase = asset.import('spice/base')
|
||||
|
||||
asset.EarthBarycenter = {
|
||||
Name = "EarthBarycenter",
|
||||
32
assets/scenes/default/default.asset
Normal file
32
assets/scenes/default/default.asset
Normal file
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
7
assets/spice/base/base.asset
Normal file
7
assets/spice/base/base.asset
Normal file
@@ -0,0 +1,7 @@
|
||||
local AssetHelper = asset.import("assethelper")
|
||||
local kernels = {
|
||||
asset.syncedResource("naif0012.tls"),
|
||||
asset.syncedResource("pck00010.tpc")
|
||||
}
|
||||
|
||||
AssetHelper.registerSpiceKernels(asset, kernels)
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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> 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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user