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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
NumBlocks: OK mines, Back places, as the mouse buttons; numblocks_saves.py
Mason Chen5f3a7009383f
diff --git a/docs/play.md b/docs/play.mdindex 01e61da6..42de2f62 100644--- a/docs/play.md+++ b/docs/play.md@@ -109,8 +109,8 @@ Shooting yourself with a blank keeps your turn.| **shift** | Jump (twice in Creative: fly) || **alpha** | Sneak || **⌫** | Sprint (or press forward twice) |-| **Back** | Mine and attack (hold it to mine) |-| **OK** or **EXE** | Place a block, use, eat |+| **OK** or **EXE** | Mine and attack, like the left mouse button (hold it to mine) |+| **Back** | Place a block, use, eat, like the right mouse button || **var** | Inventory (in Creative: every block and item) || **1** to **9** | Pick a hotbar slot || **x,n,t** | Drop the item |@@ -119,7 +119,7 @@ Shooting yourself with a blank keeps your turn.| **ans** | Pause || **Home** | Save and quit |-The left thumb looks with the arrows (shift and alpha are just under them), the right one walks. The first time you open NumBlocks it shows these keys. You can change them in Options, then Controls, which also shows the key sheet again. In NumBlocks, Back mines, so **ans** pauses. In the inventory, the arrows move between slots, **OK** picks up or puts down a stack, **EXE** one item, and **shift** with **OK** sends a stack to the other side. To spread a stack, hold **OK** and move over the slots: it splits evenly when you let go (hold **EXE** instead to drop one in each). In Creative, the Search Items tab finds blocks as you type, and **shift** with up or down jumps to the top or bottom of a tab. Commands like /gamemode and /give work in worlds with Allow Cheats on (or after Open to LAN in the pause menu); **toolbox** finishes a word as you type it. Your world saves as you play and when you quit.+The left thumb looks with the arrows (shift and alpha are just under them), the right one walks. The first time you open NumBlocks it shows these keys. You can change them in Options, then Controls, which also shows the key sheet again. In NumBlocks, Back places blocks, so **ans** pauses. In the inventory, the arrows move between slots, **OK** picks up or puts down a stack, **EXE** one item, and **shift** with **OK** sends a stack to the other side. To spread a stack, hold **OK** and move over the slots: it splits evenly when you let go (hold **EXE** instead to drop one in each). In Creative, the Search Items tab finds blocks as you type, and **shift** with up or down jumps to the top or bottom of a tab. Commands like /gamemode and /give work in worlds with Allow Cheats on (or after Open to LAN in the pause menu); **toolbox** finishes a word as you type it. Your world saves as you play and when you quit, and on its own NumBlocks keeps a copy in `numblocks_saves.py` (inside NumPlay, `numplay_saves.py` holds it), so installing again doesn't erase your worlds.<br clear="right">diff --git a/games/numblocks/README.md b/games/numblocks/README.mdindex 385f6e7e..f21d8c7a 100644--- a/games/numblocks/README.md+++ b/games/numblocks/README.md@@ -13,7 +13,7 @@ Mine, craft and survive on your NumWorks calculator, in a world like Minecraft 1- **Fire:** light it with flint and steel. It spreads and burns wood, leaves and wool, like in 1.8.- **Creative mode:** fly, every block and item, break things at once, and search for any block.- **Commands:** /gamemode, /give, /time, /weather, /tp and more, with cheats on (Allow Cheats, or Open to LAN).-- **Your worlds:** keep several, give them names, and they save as you play and when you quit.+- **Your worlds:** keep several, give them names, and they save as you play and when you quit. A copy also goes into `numblocks_saves.py`, so installing the game again doesn't erase them.## Controls@@ -24,8 +24,8 @@ Mine, craft and survive on your NumWorks calculator, in a world like Minecraft 1| shift | Jump (twice in Creative: fly) || alpha | Sneak || ⌫ | Sprint (or press forward twice) |-| Back | Mine, attack |-| OK | Place, use, eat |+| OK | Mine, attack (like the left mouse button) |+| Back | Place, use, eat (like the right mouse button) || var | Inventory || 1 to 9 | Pick a hotbar slot || x,n,t | Drop the item |diff --git a/games/numblocks/src/gui.c b/games/numblocks/src/gui.cindex b37d0918..447547b2 100644--- a/games/numblocks/src/gui.c+++ b/games/numblocks/src/gui.c@@ -1464,8 +1464,8 @@ static void arrow_pad(int cx, int cy, int r) {typedef struct { uint32_t bit; uint8_t key; const char *name, *what; } Action;/* (what: the key sheet's word for it; key: where it starts) */static const Action actions[N_ACTIONS] = {- {K_ATTACK, 5, "Attack/Destroy", "Mine, attack"}, {0, 20, "Pick Block", "Pick block"},- {K_USE, 4, "Use Item/Place Block", "Place, use"}, {K_DROP, 14, "Drop Item", "Drop"},+ {K_ATTACK, 4, "Attack/Destroy", "Mine, attack"}, {0, 20, "Pick Block", "Pick block"},+ {K_USE, 5, "Use Item/Place Block", "Place, use"}, {K_DROP, 14, "Drop Item", "Drop"},{K_SLOT1, 42, "Hotbar Slot 1", "Slot 1"}, {K_SLOT1 << 1, 43, "Hotbar Slot 2", "Slot 2"},{K_SLOT1 << 2, 44, "Hotbar Slot 3", "Slot 3"}, {K_SLOT1 << 3, 36, "Hotbar Slot 4", "Slot 4"},{K_SLOT1 << 4, 37, "Hotbar Slot 5", "Slot 5"}, {K_SLOT1 << 5, 38, "Hotbar Slot 6", "Slot 6"},@@ -1482,6 +1482,11 @@ static const uint8_t category_at[5] = {A_ATTACK, A_DROP, A_PAUSE, A_JUMP, A_CHATvoid keys_reset(void) {for (int a = 0; a < N_ACTIONS; a++) opt.keys[a] = actions[a].key;}+/* Saved with the keys as they were first (Back mining, OK placing): as they are now, OK the left mouse+ * button and Back the right; keys someone set stay as they set them */+void keys_update(int seen) {+ if (seen < 4 && opt.keys[A_ATTACK] == 5 && opt.keys[A_USE] == 4) opt.keys[A_ATTACK] = 4, opt.keys[A_USE] = 5;+}/* the keys that can be set: not the arrows (they look), Home (it leaves), On/Off or EXE (it does what OK does) */static bool settable(int k) { return k == 4 || k == 5 || (k >= 12 && k <= 51 && k != 35 && k != 41 && k != 47); }uint32_t keys_of(uint64_t raw) {diff --git a/games/numblocks/src/main.c b/games/numblocks/src/main.cindex f058faf2..063f916d 100644--- a/games/numblocks/src/main.c+++ b/games/numblocks/src/main.c@@ -88,6 +88,9 @@ static int64_t parse_seed(void) {}void game_init(void) {+ /* just installed again? the saves come back from their copy; and the copy is made if there is none */+ copy_restore();+ copy_write();load_options();#ifdef BENCH/* (a build for timing: straight into a new world of seed BENCH) */diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.hindex ccf29ab6..8defa760 100644--- a/games/numblocks/src/nb.h+++ b/games/numblocks/src/nb.h@@ -43,6 +43,10 @@ bool plat_save(const char *name, const void *data, uint32_t len);const uint8_t *plat_load(const char *name, uint32_t *len); /* unaligned, valid until storage changes */void plat_remove_prefix(const char *prefix);uint32_t plat_storage_free(void);+bool plat_record(int i, char *name, int max); /* the i-th record's name; false past the last */+uint8_t *plat_save_open(const char *name, uint32_t len); /* a record of len bytes to fill (NULL: no room, nothing changed) */+void plat_save_close(const char *name); /* (done filling it) */+bool plat_in_launcher(void); /* running inside NumPlay (which keeps its own copy of the saves) */void plat_begin(void);int plat_end(void);@@ -285,7 +289,8 @@ enum { A_ATTACK, A_PICK, A_USE, A_DROP, A_SLOT1, A_INVENTORY = A_SLOT1 + 9, A_PAA_LEFT, A_RIGHT, A_BACK, A_FORWARD, A_CHAT, A_COMMAND, N_ACTIONS };uint32_t keys_of(uint64_t raw); /* the K_ bits of the keys held (the calculator's keys, as bound) */void keys_reset(void); /* the keys as they start */-#define KEY_SHEET 3 /* (2: jump and sneak on shift and alpha; 3: chat and commands) */+void keys_update(int seen); /* keys saved by an older version (seen: its key sheet) brought up to date */+#define KEY_SHEET 4 /* (2: jump and sneak on shift and alpha; 3: chat and commands; 4: OK mines, Back places) */extern Options opt;extern int64_t world_seed;enum { WT_DEFAULT, WT_FLAT };@@ -311,6 +316,8 @@ bool rename_world(int slot, const char *name);void delete_world(int slot);void load_options(void);void save_options(void);+void copy_restore(void); /* the saves from numblocks_saves.py, when none are left (installed again) */+void copy_write(void); /* and the copy, up to date (not inside NumPlay, which keeps its own) *//* the world's settings, saved with it: cheats (Allow Cheats; or opened to LAN with them, until it* is left) and the game rules (GameRules) */extern bool world_cheats, lan_open, lan_cheats;diff --git a/games/numblocks/src/plat_eadk.c b/games/numblocks/src/plat_eadk.cindex f8a4977d..b993d347 100644--- a/games/numblocks/src/plat_eadk.c+++ b/games/numblocks/src/plat_eadk.c@@ -17,7 +17,33 @@ void plat_push(int x, int y, int w, int h, const uint16_t *px) {void plat_pull(int x, int y, int w, int h, uint16_t *px) {eadk_display_pull_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, px);}-bool plat_save(const char *name, const void *data, uint32_t len) { return ef_write(name, data, len); }+/* (no room: the copies of the saves, only copies, make way for the saves themselves) */+bool plat_save(const char *name, const void *data, uint32_t len) {+ if (ef_write(name, data, len)) return true;+ ef_remove("numblocks_saves.py");+ ef_remove("numplay_saves.py");+ return ef_write(name, data, len);+}+uint8_t *plat_save_open(const char *name, uint32_t len) {+ uint32_t n;+ return ef_write(name, NULL, len) ? (uint8_t *)(uintptr_t)ef_read(name, &n) : NULL;+}+void plat_save_close(const char *name) { (void)name; }+bool plat_record(int i, char *name, int max) {+ ef_fs_t fs;+ if (!ef_open(&fs)) return false;+ int end = ef_end(&fs);+ for (uint32_t p = 0; end >= 0 && (int)p < end; p += ef_rd16(fs.buf + p))+ if (i-- == 0) {+ const char *n = (const char *)fs.buf + p + 2;+ int k = 0;+ for (; n[k] && k < max - 1; k++) name[k] = n[k];+ name[k] = 0;+ return true;+ }+ return false;+}+bool plat_in_launcher(void) { return numplay_launcher != NULL; }const uint8_t *plat_load(const char *name, uint32_t *len) { return ef_read(name, len); }/* removes every record whose name starts with `prefix` */diff --git a/games/numblocks/src/plat_host.c b/games/numblocks/src/plat_host.cindex 7b28dd3f..9d230354 100644--- a/games/numblocks/src/plat_host.c+++ b/games/numblocks/src/plat_host.c@@ -48,6 +48,31 @@ const uint8_t *plat_load(const char *name, uint32_t *len) {return loaded;}#include <dirent.h>+static uint8_t *open_buf;+static uint32_t open_len;+uint8_t *plat_save_open(const char *name, uint32_t len) {+ (void)name;+ free(open_buf);+ open_buf = calloc(1, len ? len : 1), open_len = len;+ return open_buf;+}+void plat_save_close(const char *name) { plat_save(name, open_buf, open_len); }+bool plat_record(int i, char *name, int max) {+ DIR *d = opendir(save_dir);+ if (!d) return false;+ struct dirent *e;+ bool found = false;+ while ((e = readdir(d)))+ if (e->d_name[0] != '.' && i-- == 0) {+ snprintf(name, (size_t)max, "%s", e->d_name);+ found = true;+ break;+ }+ closedir(d);+ return found;+}+bool host_launcher;+bool plat_in_launcher(void) { return host_launcher; }void plat_remove_prefix(const char *prefix) {DIR *d = opendir(save_dir);if (!d) return;diff --git a/games/numblocks/src/save.c b/games/numblocks/src/save.cindex d2fc9e94..99b0f717 100644--- a/games/numblocks/src/save.c+++ b/games/numblocks/src/save.c@@ -149,6 +149,7 @@ bool save_world(void) {record_of(slot, rec);ok = plat_save(rec, b, n) && ok;edits_forget();+ copy_write();return ok;}@@ -224,6 +225,7 @@ bool rename_world(int s, const char *name) {bool ok = plat_save(rec, b, n);edits_forget();if (s == slot) copy_name(cur_name, name);+ copy_write();return ok;}@@ -231,7 +233,94 @@ void delete_world(int s) {char pre[4];prefix_of(s, pre);plat_remove_prefix(pre);- if (opt.last_world == s) opt.last_world = 0, save_options();+ if (opt.last_world == s) opt.last_world = 0;+ save_options(); /* (and the copy, without that world) */+}++/* ---------------------------------------------------------------- the copy+ * Installing an app from the NumWorks website empties the calculator's files but the Python scripts.+ * So NumBlocks keeps its saves in one too, as comment lines "#>name:base64" (the way NumPlay keeps its+ * games' saves, and in NumPlay it leaves that to NumPlay), and when none of its saves are left, they+ * come back from it. */+#define COPY_REC "numblocks_saves.py"+static const char copy_head[] =+ "# NumBlocks keeps a copy of your worlds here, so that installing\n"+ "# it again doesn't erase them. If you delete this file, NumBlocks\n"+ "# writes it again: your worlds stay either way.\n";+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";+static bool ours(const char *n) { return !strcmp(n, OPTIONS_REC) || (n[0] == 'n' && n[1] == 'b' && n[2] >= '1' && n[2] <= '9'); }++void copy_write(void) {+ if (plat_in_launcher()) return;+ char name[40];+ uint32_t total = 1 + sizeof copy_head - 1 + 1, len;+ for (int i = 0; plat_record(i, name, sizeof name); i++)+ if (ours(name) && plat_load(name, &len)) total += 2 + (uint32_t)strlen(name) + 1 + (len + 2) / 3 * 4 + 1;+ uint8_t *o = plat_save_open(COPY_REC, total), *end = o + total;+ if (!o) return; /* (no room: the copy there stays) */+ *o++ = 0; /* the script's status byte: not imported by the shell */+ memcpy(o, copy_head, sizeof copy_head - 1);+ o += sizeof copy_head - 1;+ for (int i = 0; plat_record(i, name, sizeof name); i++) {+ const uint8_t *d = ours(name) ? plat_load(name, &len) : NULL;+ uint32_t nl = (uint32_t)strlen(name);+ if (!d || o + 2 + nl + 1 + (len + 2) / 3 * 4 + 1 >= end) continue;+ *o++ = '#', *o++ = '>';+ memcpy(o, name, nl);+ o += nl;+ *o++ = ':';+ for (uint32_t k = 0; k < len; k += 3, o += 4) {+ /* (written out: GCC 15.2 for the calculator got a loop over the four wrong, see launcher/src/storage.c) */+ uint32_t left = len - k, v = (uint32_t)d[k] << 16 | (left > 1 ? d[k + 1] << 8 : 0) | (left > 2 ? d[k + 2] : 0);+ o[0] = (uint8_t)b64[v >> 18 & 63];+ o[1] = (uint8_t)b64[v >> 12 & 63];+ o[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';+ o[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';+ }+ *o++ = '\n';+ }+ while (o < end) *o++ = 0;+ plat_save_close(COPY_REC);+}++static int b64_value(uint8_t c) {+ for (int i = 0; i < 64; i++)+ if ((uint8_t)b64[i] == c) return i;+ return -1;+}+void copy_restore(void) {+ char name[40];+ for (int i = 0; plat_record(i, name, sizeof name); i++)+ if (ours(name)) return; /* (saves are here: they are the newest) */+ uint32_t n, at = 1;+ const uint8_t *c = plat_load(COPY_REC, &n);+ while (c && at < n) {+ /* a line: "#>name:base64" */+ const uint8_t *l = c + at, *e = l;+ while (e < c + n && *e != '\n' && *e) e++;+ uint32_t next = (uint32_t)(e - c) + 1;+ const uint8_t *colon = l;+ while (colon < e && *colon != ':') colon++;+ uint32_t nl = (uint32_t)(colon - l - 2), len = (uint32_t)(e - colon - 1);+ if (e - l > 3 && l[0] == '#' && l[1] == '>' && colon < e && nl < sizeof name && len && !(len % 4)) {+ memcpy(name, l + 2, nl), name[nl] = 0;+ uint32_t size = len / 4 * 3 - (colon[len] == '=') - (colon[len - 1] == '='), off = (uint32_t)(colon + 1 - c);+ uint8_t *out = ours(name) ? plat_save_open(name, size) : NULL;+ c = plat_load(COPY_REC, &n); /* (the files moved about) */+ if (out && c) {+ const uint8_t *data = c + off;+ for (uint32_t i = 0, k = 0; i < len; i += 4) {+ int v[4];+ for (int q = 0; q < 4; q++) v[q] = data[i + q] == '=' ? 0 : b64_value(data[i + q]);+ uint32_t w = (uint32_t)((v[0] & 63) << 18 | (v[1] & 63) << 12 | (v[2] & 63) << 6 | (v[3] & 63));+ for (int q = 0; q < 3 && k < size; q++) out[k++] = (uint8_t)(w >> (16 - 8 * q));+ }+ plat_save_close(name);+ c = plat_load(COPY_REC, &n);+ }+ }+ at = next;+ }}void load_options(void) {@@ -240,5 +329,9 @@ void load_options(void) {const uint8_t *d = plat_load(OPTIONS_REC, &len);/* (an older, shorter record: what it has; the rest stays as it was) */if (d && len <= sizeof opt) memcpy(&opt, d, len);+ keys_update(opt.keys_seen);+}+void save_options(void) {+ plat_save(OPTIONS_REC, &opt, sizeof opt);+ copy_write();}-void save_options(void) { plat_save(OPTIONS_REC, &opt, sizeof opt); }diff --git a/games/numblocks/tests/unit.c b/games/numblocks/tests/unit.cindex ac5d3f2e..aba8ba2a 100644--- a/games/numblocks/tests/unit.c+++ b/games/numblocks/tests/unit.c@@ -513,6 +513,63 @@ int main(void) {delete_world(2);world_type = WT_DEFAULT;}+ {+ /* the copy in numblocks_saves.py: every save, and after an install (only scripts left) they come back */+ for (int s = 1; s <= MAX_WORLDS; s++) delete_world(s);+ new_world(3, 42, 0, false, WT_DEFAULT, "Kept");+ world_follow(pl.x, pl.y, pl.z);+ while (world_pending()) world_follow(pl.x, pl.y, pl.z);+ for (int k = 0; k < 40; k++) world_set((int)pl.x + k, 70, (int)pl.z + 200, B_GOLD_BLOCK); /* (another region too) */+ for (int k = 0; k < 40; k++) world_set((int)pl.x + k, 70, (int)pl.z, B_DIAMOND_BLOCK);+ save_world();+ char name[40];+ static uint8_t before[8][4096];+ static char names[8][40];+ uint32_t lens[8];+ int n = 0;+ for (int i = 0; plat_record(i, name, sizeof name) && n < 8; i++) {+ uint32_t len;+ const uint8_t *d = plat_load(name, &len);+ if (!d || (strncmp(name, "nb3", 3) && strcmp(name, "numblocks.cfg"))) continue;+ if (len > sizeof before[0]) continue;+ memcpy(before[n], d, len), lens[n] = len, strcpy(names[n], name), n++;+ }+ CHECK(n >= 3); /* options, the world, its regions */+ uint32_t cl;+ CHECK(plat_load("numblocks_saves.py", &cl) && cl > 100);+ /* the install: every save gone, the script left */+ for (int i = 0; i < n; i++) plat_remove_prefix(names[i]);+ copy_restore();+ for (int i = 0; i < n; i++) {+ uint32_t len;+ const uint8_t *d = plat_load(names[i], &len);+ CHECK(d && len == lens[i] && !memcmp(d, before[i], len));+ }+ /* inside NumPlay, NumPlay keeps the copy */+ extern bool host_launcher;+ plat_remove_prefix("numblocks_saves.py");+ host_launcher = true;+ save_world();+ CHECK(!plat_load("numblocks_saves.py", &cl));+ host_launcher = false;+ delete_world(3);+ plat_remove_prefix("numblocks_saves.py");+ }+ {+ /* keys saved as they first were (Back mining, OK placing) become OK mining, Back placing; set ones stay */+ keys_reset();+ CHECK(opt.keys[A_ATTACK] == 4 && opt.keys[A_USE] == 5);+ opt.keys[A_ATTACK] = 5, opt.keys[A_USE] = 4;+ keys_update(3);+ CHECK(opt.keys[A_ATTACK] == 4 && opt.keys[A_USE] == 5);+ opt.keys[A_ATTACK] = 12, opt.keys[A_USE] = 4;+ keys_update(3);+ CHECK(opt.keys[A_ATTACK] == 12 && opt.keys[A_USE] == 4);+ opt.keys[A_ATTACK] = 5, opt.keys[A_USE] = 4;+ keys_update(KEY_SHEET); /* (chosen with this version: kept) */+ CHECK(opt.keys[A_ATTACK] == 5 && opt.keys[A_USE] == 4);+ keys_reset();+ }printf("%s (%d recipes)\n", fails ? "FAILED" : "all good", N_RECIPES);return fails != 0;}diff --git a/tests/test_games.py b/tests/test_games.pyindex f24fcb3f..bc9e6b8b 100644--- a/tests/test_games.py+++ b/tests/test_games.py@@ -133,7 +133,7 @@ SEEDS = {# games that keep a copy of their save in a Python script (the only files the# NumWorks installer keeps): before the second session, as after installing# the app again, only the scripts are left, and the save must come back-COPIES = {"championisland": "champion_saves.py"}+COPIES = {"championisland": "champion_saves.py", "numblocks": "numblocks_saves.py"}CHECKS = {# the island remembers where Lucky was; after the reinstall, from the copy"championisland": (lambda v: v[:3] == b"CI1" and b"PLAYER_LOC" in v,) * 2,diff --git a/tests/test_numblocks_progress.py b/tests/test_numblocks_progress.pynew file mode 100644index 00000000..548c60bd--- /dev/null+++ b/tests/test_numblocks_progress.py@@ -0,0 +1,90 @@+#!/usr/bin/env python3+"""Checks that NumBlocks on its own keeps its worlds through an install.++Installing an app from the NumWorks website empties the calculator's files but+the Python scripts, so NumBlocks keeps a copy of its saves in+numblocks_saves.py. In the ARM emulator:++1. a new world, a block mined, then Home: the world, its changed blocks and the+ options are saved, and the copy holds each of them, byte for byte, in a+ valid Python script;+2. "install again" (every file but the scripts deleted): started again, the+ saves come back byte for byte and the other script is untouched.++Usage: test_numblocks_progress.py build/apps/NumBlocks.nwa [--out DIR]+"""+import argparse+import base64+import os+import sys+import tempfile++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+from test_progress import presses, records, run, write_storage # noqa: E402++COPY = "numblocks_saves.py"+++def ours(name):+ return name == "numblocks.cfg" or (name.startswith("nb") and name[2:3].isdigit() and name[2] != "0")+++def main():+ ap = argparse.ArgumentParser()+ ap.add_argument("nwa")+ ap.add_argument("--out", default="build/test_numblocks_progress")+ a = ap.parse_args()+ os.makedirs(a.out, exist_ok=True)+ script = ("pi.py", b"\x01print(3.14159)\n\x00")+ problems = []+ with tempfile.TemporaryDirectory() as d:+ st = os.path.join(d, "storage.bin")+ write_storage(st, [script])+ # the key sheet, Singleplayer, Create New World, down to its button; then look down and mine (OK)+ keys = presses((3000, "ok"), (3600, "ok"), (4200, "ok"), (4800, "down"), (5100, "down"), (5400, "down"),+ (5700, "down"), (6000, "down"), (6300, "ok"), (9000, "down", 1200), (10400, "ok", 3500))+ recs, p = run(a.nwa, st, keys, 14500, a.out, "1_play")+ problems += p+ saves = {n: v for n, v in recs.items() if ours(n)}+ for want in ("numblocks.cfg", "nb1.nbw"):+ if want not in saves:+ problems.append(f"{want} not written")+ if not any(n.startswith("nb1r") for n in saves):+ problems.append("no changed blocks saved (the mined block)")+ c = recs.get(COPY)+ if c is None:+ problems.append(f"{COPY} not written")+ else:+ if c[0] != 0 or c[-1] != 0 or 0 in c[1:-1]:+ problems.append(f"{COPY}: not a script")+ copied = {}+ for line in c[1:-1].decode("ascii", "replace").split("\n"):+ if line and not line.startswith("#"):+ problems.append(f"{COPY}: a line that isn't a comment: {line[:40]!r}")+ if line.startswith("#>"):+ name, _, data = line[2:].partition(":")+ try:+ copied[name] = base64.b64decode(data, validate=True)+ except ValueError:+ problems.append(f"{COPY}: {name} is not base64")+ if copied != saves:+ problems.append(f"{COPY} holds {sorted(copied)}, the saves are {sorted(saves)}"+ + ("" if set(copied) != set(saves) else " (contents differ)"))+ # installed again: only the scripts are left+ write_storage(st, [(n, v) for n, v in records(open(st, "rb").read()) if n.endswith(".py")])+ recs, p = run(a.nwa, st, [], 4000, a.out, "2_restore")+ problems += p+ for n, v in saves.items():+ if recs.get(n) != v:+ problems.append(f"after installing again, {n} " + ("missing" if n not in recs else "differs"))+ if recs.get(script[0]) != script[1]:+ problems.append("the other script changed")+ print(f" saves: {', '.join(f'{n} ({len(v)} bytes)' for n, v in sorted(saves.items()))}")+ for x in problems:+ print(" ", x)+ print("PASS" if not problems else "FAIL")+ sys.exit(1 if problems else 0)+++if __name__ == "__main__":+ main()