diff --git a/VortexEngine/src/Modes/Modes.cpp b/VortexEngine/src/Modes/Modes.cpp index c37a226c5a..933b5e3718 100644 --- a/VortexEngine/src/Modes/Modes.cpp +++ b/VortexEngine/src/Modes/Modes.cpp @@ -97,98 +97,84 @@ void Modes::play() m_pCurModeLink->play(); } -bool Modes::serializeSaveHeader(ByteStream &saveBuffer) +// full save/load to/from buffer +bool Modes::saveToBuffer(ByteStream &modesBuffer) { - // serialize the engine version into the modes buffer - if (!VortexEngine::serializeVersion(saveBuffer)) { + // first write out the header + if (!serializeSaveHeader(modesBuffer)) { return false; } - // NOTE: instead of global brightness the duo uses this to store the - // startup mode ID. The duo doesn't offer a global brightness option - if (!saveBuffer.serialize8(m_globalFlags)) { + // serialize all modes data into the modesBuffer + if (!serialize(modesBuffer)) { return false; } - // serialize the global brightness - if (!saveBuffer.serialize8((uint8_t)Leds::getBrightness())) { + DEBUG_LOGF("Serialized all modes, uncompressed size: %u", modesBuffer.size()); + if (!modesBuffer.compress()) { return false; } - DEBUG_LOGF("Serialized all modes, uncompressed size: %u", saveBuffer.size()); return true; } -bool Modes::unserializeSaveHeader(ByteStream &saveHeader) +// load modes from a save buffer +bool Modes::loadFromBuffer(ByteStream &modesBuffer) { - if (!saveHeader.decompress()) { - // failed to decompress? - return false; - } - // reset the unserializer index before unserializing anything - saveHeader.resetUnserializer(); - uint8_t major = 0; - uint8_t minor = 0; - // unserialize the vortex version - if (!saveHeader.unserialize8(&major)) { - return false; - } - if (!saveHeader.unserialize8(&minor)) { - return false; - } - // check the version for incompatibility - if (!VortexEngine::checkVersion(major, minor)) { - // incompatible version - ERROR_LOGF("Incompatible savefile version: %u.%u", major, minor); - return false; - } - // NOTE: instead of global brightness the duo uses this to store the - // startup mode ID. The duo doesn't offer a global brightness option - // unserialize the global brightness - if (!saveHeader.unserialize8(&m_globalFlags)) { + if (!unserializeSaveHeader(modesBuffer)) { return false; } - // unserialize the global brightness - uint8_t brightness = 0; - if (!saveHeader.unserialize8(&brightness)) { + // now just unserialize the list of modes + if (!unserialize(modesBuffer)) { return false; } - if (brightness) { - Leds::setBrightness(brightness); + if (oneClickModeEnabled()) { + // set the current mode to the startup mode + switchToStartupMode(); } return true; } -// full save/load to/from buffer -bool Modes::saveToBuffer(ByteStream &modesBuffer) +bool Modes::saveHeader() { - // first write out the header - if (!serializeSaveHeader(modesBuffer)) { + ByteStream headerBuffer(MAX_MODE_SIZE); + if (!serializeSaveHeader(headerBuffer)) { return false; } - // serialize all modes data into the modesBuffer - if (!serialize(modesBuffer)) { + // serialize the number of modes + if (!headerBuffer.serialize8(m_numModes)) { return false; } - DEBUG_LOGF("Serialized all modes, uncompressed size: %u", modesBuffer.size()); - if (!modesBuffer.compress()) { + if (!Storage::write(0, headerBuffer)) { return false; } return true; } -// load modes from a save buffer -bool Modes::loadFromBuffer(ByteStream &modesBuffer) +bool Modes::loadHeader() { - if (!unserializeSaveHeader(modesBuffer)) { + ByteStream headerBuffer; + // only read storage if the modebuffer isn't filled + if (!Storage::read(0, headerBuffer) || !headerBuffer.size()) { + DEBUG_LOG("Empty buffer read from storage"); + // this kinda sucks whatever they had loaded is gone return false; } - // now just unserialize the list of modes - if (!unserialize(modesBuffer)) { + // this erases what is stored before we know whether there is data + // but it's the easiest way to just re-load new data from storage + clearModes(); + // read the header and load the data + if (!unserializeSaveHeader(headerBuffer)) { return false; } + // NOTE: We do not bother loading the number of modes because + // we can't really do anything with it anyway return true; } bool Modes::loadStorage() { + // NOTE: We could call loadHeader here but then we wouldn't have the headerBuffer + // and in turn wouldn't be able to unserialize the number of modes. The number + // of modes is a weird case, it's technically part of the mode list not the + // header but it is stored in the same storage slot as the header ByteStream headerBuffer; // only read storage if the modebuffer isn't filled if (!Storage::read(0, headerBuffer) || !headerBuffer.size()) { @@ -228,38 +214,6 @@ bool Modes::loadStorage() return true; } -bool Modes::saveHeader() -{ - ByteStream headerBuffer(MAX_MODE_SIZE); - if (!serializeSaveHeader(headerBuffer)) { - return false; - } - // serialize the number of modes - if (!headerBuffer.serialize8(m_numModes)) { - return false; - } - if (!Storage::write(0, headerBuffer)) { - return false; - } - return true; -} - -bool Modes::saveHeader() -{ - ByteStream headerBuffer(MAX_MODE_SIZE); - if (!serializeSaveHeader(headerBuffer)) { - return false; - } - // serialize the number of modes - if (!headerBuffer.serialize8(m_numModes)) { - return false; - } - if (!Storage::write(0, headerBuffer)) { - return false; - } - return true; -} - // NOTE: Flash storage is limited to about 10,000 writes so // use this function sparingly! bool Modes::saveStorage() @@ -303,6 +257,65 @@ bool Modes::saveStorage() return true; } +bool Modes::serializeSaveHeader(ByteStream &saveBuffer) +{ + // serialize the engine version into the modes buffer + if (!VortexEngine::serializeVersion(saveBuffer)) { + return false; + } + // NOTE: instead of global brightness the duo uses this to store the + // startup mode ID. The duo doesn't offer a global brightness option + if (!saveBuffer.serialize8(m_globalFlags)) { + return false; + } + // serialize the global brightness + if (!saveBuffer.serialize8((uint8_t)Leds::getBrightness())) { + return false; + } + DEBUG_LOGF("Serialized all modes, uncompressed size: %u", saveBuffer.size()); + return true; +} + +bool Modes::unserializeSaveHeader(ByteStream &saveHeader) +{ + if (!saveHeader.decompress()) { + // failed to decompress? + return false; + } + // reset the unserializer index before unserializing anything + saveHeader.resetUnserializer(); + uint8_t major = 0; + uint8_t minor = 0; + // unserialize the vortex version + if (!saveHeader.unserialize8(&major)) { + return false; + } + if (!saveHeader.unserialize8(&minor)) { + return false; + } + // check the version for incompatibility + if (!VortexEngine::checkVersion(major, minor)) { + // incompatible version + ERROR_LOGF("Incompatible savefile version: %u.%u", major, minor); + return false; + } + // NOTE: instead of global brightness the duo uses this to store the + // startup mode ID. The duo doesn't offer a global brightness option + // unserialize the global brightness + if (!saveHeader.unserialize8(&m_globalFlags)) { + return false; + } + // unserialize the global brightness + uint8_t brightness = 0; + if (!saveHeader.unserialize8(&brightness)) { + return false; + } + if (brightness) { + Leds::setBrightness(brightness); + } + return true; +} + // Save all of the modes to a serial buffer bool Modes::serialize(ByteStream &modesBuffer) { diff --git a/VortexEngine/src/Modes/Modes.h b/VortexEngine/src/Modes/Modes.h index f9371d8c37..8de629da45 100644 --- a/VortexEngine/src/Modes/Modes.h +++ b/VortexEngine/src/Modes/Modes.h @@ -46,12 +46,17 @@ class Modes static bool saveToBuffer(ByteStream &saveBuffer); static bool loadFromBuffer(ByteStream &saveBuffer); - // save the header to storage + // save/load the global settings to/from storage static bool saveHeader(); + static bool loadHeader(); // full save/load to/from storage - static bool loadStorage(); static bool saveStorage(); + static bool loadStorage(); + + // save load the savefile header from storage + static bool serializeSaveHeader(ByteStream &saveBuffer); + static bool unserializeSaveHeader(ByteStream &saveBuffer); // saves all modes to a buffer static bool serialize(ByteStream &buffer); @@ -209,10 +214,6 @@ class Modes ModeLink *m_prev; }; - // save load the savefile header from storage - static bool serializeSaveHeader(ByteStream &saveBuffer); - static bool unserializeSaveHeader(ByteStream &saveBuffer); - // fetch a link from the chain by index static ModeLink *getModeLink(uint32_t index);