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(cocos): the importer records its revision, so a project it wrote earlier is re-imported on open

yhhbfbce864ea9d
diff --git a/desktop b/desktop
index 6c15ab9ff..e0b35125d 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 6c15ab9ff6d7a86a2090ba31b69e81d7e76a51c8
+Subproject commit e0b35125dc714b379a9071e4cea08d3b8f42b527
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index eccca9f42..e2d6b0865 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -23,6 +23,23 @@ import { convertAnimationClip } from './animationClip';
import { convertAnimationGraph } from './animationGraph';
import type { MaterialInfo } from './components';
+/**
+ * The revision of what an import writes. It is recorded in the state file; a converted project whose record is older was written by an
+ * importer that made less (or differently), and is brought up to date when it is opened. Raise it when the converted output changes in a
+ * way an existing project needs to pick up: a new generated file, a field a document or a manifest now carries.
+ */
+export const COCOS_IMPORT_REVISION = 1;
+
+/** Whether the project at @p projectRoot was written by an importer of this revision. A project that was not converted has nothing to bring up. */
+export function cocosImportIsCurrent(projectRoot: string): boolean {
+ try {
+ const state = JSON.parse(readFileSync(path.join(projectRoot, 'assets', 'cocos', '.import-state.json'), 'utf8')) as { revision?: unknown };
+ return state.revision === COCOS_IMPORT_REVISION;
+ } catch {
+ return true;
+ }
+}
+
export interface CocosImportOptions {
/** The Cocos project folder (the one holding `assets/` and `settings/`). */
source: string;
@@ -788,7 +805,7 @@ export async function importCocosProject(opts: CocosImportOptions): Promise<Impo
}
}
progress('finish', 0, 1);
- writeOut(stateFile, `${JSON.stringify({ source, documents: written }, null, 2)}\n`);
+ writeOut(stateFile, `${JSON.stringify({ revision: COCOS_IMPORT_REVISION, source, documents: written }, null, 2)}\n`);
writeOut(path.join(out, 'assets', into, 'import-report.json'), `${JSON.stringify(report.toJSON(), null, 2)}\n`);
}
return report.toJSON();
diff --git a/pipeline/src/import/cocos/index.ts b/pipeline/src/import/cocos/index.ts
index 067686220..98f9d6706 100644
--- a/pipeline/src/import/cocos/index.ts
+++ b/pipeline/src/import/cocos/index.ts
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-export { importCocosProject, type CocosImportOptions, type CocosImportProgress } from './importProject';
+export { importCocosProject, cocosImportIsCurrent, COCOS_IMPORT_REVISION, type CocosImportOptions, type CocosImportProgress } from './importProject';
export { parseCocosDocument, type CocosDocument, type CocosNode } from './document';
export { buildCocosAssetIndex, type CocosAssetIndex } from './assets';
export { convertPrefab, convertScene, type ConvertContext, type PrefabFile, type SceneFile } from './convert';
diff --git a/pipeline/tests/cocos-import.test.ts b/pipeline/tests/cocos-import.test.ts
index 0470d883a..5eaf8eaf2 100644
--- a/pipeline/tests/cocos-import.test.ts
+++ b/pipeline/tests/cocos-import.test.ts
@@ -18,7 +18,7 @@ import { createHash } from 'node:crypto';
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
-import { importCocosProject } from '../src/import/cocos';
+import { COCOS_IMPORT_REVISION, cocosImportIsCurrent, importCocosProject } from '../src/import/cocos';
const IMG = '11111111-1111-4111-8111-111111111111';
const ITEM = '22222222-2222-4222-8222-222222222222';
@@ -280,6 +280,19 @@ describe('importing again over the same project', () => {
const ITEM_REL = 'assets/cocos/ui/item.esprefab';
const stateOf = (): { documents: Record<string, string> } => readJson('assets/cocos/.import-state.json');
+ it('records the importer revision, so a project written by an older importer is known to need bringing up', () => {
+ expect(cocosImportIsCurrent(out)).toBe(true);
+ const state = { ...readJson('assets/cocos/.import-state.json') } as { revision?: number };
+ expect(state.revision).toBe(COCOS_IMPORT_REVISION);
+ const file = path.join(out, 'assets/cocos/.import-state.json');
+ const text = readFileSync(file, 'utf8');
+ delete state.revision;
+ writeFileSync(file, JSON.stringify(state));
+ expect(cocosImportIsCurrent(out)).toBe(false);
+ writeFileSync(file, text);
+ expect(cocosImportIsCurrent(path.join(out, 'nowhere'))).toBe(true); // not converted: nothing to bring up
+ });
+
it('finds nothing to do when neither side changed', async () => {
const again = await importCocosProject({ source: src, out });
expect(again.sync.added).toBe(0);