Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Hollow Knight: saves also kept in hollowknight_saves.py (back after a reinstall); Home saves before quitting, as Quit to Menu

Mason Chen8bfa3636cfb3
diff --git a/games/hollowknight/src/game.h b/games/hollowknight/src/game.h
index c55579a2..50dfdcca 100644
--- a/games/hollowknight/src/game.h
+++ b/games/hollowknight/src/game.h
@@ -113,6 +113,7 @@ typedef struct {
} SaveStats;
int save_stats(int slot, SaveStats *st); /* -> 0 no game, 1 a game, -1 a file that does not check out */
bool save_clear(int slot);
+void save_restore(void); /* (the saves gone: from their copy, hollowknight_saves.py) */
void save_set_respawn(const char *marker, bool facing_right); /* (Bench Control's Rest Burst) */
void save_set_respawn_kind(const char *marker, bool facing_right, int type); /* (respawnType: 0 face down) */
diff --git a/games/hollowknight/src/hk.h b/games/hollowknight/src/hk.h
index 4aec06c3..efbe64fb 100644
--- a/games/hollowknight/src/hk.h
+++ b/games/hollowknight/src/hk.h
@@ -34,6 +34,8 @@ void plat_fill(int x, int y, int w, int h, uint16_t c);
bool plat_save(const char *name, const void *data, uint32_t len);
const uint8_t *plat_load(const char *name, uint32_t *len);
bool plat_remove(const char *name);
+uint8_t *plat_reserve(const char *name, uint32_t len); /* a file len long to fill in place (NULL: no room) */
+void plat_reserved(const char *name, uint32_t len); /* (then: it is written) */
/* ---------------------------------------------------------------- data.bin */
extern const uint8_t *hk_bin;
diff --git a/games/hollowknight/src/main.c b/games/hollowknight/src/main.c
index 91a9cc7f..5bca997f 100644
--- a/games/hollowknight/src/main.c
+++ b/games/hollowknight/src/main.c
@@ -53,7 +53,12 @@ int main(void) {
uint32_t last = plat_millis(), acc = 0;
for (;;) {
uint32_t keys = plat_keys();
- if ((keys & K_HOME) || menu_quit()) break;
+ if (keys & K_HOME) {
+ /* (as Quit to Menu: saved, when the pause menu could be opened) */
+ if (menu_in_game() && !g_pd.disable_pause && !game_changing_room()) save_game();
+ break;
+ }
+ if (menu_quit()) break;
uint32_t now = plat_millis();
acc += now - last, last = now;
int ticks = 0;
diff --git a/games/hollowknight/src/menu.c b/games/hollowknight/src/menu.c
index c21e5d4a..af63a60e 100644
--- a/games/hollowknight/src/menu.c
+++ b/games/hollowknight/src/menu.c
@@ -70,6 +70,7 @@ static void go(int screen, int sel) {
void menu_start(void) {
memset(&mn, 0, sizeof mn);
+ save_restore(); /* (the saves the app's install took away: from their copy) */
go(MS_TITLE, 0);
}
diff --git a/games/hollowknight/src/plat_eadk.c b/games/hollowknight/src/plat_eadk.c
index 42f10f2c..fbed1314 100644
--- a/games/hollowknight/src/plat_eadk.c
+++ b/games/hollowknight/src/plat_eadk.c
@@ -37,5 +37,10 @@ void plat_fill(int x, int y, int w, int h, uint16_t c) {
bool plat_save(const char *name, const void *data, uint32_t len) { return ef_write(name, data, len); }
const uint8_t *plat_load(const char *name, uint32_t *len) { return ef_read(name, len); }
bool plat_remove(const char *name) { return ef_remove(name); }
+uint8_t *plat_reserve(const char *name, uint32_t len) {
+ uint32_t n;
+ return ef_write(name, NULL, len) ? (uint8_t *)(uintptr_t)ef_read(name, &n) : NULL; /* (the storage is RAM) */
+}
+void plat_reserved(const char *name, uint32_t len) { (void)name, (void)len; }
void plat_begin(void) { np_app_begin(); }
int plat_end(void) { return np_app_end(); }
diff --git a/games/hollowknight/src/plat_host.c b/games/hollowknight/src/plat_host.c
index 200ffcaa..f404f8a6 100644
--- a/games/hollowknight/src/plat_host.c
+++ b/games/hollowknight/src/plat_host.c
@@ -41,6 +41,9 @@ const uint8_t *plat_load(const char *name, uint32_t *len) {
fclose(f);
return loaded;
}
+static uint8_t reserved[65536];
+uint8_t *plat_reserve(const char *name, uint32_t len) { return len <= sizeof reserved ? reserved : NULL; }
+void plat_reserved(const char *name, uint32_t len) { plat_save(name, reserved, len); }
bool plat_remove(const char *name) {
char p[512];
snprintf(p, sizeof p, "%s/%s", save_dir, name);
diff --git a/games/hollowknight/src/save.c b/games/hollowknight/src/save.c
index 9231bc64..6c4c6052 100644
--- a/games/hollowknight/src/save.c
+++ b/games/hollowknight/src/save.c
@@ -35,6 +35,8 @@ static const uint8_t *phash(uint32_t *n) {
return s + 4;
}
+static void save_copy(void);
+
bool save_game(void) {
/* (built in the decoders' scratch memory: no decoding goes on meanwhile) */
uint8_t *buf = (uint8_t *)g_scratch;
@@ -59,15 +61,13 @@ bool save_game(void) {
memcpy(buf, &h, sizeof h);
char name[8];
slot_name(cur_slot, name);
- return plat_save(name, buf, at);
+ if (!plat_save(name, buf, at)) return false;
+ save_copy();
+ return true;
}
-/* a slot's file, checked: its PlayerData part (*pd_size long: its version's), its states' names */
-static const uint8_t *checked(int slot, uint32_t *count, uint32_t *pd_size) {
- char name[8];
- slot_name(slot, name);
- uint32_t len;
- const uint8_t *p = plat_load(name, &len);
+/* a save's bytes, checked: its PlayerData part (*pd_size long: its version's), its states' names */
+static const uint8_t *whole(const uint8_t *p, uint32_t len, uint32_t *count, uint32_t *pd_size) {
SaveHead h;
if (!p || len < sizeof h) return NULL;
memcpy(&h, p, sizeof h);
@@ -81,6 +81,99 @@ static const uint8_t *checked(int slot, uint32_t *count, uint32_t *pd_size) {
return p + sizeof h;
}
+/* a slot's file, checked */
+static const uint8_t *checked(int slot, uint32_t *count, uint32_t *pd_size) {
+ char name[8];
+ slot_name(slot, name);
+ uint32_t len;
+ const uint8_t *p = plat_load(name, &len);
+ return whole(p, len, count, pd_size);
+}
+
+/* The copy: installing apps from the NumWorks website keeps only Python scripts, so the saves are also kept in one,
+ * a comment line "#>hk1.sav:base64" a slot (the way NumPlay keeps its games' saves), and come back from it when a
+ * save itself is gone. */
+#define COPY_NAME "hollowknight_saves.py"
+static const char copy_head[] =
+ "# Hollow Knight keeps a copy of your saves here, so that\n"
+ "# reinstalling it doesn't erase them. If you delete this file,\n"
+ "# Hollow Knight writes it again: your saves stay either way.\n";
+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+static void save_copy(void) {
+ /* (its size first, then written in place: the files read again, as writing it may have moved them) */
+ uint32_t total = 1 + (sizeof copy_head - 1) + 1, len, count, pd;
+ char name[8];
+ for (int s = 0; s < SAVE_SLOTS; s++) {
+ slot_name(s, name);
+ if (whole(plat_load(name, &len), len, &count, &pd)) total += 2 + 7 + 1 + (len + 2) / 3 * 4 + 1;
+ }
+ uint8_t *o = plat_reserve(COPY_NAME, total), *p = o;
+ if (!o) return;
+ *p++ = 0; /* (the script's status byte: not imported) */
+ memcpy(p, copy_head, sizeof copy_head - 1), p += sizeof copy_head - 1;
+ for (int s = 0; s < SAVE_SLOTS; s++) {
+ slot_name(s, name);
+ const uint8_t *d = plat_load(name, &len);
+ if (!whole(d, len, &count, &pd)) continue;
+ *p++ = '#', *p++ = '>';
+ memcpy(p, name, 7), p += 7;
+ *p++ = ':';
+ for (uint32_t i = 0; i < len; i += 3, p += 4) {
+ /* (written out: GCC 15.2 for the calculator got a loop over the four wrong, see NumPlay's
+ * launcher/src/storage.c) */
+ uint32_t left = len - i, v = (uint32_t)d[i] << 16 | (left > 1 ? d[i + 1] << 8 : 0) | (left > 2 ? d[i + 2] : 0);
+ p[0] = (uint8_t)b64[v >> 18 & 63];
+ p[1] = (uint8_t)b64[v >> 12 & 63];
+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';
+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';
+ }
+ *p++ = '\n';
+ }
+ *p++ = 0;
+ plat_reserved(COPY_NAME, (uint32_t)(p - o));
+}
+
+static int b64_value(uint8_t c) {
+ for (int i = 0; i < 64; i++)
+ if ((uint8_t)b64[i] == c) return i;
+ return -1;
+}
+
+/* the saves from the copy, those gone or broken (after the app was installed again) */
+void save_restore(void) {
+ for (int s = 0; s < SAVE_SLOTS; s++) {
+ uint32_t n, count, pd;
+ if (checked(s, &count, &pd)) continue;
+ char tag[11] = "#>hk1.sav:";
+ tag[4] = (char)('1' + s);
+ const uint8_t *c = plat_load(COPY_NAME, &n), *l = NULL;
+ if (!c) return;
+ for (uint32_t i = 0; i + 10 <= n && !l; i++)
+ if (!memcmp(c + i, tag, 10)) l = c + i + 10;
+ if (!l) continue;
+ uint32_t len = 0, o = 0;
+ while (l + len < c + n && l[len] != '\n' && l[len]) len++;
+ if (!len || len % 4) continue;
+ uint32_t size = len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=');
+ uint8_t *out = (uint8_t *)g_scratch;
+ if (size > sizeof g_scratch) continue;
+ bool ok = true;
+ for (uint32_t i = 0; i < len && ok; i += 4) {
+ int v0 = l[i] == '=' ? 0 : b64_value(l[i]), v1 = l[i + 1] == '=' ? 0 : b64_value(l[i + 1]);
+ int v2 = l[i + 2] == '=' ? 0 : b64_value(l[i + 2]), v3 = l[i + 3] == '=' ? 0 : b64_value(l[i + 3]);
+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) ok = false;
+ uint32_t w = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);
+ if (o < size) out[o++] = (uint8_t)(w >> 16);
+ if (o < size) out[o++] = (uint8_t)(w >> 8);
+ if (o < size) out[o++] = (uint8_t)w;
+ }
+ char name[8];
+ slot_name(s, name);
+ if (ok && o == size && whole(out, size, &count, &pd)) plat_save(name, out, size);
+ }
+}
+
int save_stats(int slot, SaveStats *st) {
char name[8];
slot_name(slot, name);
@@ -100,7 +193,9 @@ int save_stats(int slot, SaveStats *st) {
bool save_clear(int slot) {
char name[8];
slot_name(slot, name);
- return plat_remove(name);
+ bool ok = plat_remove(name);
+ save_copy();
+ return ok;
}
bool save_exists(int slot) {