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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(tilemap): a cooked map's refs are logical, not relative to the map
yhh12d88750e856
diff --git a/sdk/src/asset/loaders/TilemapAssetLoader.ts b/sdk/src/asset/loaders/TilemapAssetLoader.tsindex 11fd41b7d..c5e574579 100644--- a/sdk/src/asset/loaders/TilemapAssetLoader.ts+++ b/sdk/src/asset/loaders/TilemapAssetLoader.ts@@ -2,7 +2,7 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport type { AssetLoader, LoadContext, TilemapResult } from '../AssetLoader';import {- packCollectionGrid, parseTmjWithExternals, resolveRelativePath,+ packCollectionGrid, parseTmjWithExternals, resolveTiledRef,type TiledMapData, type TiledTilesetData,} from '../../tilemap/tiledLoader';import { registerTilemapSource, unregisterTilemapSource, type LoadedTilemapTileset } from '../../tilemap/tilesetCache';@@ -26,7 +26,7 @@ export class TilemapAssetLoader implements AssetLoader<TilemapResult> {// External .tsj tilesets resolve relative to the map, through the same// text channel as the map itself.const mapData = await parseTmjWithExternals(JSON.parse(text), (source) =>- ctx.loadText(ctx.catalog.getBuildPath(resolveRelativePath(path, source))));+ ctx.loadText(ctx.catalog.getBuildPath(resolveTiledRef(path, source))));if (!mapData) {throw new Error(`Failed to parse tilemap: ${path}`);}@@ -39,7 +39,7 @@ export class TilemapAssetLoader implements AssetLoader<TilemapResult> {tilesets.push(await this.foldCollection_(path, ts, mapData, ctx));continue;}- const imagePath = resolveRelativePath(path, ts.image);+ const imagePath = resolveTiledRef(path, ts.image);let textureHandle = 0;try {const result = await ctx.loadTexture(imagePath, true);@@ -108,7 +108,7 @@ export class TilemapAssetLoader implements AssetLoader<TilemapResult> {+ 'asset provider cannot decode/compose pixels — load it through the app Assets channel.');}const tiles = await Promise.all(ts.collectionTiles!.map(async (tile) => {- const decoded = await ctx.decodePixels!(resolveRelativePath(mapPath, tile.image));+ const decoded = await ctx.decodePixels!(resolveTiledRef(mapPath, tile.image));if (decoded.width !== mapData.tileWidth || decoded.height !== mapData.tileHeight) {throw new Error(`[tilemap] "${mapPath}": collection tile "${tile.image}" is `diff --git a/sdk/src/tilemap/tiledLoader.ts b/sdk/src/tilemap/tiledLoader.tsindex aeb5848d9..ade82fb0b 100644--- a/sdk/src/tilemap/tiledLoader.ts+++ b/sdk/src/tilemap/tiledLoader.ts@@ -44,7 +44,7 @@ import { mergeCollisionTiles } from './collisionMerge';import { CHUNK_SIZE } from './chunkCodec';import { tileIdOf, tileFlagsOf } from './tileBits';import { log } from '../logger';-import { resolveRelativePath } from './tiledPath';+import { resolveRelativePath, resolveTiledRef, isLogicalAssetRef } from './tiledPath';export interface TiledChunkData {x: number;@@ -581,7 +581,7 @@ export async function parseTmjWithExternals(if (typeof ts.source !== 'string') return ts;let external = JSON.parse(await resolveExternal(ts.source)) as Record<string, unknown>;if (typeof external.image === 'string' && external.image) {- external = { ...external, image: resolveRelativePath(ts.source, external.image) };+ external = { ...external, image: resolveTiledRef(ts.source, external.image) };}// Collection tilesets carry per-tile images — rewrite those too.if (Array.isArray(external.tiles)) {@@ -589,7 +589,7 @@ export async function parseTmjWithExternals(...external,tiles: (external.tiles as Array<Record<string, unknown>>).map((t) =>typeof t?.image === 'string' && t.image- ? { ...t, image: resolveRelativePath(ts.source as string, t.image) }+ ? { ...t, image: resolveTiledRef(ts.source as string, t.image) }: t),};}@@ -603,7 +603,7 @@ export async function parseTmjWithExternals(// resolveRelativePath now lives in ./tiledPath (a dependency-free leaf) so the editor's// asset-dependency scan / cook can share the exact resolution the runtime loads with,// without pulling the loader's engine deps. Re-exported here for existing importers.-export { resolveRelativePath };+export { resolveRelativePath, resolveTiledRef, isLogicalAssetRef };export interface CollectionGridTile {/** Local tile id — also the grid slot, so gid → cell stays the identitydiff --git a/sdk/src/tilemap/tiledPath.ts b/sdk/src/tilemap/tiledPath.tsindex 4a33fab46..c74028d0e 100644--- a/sdk/src/tilemap/tiledPath.ts+++ b/sdk/src/tilemap/tiledPath.ts@@ -12,6 +12,34 @@* cook embeds one path and the runtime asks for another (the single-file playable then* 404s a tileset it did ship, or never shipped it at all).*/+/**+ * Whether `ref` is already a LOGICAL project path rather than one relative to the+ * document carrying it.+ *+ * The cook rewrites a map's document-relative refs into logical ones+ * (`"../textures/x.png"` → `"assets/textures/x.png"`, see `rewriteTilemapRefs`),+ * because a cooked payload has no directory structure left to be relative to. That+ * only worked while such a map was loaded by `@uuid` — a base path with no+ * directory, so joining was a no-op. Load the same map by its cooked FILE path (a+ * native app reads files, not uuids) and joining doubles the prefix, which is how+ * "assets/textures/tileset.png" became "assets/assets/textures/tileset.png" and+ * every tile went invisible.+ *+ * So the shape the cook emits is recognized here, in the one place both sides+ * share.+ */+export function isLogicalAssetRef(ref: string): boolean {+ return ref.startsWith('/') || ref.startsWith('assets/');+}++/**+ * Resolve a ref a Tiled document carries: a logical project path stands as it is,+ * anything else is relative to the document.+ */+export function resolveTiledRef(documentPath: string, ref: string): string {+ return isLogicalAssetRef(ref) ? ref : resolveRelativePath(documentPath, ref);+}+export function resolveRelativePath(basePath: string, relativePath: string): string {// Preserve a URL scheme+authority (e.g. "estella://project", "http://host")// before normalizing: the "//" after the scheme must survive, but the segmentdiff --git a/sdk/tests/tiled-path-resolution.test.ts b/sdk/tests/tiled-path-resolution.test.tsnew file mode 100644index 000000000..915f75c17--- /dev/null+++ b/sdk/tests/tiled-path-resolution.test.ts@@ -0,0 +1,62 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+//+// How a Tiled document's refs resolve. Two kinds reach the loader and they must not+// be treated the same:+//+// document-relative "../textures/tileset.png" — what Tiled authors+// logical project "assets/textures/tileset.png" — what the COOK rewrites them to+//+// A cooked payload has no directory structure left, so the cook makes the refs+// logical. Joining one against the map's own path doubles the prefix — which stayed+// invisible while cooked maps were only ever loaded by `@uuid` (a base with no+// directory, so the join was a no-op) and appeared the moment a native app loaded+// the same map by its cooked FILE path: every tile went invisible on the device with+// "assets/assets/textures/tileset.png" not found.+import { describe, it, expect } from 'vitest';+import { resolveRelativePath, resolveTiledRef, isLogicalAssetRef } from '../src/tilemap/tiledPath';++describe('isLogicalAssetRef', () => {+ it('recognizes the shape the cook emits', () => {+ expect(isLogicalAssetRef('assets/textures/tileset.png')).toBe(true);+ expect(isLogicalAssetRef('/scenes/level.tmj')).toBe(true);+ });++ it('leaves an authored document-relative ref alone', () => {+ expect(isLogicalAssetRef('../textures/tileset.png')).toBe(false);+ expect(isLogicalAssetRef('./tiles.png')).toBe(false);+ expect(isLogicalAssetRef('tiles.png')).toBe(false);+ });+});++describe('resolveTiledRef', () => {+ it('joins an authored ref against the map that carries it', () => {+ expect(resolveTiledRef('assets/maps/level.tmj', '../textures/tileset.png'))+ .toBe('assets/textures/tileset.png');+ });++ it('passes a cooked logical ref through — whatever the map path looks like', () => {+ // The three ways one map gets addressed: by uuid (playable/web), by its+ // content-addressed cooked file (a native app), and in the editor.+ for (const mapPath of ['@uuid:1111', 'assets/d553da0f64d68b5d.tmj', 'assets/maps/level.tmj']) {+ expect(resolveTiledRef(mapPath, 'assets/textures/tileset.png'))+ .toBe('assets/textures/tileset.png');+ }+ });++ it('is what the uuid path did by accident, now on purpose', () => {+ // The old behaviour, kept for the case it was right about.+ expect(resolveRelativePath('@uuid:1111', 'assets/textures/tileset.png'))+ .toBe('assets/textures/tileset.png');+ // …and the case it got wrong.+ expect(resolveRelativePath('assets/level.tmj', 'assets/textures/tileset.png'))+ .toBe('assets/assets/textures/tileset.png');+ expect(resolveTiledRef('assets/level.tmj', 'assets/textures/tileset.png'))+ .toBe('assets/textures/tileset.png');+ });++ it('keeps a URL base intact', () => {+ expect(resolveTiledRef('estella://project/assets/maps/level.tmj', '../textures/t.png'))+ .toBe('estella://project/assets/textures/t.png');+ });+});