Skip to content

Commit

Permalink
Merge branch 'master' into orbit
Browse files Browse the repository at this point in the history
  • Loading branch information
Unreal-Dan committed Apr 27, 2024
2 parents 040b482 + 6e57d56 commit c14ed77
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
5 changes: 3 additions & 2 deletions VortexEngine/VortexLib/VortexLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,9 @@ bool Vortex::init(VortexCallbacks *callbacks)

// init the engine
VortexEngine::init();
// clear the modes
//Modes::clearModes();
// load the modes
// TODO: don't load modes here? separate api?
Modes::load();
// save and set undo buffer
doSave();

Expand Down
60 changes: 40 additions & 20 deletions VortexEngine/src/Modes/Modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "../Log/Log.h"

// static members
bool Modes::m_loaded = false;
uint8_t Modes::m_curMode = 0;
uint8_t Modes::m_numModes = 0;
// the current instantiated mode and it's respective link
Expand All @@ -34,15 +35,10 @@ bool Modes::init()
test();
return true;
#endif
// try to load the saved settings or set defaults
if (!loadStorage()) {
if (!setDefaults()) {
return false;
}
if (!saveStorage()) {
return false;
}
}
ByteStream headerBuffer;
Storage::read(0, headerBuffer);
unserializeSaveHeader(headerBuffer);
m_loaded = false;
#ifdef VORTEX_LIB
// enable the adv menus by default in vortex lib
m_globalFlags |= MODES_FLAG_ADV_MENUS;
Expand All @@ -55,6 +51,24 @@ void Modes::cleanup()
clearModes();
}

bool Modes::load()
{
if (m_loaded) {
return true;
}
// try to load the saved settings or set defaults
if (!loadStorage()) {
if (!setDefaults()) {
return false;
}
if (!saveStorage()) {
return false;
}
}
m_loaded = true;
return true;
}

void Modes::play()
{
if (!m_numModes) {
Expand Down Expand Up @@ -214,6 +228,22 @@ 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;
}

// NOTE: Flash storage is limited to about 10,000 writes so
// use this function sparingly!
bool Modes::saveStorage()
Expand Down Expand Up @@ -684,17 +714,7 @@ bool Modes::setFlag(uint8_t flag, bool enable, bool save)
m_globalFlags &= ~flag;
}
DEBUG_LOGF("Toggled instant on/off to %s", enable ? "on" : "off");
return !save || saveStorage();
}

bool Modes::getFlag(uint8_t flag)
{
return ((m_globalFlags & flag) != 0);
}

void Modes::resetFlags()
{
m_globalFlags = 0;
return !save || saveHeader();
}

#ifdef VORTEX_LIB
Expand Down
21 changes: 16 additions & 5 deletions VortexEngine/src/Modes/Modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ class Modes
static bool init();
static void cleanup();

// load modes so they are ready to play
static bool load();

// play the current mode
static void play();

// full save/load to/from buffer
static bool saveToBuffer(ByteStream &saveBuffer);
static bool loadFromBuffer(ByteStream &saveBuffer);

// save the header to storage
static bool saveHeader();

// full save/load to/from storage
static bool loadStorage();
static bool saveStorage();
Expand Down Expand Up @@ -107,9 +113,10 @@ class Modes

// set or get flags
static bool setFlag(uint8_t flag, bool enable, bool save = true);
static bool getFlag(uint8_t flag);
static bool getFlag(uint8_t flag) { return ((m_globalFlags & flag) != 0); }

// reset flags to factory default (must save after)
static void resetFlags();
static void resetFlags() { m_globalFlags = 0; }

// inline functions to toggle the various flags
static bool setOneClickMode(bool enable, bool save = true) {
Expand Down Expand Up @@ -154,9 +161,6 @@ class Modes
#endif

private:
static bool serializeSaveHeader(ByteStream &saveBuffer);
static bool unserializeSaveHeader(ByteStream &saveBuffer);

// linked list of internal mode storage
class ModeLink {
public:
Expand Down Expand Up @@ -204,6 +208,10 @@ 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);

Expand All @@ -212,6 +220,9 @@ class Modes
static Mode *initCurMode(bool force = false);
static bool saveCurMode();

// whether modes have been loaded
static bool m_loaded;

// the current mode we're on
static uint8_t m_curMode;

Expand Down
2 changes: 1 addition & 1 deletion VortexEngine/src/Serial/ByteStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ uint32_t ByteStream::recalcCRC(bool force)
void ByteStream::sanity()
{
// to ensure size never exceeds the buffer capacity
if (m_pData->size > m_capacity) {
if (m_pData && m_pData->size > m_capacity) {
m_pData->size = m_capacity;
}
}
Expand Down
6 changes: 6 additions & 0 deletions VortexEngine/src/VortexEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ void VortexEngine::runMainLogic()
// the current tick
uint32_t now = Time::getCurtime();

// load modes if necessary
if (!Modes::load()) {
// don't do anything if modes couldn't load
return;
}

// check if the device has been plugged in
SerialComs::checkSerial();

Expand Down

0 comments on commit c14ed77

Please sign in to comment.