webgal-parser 4.3.11-1 → 4.3.13-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/package.json +9 -5
  2. package/rollup.config.js +45 -29
  3. package/{config → src/config}/scriptConfig.ts +1 -0
  4. package/{sceneParser.ts → src/sceneParser.ts} +5 -5
  5. package/test/parser.test.ts +29 -0
  6. package/test/{test.ts → test-resources/start.txt} +0 -12
  7. package/build/index.global.js +0 -482
  8. package/build/index.global.js.map +0 -1
  9. package/build/index.js +0 -460
  10. package/build/index.js.map +0 -1
  11. package/build/types/index.d.ts +0 -10
  12. package/build/types/interface/assets.d.ts +0 -12
  13. package/build/types/interface/runtimeInterface.d.ts +0 -9
  14. package/build/types/interface/sceneInterface.d.ts +0 -94
  15. package/build/types/sceneParser.d.ts +0 -14
  16. package/build/types/scriptParser/argsParser.d.ts +0 -9
  17. package/build/types/scriptParser/assetsScanner.d.ts +0 -9
  18. package/build/types/scriptParser/commandParser.d.ts +0 -9
  19. package/build/types/scriptParser/contentParser.d.ts +0 -9
  20. package/build/types/scriptParser/scriptParser.d.ts +0 -9
  21. package/build/types/scriptParser/subSceneScanner.d.ts +0 -7
  22. package/config/scriptConfig.js +0 -46
  23. package/index.d.ts +0 -10
  24. package/index.js +0 -483
  25. package/index.js.map +0 -1
  26. package/interface/assets.d.ts +0 -12
  27. package/interface/assets.js +0 -16
  28. package/interface/runtimeInterface.d.ts +0 -9
  29. package/interface/runtimeInterface.js +0 -2
  30. package/interface/sceneInterface.d.ts +0 -91
  31. package/interface/sceneInterface.js +0 -32
  32. package/sceneParser.d.ts +0 -14
  33. package/sceneParser.js +0 -53
  34. package/scriptParser/argsParser.d.ts +0 -9
  35. package/scriptParser/argsParser.js +0 -70
  36. package/scriptParser/assetsScanner.d.ts +0 -9
  37. package/scriptParser/assetsScanner.js +0 -75
  38. package/scriptParser/commandParser.d.ts +0 -9
  39. package/scriptParser/commandParser.js +0 -60
  40. package/scriptParser/contentParser.d.ts +0 -9
  41. package/scriptParser/contentParser.js +0 -68
  42. package/scriptParser/scriptParser.d.ts +0 -9
  43. package/scriptParser/scriptParser.js +0 -81
  44. package/scriptParser/subSceneScanner.d.ts +0 -7
  45. package/scriptParser/subSceneScanner.js +0 -26
  46. package/test/test.js +0 -10
  47. /package/{index.ts → src/index.ts} +0 -0
  48. /package/{interface → src/interface}/assets.ts +0 -0
  49. /package/{interface → src/interface}/runtimeInterface.ts +0 -0
  50. /package/{interface → src/interface}/sceneInterface.ts +0 -0
  51. /package/{scriptParser → src/scriptParser}/argsParser.ts +0 -0
  52. /package/{scriptParser → src/scriptParser}/assetsScanner.ts +0 -0
  53. /package/{scriptParser → src/scriptParser}/commandParser.ts +0 -0
  54. /package/{scriptParser → src/scriptParser}/contentParser.ts +0 -0
  55. /package/{scriptParser → src/scriptParser}/scriptParser.ts +0 -0
  56. /package/{scriptParser → src/scriptParser}/subSceneScanner.ts +0 -0
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "webgal-parser",
3
- "version": "4.3.11-1",
3
+ "version": "4.3.13-1",
4
4
  "description": "WebGAL script parser",
5
5
  "scripts": {
6
- "test": "node test/test.js",
6
+ "test": "vitest",
7
+ "coverage": "vitest run --coverage",
7
8
  "build": "rimraf -rf ./build && rollup --config"
8
9
  },
9
- "module": "./build/index.js",
10
- "main": "index.js",
10
+ "module": "./build/es/index.js",
11
+ "main": "./build/cjs/index.js",
11
12
  "author": "Mahiru <Mahiru_@outlook.com>",
12
13
  "license": "MPL-2.0",
13
14
  "dependencies": {
@@ -20,6 +21,8 @@
20
21
  "@rollup/plugin-node-resolve": "^15.0.1",
21
22
  "@rollup/plugin-typescript": "^9.0.2",
22
23
  "@types/lodash": "^4.14.189",
24
+ "@types/node": "^18.14.0",
25
+ "@vitest/coverage-c8": "^0.28.5",
23
26
  "rollup": "^3.3.0",
24
27
  "rollup-plugin-babel": "^4.4.0",
25
28
  "rollup-plugin-dts": "^5.0.0",
@@ -27,7 +30,8 @@
27
30
  "rollup-plugin-typescript2": "^0.34.1",
28
31
  "ts-node": "^10.9.1",
29
32
  "tslib": "^2.4.1",
30
- "typescript": "^4.9.3"
33
+ "typescript": "^4.9.3",
34
+ "vitest": "^0.28.5"
31
35
  },
32
36
  "type": "module"
33
37
  }
package/rollup.config.js CHANGED
@@ -1,32 +1,48 @@
1
- import typescript from 'rollup-plugin-typescript2';
1
+ import typescript from "rollup-plugin-typescript2";
2
+ import resolve from "@rollup/plugin-node-resolve";
3
+ import commonjs from "@rollup/plugin-commonjs";
2
4
 
3
5
  const mode = process.env.MODE;
4
- const isProd = mode === 'prod';
5
- import pkg from './package.json' assert { type: "json" };
6
+ const isProd = mode === "prod";
6
7
 
7
- export default {
8
- input: `index.ts`,
9
- output: [
10
- {
11
- file: pkg.main,
12
- exports: 'named',
13
- format: 'cjs',
14
- sourcemap: !isProd
15
- },
16
- {
17
- file: pkg.module,
18
- format: 'es',
19
- sourcemap: !isProd
20
- },
21
- {
22
- file: 'build/index.global.js',
23
- name: 'webgalparser',
24
- format: 'iife',
25
- sourcemap: !isProd
26
- },
27
- ],
28
- plugins: [typescript({
29
- useTsconfigDeclarationDir: true,
30
- tsconfigOverride: { compilerOptions: { sourceMap: !isProd } }
31
- })],
32
- };
8
+ export default [
9
+ {
10
+ input: `./src/index.ts`,
11
+ output:
12
+ {
13
+ file: "./build/es/index.js",
14
+ format: "es",
15
+ sourcemap: !isProd
16
+ },
17
+ plugins: [
18
+ resolve(), commonjs(), typescript({
19
+ useTsconfigDeclarationDir: true,
20
+ tsconfigOverride: {
21
+ compilerOptions: {
22
+ sourceMap: !isProd,
23
+ declarationDir: "build/es"
24
+ }, include: ["src"]
25
+ }
26
+ })]
27
+ }, {
28
+ input: `./src/index.ts`,
29
+ output: [
30
+ {
31
+ file: "./build/cjs/index.js",
32
+ exports: "named",
33
+ format: "cjs",
34
+ sourcemap: !isProd
35
+ },
36
+ ],
37
+ plugins: [
38
+ resolve(), commonjs(), typescript({
39
+ useTsconfigDeclarationDir: true,
40
+ tsconfigOverride: {
41
+ compilerOptions: {
42
+ sourceMap: !isProd,
43
+ declarationDir: "build/cjs"
44
+ }, include: ["src"]
45
+ }
46
+ })],
47
+ }
48
+ ];
@@ -29,6 +29,7 @@ export const SCRIPT_CONFIG = [
29
29
  { scriptString: 'callScene', scriptType: commandType.callScene },
30
30
  { scriptString: 'setTextbox', scriptType: commandType.setTextbox},
31
31
  { scriptString: 'setAnimation', scriptType: commandType.setAnimation },
32
+ { scriptString: 'playEffect', scriptType: commandType.playEffect },
32
33
  ];
33
34
  export const ADD_NEXT_ARG_LIST = [
34
35
  commandType.bgm,
@@ -1,6 +1,6 @@
1
1
  import { IAsset, IScene, ISentence } from "./interface/sceneInterface";
2
2
  import { scriptParser } from "./scriptParser/scriptParser";
3
- import * as __ from "lodash";
3
+ import uniqWith from "lodash/uniqWith";
4
4
  import { fileType } from "./interface/assets";
5
5
 
6
6
  /**
@@ -16,13 +16,13 @@ import { fileType } from "./interface/assets";
16
16
  */
17
17
  export const sceneParser = (rawScene: string, sceneName: string, sceneUrl: string
18
18
  , assetsPrefetcher: ((assetList: Array<IAsset>) => void), assetSetter: (fileName: string, assetType: fileType) => string
19
- , ADD_NEXT_ARG_LIST:any, SCRIPT_CONFIG:any): IScene => {
19
+ , ADD_NEXT_ARG_LIST: any, SCRIPT_CONFIG: any): IScene => {
20
20
  const rawSentenceList = rawScene.split("\n"); // 原始句子列表
21
21
 
22
22
  // 去分号留到后面去做了,现在注释要单独处理
23
23
  const rawSentenceListWithoutEmpty = rawSentenceList;
24
- // .map((sentence) => sentence.split(";")[0])
25
- // .filter((sentence) => sentence.trim() !== "");
24
+ // .map((sentence) => sentence.split(";")[0])
25
+ // .filter((sentence) => sentence.trim() !== "");
26
26
  let assetsList: Array<IAsset> = []; // 场景资源列表
27
27
  let subSceneList: Array<string> = []; // 子场景列表
28
28
  const sentenceList: Array<ISentence> = rawSentenceListWithoutEmpty.map((sentence) => {
@@ -34,7 +34,7 @@ export const sceneParser = (rawScene: string, sceneName: string, sceneUrl: strin
34
34
  });
35
35
 
36
36
  // 开始资源的预加载
37
- assetsList = __.uniqWith(assetsList); // 去重
37
+ assetsList = uniqWith(assetsList); // 去重
38
38
  assetsPrefetcher(assetsList);
39
39
 
40
40
  return {
@@ -0,0 +1,29 @@
1
+ import SceneParser from "../src/index";
2
+ import { ADD_NEXT_ARG_LIST, SCRIPT_CONFIG } from "../src/config/scriptConfig";
3
+ import { expect, test } from "vitest";
4
+ import { commandType, ISentence } from "../src/interface/sceneInterface";
5
+ import * as fsp from 'fs/promises'
6
+
7
+ test("label", async () => {
8
+
9
+ const sceneRaw = await fsp.readFile('test/test-resources/start.txt');
10
+ const sceneText = sceneRaw.toString();
11
+
12
+ const parser = new SceneParser((assetList) => {
13
+ }, (fileName, assetType) => {
14
+ return fileName;
15
+ }, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
16
+
17
+ const result = parser.parse(sceneText, "start", "/start.txt");
18
+ const expectSentenceItem: ISentence = {
19
+ command: commandType.label,
20
+ commandRaw: "label",
21
+ content: "end",
22
+ args: [
23
+ { key: "next", value: true }
24
+ ],
25
+ sentenceAssets: [],
26
+ subScene: []
27
+ };
28
+ expect(result.sentenceList).toContainEqual(expectSentenceItem);
29
+ });
@@ -1,12 +1,3 @@
1
- import { SceneParser } from "../index";
2
- import { ADD_NEXT_ARG_LIST, SCRIPT_CONFIG } from "../config/scriptConfig";
3
-
4
- const parser = new SceneParser((assetList) => {
5
- }, (fileName, assetType) => {
6
- return fileName;
7
- }, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
8
-
9
- const result = parser.parse(`
10
1
  ; 初始场景,以及特效演示
11
2
  changeBg:c4.jpg -next;
12
3
  unlockCg:c4.jpg -name=街前; 解锁部分CG
@@ -112,6 +103,3 @@ jumpLabel:end;
112
103
  label:end;
113
104
  changeFigure:none -next;
114
105
  WebGAL:基础演出的展示已经结束。;
115
- `, "test", "/test.txt");
116
-
117
- console.log(result);
@@ -1,482 +0,0 @@
1
- var webgalparser = (function (__) {
2
- 'use strict';
3
-
4
- function _interopNamespaceDefault(e) {
5
- var n = Object.create(null);
6
- if (e) {
7
- Object.keys(e).forEach(function (k) {
8
- if (k !== 'default') {
9
- var d = Object.getOwnPropertyDescriptor(e, k);
10
- Object.defineProperty(n, k, d.get ? d : {
11
- enumerable: true,
12
- get: function () { return e[k]; }
13
- });
14
- }
15
- });
16
- }
17
- n.default = e;
18
- return Object.freeze(n);
19
- }
20
-
21
- var ____namespace = /*#__PURE__*/_interopNamespaceDefault(__);
22
-
23
- var commandType;
24
- (function (commandType) {
25
- commandType[commandType["say"] = 0] = "say";
26
- commandType[commandType["changeBg"] = 1] = "changeBg";
27
- commandType[commandType["changeFigure"] = 2] = "changeFigure";
28
- commandType[commandType["bgm"] = 3] = "bgm";
29
- commandType[commandType["video"] = 4] = "video";
30
- commandType[commandType["pixi"] = 5] = "pixi";
31
- commandType[commandType["pixiInit"] = 6] = "pixiInit";
32
- commandType[commandType["intro"] = 7] = "intro";
33
- commandType[commandType["miniAvatar"] = 8] = "miniAvatar";
34
- commandType[commandType["changeScene"] = 9] = "changeScene";
35
- commandType[commandType["choose"] = 10] = "choose";
36
- commandType[commandType["end"] = 11] = "end";
37
- commandType[commandType["setComplexAnimation"] = 12] = "setComplexAnimation";
38
- commandType[commandType["setFilter"] = 13] = "setFilter";
39
- commandType[commandType["label"] = 14] = "label";
40
- commandType[commandType["jumpLabel"] = 15] = "jumpLabel";
41
- commandType[commandType["chooseLabel"] = 16] = "chooseLabel";
42
- commandType[commandType["setVar"] = 17] = "setVar";
43
- commandType[commandType["if"] = 18] = "if";
44
- commandType[commandType["callScene"] = 19] = "callScene";
45
- commandType[commandType["showVars"] = 20] = "showVars";
46
- commandType[commandType["unlockCg"] = 21] = "unlockCg";
47
- commandType[commandType["unlockBgm"] = 22] = "unlockBgm";
48
- commandType[commandType["filmMode"] = 23] = "filmMode";
49
- commandType[commandType["setTextbox"] = 24] = "setTextbox";
50
- commandType[commandType["setAnimation"] = 25] = "setAnimation";
51
- commandType[commandType["playEffect"] = 26] = "playEffect";
52
- commandType[commandType["setTempAnimation"] = 27] = "setTempAnimation";
53
- commandType[commandType["comment"] = 28] = "comment";
54
- })(commandType || (commandType = {}));
55
-
56
- /**
57
- * 处理命令
58
- * @param commandRaw
59
- * @param ADD_NEXT_ARG_LIST
60
- * @param SCRIPT_CONFIG
61
- * @return {parsedCommand} 处理后的命令
62
- */
63
- const commandParser = (commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
64
- let returnCommand = {
65
- type: commandType.say,
66
- additionalArgs: []
67
- };
68
- // 开始处理命令内容
69
- const type = getCommandType(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
70
- returnCommand.type = type;
71
- // 如果是对话,加上额外的参数
72
- if (type === commandType.say) {
73
- returnCommand.additionalArgs.push({
74
- key: "speaker",
75
- value: commandRaw
76
- });
77
- }
78
- returnCommand = addNextArg(returnCommand, type, ADD_NEXT_ARG_LIST);
79
- return returnCommand;
80
- };
81
- /**
82
- * 根据command原始值判断是什么命令
83
- * @param command command原始值
84
- * @param ADD_NEXT_ARG_LIST
85
- * @param SCRIPT_CONFIG
86
- * @return {commandType} 得到的command类型
87
- */
88
- function getCommandType(command, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
89
- // if (command.match(/if/)) {
90
- // return commandType.if;
91
- // }
92
- const commandMap = new Map();
93
- SCRIPT_CONFIG.forEach((e) => {
94
- commandMap.set(e.scriptString, e.scriptType);
95
- });
96
- if (commandMap.has(command)) {
97
- return commandMap.get(command);
98
- }
99
- else
100
- return commandType.say;
101
- }
102
- function addNextArg(commandToParse, thisCommandType, ADD_NEXT_ARG_LIST) {
103
- if (ADD_NEXT_ARG_LIST.includes(thisCommandType)) {
104
- commandToParse.additionalArgs.push({
105
- key: "next",
106
- value: true
107
- });
108
- }
109
- return commandToParse;
110
- }
111
-
112
- /**
113
- * 内置资源类型的枚举
114
- */
115
- var fileType;
116
- (function (fileType) {
117
- fileType[fileType["background"] = 0] = "background";
118
- fileType[fileType["bgm"] = 1] = "bgm";
119
- fileType[fileType["figure"] = 2] = "figure";
120
- fileType[fileType["scene"] = 3] = "scene";
121
- fileType[fileType["tex"] = 4] = "tex";
122
- fileType[fileType["vocal"] = 5] = "vocal";
123
- fileType[fileType["video"] = 6] = "video";
124
- })(fileType || (fileType = {}));
125
-
126
- /**
127
- * 参数解析器
128
- * @param argsRaw 原始参数字符串
129
- * @param assetSetter
130
- * @return {Array<arg>} 解析后的参数列表
131
- */
132
- function argsParser(argsRaw, assetSetter) {
133
- const returnArrayList = [];
134
- // 处理参数
135
- // 不要去空格
136
- let newArgsRaw = argsRaw.replace(/ /g, " ");
137
- // 分割参数列表
138
- let rawArgsList = newArgsRaw.split(" -");
139
- // 去除空字符串
140
- rawArgsList = rawArgsList.filter((e) => {
141
- return e !== "";
142
- });
143
- rawArgsList.forEach((e) => {
144
- const argName = e.split("=")[0];
145
- const argValue = e.split("=")[1];
146
- // 判断是不是语音参数
147
- if (e.match(/.ogg|.mp3|.wav/)) {
148
- returnArrayList.push({
149
- key: "vocal",
150
- value: assetSetter(e, fileType.vocal)
151
- });
152
- }
153
- else {
154
- // 判断是不是省略参数
155
- if (argValue === undefined) {
156
- returnArrayList.push({
157
- key: argName,
158
- value: true
159
- });
160
- }
161
- else {
162
- // 是字符串描述的布尔值
163
- if (argValue === "true" || argValue === "false") {
164
- returnArrayList.push({
165
- key: argName,
166
- value: argValue === "true"
167
- });
168
- }
169
- else {
170
- // 是数字
171
- if (!isNaN(Number(argValue))) {
172
- returnArrayList.push({
173
- key: argName,
174
- value: Number(argValue)
175
- });
176
- }
177
- else {
178
- // 是普通参数
179
- returnArrayList.push({
180
- key: argName,
181
- value: argValue
182
- });
183
- }
184
- }
185
- }
186
- }
187
- });
188
- return returnArrayList;
189
- }
190
-
191
- /**
192
- * 解析语句内容的函数,主要作用是把文件名改为绝对地址或相对地址(根据使用情况而定)
193
- * @param contentRaw 原始语句内容
194
- * @param type 语句类型
195
- * @param assetSetter
196
- * @return {string} 解析后的语句内容
197
- */
198
- const contentParser = (contentRaw, type, assetSetter) => {
199
- if (contentRaw === "none" || contentRaw === "") {
200
- return "";
201
- }
202
- switch (type) {
203
- case commandType.playEffect:
204
- return assetSetter(contentRaw, fileType.vocal);
205
- case commandType.changeBg:
206
- return assetSetter(contentRaw, fileType.background);
207
- case commandType.changeFigure:
208
- return assetSetter(contentRaw, fileType.figure);
209
- case commandType.bgm:
210
- return assetSetter(contentRaw, fileType.bgm);
211
- case commandType.callScene:
212
- return assetSetter(contentRaw, fileType.scene);
213
- case commandType.changeScene:
214
- return assetSetter(contentRaw, fileType.scene);
215
- case commandType.miniAvatar:
216
- return assetSetter(contentRaw, fileType.figure);
217
- case commandType.video:
218
- return assetSetter(contentRaw, fileType.video);
219
- case commandType.choose:
220
- return getChooseContent(contentRaw, assetSetter);
221
- case commandType.unlockBgm:
222
- return assetSetter(contentRaw, fileType.bgm);
223
- case commandType.unlockCg:
224
- return assetSetter(contentRaw, fileType.background);
225
- default:
226
- return contentRaw;
227
- }
228
- };
229
- function getChooseContent(contentRaw, assetSetter) {
230
- const chooseList = contentRaw.split("|");
231
- const chooseKeyList = [];
232
- const chooseValueList = [];
233
- for (const e of chooseList) {
234
- chooseKeyList.push(e.split(":")[0]);
235
- chooseValueList.push(e.split(":")[1]);
236
- }
237
- const parsedChooseList = chooseValueList.map((e) => {
238
- if (e.match(/\./)) {
239
- return assetSetter(e, fileType.scene);
240
- }
241
- else {
242
- return e;
243
- }
244
- });
245
- let ret = "";
246
- for (let i = 0; i < chooseKeyList.length; i++) {
247
- if (i !== 0) {
248
- ret = ret + "|";
249
- }
250
- ret = ret + `${chooseKeyList[i]}:${parsedChooseList[i]}`;
251
- }
252
- return ret;
253
- }
254
-
255
- /**
256
- * 根据语句类型、语句内容、参数列表,扫描该语句可能携带的资源
257
- * @param command 语句类型
258
- * @param content 语句内容
259
- * @param args 参数列表
260
- * @return {Array<IAsset>} 语句携带的参数列表
261
- */
262
- const assetsScanner = (command, content, args) => {
263
- const returnAssetsList = [];
264
- if (command === commandType.say) {
265
- args.forEach((e) => {
266
- if (e.key === 'vocal') {
267
- returnAssetsList.push({
268
- name: e.value,
269
- url: e.value,
270
- lineNumber: 0,
271
- type: fileType.vocal,
272
- });
273
- }
274
- });
275
- }
276
- if (content === 'none' || content === '') {
277
- return returnAssetsList;
278
- }
279
- // 处理语句携带的资源
280
- if (command === commandType.changeBg) {
281
- returnAssetsList.push({
282
- name: content,
283
- url: content,
284
- lineNumber: 0,
285
- type: fileType.background,
286
- });
287
- }
288
- if (command === commandType.changeFigure) {
289
- returnAssetsList.push({
290
- name: content,
291
- url: content,
292
- lineNumber: 0,
293
- type: fileType.figure,
294
- });
295
- }
296
- if (command === commandType.miniAvatar) {
297
- returnAssetsList.push({
298
- name: content,
299
- url: content,
300
- lineNumber: 0,
301
- type: fileType.figure,
302
- });
303
- }
304
- if (command === commandType.video) {
305
- returnAssetsList.push({
306
- name: content,
307
- url: content,
308
- lineNumber: 0,
309
- type: fileType.video,
310
- });
311
- }
312
- if (command === commandType.bgm) {
313
- returnAssetsList.push({
314
- name: content,
315
- url: content,
316
- lineNumber: 0,
317
- type: fileType.bgm,
318
- });
319
- }
320
- return returnAssetsList;
321
- };
322
-
323
- /**
324
- * 扫描子场景
325
- * @param content 语句内容
326
- * @return {Array<string>} 子场景列表
327
- */
328
- const subSceneScanner = (command, content) => {
329
- const subSceneList = [];
330
- if (command === commandType.changeScene || command === commandType.callScene) {
331
- subSceneList.push(content);
332
- }
333
- if (command === commandType.choose) {
334
- const chooseList = content.split('|');
335
- const chooseValue = chooseList.map((e) => e.split(':')[1]);
336
- chooseValue.forEach((e) => {
337
- if (e.match(/\./)) {
338
- subSceneList.push(e);
339
- }
340
- });
341
- }
342
- return subSceneList;
343
- };
344
-
345
- /**
346
- * 语句解析器
347
- * @param sentenceRaw 原始语句
348
- * @param assetSetter
349
- * @param ADD_NEXT_ARG_LIST
350
- * @param SCRIPT_CONFIG
351
- */
352
- const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
353
- let command; // 默认为对话
354
- let content; // 语句内容
355
- let subScene; // 语句携带的子场景(可能没有)
356
- const args = []; // 语句参数列表
357
- let sentenceAssets; // 语句携带的资源列表
358
- let parsedCommand; // 解析后的命令
359
- let commandRaw;
360
- // 正式开始解析
361
- // 去分号,前面已做,这里不再需要
362
- let newSentenceRaw = sentenceRaw.split(";")[0];
363
- if (newSentenceRaw === "") {
364
- // 注释提前返回
365
- return {
366
- command: commandType.comment,
367
- commandRaw: "comment",
368
- content: sentenceRaw.split(";")[1] ?? "",
369
- args: [{ key: "next", value: true }],
370
- sentenceAssets: [],
371
- subScene: [] // 语句携带的子场景
372
- };
373
- }
374
- // 截取命令
375
- const getCommandResult = /:/.exec(newSentenceRaw);
376
- /**
377
- * 拆分命令和语句,同时处理连续对话。
378
- */
379
- // 没有command,说明这是一条连续对话或单条语句
380
- if (getCommandResult === null) {
381
- commandRaw = newSentenceRaw;
382
- parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
383
- command = parsedCommand.type;
384
- for (const e of parsedCommand.additionalArgs) {
385
- // 由于是连续对话,所以我们去除 speaker 参数。
386
- if (command === commandType.say && e.key === "speaker") {
387
- continue;
388
- }
389
- args.push(e);
390
- }
391
- }
392
- else {
393
- commandRaw = newSentenceRaw.substring(0, getCommandResult.index);
394
- // 划分命令区域和content区域
395
- newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
396
- parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
397
- command = parsedCommand.type;
398
- for (const e of parsedCommand.additionalArgs) {
399
- args.push(e);
400
- }
401
- }
402
- // 截取参数区域
403
- const getArgsResult = / -/.exec(newSentenceRaw);
404
- // 获取到参数
405
- if (getArgsResult) {
406
- const argsRaw = newSentenceRaw.substring(getArgsResult.index, sentenceRaw.length);
407
- newSentenceRaw = newSentenceRaw.substring(0, getArgsResult.index);
408
- for (const e of argsParser(argsRaw, assetSetter)) {
409
- args.push(e);
410
- }
411
- }
412
- content = contentParser(newSentenceRaw, command, assetSetter); // 将语句内容里的文件名转为相对或绝对路径
413
- sentenceAssets = assetsScanner(command, content, args); // 扫描语句携带资源
414
- subScene = subSceneScanner(command, content); // 扫描语句携带子场景
415
- return {
416
- command: command,
417
- commandRaw: commandRaw,
418
- content: content,
419
- args: args,
420
- sentenceAssets: sentenceAssets,
421
- subScene: subScene // 语句携带的子场景
422
- };
423
- };
424
-
425
- /**
426
- * 场景解析器
427
- * @param rawScene 原始场景
428
- * @param sceneName 场景名称
429
- * @param sceneUrl 场景url
430
- * @param assetsPrefetcher
431
- * @param assetSetter
432
- * @param ADD_NEXT_ARG_LIST
433
- * @param SCRIPT_CONFIG
434
- * @return {IScene} 解析后的场景
435
- */
436
- const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
437
- const rawSentenceList = rawScene.split("\n"); // 原始句子列表
438
- // 去分号留到后面去做了,现在注释要单独处理
439
- const rawSentenceListWithoutEmpty = rawSentenceList;
440
- // .map((sentence) => sentence.split(";")[0])
441
- // .filter((sentence) => sentence.trim() !== "");
442
- let assetsList = []; // 场景资源列表
443
- let subSceneList = []; // 子场景列表
444
- const sentenceList = rawSentenceListWithoutEmpty.map((sentence) => {
445
- const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
446
- // 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
447
- assetsList = [...assetsList, ...returnSentence.sentenceAssets];
448
- subSceneList = [...subSceneList, ...returnSentence.subScene];
449
- return returnSentence;
450
- });
451
- // 开始资源的预加载
452
- assetsList = ____namespace.uniqWith(assetsList); // 去重
453
- assetsPrefetcher(assetsList);
454
- return {
455
- sceneName: sceneName,
456
- sceneUrl: sceneUrl,
457
- sentenceList: sentenceList,
458
- assetsList: assetsList,
459
- subSceneList: subSceneList // 子场景列表
460
- };
461
- };
462
-
463
- class SceneParser {
464
- assetsPrefetcher;
465
- assetSetter;
466
- ADD_NEXT_ARG_LIST;
467
- SCRIPT_CONFIG;
468
- constructor(assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
469
- this.assetsPrefetcher = assetsPrefetcher;
470
- this.assetSetter = assetSetter;
471
- this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
472
- this.SCRIPT_CONFIG = SCRIPT_CONFIG;
473
- }
474
- parse(rawScene, sceneName, sceneUrl) {
475
- return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
476
- }
477
- }
478
-
479
- return SceneParser;
480
-
481
- })(__);
482
- //# sourceMappingURL=index.global.js.map