From f7f4f7b0ebbd8efcb9b59ad06d200d79b62795bc Mon Sep 17 00:00:00 2001 From: James H Ball Date: Sun, 20 Apr 2025 20:11:11 +0100 Subject: [PATCH] Improve crash detection logic --- Source/CommonPluginProcessor.cpp | 41 ++++++++++++++++++++++++++++++++ Source/CommonPluginProcessor.h | 3 +++ Source/PluginProcessor.cpp | 31 ++---------------------- Source/SosciPluginProcessor.cpp | 4 ++++ 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Source/CommonPluginProcessor.cpp b/Source/CommonPluginProcessor.cpp index a5668d3..fc2e656 100644 --- a/Source/CommonPluginProcessor.cpp +++ b/Source/CommonPluginProcessor.cpp @@ -360,3 +360,44 @@ void CommonAudioProcessor::saveGlobalSettings() if (globalSettings != nullptr) globalSettings->saveIfNeeded(); } + +bool CommonAudioProcessor::programCrashedAndUserWantsToReset() { + bool userWantsToReset = false; + + if (!hasSetSessionStartTime) { + // check that the previous end time is later than the start time. + // if not, the program did not close properly. + juce::String startTime = getGlobalStringValue("startTime"); + juce::String endTime = getGlobalStringValue("endTime"); + + if ((startTime.isNotEmpty() && endTime.isNotEmpty()) || (startTime.isNotEmpty() && endTime.isEmpty())) { + juce::Time start = juce::Time::fromISO8601(startTime); + juce::Time end = juce::Time::fromISO8601(endTime); + + if ((start > end || end == juce::Time()) && juce::MessageManager::getInstance()->isThisTheMessageThread()) { + juce::String message = "It appears that " + juce::String(ProjectInfo::projectName) + " did not close properly during your last session. This may indicate a problem with your project or session."; + + // Use a synchronous dialog to ensure user makes a choice before proceeding + bool userPressedReset = juce::AlertWindow::showOkCancelBox( + juce::AlertWindow::WarningIcon, + "Possible Crash Detected", + message + "\n\nDo you want to reset to a new project, or continue loading your previous session?", + "Reset to New Project", + "Continue", + nullptr, + nullptr + ); + + if (userPressedReset) { + userWantsToReset = true; + } + } + } + + setGlobalValue("startTime", juce::Time::getCurrentTime().toISO8601(true)); + saveGlobalSettings(); + hasSetSessionStartTime = true; + } + + return userWantsToReset; +} diff --git a/Source/CommonPluginProcessor.h b/Source/CommonPluginProcessor.h index 5d50fda..76179e9 100644 --- a/Source/CommonPluginProcessor.h +++ b/Source/CommonPluginProcessor.h @@ -74,6 +74,9 @@ public: void removeGlobalValue(const juce::String& keyName); void saveGlobalSettings(); + bool hasSetSessionStartTime = false; + bool programCrashedAndUserWantsToReset(); + juce::SpinLock audioPlayerListenersLock; std::vector audioPlayerListeners; diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index b5cd858..0185fcf 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -709,37 +709,10 @@ void OscirenderAudioProcessor::getStateInformation(juce::MemoryBlock& destData) } void OscirenderAudioProcessor::setStateInformation(const void* data, int sizeInBytes) { - // check that the previous end time is later than the start time. - // if not, the program did not close properly. - juce::String startTime = getGlobalStringValue("startTime"); - juce::String endTime = getGlobalStringValue("endTime"); - - if (startTime.isNotEmpty() && endTime.isNotEmpty()) { - juce::Time start = juce::Time::fromISO8601(startTime); - juce::Time end = juce::Time::fromISO8601(endTime); - - if ((start > end || end == juce::Time()) && juce::MessageManager::getInstance()->isThisTheMessageThread()) { - juce::String message = "It appears that " + juce::String(ProjectInfo::projectName) + " did not close properly during your last session. This may indicate a problem with your project or session."; - - // Use a synchronous dialog to ensure user makes a choice before proceeding - bool userPressedReset = juce::AlertWindow::showOkCancelBox( - juce::AlertWindow::WarningIcon, - "Possible Crash Detected", - message + "\n\nDo you want to reset to a new project, or continue loading your previous session?", - "Reset to New Project", - "Continue", - nullptr, - nullptr - ); - - if (userPressedReset) { - return; - } - } + if (juce::JUCEApplicationBase::isStandaloneApp() && programCrashedAndUserWantsToReset()) { + return; } - setGlobalValue("startTime", juce::Time::getCurrentTime().toISO8601(true)); - std::unique_ptr xml; const uint32_t magicXmlNumber = 0x21324356; diff --git a/Source/SosciPluginProcessor.cpp b/Source/SosciPluginProcessor.cpp index 6e130ac..b6fb0b2 100644 --- a/Source/SosciPluginProcessor.cpp +++ b/Source/SosciPluginProcessor.cpp @@ -135,6 +135,10 @@ void SosciAudioProcessor::getStateInformation(juce::MemoryBlock& destData) { } void SosciAudioProcessor::setStateInformation(const void* data, int sizeInBytes) { + if (juce::JUCEApplicationBase::isStandaloneApp() && programCrashedAndUserWantsToReset()) { + return; + } + std::unique_ptr xml; const uint32_t magicXmlNumber = 0x21324356;