Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(sdk): a save that moved directories is still the player's save

yhh5385c1e90b93
diff --git a/docs/astro/src/content/docs/guides/save.mdx b/docs/astro/src/content/docs/guides/save.mdx
index e337a054d..292a3b268 100644
--- a/docs/astro/src/content/docs/guides/save.mdx
+++ b/docs/astro/src/content/docs/guides/save.mdx
@@ -115,6 +115,10 @@ Caches whenever it wants the space back, so a save kept there is one the player
can lose. Both mobile directories are private to the app and included in the
device backup a player restores a new phone from.
+Builds before Estella 0.41 kept the same file in the cache directory. If you have
+already shipped one, the first launch after the update finds that save and moves
+it — your players keep their progress and there is nothing to write.
+
`platformReadFile()` is **not** a way to reach these. It reads files packaged
*into* the build (the APK's `assets/`, the iOS app bundle) and is read-only.
diff --git a/docs/astro/src/content/docs/zh-cn/guides/save.mdx b/docs/astro/src/content/docs/zh-cn/guides/save.mdx
index b18af46b1..450ae3797 100644
--- a/docs/astro/src/content/docs/zh-cn/guides/save.mdx
+++ b/docs/astro/src/content/docs/zh-cn/guides/save.mdx
@@ -106,6 +106,9 @@ iOS 上用的是 Application Support 而不是 Caches,这是有意为之——iO
会清空 Caches,存在那里的存档玩家可能凭空丢失。两个移动平台的目录都是应用私有的,并且
会进入玩家换新手机时恢复的设备备份。
+Estella 0.41 之前的构建把同一个文件放在缓存目录里。如果你已经发布过这样的版本,更新
+后的第一次启动会找到那份存档并把它搬过来——玩家的进度不会丢,你也不需要写任何代码。
+
`platformReadFile()` **不是**读它们的途径。它读的是**打包进**构建产物里的文件
(APK 的 `assets/`、iOS 的 app bundle),且只读。
diff --git a/sdk/src/platform/native/hostBridge.ts b/sdk/src/platform/native/hostBridge.ts
index 54005cbcc..a1d2ca940 100644
--- a/sdk/src/platform/native/hostBridge.ts
+++ b/sdk/src/platform/native/hostBridge.ts
@@ -346,10 +346,14 @@ const STORAGE_FILE = 'estella-storage.json';
* (localStorage's shape), so the file variant keeps the map in memory and writes
* through on every mutation — storage holds saves and settings, not bulk data.
*
- * The file goes to the DATA store, never the cache. An older host that binds only
- * the cache pair still persists through it, because a cache that usually survives
- * beats losing the save at every exit — but it is the wrong directory on any
- * platform that reclaims one, so say so once rather than let a player find out.
+ * The file is WRITTEN to the DATA store, never the cache. An older host that binds
+ * only the cache pair still persists through it, because a cache that usually
+ * survives beats losing the save at every exit — but it is the wrong directory on
+ * any platform that reclaims one, so say so once rather than let a player find out.
+ *
+ * It is READ from the cache too, once, when the durable store has nothing: builds
+ * before the split wrote every save there, and a player updating across it would
+ * otherwise open the game to a blank profile.
*/
function hostStorage(bindings: NativeHostBindings): Pick<
NativeBridge, 'getStorageItem' | 'setStorageItem' | 'removeStorageItem' | 'storageKeys'
@@ -370,20 +374,41 @@ function hostStorage(bindings: NativeHostBindings): Pick<
const entries = new Map<string, string>();
const persistent = typeof read === 'function' && typeof write === 'function';
+ /** Read a stored blob into `entries`. False when it was not readable. */
+ const adopt = (bytes: ArrayBuffer): boolean => {
+ try {
+ for (const [k, v] of Object.entries(JSON.parse(new TextDecoder().decode(bytes)) as Record<string, string>)) {
+ entries.set(k, v);
+ }
+ return true;
+ } catch {
+ log.warn('native', 'stored data was unreadable — starting empty');
+ return false;
+ }
+ };
+
+ /** The save came from the cache directory and owes the durable one a copy. */
+ let inherited = false;
if (!persistent) {
log.warn('native', 'host has no storage or file bindings — saves last only for this session');
} else {
if (!durable) {
log.warn('native', 'host binds no durable store — saves go to its cache, which the platform may reclaim');
}
- const bytes = read!(STORAGE_FILE);
- if (bytes) {
- try {
- for (const [k, v] of Object.entries(JSON.parse(new TextDecoder().decode(bytes)) as Record<string, string>)) {
- entries.set(k, v);
- }
- } catch {
- log.warn('native', 'stored data was unreadable — starting empty');
+ const own = read!(STORAGE_FILE);
+ if (own) {
+ adopt(own);
+ } else if (durable) {
+ // Nothing in the durable store. Either nothing was ever saved, in which
+ // case the cache holds nothing either — or the player updated from a
+ // build that knew only the cache directory, and their save is the file
+ // sitting in it. Not looking there is indistinguishable from having
+ // deleted it, and it would happen on exactly the version that claims to
+ // have made saves safe.
+ const previous = bindings.es_readCacheFile?.(STORAGE_FILE) ?? null;
+ if (previous && adopt(previous) && entries.size > 0) {
+ inherited = true;
+ log.info('native', 'adopted the save an earlier build left in the cache directory');
}
}
}
@@ -393,6 +418,10 @@ function hostStorage(bindings: NativeHostBindings): Pick<
// JS engine has no TextEncoder to reach for.
write!(STORAGE_FILE, JSON.stringify(Object.fromEntries(entries)));
};
+ // Write an inherited save through now rather than at the next mutation: the
+ // cache copy is never read again, and the directory it is in is the one the
+ // platform is allowed to empty.
+ if (inherited) flush();
return {
getStorageItem: (key) => entries.get(key) ?? null,
setStorageItem: (key, value) => { entries.set(key, value); flush(); },
diff --git a/sdk/tests/native-host-bridge.test.ts b/sdk/tests/native-host-bridge.test.ts
index dbe8c7e7f..f1e900f1d 100644
--- a/sdk/tests/native-host-bridge.test.ts
+++ b/sdk/tests/native-host-bridge.test.ts
@@ -133,6 +133,37 @@ describe('createHostBridge', () => {
expect(createHostBridge(scope).getStorageItem('save')).toBe('{"level":3}');
});
+ // The upgrade path. Every build before the split wrote the save into the cache
+ // directory, so a player who updates has it there and in no other place.
+ it('adopts the save a pre-split build left in the cache directory', () => {
+ const { scope, cache, data } = twoStoreScope();
+ cache.set('estella-storage.json', '{"save":"{\\"level\\":3}"}');
+
+ expect(createHostBridge(scope).getStorageItem('save')).toBe('{"level":3}');
+ // Written through at once: the directory it came from is the one the
+ // platform may empty, so leaving it there is leaving it lost.
+ expect(data.get('estella-storage.json')).toContain('level');
+ });
+
+ it('adopts it once — a cache emptied afterwards costs nothing', () => {
+ const { scope, cache } = twoStoreScope();
+ cache.set('estella-storage.json', '{"save":"{\\"level\\":3}"}');
+ createHostBridge(scope);
+ cache.clear();
+
+ expect(createHostBridge(scope).getStorageItem('save')).toBe('{"level":3}');
+ });
+
+ // A durable save is the truth even when a stale cache copy is still on disk:
+ // the cache is only consulted when there is nothing to consult it against.
+ it('never lets a stale cache copy overwrite the durable save', () => {
+ const { scope, cache } = twoStoreScope();
+ createHostBridge(scope).setStorageItem('save', '{"level":9}');
+ cache.set('estella-storage.json', '{"save":"{\\"level\\":3}"}');
+
+ expect(createHostBridge(scope).getStorageItem('save')).toBe('{"level":9}');
+ });
+
it('degrades to session storage when the host has no cache, without throwing', () => {
const bridge = createHostBridge(hostScope());
bridge.setStorageItem('k', 'v');