Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/parser/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ test
.babelrc
rollup.config.js
tsconfig.json
tsconfig.node.json
14 changes: 2 additions & 12 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,16 @@
"main": "./build/cjs/index.cjs",
"author": "Mahiru <Mahiru_@outlook.com>",
"license": "MPL-2.0",
"dependencies": {
"chevrotain": "^10.5.0",
"cloudlogjs": "^1.0.11",
"lodash": "^4.17.23",
"tsx": "^3.12.7"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^23.0.2",
"@rollup/plugin-json": "^5.0.1",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^9.0.2",
"@types/lodash": "^4.14.189",
"@types/node": "^18.14.0",
"@vitest/coverage-c8": "^0.28.5",
"rimraf": "^3.0.2",
"rollup": "^3.29.5",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.34.1",
"ts-node": "^10.9.1",
"tslib": "^2.4.1",
"tsx": "^3.12.7",
"typescript": "^4.9.3",
"vitest": "^0.28.5"
},
Expand Down
1 change: 1 addition & 0 deletions packages/parser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default [
file: "./build/umd/index.global.js",
name: 'webgalParser',
format: 'iife',
exports: "named",
sourcemap: !isProd
},
],
Expand Down
18 changes: 16 additions & 2 deletions packages/parser/src/sceneParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ISentence,
} from './interface/sceneInterface';
import { scriptParser } from './scriptParser/scriptParser';
import uniqWith from 'lodash/uniqWith';
import { fileType } from './interface/assets';
import { ConfigMap } from './config/scriptConfig';

Expand Down Expand Up @@ -54,7 +53,7 @@ export const sceneParser = (
);

// 开始资源的预加载
assetsList = uniqWith(assetsList); // 去重
assetsList = deduplicateAssets(assetsList);
assetsPrefetcher(assetsList);

return {
Expand All @@ -65,3 +64,18 @@ export const sceneParser = (
subSceneList: subSceneList, // 子场景列表
};
};

const deduplicateAssets = (assetsList: IAsset[]): IAsset[] => {
const seenAssets = new Set<string>();
return assetsList.filter((asset) => {
if (!asset || typeof asset.url !== 'string' || asset.url === '') {
return false;
}
const assetKey = `${asset.type}:${asset.url}`;
if (seenAssets.has(assetKey)) {
return false;
}
seenAssets.add(assetKey);
return true;
});
};
Comment thread
A-kirami marked this conversation as resolved.
35 changes: 34 additions & 1 deletion packages/parser/test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SceneParser from "../src/index";
import { ADD_NEXT_ARG_LIST, SCRIPT_CONFIG } from "../src/config/scriptConfig";
import { expect, test } from "vitest";
import { commandType, ISentence } from "../src/interface/sceneInterface";
import { commandType, IAsset, ISentence } from "../src/interface/sceneInterface";
import * as fsp from 'fs/promises';
import { fileType } from "../src/interface/assets";

Expand Down Expand Up @@ -223,6 +223,39 @@ test("say statement applies asset setter to vocal named argument", async () => {
});
});

test("scene assets are deduplicated by type and url", async () => {
let prefetchedAssets: IAsset[] = [];
const parser = new SceneParser((assetList) => {
prefetchedAssets = assetList;
}, (fileName, assetType) => {
return fileName;
}, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);

const result = parser.parse(`changeBg:shared.webp;
changeFigure:shared.webp;
changeBg:shared.webp;`, 'test', 'test');

expect(result.assetsList).toEqual([
{ name: "shared.webp", url: 'shared.webp', type: fileType.background, lineNumber: 0 },
{ name: "shared.webp", url: 'shared.webp', type: fileType.figure, lineNumber: 1 },
]);
expect(prefetchedAssets).toEqual(result.assetsList);
});

test("scene assets skip entries with empty urls", async () => {
const parser = new SceneParser((assetList) => {
}, (fileName, assetType) => {
if (assetType === fileType.vocal) {
return '';
}
return fileName;
}, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);

const result = parser.parse(`say:123 -vocal=missing.mp3;`, 'test', 'test');

expect(result.assetsList).toEqual([]);
});

test("wait command", async () => {
const parser = new SceneParser((assetList) => {
}, (fileName, assetType) => {
Expand Down
7 changes: 3 additions & 4 deletions packages/parser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declarationDir": "build/types",
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"lib": ["ESNext"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": false,
Expand All @@ -17,13 +17,12 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"types": ["node"],
"paths": {
"@/*": ["./src/*"]
}
},
"include": [
"src"
],
"references": [{ "path": "./tsconfig.node.json"}]
]
}
10 changes: 0 additions & 10 deletions packages/parser/tsconfig.node.json

This file was deleted.

Loading
Loading