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.

feat(editor): a file type can be claimed by its name, not only its extension

esenginedcb9a079b901
diff --git a/desktop/src/project/assetTypes.ts b/desktop/src/project/assetTypes.ts
index e8407f483..9153421f5 100644
--- a/desktop/src/project/assetTypes.ts
+++ b/desktop/src/project/assetTypes.ts
@@ -28,6 +28,13 @@ export interface AssetTypeDef {
/** Extensions (lower-case, no dot) that resolve to this type. Omitted for the
* virtual `folder`/`file` types, which aren't extension-derived. */
extensions?: readonly string[];
+ /**
+ * Full lower-case name endings that resolve to this type, matched BEFORE
+ * extensions because they are the more specific claim. For formats whose files
+ * are told apart by a name convention rather than a suffix — DragonBones ships
+ * `_ske.json` beside `_tex.json`, and an extension alone cannot see that.
+ */
+ suffixes?: readonly string[];
/** Short uppercase code shown in a tile's corner badge ('' = no badge). */
badge: string;
icon: LucideIcon;
@@ -48,6 +55,13 @@ export const ASSET_TYPES: Record<BuiltinAssetType, AssetTypeDef> = {
texture: { extensions: ['png', 'webp'], badge: 'TEX', icon: FileImage, tint: '#7fa6c4', externalProgram: 'image' },
sprite: { extensions: ['jpg', 'jpeg', 'gif'], badge: 'IMG', icon: Image, tint: '#7fa6c4', externalProgram: 'image' },
spine: { extensions: ['atlas', 'skel'], badge: 'SPN', icon: PersonStanding, tint: '#9b8fc0' },
+ // Both halves are one type, as Spine's .skel and .atlas are: which file plays
+ // which role is the component's business, not the content browser's.
+ dragonbones: {
+ extensions: ['dbbin'],
+ suffixes: ['_ske.json', '_tex.json'],
+ badge: 'DB', icon: PersonStanding, tint: '#a89b6b',
+ },
audio: { extensions: ['ogg', 'mp3', 'wav', 'aac', 'flac', 'm4a', 'webm'], badge: 'AUD', icon: Music, tint: '#7faf9c' },
video: { extensions: ['mp4', 'm4v', 'mov'], badge: 'VID', icon: Video, tint: '#c08fb5' },
prefab: { extensions: ['esprefab'], badge: 'PFB', icon: Component, tint: '#c2a274' },
@@ -110,7 +124,17 @@ export function assetTypeDef(type: AssetType): AssetTypeDef {
* Built-ins are matched first, so a plugin cannot re-map `.png`.
*/
export function assetTypeOf(name: string): AssetType {
- const ext = name.split('.').pop()?.toLowerCase() ?? '';
+ const lower = name.toLowerCase();
+ // Suffixes first: `foo_ske.json` is a DragonBones skeleton, and letting the
+ // extension answer would call it whatever `.json` maps to.
+ for (const [type, def] of Object.entries(ASSET_TYPES) as [AssetType, AssetTypeDef][]) {
+ if (def.suffixes?.some((s) => lower.endsWith(s))) return type;
+ }
+ for (const type of contributed.all()) {
+ if (type.suffixes?.some((s) => lower.endsWith(s))) return type.id;
+ }
+
+ const ext = lower.split('.').pop() ?? '';
const builtin = byExt.get(ext);
if (builtin) return builtin;
for (const type of contributed.all()) {
diff --git a/desktop/src/types.ts b/desktop/src/types.ts
index 105fc76d9..b1d68ec06 100644
--- a/desktop/src/types.ts
+++ b/desktop/src/types.ts
@@ -197,6 +197,7 @@ export type BuiltinAssetType =
| 'sprite'
| 'texture'
| 'spine'
+ | 'dragonbones'
| 'audio'
| 'video'
| 'prefab'
diff --git a/desktop/tests/asset-type-suffix.test.ts b/desktop/tests/asset-type-suffix.test.ts
new file mode 100644
index 000000000..aa00670f3
--- /dev/null
+++ b/desktop/tests/asset-type-suffix.test.ts
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+//
+// DragonBones tells its two halves apart by name, not by extension: `_ske.json`
+// beside `_tex.json`. So the type table had to learn suffixes, and a suffix has to
+// beat an extension — otherwise the day something claims `.json`, every skeleton
+// in every project silently becomes that instead.
+import { describe, it, expect } from 'vitest';
+import { assetTypeOf, ASSET_TYPES, assetTypeRegistry } from '@/project/assetTypes';
+
+describe('assetTypeOf — suffixes', () => {
+ it('reads both halves of a DragonBones asset as one type', () => {
+ // One type, as Spine's .skel and .atlas are one: which file plays which
+ // role is the component's business, not the content browser's.
+ expect(assetTypeOf('DragonBoy_ske.json')).toBe('dragonbones');
+ expect(assetTypeOf('DragonBoy_tex.json')).toBe('dragonbones');
+ expect(assetTypeOf('DragonBoy.dbbin')).toBe('dragonbones');
+ });
+
+ it('is case-insensitive, because a file name on two of three platforms is', () => {
+ expect(assetTypeOf('Mecha_SKE.JSON')).toBe('dragonbones');
+ });
+
+ it('leaves an ordinary json alone', () => {
+ expect(assetTypeOf('package.json')).toBe('file');
+ expect(assetTypeOf('skeleton.json')).toBe('file');
+ });
+
+ it('does not mistake a name that merely contains the suffix', () => {
+ // The claim is on the END of the name; `_ske.json.bak` is a backup.
+ expect(assetTypeOf('DragonBoy_ske.json.bak')).toBe('file');
+ });
+
+ it('leaves every other type resolving exactly as before', () => {
+ expect(assetTypeOf('hero.png')).toBe('texture');
+ expect(assetTypeOf('level.esscene')).toBe('scene');
+ expect(assetTypeOf('hero.skel')).toBe('spine');
+ expect(assetTypeOf('hero.atlas')).toBe('spine');
+ expect(assetTypeOf('main.ts')).toBe('script');
+ });
+
+ it('lets a contributed type claim a suffix too', () => {
+ const disposable = assetTypeRegistry.register('test-owner', {
+ id: 'testcfg',
+ suffixes: ['.config.yaml'],
+ badge: 'CFG',
+ icon: ASSET_TYPES.file.icon,
+ tint: '#888888',
+ });
+ expect(assetTypeOf('build.config.yaml')).toBe('testcfg');
+ disposable.dispose();
+ expect(assetTypeOf('build.config.yaml')).toBe('file');
+ });
+});