Game Storage
Source: src/game_storage.c · 103 lines · 4 functions.
Module entry points
These functions are exposed to the rest of the project. Open one to follow its decisions and the helpers it calls.
How to read these diagrams
Each function has its own expandable diagram using the actual expressions and statements from this file. Decisions split into True and False; loop bodies are indented. Returns end the function immediately; break and continue follow their C meanings. Arrows between statements show their order when execution reaches them.
These are structural source diagrams before preprocessing, not runtime traces. Conditional compilation and macros remain source expressions. Ternary choices remain inside their statement. Calls are linked separately; callback execution and task scheduling are not inferred.
Functions in this file
game_storage_initialize() · source line 9
bool game_storage_initialize(void)
- Decision
readyTrue- Exit this function
return true;
FalseContinue after this decision.
- Execute
const esp_err_t result = nvs_flash_init(); - Execute
ready = result == ESP_OK; - Decision
!readyTrue- Execute
ESP_LOGE(TAG, "NVS unavailable: %s; stored data left intact", esp_err_to_name(result));
FalseContinue after this decision.
- Exit this function
return ready;
Read original function source
bool game_storage_initialize(void)
{
if (ready) {
return true;
}
const esp_err_t result = nvs_flash_init();
ready = result == ESP_OK;
if (!ready) {
// Never erase existing settings as an automatic recovery action.
ESP_LOGE(TAG, "NVS unavailable: %s; stored data left intact",
esp_err_to_name(result));
}
return ready;
}game_storage_read_sized() · source line 24
bool game_storage_read_sized(const char *name_space, const char *key, void *record,
size_t capacity, size_t *record_size, bool *found)- Decision
found == NULL || record_size == NULLTrue- Exit this function
return false;
FalseContinue after this decision.
- Execute
*found = false; - Execute
*record_size = 0; - Decision
!ready || record == NULL || capacity == 0True- Exit this function
return false;
FalseContinue after this decision.
- Execute
nvs_handle_t handle; - Execute
esp_err_t result = nvs_open(name_space, NVS_READONLY, &handle); - Decision
result == ESP_ERR_NVS_NOT_FOUNDTrue- Exit this function
return true;
FalseContinue after this decision.
- Decision
result != ESP_OKTrue- Execute
ESP_LOGE(TAG, "Cannot open %s: %s", name_space, esp_err_to_name(result)); - Exit this function
return false;
FalseContinue after this decision.
- Execute
size_t length = 0; - Execute
result = nvs_get_blob(handle, key, NULL, &length); - Decision
result == ESP_ERR_NVS_NOT_FOUNDTrue- Execute
nvs_close(handle); - Exit this function
return true;
FalseContinue after this decision.
- Decision
result == ESP_OK && length <= capacityTrue- Execute
result = nvs_get_blob(handle, key, record, &length); - Execute
*found = result == ESP_OK; - Decision
*foundTrue- Execute
*record_size = length;
FalseContinue after this decision.
False- Decision
result == ESP_OKTrue- Execute
result = ESP_ERR_NVS_INVALID_LENGTH;
FalseContinue after this decision.
- Execute
nvs_close(handle); - Decision
result != ESP_OKTrue- Execute
ESP_LOGE(TAG, "Cannot read %s/%s: %s", name_space, key, esp_err_to_name(result));
FalseContinue after this decision.
- Exit this function
return result == ESP_OK;
Read original function source
bool game_storage_read_sized(const char *name_space, const char *key, void *record,
size_t capacity, size_t *record_size, bool *found)
{
if (found == NULL || record_size == NULL) {
return false;
}
*found = false;
*record_size = 0;
if (!ready || record == NULL || capacity == 0) {
return false;
}
nvs_handle_t handle;
esp_err_t result = nvs_open(name_space, NVS_READONLY, &handle);
if (result == ESP_ERR_NVS_NOT_FOUND) {
return true;
}
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot open %s: %s", name_space, esp_err_to_name(result));
return false;
}
size_t length = 0;
result = nvs_get_blob(handle, key, NULL, &length);
if (result == ESP_ERR_NVS_NOT_FOUND) {
nvs_close(handle);
return true;
}
if (result == ESP_OK && length <= capacity) {
result = nvs_get_blob(handle, key, record, &length);
*found = result == ESP_OK;
if (*found) {
*record_size = length;
}
} else if (result == ESP_OK) {
result = ESP_ERR_NVS_INVALID_LENGTH;
}
nvs_close(handle);
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot read %s/%s: %s", name_space, key, esp_err_to_name(result));
}
return result == ESP_OK;
}game_storage_read() · source line 66
bool game_storage_read(const char *name_space, const char *key, void *record,
size_t size, bool *found)Calls into this documentation:
- Execute
size_t record_size = 0; - Decision
!game_storage_read_sized(name_space, key, record, size, &record_size, found)True- Exit this function
return false;
FalseContinue after this decision.
- Decision
*found && record_size != sizeTrue- Execute
ESP_LOGE(TAG, "Cannot read %s/%s: unexpected record size %u", name_space, key, (unsigned)record_size); - Execute
*found = false; - Exit this function
return false;
FalseContinue after this decision.
- Exit this function
return true;
Read original function source
bool game_storage_read(const char *name_space, const char *key, void *record,
size_t size, bool *found)
{
size_t record_size = 0;
if (!game_storage_read_sized(name_space, key, record, size, &record_size, found)) {
return false;
}
if (*found && record_size != size) {
ESP_LOGE(TAG, "Cannot read %s/%s: unexpected record size %u", name_space, key,
(unsigned)record_size);
*found = false;
return false;
}
return true;
}game_storage_write() · source line 82
bool game_storage_write(const char *name_space, const char *key,
const void *record, size_t size)- Decision
!ready || record == NULL || size == 0True- Exit this function
return false;
FalseContinue after this decision.
- Execute
nvs_handle_t handle; - Execute
esp_err_t result = nvs_open(name_space, NVS_READWRITE, &handle); - Decision
result != ESP_OKTrue- Execute
ESP_LOGE(TAG, "Cannot open %s: %s", name_space, esp_err_to_name(result)); - Exit this function
return false;
FalseContinue after this decision.
- Execute
result = nvs_set_blob(handle, key, record, size); - Decision
result == ESP_OKTrue- Execute
result = nvs_commit(handle);
FalseContinue after this decision.
- Execute
nvs_close(handle); - Decision
result != ESP_OKTrue- Execute
ESP_LOGE(TAG, "Cannot save %s/%s: %s", name_space, key, esp_err_to_name(result));
FalseContinue after this decision.
- Exit this function
return result == ESP_OK;
Read original function source
bool game_storage_write(const char *name_space, const char *key,
const void *record, size_t size)
{
if (!ready || record == NULL || size == 0) {
return false;
}
nvs_handle_t handle;
esp_err_t result = nvs_open(name_space, NVS_READWRITE, &handle);
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot open %s: %s", name_space, esp_err_to_name(result));
return false;
}
result = nvs_set_blob(handle, key, record, size);
if (result == ESP_OK) {
result = nvs_commit(handle);
}
nvs_close(handle);
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot save %s/%s: %s", name_space, key, esp_err_to_name(result));
}
return result == ESP_OK;
}Complete original file, including constants and data
/* Coded By JD(2026) */
#error "Reference listing only — not provided as build-ready source."
#include "game_storage.h"
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
static const char *TAG = "game_storage";
static bool ready = false;
bool game_storage_initialize(void)
{
if (ready) {
return true;
}
const esp_err_t result = nvs_flash_init();
ready = result == ESP_OK;
if (!ready) {
// Never erase existing settings as an automatic recovery action.
ESP_LOGE(TAG, "NVS unavailable: %s; stored data left intact",
esp_err_to_name(result));
}
return ready;
}
bool game_storage_read_sized(const char *name_space, const char *key, void *record,
size_t capacity, size_t *record_size, bool *found)
{
if (found == NULL || record_size == NULL) {
return false;
}
*found = false;
*record_size = 0;
if (!ready || record == NULL || capacity == 0) {
return false;
}
nvs_handle_t handle;
esp_err_t result = nvs_open(name_space, NVS_READONLY, &handle);
if (result == ESP_ERR_NVS_NOT_FOUND) {
return true;
}
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot open %s: %s", name_space, esp_err_to_name(result));
return false;
}
size_t length = 0;
result = nvs_get_blob(handle, key, NULL, &length);
if (result == ESP_ERR_NVS_NOT_FOUND) {
nvs_close(handle);
return true;
}
if (result == ESP_OK && length <= capacity) {
result = nvs_get_blob(handle, key, record, &length);
*found = result == ESP_OK;
if (*found) {
*record_size = length;
}
} else if (result == ESP_OK) {
result = ESP_ERR_NVS_INVALID_LENGTH;
}
nvs_close(handle);
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot read %s/%s: %s", name_space, key, esp_err_to_name(result));
}
return result == ESP_OK;
}
bool game_storage_read(const char *name_space, const char *key, void *record,
size_t size, bool *found)
{
size_t record_size = 0;
if (!game_storage_read_sized(name_space, key, record, size, &record_size, found)) {
return false;
}
if (*found && record_size != size) {
ESP_LOGE(TAG, "Cannot read %s/%s: unexpected record size %u", name_space, key,
(unsigned)record_size);
*found = false;
return false;
}
return true;
}
bool game_storage_write(const char *name_space, const char *key,
const void *record, size_t size)
{
if (!ready || record == NULL || size == 0) {
return false;
}
nvs_handle_t handle;
esp_err_t result = nvs_open(name_space, NVS_READWRITE, &handle);
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot open %s: %s", name_space, esp_err_to_name(result));
return false;
}
result = nvs_set_blob(handle, key, record, size);
if (result == ESP_OK) {
result = nvs_commit(handle);
}
nvs_close(handle);
if (result != ESP_OK) {
ESP_LOGE(TAG, "Cannot save %s/%s: %s", name_space, key, esp_err_to_name(result));
}
return result == ESP_OK;
}