From bb151caee7cebb2c3f2ad58156e56e5286beaf8c Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Sat, 6 Dec 2025 22:04:59 +1100 Subject: [PATCH] Version section bin files --- lib/Epub/Epub/Section.cpp | 38 ++++++++++++++++++++------------ lib/Epub/Epub/Section.h | 5 +++-- src/CrossPointState.cpp | 6 ++--- src/screens/EpubReaderScreen.cpp | 2 +- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 902127a..aa9ad95 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -7,6 +7,9 @@ #include "EpubHtmlParserSlim.h" #include "Page.h" +#include "Serialization.h" + +constexpr uint8_t SECTION_FILE_VERSION = 2; void Section::onPageComplete(const Page* page) { Serial.printf("Page %d complete - free mem: %lu\n", pageCount, ESP.getFreeHeap()); @@ -21,7 +24,14 @@ void Section::onPageComplete(const Page* page) { delete page; } -bool Section::hasCache() { +void Section::writeCacheMetadata() const { + std::ofstream outputFile(("/sd" + cachePath + "/section.bin").c_str()); + serialization::writePod(outputFile, SECTION_FILE_VERSION); + serialization::writePod(outputFile, pageCount); + outputFile.close(); +} + +bool Section::loadCacheMetadata() { if (!SD.exists(cachePath.c_str())) { return false; } @@ -31,14 +41,18 @@ bool Section::hasCache() { return false; } - File sectionFile = SD.open(sectionFilePath.c_str(), FILE_READ); - uint8_t pageCountBytes[2] = {0, 0}; - sectionFile.read(pageCountBytes, 2); - sectionFile.close(); - - pageCount = pageCountBytes[0] + (pageCountBytes[1] << 8); + std::ifstream inputFile(("/sd" + sectionFilePath).c_str()); + uint8_t version; + serialization::readPod(inputFile, version); + if (version != SECTION_FILE_VERSION) { + inputFile.close(); + SD.remove(sectionFilePath.c_str()); + Serial.printf("Section state file: Unknown version %u\n", version); + return false; + } + serialization::readPod(inputFile, pageCount); + inputFile.close(); Serial.printf("Loaded cache: %d pages\n", pageCount); - return true; } @@ -86,16 +100,12 @@ bool Section::persistPageDataToSD() { return false; } - File sectionFile = SD.open((cachePath + "/section.bin").c_str(), FILE_WRITE, true); - const uint8_t pageCountBytes[2] = {static_cast(pageCount & 0xFF), - static_cast((pageCount >> 8) & 0xFF)}; - sectionFile.write(pageCountBytes, 2); - sectionFile.close(); + writeCacheMetadata(); return true; } -void Section::renderPage() { +void Section::renderPage() const { if (0 <= currentPage && currentPage < pageCount) { const auto filePath = "/sd" + cachePath + "/page_" + std::to_string(currentPage) + ".bin"; std::ifstream inputFile(filePath); diff --git a/lib/Epub/Epub/Section.h b/lib/Epub/Epub/Section.h index 4a8d2e1..86acebe 100644 --- a/lib/Epub/Epub/Section.h +++ b/lib/Epub/Epub/Section.h @@ -21,9 +21,10 @@ class Section { cachePath = epub->getCachePath() + "/" + std::to_string(spineIndex); } ~Section() = default; - bool hasCache(); + void writeCacheMetadata() const; + bool loadCacheMetadata(); void setupCacheDir() const; void clearCache() const; bool persistPageDataToSD(); - void renderPage(); + void renderPage() const; }; diff --git a/src/CrossPointState.cpp b/src/CrossPointState.cpp index 7564aac..bb76603 100644 --- a/src/CrossPointState.cpp +++ b/src/CrossPointState.cpp @@ -6,12 +6,12 @@ #include -constexpr uint8_t STATE_VERSION = 1; +constexpr uint8_t STATE_FILE_VERSION = 1; constexpr char STATE_FILE[] = "/sd/.crosspoint/state.bin"; bool CrossPointState::saveToFile() const { std::ofstream outputFile(STATE_FILE); - serialization::writePod(outputFile, STATE_VERSION); + serialization::writePod(outputFile, STATE_FILE_VERSION); serialization::writeString(outputFile, openEpubPath); outputFile.close(); return true; @@ -22,7 +22,7 @@ bool CrossPointState::loadFromFile() { uint8_t version; serialization::readPod(inputFile, version); - if (version != STATE_VERSION) { + if (version != STATE_FILE_VERSION) { Serial.printf("CrossPointState: Unknown version %u\n", version); inputFile.close(); return false; diff --git a/src/screens/EpubReaderScreen.cpp b/src/screens/EpubReaderScreen.cpp index 374b468..6452df8 100644 --- a/src/screens/EpubReaderScreen.cpp +++ b/src/screens/EpubReaderScreen.cpp @@ -148,7 +148,7 @@ void EpubReaderScreen::renderPage() { const auto filepath = epub->getSpineItem(currentSpineIndex); Serial.printf("Loading file: %s, index: %d\n", filepath.c_str(), currentSpineIndex); section = new Section(epub, currentSpineIndex, renderer); - if (!section->hasCache()) { + if (!section->loadCacheMetadata()) { Serial.println("Cache not found, building..."); {