Feature/globals handling (#1352)

* Cleaner handling of global state
* Prevent Lua memory corruption (closes #982)
* Initialize glfw first thing to prevent weird joystick loading bug during startup
This commit is contained in:
Alexander Bock
2020-10-21 22:30:05 +02:00
committed by GitHub
parent 1525a0490d
commit efffc25ce0
164 changed files with 1484 additions and 1390 deletions

View File

@@ -57,16 +57,16 @@ void DocumentationTopic::handleJson(const nlohmann::json& json) {
// Do not parse generated json. Instead implement ability to get
// ghoul::Dictionary objects from ScriptEngine, FactoryManager, and KeybindingManager.
if (requestedType == TypeLua) {
response = json::parse(global::scriptEngine.generateJson());
response = json::parse(global::scriptEngine->generateJson());
}
else if (requestedType == TypeFactories) {
response = json::parse(FactoryManager::ref().generateJson());
}
else if (requestedType == TypeKeyboard) {
response = json::parse(global::keybindingManager.generateJson());
response = json::parse(global::keybindingManager->generateJson());
}
else if (requestedType == TypeAsset) {
response = json::parse(global::keybindingManager.generateJson());
response = json::parse(global::keybindingManager->generateJson());
}
else if (requestedType == TypeMeta) {
std::string docs = SceneLicenseWriter().generateJson();

View File

@@ -159,20 +159,20 @@ namespace openspace {
FlightControllerTopic::FlightControllerTopic() {
for (auto it = AxisIndexMap.begin(); it != AxisIndexMap.end(); ++it) {
global::navigationHandler.setWebsocketAxisMapping(
global::navigationHandler->setWebsocketAxisMapping(
int(std::distance(AxisIndexMap.begin(), it)),
it->second
);
}
// Add WebsocketInputState to global states
global::websocketInputStates[_topicId] = &_inputState;
(*global::websocketInputStates)[_topicId] = &_inputState;
}
FlightControllerTopic::~FlightControllerTopic() {
// Reset global websocketInputStates
global::websocketInputStates.erase(_topicId);
global::websocketInputStates = interaction::WebsocketInputStates();
global::websocketInputStates->erase(_topicId);
*global::websocketInputStates = interaction::WebsocketInputStates();
}
bool FlightControllerTopic::isDone() const {
@@ -234,7 +234,7 @@ void FlightControllerTopic::connect() {
void FlightControllerTopic::setFocusNodes() {
// Get all scene nodes
std::vector<SceneGraphNode*> nodes =
global::renderEngine.scene()->allSceneGraphNodes();
global::renderEngine->scene()->allSceneGraphNodes();
// Remove all nodes with no renderable
nodes.erase(
@@ -271,7 +271,7 @@ void FlightControllerTopic::setFocusNodes() {
void FlightControllerTopic::setInterestingTimes() {
std::vector<Scene::InterestingTime> times =
global::renderEngine.scene()->interestingTimes();
global::renderEngine->scene()->interestingTimes();
std::sort(
times.begin(),
@@ -323,30 +323,30 @@ void FlightControllerTopic::changeFocus(const nlohmann::json& json) const {
const bool retargetAnchor = json[RetargetAnchorKey];
const bool retargetAim = json[RetargetAimKey];
Scene* scene = global::renderEngine.scene();
Scene* scene = global::renderEngine->scene();
const SceneGraphNode* focusNode = scene->sceneGraphNode(focus);
const SceneGraphNode* aimNode = scene->sceneGraphNode(aim);
const SceneGraphNode* anchorNode = scene->sceneGraphNode(anchor);
if (focusNode) {
global::navigationHandler.orbitalNavigator().setFocusNode(
global::navigationHandler->orbitalNavigator().setFocusNode(
focusNode,
resetVelocities
);
}
else {
if (aimNode) {
global::navigationHandler.orbitalNavigator().setAimNode(aim);
global::navigationHandler->orbitalNavigator().setAimNode(aim);
}
if (anchorNode) {
global::navigationHandler.orbitalNavigator().setAnchorNode(anchor);
global::navigationHandler->orbitalNavigator().setAnchorNode(anchor);
}
}
if (retargetAnchor) {
global::navigationHandler.orbitalNavigator().startRetargetAnchor();
global::navigationHandler->orbitalNavigator().startRetargetAnchor();
}
if (retargetAim) {
global::navigationHandler.orbitalNavigator().startRetargetAim();
global::navigationHandler->orbitalNavigator().startRetargetAim();
}
}
@@ -362,7 +362,7 @@ void FlightControllerTopic::setRenderableEnabled(const nlohmann::json& json) con
const std::string name = json[RenderableKey][SceneNodeName];
const bool enabled = json[RenderableKey][SceneNodeEnabled];
const SceneGraphNode* node = global::renderEngine.scene()->sceneGraphNode(name);
const SceneGraphNode* node = global::renderEngine->scene()->sceneGraphNode(name);
if (node && node->renderable() != nullptr) {
node->renderable()->property(RenderableEnabled)->set(enabled);
}
@@ -370,8 +370,8 @@ void FlightControllerTopic::setRenderableEnabled(const nlohmann::json& json) con
void FlightControllerTopic::disconnect() {
// Reset global websocketInputStates
global::websocketInputStates.erase(_topicId);
global::websocketInputStates = interaction::WebsocketInputStates();
global::websocketInputStates->erase(_topicId);
*global::websocketInputStates = interaction::WebsocketInputStates();
// Update FlightController
nlohmann::json j;
@@ -388,7 +388,7 @@ void FlightControllerTopic::setFriction(bool all) const {
void FlightControllerTopic::setFriction(bool roll, bool rotation, bool zoom) const {
const interaction::OrbitalNavigator& navigator =
global::navigationHandler.orbitalNavigator();
global::navigationHandler->orbitalNavigator();
navigator.property(RollFriction)->set(roll);
navigator.property(RotationalFriction)->set(rotation);
@@ -476,7 +476,7 @@ void FlightControllerTopic::processInputState(const nlohmann::json& json) {
void FlightControllerTopic::processLua(const nlohmann::json &json) {
const std::string script = json[LuaScript];
global::scriptEngine.queueScript(
global::scriptEngine->queueScript(
script,
openspace::scripting::ScriptEngine::RemoteScripting::Yes
);

View File

@@ -64,7 +64,7 @@ void GetPropertyTopic::handleJson(const nlohmann::json& json) {
}
else if (requestedKey == AllScreenSpaceRenderablesValue) {
response = wrappedPayload({
{ "value", global::renderEngine.screenSpaceRenderables() }
{ "value", global::renderEngine->screenSpaceRenderables() }
});
}
else if (requestedKey == RootPropertyOwner) {

View File

@@ -190,7 +190,7 @@ void LuaScriptTopic::runScript(const std::string& script, bool shouldReturn) {
_waitingForReturnValue = false;
}
global::scriptEngine.queueScript(
global::scriptEngine->queueScript(
std::move(script),
scripting::ScriptEngine::RemoteScripting::No,
callback

View File

@@ -51,7 +51,7 @@ SessionRecordingTopic::SessionRecordingTopic() {
SessionRecordingTopic::~SessionRecordingTopic() {
if (_stateCallbackHandle != UnsetOnChangeHandle) {
global::sessionRecording.removeStateChangeCallback(_stateCallbackHandle);
global::sessionRecording->removeStateChangeCallback(_stateCallbackHandle);
}
}
@@ -98,10 +98,10 @@ void SessionRecordingTopic::handleJson(const nlohmann::json& json) {
sendJsonData();
if (event == SubscribeEvent && _sendState) {
_stateCallbackHandle = global::sessionRecording.addStateChangeCallback(
_stateCallbackHandle = global::sessionRecording->addStateChangeCallback(
[this]() {
interaction::SessionRecording::SessionState currentState =
global::sessionRecording.state();
global::sessionRecording->state();
if (currentState != _lastState) {
sendJsonData();
_lastState = currentState;
@@ -115,7 +115,7 @@ void SessionRecordingTopic::sendJsonData() {
json stateJson;
using SessionRecording = openspace::interaction::SessionRecording;
if (_sendState) {
SessionRecording::SessionState state = global::sessionRecording.state();
SessionRecording::SessionState state = global::sessionRecording->state();
std::string stateString;
switch (state) {
case SessionRecording::SessionState::Recording:
@@ -131,7 +131,7 @@ void SessionRecordingTopic::sendJsonData() {
stateJson[StateKey] = stateString;
};
if (_sendFiles) {
stateJson[FilesKey] = global::sessionRecording.playbackList();
stateJson[FilesKey] = global::sessionRecording->playbackList();
}
if (!stateJson.empty()) {
_connection->sendJson(wrappedPayload(stateJson));

View File

@@ -110,13 +110,13 @@ void SetPropertyTopic::handleJson(const nlohmann::json& json) {
if (propertyKey == SpecialKeyTime) {
Time newTime;
newTime.setTime(json.at(ValueKey).get<std::string>());
global::timeManager.setTimeNextFrame(newTime);
global::timeManager->setTimeNextFrame(newTime);
}
else {
nlohmann::json value = json.at(ValueKey);
std::string literal = luaLiteralFromJson(value);
global::scriptEngine.queueScript(
global::scriptEngine->queueScript(
fmt::format(
"openspace.setPropertyValueSingle(\"{}\", {})", propertyKey, literal
),

View File

@@ -47,7 +47,7 @@ std::vector<nlohmann::json> ShortcutTopic::shortcutsJson() const {
using ShortcutInformation = interaction::ShortcutManager::ShortcutInformation;
const std::vector<ShortcutInformation>& shortcuts =
global::shortcutManager.shortcuts();
global::shortcutManager->shortcuts();
std::vector<nlohmann::json> json;
for (const ShortcutInformation& shortcut : shortcuts) {
@@ -64,7 +64,7 @@ std::vector<nlohmann::json> ShortcutTopic::shortcutsJson() const {
using KeyInformation = interaction::KeybindingManager::KeyInformation;
const std::multimap<KeyWithModifier, KeyInformation>& keyBindings =
global::keybindingManager.keyBindings();
global::keybindingManager->keyBindings();
for (const std::pair<const KeyWithModifier, KeyInformation>& keyBinding : keyBindings)
{

View File

@@ -48,13 +48,15 @@ TimeTopic::TimeTopic()
TimeTopic::~TimeTopic() {
if (_timeCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeTimeChangeCallback(_timeCallbackHandle);
global::timeManager->removeTimeChangeCallback(_timeCallbackHandle);
}
if (_deltaTimeCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeDeltaTimeChangeCallback(_deltaTimeCallbackHandle);
global::timeManager->removeDeltaTimeChangeCallback(_deltaTimeCallbackHandle);
}
if (_deltaTimeStepsCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeDeltaTimeStepsChangeCallback(_deltaTimeStepsCallbackHandle);
global::timeManager->removeDeltaTimeStepsChangeCallback(
_deltaTimeStepsCallbackHandle
);
}
}
@@ -77,20 +79,20 @@ void TimeTopic::handleJson(const nlohmann::json& json) {
return;
}
_timeCallbackHandle = global::timeManager.addTimeChangeCallback([this]() {
_timeCallbackHandle = global::timeManager->addTimeChangeCallback([this]() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
if (now - _lastUpdateTime > TimeUpdateInterval) {
sendCurrentTime();
}
});
_deltaTimeCallbackHandle = global::timeManager.addDeltaTimeChangeCallback([this]() {
_deltaTimeCallbackHandle = global::timeManager->addDeltaTimeChangeCallback([this]() {
// Throttle by last update,
// but force update if pause state or target delta changes.
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
const double targetDeltaTime = global::timeManager.targetDeltaTime();
const bool isPaused = global::timeManager.isPaused();
const double targetDeltaTime = global::timeManager->targetDeltaTime();
const bool isPaused = global::timeManager->isPaused();
const bool forceUpdate =
isPaused != _lastPauseState || targetDeltaTime != _lastTargetDeltaTime;
@@ -99,9 +101,9 @@ void TimeTopic::handleJson(const nlohmann::json& json) {
}
});
_deltaTimeStepsCallbackHandle = global::timeManager.addDeltaTimeStepsChangeCallback(
_deltaTimeStepsCallbackHandle = global::timeManager->addDeltaTimeStepsChangeCallback(
[this]() {
const std::vector<double> steps = global::timeManager.deltaTimeSteps();
const std::vector<double> steps = global::timeManager->deltaTimeSteps();
if (steps != _lastDeltaTimeSteps) {
sendDeltaTimeSteps();
}
@@ -110,8 +112,8 @@ void TimeTopic::handleJson(const nlohmann::json& json) {
}
const json TimeTopic::getNextPrevDeltaTimeStepJson() {
const std::optional<double> nextStep = global::timeManager.nextDeltaTimeStep();
const std::optional<double> prevStep = global::timeManager.previousDeltaTimeStep();
const std::optional<double> nextStep = global::timeManager->nextDeltaTimeStep();
const std::optional<double> prevStep = global::timeManager->previousDeltaTimeStep();
const bool hasNext = nextStep.has_value();
const bool hasPrev = prevStep.has_value();
@@ -135,7 +137,7 @@ void TimeTopic::sendCurrentTime() {
ZoneScoped
const json timeJson = {
{ "time", global::timeManager.time().ISO8601() }
{ "time", global::timeManager->time().ISO8601() }
};
const json payload = wrappedPayload(timeJson);
_connection->sendJson(payload);
@@ -143,10 +145,10 @@ void TimeTopic::sendCurrentTime() {
}
void TimeTopic::sendFullTimeData() {
std::string_view currentTime = global::timeManager.time().ISO8601();
const double deltaTime = global::timeManager.deltaTime();
const double targetDeltaTime = global::timeManager.targetDeltaTime();
const bool isPaused = global::timeManager.isPaused();
std::string_view currentTime = global::timeManager->time().ISO8601();
const double deltaTime = global::timeManager->deltaTime();
const double targetDeltaTime = global::timeManager->targetDeltaTime();
const bool isPaused = global::timeManager->isPaused();
json timeJson = {
{ "time", currentTime },
@@ -165,7 +167,7 @@ void TimeTopic::sendFullTimeData() {
}
void TimeTopic::sendDeltaTimeSteps() {
const std::vector<double>& steps = global::timeManager.deltaTimeSteps();
const std::vector<double>& steps = global::timeManager->deltaTimeSteps();
json deltaTimeStepsJson = {
{ "deltaTimeSteps", steps }

View File

@@ -42,7 +42,7 @@ namespace openspace {
void TriggerPropertyTopic::handleJson(const nlohmann::json& json) {
try {
const std::string& propertyKey = json.at(PropertyKey).get<std::string>();
global::scriptEngine.queueScript(
global::scriptEngine->queueScript(
fmt::format(
"openspace.setPropertyValueSingle(\"{}\", nil)", propertyKey
),

View File

@@ -56,9 +56,9 @@ void VersionTopic::handleJson(const nlohmann::json&) {
}
};
if (global::versionChecker.hasLatestVersionInfo()) {
if (global::versionChecker->hasLatestVersionInfo()) {
VersionChecker::SemanticVersion latestVersion =
global::versionChecker.latestVersion();
global::versionChecker->latestVersion();
versionJson["latestOpenSpaceVersion"] = {
{ "major", latestVersion.major },