webgal-parser 4.3.6 → 4.3.7

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.
package/build/index.js CHANGED
@@ -1,444 +1,455 @@
1
1
  import * as __ from 'lodash';
2
2
 
3
- var commandType;
4
- (function (commandType) {
5
- commandType[commandType["say"] = 0] = "say";
6
- commandType[commandType["changeBg"] = 1] = "changeBg";
7
- commandType[commandType["changeFigure"] = 2] = "changeFigure";
8
- commandType[commandType["bgm"] = 3] = "bgm";
9
- commandType[commandType["video"] = 4] = "video";
10
- commandType[commandType["pixi"] = 5] = "pixi";
11
- commandType[commandType["pixiInit"] = 6] = "pixiInit";
12
- commandType[commandType["intro"] = 7] = "intro";
13
- commandType[commandType["miniAvatar"] = 8] = "miniAvatar";
14
- commandType[commandType["changeScene"] = 9] = "changeScene";
15
- commandType[commandType["choose"] = 10] = "choose";
16
- commandType[commandType["end"] = 11] = "end";
17
- commandType[commandType["setComplexAnimation"] = 12] = "setComplexAnimation";
18
- commandType[commandType["setFilter"] = 13] = "setFilter";
19
- commandType[commandType["label"] = 14] = "label";
20
- commandType[commandType["jumpLabel"] = 15] = "jumpLabel";
21
- commandType[commandType["chooseLabel"] = 16] = "chooseLabel";
22
- commandType[commandType["setVar"] = 17] = "setVar";
23
- commandType[commandType["if"] = 18] = "if";
24
- commandType[commandType["callScene"] = 19] = "callScene";
25
- commandType[commandType["showVars"] = 20] = "showVars";
26
- commandType[commandType["unlockCg"] = 21] = "unlockCg";
27
- commandType[commandType["unlockBgm"] = 22] = "unlockBgm";
28
- commandType[commandType["filmMode"] = 23] = "filmMode";
29
- commandType[commandType["setTextbox"] = 24] = "setTextbox";
30
- commandType[commandType["setAnimation"] = 25] = "setAnimation";
3
+ var commandType;
4
+ (function (commandType) {
5
+ commandType[commandType["say"] = 0] = "say";
6
+ commandType[commandType["changeBg"] = 1] = "changeBg";
7
+ commandType[commandType["changeFigure"] = 2] = "changeFigure";
8
+ commandType[commandType["bgm"] = 3] = "bgm";
9
+ commandType[commandType["video"] = 4] = "video";
10
+ commandType[commandType["pixi"] = 5] = "pixi";
11
+ commandType[commandType["pixiInit"] = 6] = "pixiInit";
12
+ commandType[commandType["intro"] = 7] = "intro";
13
+ commandType[commandType["miniAvatar"] = 8] = "miniAvatar";
14
+ commandType[commandType["changeScene"] = 9] = "changeScene";
15
+ commandType[commandType["choose"] = 10] = "choose";
16
+ commandType[commandType["end"] = 11] = "end";
17
+ commandType[commandType["setComplexAnimation"] = 12] = "setComplexAnimation";
18
+ commandType[commandType["setFilter"] = 13] = "setFilter";
19
+ commandType[commandType["label"] = 14] = "label";
20
+ commandType[commandType["jumpLabel"] = 15] = "jumpLabel";
21
+ commandType[commandType["chooseLabel"] = 16] = "chooseLabel";
22
+ commandType[commandType["setVar"] = 17] = "setVar";
23
+ commandType[commandType["if"] = 18] = "if";
24
+ commandType[commandType["callScene"] = 19] = "callScene";
25
+ commandType[commandType["showVars"] = 20] = "showVars";
26
+ commandType[commandType["unlockCg"] = 21] = "unlockCg";
27
+ commandType[commandType["unlockBgm"] = 22] = "unlockBgm";
28
+ commandType[commandType["filmMode"] = 23] = "filmMode";
29
+ commandType[commandType["setTextbox"] = 24] = "setTextbox";
30
+ commandType[commandType["setAnimation"] = 25] = "setAnimation";
31
+ commandType[commandType["playEffect"] = 26] = "playEffect";
31
32
  })(commandType || (commandType = {}));
32
33
 
33
- /**
34
- * 处理命令
35
- * @param commandRaw
36
- * @param ADD_NEXT_ARG_LIST
37
- * @param SCRIPT_CONFIG
38
- * @return {parsedCommand} 处理后的命令
39
- */
40
- const commandParser = (commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
41
- let returnCommand = {
42
- type: commandType.say,
43
- additionalArgs: []
44
- };
45
- // 开始处理命令内容
46
- const type = getCommandType(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
47
- returnCommand.type = type;
48
- // 如果是对话,加上额外的参数
49
- if (type === commandType.say) {
50
- returnCommand.additionalArgs.push({
51
- key: "speaker",
52
- value: commandRaw
53
- });
54
- }
55
- returnCommand = addNextArg(returnCommand, type, ADD_NEXT_ARG_LIST);
56
- return returnCommand;
57
- };
58
- /**
59
- * 根据command原始值判断是什么命令
60
- * @param command command原始值
61
- * @param ADD_NEXT_ARG_LIST
62
- * @param SCRIPT_CONFIG
63
- * @return {commandType} 得到的command类型
64
- */
65
- function getCommandType(command, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
66
- // if (command.match(/if/)) {
67
- // return commandType.if;
68
- // }
69
- const commandMap = new Map();
70
- SCRIPT_CONFIG.forEach((e) => {
71
- commandMap.set(e.scriptString, e.scriptType);
72
- });
73
- if (commandMap.has(command)) {
74
- return commandMap.get(command);
75
- }
76
- else
77
- return commandType.say;
78
- }
79
- function addNextArg(commandToParse, thisCommandType, ADD_NEXT_ARG_LIST) {
80
- if (ADD_NEXT_ARG_LIST.includes(thisCommandType)) {
81
- commandToParse.additionalArgs.push({
82
- key: "next",
83
- value: true
84
- });
85
- }
86
- return commandToParse;
34
+ /**
35
+ * 处理命令
36
+ * @param commandRaw
37
+ * @param ADD_NEXT_ARG_LIST
38
+ * @param SCRIPT_CONFIG
39
+ * @return {parsedCommand} 处理后的命令
40
+ */
41
+ const commandParser = (commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
42
+ let returnCommand = {
43
+ type: commandType.say,
44
+ additionalArgs: []
45
+ };
46
+ // 开始处理命令内容
47
+ const type = getCommandType(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
48
+ returnCommand.type = type;
49
+ // 如果是对话,加上额外的参数
50
+ if (type === commandType.say) {
51
+ returnCommand.additionalArgs.push({
52
+ key: "speaker",
53
+ value: commandRaw
54
+ });
55
+ }
56
+ returnCommand = addNextArg(returnCommand, type, ADD_NEXT_ARG_LIST);
57
+ return returnCommand;
58
+ };
59
+ /**
60
+ * 根据command原始值判断是什么命令
61
+ * @param command command原始值
62
+ * @param ADD_NEXT_ARG_LIST
63
+ * @param SCRIPT_CONFIG
64
+ * @return {commandType} 得到的command类型
65
+ */
66
+ function getCommandType(command, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
67
+ // if (command.match(/if/)) {
68
+ // return commandType.if;
69
+ // }
70
+ const commandMap = new Map();
71
+ SCRIPT_CONFIG.forEach((e) => {
72
+ commandMap.set(e.scriptString, e.scriptType);
73
+ });
74
+ if (commandMap.has(command)) {
75
+ return commandMap.get(command);
76
+ }
77
+ else
78
+ return commandType.say;
79
+ }
80
+ function addNextArg(commandToParse, thisCommandType, ADD_NEXT_ARG_LIST) {
81
+ if (ADD_NEXT_ARG_LIST.includes(thisCommandType)) {
82
+ commandToParse.additionalArgs.push({
83
+ key: "next",
84
+ value: true
85
+ });
86
+ }
87
+ return commandToParse;
87
88
  }
88
89
 
89
- /**
90
- * 内置资源类型的枚举
91
- */
92
- var fileType;
93
- (function (fileType) {
94
- fileType[fileType["background"] = 0] = "background";
95
- fileType[fileType["bgm"] = 1] = "bgm";
96
- fileType[fileType["figure"] = 2] = "figure";
97
- fileType[fileType["scene"] = 3] = "scene";
98
- fileType[fileType["tex"] = 4] = "tex";
99
- fileType[fileType["vocal"] = 5] = "vocal";
100
- fileType[fileType["video"] = 6] = "video";
90
+ /**
91
+ * 内置资源类型的枚举
92
+ */
93
+ var fileType;
94
+ (function (fileType) {
95
+ fileType[fileType["background"] = 0] = "background";
96
+ fileType[fileType["bgm"] = 1] = "bgm";
97
+ fileType[fileType["figure"] = 2] = "figure";
98
+ fileType[fileType["scene"] = 3] = "scene";
99
+ fileType[fileType["tex"] = 4] = "tex";
100
+ fileType[fileType["vocal"] = 5] = "vocal";
101
+ fileType[fileType["video"] = 6] = "video";
101
102
  })(fileType || (fileType = {}));
102
103
 
103
- /**
104
- * 参数解析器
105
- * @param argsRaw 原始参数字符串
106
- * @param assetSetter
107
- * @return {Array<arg>} 解析后的参数列表
108
- */
109
- function argsParser(argsRaw, assetSetter) {
110
- const returnArrayList = [];
111
- // 处理参数
112
- // 不要去空格
113
- let newArgsRaw = argsRaw.replace(/ /g, " ");
114
- // 分割参数列表
115
- let rawArgsList = newArgsRaw.split(" -");
116
- // 去除空字符串
117
- rawArgsList = rawArgsList.filter((e) => {
118
- return e !== "";
119
- });
120
- rawArgsList.forEach((e) => {
121
- const argName = e.split("=")[0];
122
- const argValue = e.split("=")[1];
123
- // 判断是不是语音参数
124
- if (e.match(/.ogg|.mp3|.wav/)) {
125
- returnArrayList.push({
126
- key: "vocal",
127
- value: assetSetter(e, fileType.vocal)
128
- });
129
- }
130
- else {
131
- // 判断是不是省略参数
132
- if (argValue === undefined) {
133
- returnArrayList.push({
134
- key: argName,
135
- value: true
136
- });
137
- }
138
- else {
139
- // 是字符串描述的布尔值
140
- if (argValue === "true" || argValue === "false") {
141
- returnArrayList.push({
142
- key: argName,
143
- value: argValue === "true"
144
- });
145
- }
146
- else {
147
- // 是数字
148
- if (!isNaN(Number(argValue))) {
149
- returnArrayList.push({
150
- key: argName,
151
- value: Number(argValue)
152
- });
153
- }
154
- else {
155
- // 是普通参数
156
- returnArrayList.push({
157
- key: argName,
158
- value: argValue
159
- });
160
- }
161
- }
162
- }
163
- }
164
- });
165
- return returnArrayList;
104
+ /**
105
+ * 参数解析器
106
+ * @param argsRaw 原始参数字符串
107
+ * @param assetSetter
108
+ * @return {Array<arg>} 解析后的参数列表
109
+ */
110
+ function argsParser(argsRaw, assetSetter) {
111
+ const returnArrayList = [];
112
+ // 处理参数
113
+ // 不要去空格
114
+ let newArgsRaw = argsRaw.replace(/ /g, " ");
115
+ // 分割参数列表
116
+ let rawArgsList = newArgsRaw.split(" -");
117
+ // 去除空字符串
118
+ rawArgsList = rawArgsList.filter((e) => {
119
+ return e !== "";
120
+ });
121
+ rawArgsList.forEach((e) => {
122
+ const argName = e.split("=")[0];
123
+ const argValue = e.split("=")[1];
124
+ // 判断是不是语音参数
125
+ if (e.match(/.ogg|.mp3|.wav/)) {
126
+ returnArrayList.push({
127
+ key: "vocal",
128
+ value: assetSetter(e, fileType.vocal)
129
+ });
130
+ }
131
+ else {
132
+ // 判断是不是省略参数
133
+ if (argValue === undefined) {
134
+ returnArrayList.push({
135
+ key: argName,
136
+ value: true
137
+ });
138
+ }
139
+ else {
140
+ // 是字符串描述的布尔值
141
+ if (argValue === "true" || argValue === "false") {
142
+ returnArrayList.push({
143
+ key: argName,
144
+ value: argValue === "true"
145
+ });
146
+ }
147
+ else {
148
+ // 是数字
149
+ if (!isNaN(Number(argValue))) {
150
+ returnArrayList.push({
151
+ key: argName,
152
+ value: Number(argValue)
153
+ });
154
+ }
155
+ else {
156
+ // 是普通参数
157
+ returnArrayList.push({
158
+ key: argName,
159
+ value: argValue
160
+ });
161
+ }
162
+ }
163
+ }
164
+ }
165
+ });
166
+ return returnArrayList;
166
167
  }
167
168
 
168
- /**
169
- * 解析语句内容的函数,主要作用是把文件名改为绝对地址或相对地址(根据使用情况而定)
170
- * @param contentRaw 原始语句内容
171
- * @param type 语句类型
172
- * @param assetSetter
173
- * @return {string} 解析后的语句内容
174
- */
175
- const contentParser = (contentRaw, type, assetSetter) => {
176
- if (contentRaw === "none" || contentRaw === "") {
177
- return "";
178
- }
179
- switch (type) {
180
- case commandType.changeBg:
181
- return assetSetter(contentRaw, fileType.background);
182
- case commandType.changeFigure:
183
- return assetSetter(contentRaw, fileType.figure);
184
- case commandType.bgm:
185
- return assetSetter(contentRaw, fileType.bgm);
186
- case commandType.callScene:
187
- return assetSetter(contentRaw, fileType.scene);
188
- case commandType.changeScene:
189
- return assetSetter(contentRaw, fileType.scene);
190
- case commandType.miniAvatar:
191
- return assetSetter(contentRaw, fileType.figure);
192
- case commandType.video:
193
- return assetSetter(contentRaw, fileType.video);
194
- case commandType.choose:
195
- return getChooseContent(contentRaw, assetSetter);
196
- case commandType.unlockBgm:
197
- return assetSetter(contentRaw, fileType.bgm);
198
- case commandType.unlockCg:
199
- return assetSetter(contentRaw, fileType.background);
200
- default:
201
- return contentRaw;
202
- }
203
- };
204
- function getChooseContent(contentRaw, assetSetter) {
205
- const chooseList = contentRaw.split("|");
206
- const chooseKeyList = [];
207
- const chooseValueList = [];
208
- for (const e of chooseList) {
209
- chooseKeyList.push(e.split(":")[0]);
210
- chooseValueList.push(e.split(":")[1]);
211
- }
212
- const parsedChooseList = chooseValueList.map((e) => {
213
- if (e.match(/\./)) {
214
- return assetSetter(e, fileType.scene);
215
- }
216
- else {
217
- return e;
218
- }
219
- });
220
- let ret = "";
221
- for (let i = 0; i < chooseKeyList.length; i++) {
222
- if (i !== 0) {
223
- ret = ret + "|";
224
- }
225
- ret = ret + `${chooseKeyList[i]}:${parsedChooseList[i]}`;
226
- }
227
- return ret;
169
+ /**
170
+ * 解析语句内容的函数,主要作用是把文件名改为绝对地址或相对地址(根据使用情况而定)
171
+ * @param contentRaw 原始语句内容
172
+ * @param type 语句类型
173
+ * @param assetSetter
174
+ * @return {string} 解析后的语句内容
175
+ */
176
+ const contentParser = (contentRaw, type, assetSetter) => {
177
+ if (contentRaw === "none" || contentRaw === "") {
178
+ return "";
179
+ }
180
+ switch (type) {
181
+ case commandType.changeBg:
182
+ return assetSetter(contentRaw, fileType.background);
183
+ case commandType.changeFigure:
184
+ return assetSetter(contentRaw, fileType.figure);
185
+ case commandType.bgm:
186
+ return assetSetter(contentRaw, fileType.bgm);
187
+ case commandType.callScene:
188
+ return assetSetter(contentRaw, fileType.scene);
189
+ case commandType.changeScene:
190
+ return assetSetter(contentRaw, fileType.scene);
191
+ case commandType.miniAvatar:
192
+ return assetSetter(contentRaw, fileType.figure);
193
+ case commandType.video:
194
+ return assetSetter(contentRaw, fileType.video);
195
+ case commandType.choose:
196
+ return getChooseContent(contentRaw, assetSetter);
197
+ case commandType.unlockBgm:
198
+ return assetSetter(contentRaw, fileType.bgm);
199
+ case commandType.unlockCg:
200
+ return assetSetter(contentRaw, fileType.background);
201
+ case commandType.playEffect:
202
+ return assetSetter(contentRaw, fileType.vocal);
203
+ default:
204
+ return contentRaw;
205
+ }
206
+ };
207
+ function getChooseContent(contentRaw, assetSetter) {
208
+ const chooseList = contentRaw.split("|");
209
+ const chooseKeyList = [];
210
+ const chooseValueList = [];
211
+ for (const e of chooseList) {
212
+ chooseKeyList.push(e.split(":")[0]);
213
+ chooseValueList.push(e.split(":")[1]);
214
+ }
215
+ const parsedChooseList = chooseValueList.map((e) => {
216
+ if (e.match(/\./)) {
217
+ return assetSetter(e, fileType.scene);
218
+ }
219
+ else {
220
+ return e;
221
+ }
222
+ });
223
+ let ret = "";
224
+ for (let i = 0; i < chooseKeyList.length; i++) {
225
+ if (i !== 0) {
226
+ ret = ret + "|";
227
+ }
228
+ ret = ret + `${chooseKeyList[i]}:${parsedChooseList[i]}`;
229
+ }
230
+ return ret;
228
231
  }
229
232
 
230
- /**
231
- * 根据语句类型、语句内容、参数列表,扫描该语句可能携带的资源
232
- * @param command 语句类型
233
- * @param content 语句内容
234
- * @param args 参数列表
235
- * @return {Array<IAsset>} 语句携带的参数列表
236
- */
237
- const assetsScanner = (command, content, args) => {
238
- const returnAssetsList = [];
239
- if (command === commandType.say) {
240
- args.forEach((e) => {
241
- if (e.key === 'vocal') {
242
- returnAssetsList.push({
243
- name: e.value,
244
- url: e.value,
245
- lineNumber: 0,
246
- type: fileType.vocal,
247
- });
248
- }
249
- });
250
- }
251
- if (content === 'none' || content === '') {
252
- return returnAssetsList;
253
- }
254
- // 处理语句携带的资源
255
- if (command === commandType.changeBg) {
256
- returnAssetsList.push({
257
- name: content,
258
- url: content,
259
- lineNumber: 0,
260
- type: fileType.background,
261
- });
262
- }
263
- if (command === commandType.changeFigure) {
264
- returnAssetsList.push({
265
- name: content,
266
- url: content,
267
- lineNumber: 0,
268
- type: fileType.figure,
269
- });
270
- }
271
- if (command === commandType.miniAvatar) {
272
- returnAssetsList.push({
273
- name: content,
274
- url: content,
275
- lineNumber: 0,
276
- type: fileType.figure,
277
- });
278
- }
279
- if (command === commandType.video) {
280
- returnAssetsList.push({
281
- name: content,
282
- url: content,
283
- lineNumber: 0,
284
- type: fileType.video,
285
- });
286
- }
287
- if (command === commandType.bgm) {
288
- returnAssetsList.push({
289
- name: content,
290
- url: content,
291
- lineNumber: 0,
292
- type: fileType.bgm,
293
- });
294
- }
295
- return returnAssetsList;
233
+ /**
234
+ * 根据语句类型、语句内容、参数列表,扫描该语句可能携带的资源
235
+ * @param command 语句类型
236
+ * @param content 语句内容
237
+ * @param args 参数列表
238
+ * @return {Array<IAsset>} 语句携带的参数列表
239
+ */
240
+ const assetsScanner = (command, content, args) => {
241
+ const returnAssetsList = [];
242
+ if (command === commandType.say) {
243
+ args.forEach((e) => {
244
+ if (e.key === 'vocal') {
245
+ returnAssetsList.push({
246
+ name: e.value,
247
+ url: e.value,
248
+ lineNumber: 0,
249
+ type: fileType.vocal,
250
+ });
251
+ }
252
+ });
253
+ }
254
+ if (content === 'none' || content === '') {
255
+ return returnAssetsList;
256
+ }
257
+ // 处理语句携带的资源
258
+ if (command === commandType.changeBg) {
259
+ returnAssetsList.push({
260
+ name: content,
261
+ url: content,
262
+ lineNumber: 0,
263
+ type: fileType.background,
264
+ });
265
+ }
266
+ if (command === commandType.changeFigure) {
267
+ returnAssetsList.push({
268
+ name: content,
269
+ url: content,
270
+ lineNumber: 0,
271
+ type: fileType.figure,
272
+ });
273
+ }
274
+ if (command === commandType.miniAvatar) {
275
+ returnAssetsList.push({
276
+ name: content,
277
+ url: content,
278
+ lineNumber: 0,
279
+ type: fileType.figure,
280
+ });
281
+ }
282
+ if (command === commandType.video) {
283
+ returnAssetsList.push({
284
+ name: content,
285
+ url: content,
286
+ lineNumber: 0,
287
+ type: fileType.video,
288
+ });
289
+ }
290
+ if (command === commandType.bgm) {
291
+ returnAssetsList.push({
292
+ name: content,
293
+ url: content,
294
+ lineNumber: 0,
295
+ type: fileType.bgm,
296
+ });
297
+ }
298
+ if (command === commandType.playEffect) {
299
+ returnAssetsList.push({
300
+ name: content,
301
+ url: content,
302
+ lineNumber: 0,
303
+ type: fileType.vocal,
304
+ });
305
+ }
306
+ return returnAssetsList;
296
307
  };
297
308
 
298
- /**
299
- * 扫描子场景
300
- * @param content 语句内容
301
- * @return {Array<string>} 子场景列表
302
- */
303
- const subSceneScanner = (command, content) => {
304
- const subSceneList = [];
305
- if (command === commandType.changeScene || command === commandType.callScene) {
306
- subSceneList.push(content);
307
- }
308
- if (command === commandType.choose) {
309
- const chooseList = content.split('|');
310
- const chooseValue = chooseList.map((e) => e.split(':')[1]);
311
- chooseValue.forEach((e) => {
312
- if (e.match(/\./)) {
313
- subSceneList.push(e);
314
- }
315
- });
316
- }
317
- return subSceneList;
309
+ /**
310
+ * 扫描子场景
311
+ * @param content 语句内容
312
+ * @return {Array<string>} 子场景列表
313
+ */
314
+ const subSceneScanner = (command, content) => {
315
+ const subSceneList = [];
316
+ if (command === commandType.changeScene || command === commandType.callScene) {
317
+ subSceneList.push(content);
318
+ }
319
+ if (command === commandType.choose) {
320
+ const chooseList = content.split('|');
321
+ const chooseValue = chooseList.map((e) => e.split(':')[1]);
322
+ chooseValue.forEach((e) => {
323
+ if (e.match(/\./)) {
324
+ subSceneList.push(e);
325
+ }
326
+ });
327
+ }
328
+ return subSceneList;
318
329
  };
319
330
 
320
- /**
321
- * 语句解析器
322
- * @param sentenceRaw 原始语句
323
- * @param assetSetter
324
- * @param ADD_NEXT_ARG_LIST
325
- * @param SCRIPT_CONFIG
326
- */
327
- const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
328
- let command; // 默认为对话
329
- let content; // 语句内容
330
- let subScene; // 语句携带的子场景(可能没有)
331
- const args = []; // 语句参数列表
332
- let sentenceAssets; // 语句携带的资源列表
333
- let parsedCommand; // 解析后的命令
334
- let commandRaw;
335
- // 正式开始解析
336
- // 去分号,前面已做,这里不再需要
337
- let newSentenceRaw = sentenceRaw;
338
- // 截取命令
339
- const getCommandResult = /:/.exec(newSentenceRaw);
340
- /**
341
- * 拆分命令和语句,同时处理连续对话。
342
- */
343
- // 没有command,说明这是一条连续对话或单条语句
344
- if (getCommandResult === null) {
345
- commandRaw = newSentenceRaw;
346
- parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
347
- command = parsedCommand.type;
348
- for (const e of parsedCommand.additionalArgs) {
349
- // 由于是连续对话,所以我们去除 speaker 参数。
350
- if (command === commandType.say && e.key === "speaker") {
351
- continue;
352
- }
353
- args.push(e);
354
- }
355
- }
356
- else {
357
- commandRaw = newSentenceRaw.substring(0, getCommandResult.index);
358
- // 划分命令区域和content区域
359
- newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
360
- parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
361
- command = parsedCommand.type;
362
- for (const e of parsedCommand.additionalArgs) {
363
- args.push(e);
364
- }
365
- }
366
- // 截取参数区域
367
- const getArgsResult = / -/.exec(newSentenceRaw);
368
- // 获取到参数
369
- if (getArgsResult) {
370
- const argsRaw = newSentenceRaw.substring(getArgsResult.index, sentenceRaw.length);
371
- newSentenceRaw = newSentenceRaw.substring(0, getArgsResult.index);
372
- for (const e of argsParser(argsRaw, assetSetter)) {
373
- args.push(e);
374
- }
375
- }
376
- content = contentParser(newSentenceRaw, command, assetSetter); // 将语句内容里的文件名转为相对或绝对路径
377
- sentenceAssets = assetsScanner(command, content, args); // 扫描语句携带资源
378
- subScene = subSceneScanner(command, content); // 扫描语句携带子场景
379
- return {
380
- command: command,
381
- commandRaw: commandRaw,
382
- content: content,
383
- args: args,
384
- sentenceAssets: sentenceAssets,
385
- subScene: subScene // 语句携带的子场景
386
- };
331
+ /**
332
+ * 语句解析器
333
+ * @param sentenceRaw 原始语句
334
+ * @param assetSetter
335
+ * @param ADD_NEXT_ARG_LIST
336
+ * @param SCRIPT_CONFIG
337
+ */
338
+ const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
339
+ let command; // 默认为对话
340
+ let content; // 语句内容
341
+ let subScene; // 语句携带的子场景(可能没有)
342
+ const args = []; // 语句参数列表
343
+ let sentenceAssets; // 语句携带的资源列表
344
+ let parsedCommand; // 解析后的命令
345
+ let commandRaw;
346
+ // 正式开始解析
347
+ // 去分号,前面已做,这里不再需要
348
+ let newSentenceRaw = sentenceRaw;
349
+ // 截取命令
350
+ const getCommandResult = /:/.exec(newSentenceRaw);
351
+ /**
352
+ * 拆分命令和语句,同时处理连续对话。
353
+ */
354
+ // 没有command,说明这是一条连续对话或单条语句
355
+ if (getCommandResult === null) {
356
+ commandRaw = newSentenceRaw;
357
+ parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
358
+ command = parsedCommand.type;
359
+ for (const e of parsedCommand.additionalArgs) {
360
+ // 由于是连续对话,所以我们去除 speaker 参数。
361
+ if (command === commandType.say && e.key === "speaker") {
362
+ continue;
363
+ }
364
+ args.push(e);
365
+ }
366
+ }
367
+ else {
368
+ commandRaw = newSentenceRaw.substring(0, getCommandResult.index);
369
+ // 划分命令区域和content区域
370
+ newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
371
+ parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
372
+ command = parsedCommand.type;
373
+ for (const e of parsedCommand.additionalArgs) {
374
+ args.push(e);
375
+ }
376
+ }
377
+ // 截取参数区域
378
+ const getArgsResult = / -/.exec(newSentenceRaw);
379
+ // 获取到参数
380
+ if (getArgsResult) {
381
+ const argsRaw = newSentenceRaw.substring(getArgsResult.index, sentenceRaw.length);
382
+ newSentenceRaw = newSentenceRaw.substring(0, getArgsResult.index);
383
+ for (const e of argsParser(argsRaw, assetSetter)) {
384
+ args.push(e);
385
+ }
386
+ }
387
+ content = contentParser(newSentenceRaw, command, assetSetter); // 将语句内容里的文件名转为相对或绝对路径
388
+ sentenceAssets = assetsScanner(command, content, args); // 扫描语句携带资源
389
+ subScene = subSceneScanner(command, content); // 扫描语句携带子场景
390
+ return {
391
+ command: command,
392
+ commandRaw: commandRaw,
393
+ content: content,
394
+ args: args,
395
+ sentenceAssets: sentenceAssets,
396
+ subScene: subScene // 语句携带的子场景
397
+ };
387
398
  };
388
399
 
389
- /**
390
- * 场景解析器
391
- * @param rawScene 原始场景
392
- * @param sceneName 场景名称
393
- * @param sceneUrl 场景url
394
- * @param assetsPrefetcher
395
- * @param assetSetter
396
- * @param ADD_NEXT_ARG_LIST
397
- * @param SCRIPT_CONFIG
398
- * @return {IScene} 解析后的场景
399
- */
400
- const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
401
- const rawSentenceList = rawScene.split("\n"); // 原始句子列表
402
- // 去除冒号后的内容
403
- // 去除分号后的内容
404
- const rawSentenceListWithoutEmpty = rawSentenceList
405
- .map((sentence) => sentence.split(";")[0])
406
- .filter((sentence) => sentence.trim() !== "");
407
- let assetsList = []; // 场景资源列表
408
- let subSceneList = []; // 子场景列表
409
- const sentenceList = rawSentenceListWithoutEmpty.map((sentence) => {
410
- const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
411
- // 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
412
- assetsList = [...assetsList, ...returnSentence.sentenceAssets];
413
- subSceneList = [...subSceneList, ...returnSentence.subScene];
414
- return returnSentence;
415
- });
416
- // 开始资源的预加载
417
- assetsList = __.uniqWith(assetsList); // 去重
418
- assetsPrefetcher(assetsList);
419
- return {
420
- sceneName: sceneName,
421
- sceneUrl: sceneUrl,
422
- sentenceList: sentenceList,
423
- assetsList: assetsList,
424
- subSceneList: subSceneList // 子场景列表
425
- };
400
+ /**
401
+ * 场景解析器
402
+ * @param rawScene 原始场景
403
+ * @param sceneName 场景名称
404
+ * @param sceneUrl 场景url
405
+ * @param assetsPrefetcher
406
+ * @param assetSetter
407
+ * @param ADD_NEXT_ARG_LIST
408
+ * @param SCRIPT_CONFIG
409
+ * @return {IScene} 解析后的场景
410
+ */
411
+ const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
412
+ const rawSentenceList = rawScene.split("\n"); // 原始句子列表
413
+ // 去除冒号后的内容
414
+ // 去除分号后的内容
415
+ const rawSentenceListWithoutEmpty = rawSentenceList
416
+ .map((sentence) => sentence.split(";")[0])
417
+ .filter((sentence) => sentence.trim() !== "");
418
+ let assetsList = []; // 场景资源列表
419
+ let subSceneList = []; // 子场景列表
420
+ const sentenceList = rawSentenceListWithoutEmpty.map((sentence) => {
421
+ const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
422
+ // 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
423
+ assetsList = [...assetsList, ...returnSentence.sentenceAssets];
424
+ subSceneList = [...subSceneList, ...returnSentence.subScene];
425
+ return returnSentence;
426
+ });
427
+ // 开始资源的预加载
428
+ assetsList = __.uniqWith(assetsList); // 去重
429
+ assetsPrefetcher(assetsList);
430
+ return {
431
+ sceneName: sceneName,
432
+ sceneUrl: sceneUrl,
433
+ sentenceList: sentenceList,
434
+ assetsList: assetsList,
435
+ subSceneList: subSceneList // 子场景列表
436
+ };
426
437
  };
427
438
 
428
- class SceneParser {
429
- assetsPrefetcher;
430
- assetSetter;
431
- ADD_NEXT_ARG_LIST;
432
- SCRIPT_CONFIG;
433
- constructor(assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
434
- this.assetsPrefetcher = assetsPrefetcher;
435
- this.assetSetter = assetSetter;
436
- this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
437
- this.SCRIPT_CONFIG = SCRIPT_CONFIG;
438
- }
439
- parse(rawScene, sceneName, sceneUrl) {
440
- return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
441
- }
439
+ class SceneParser {
440
+ assetsPrefetcher;
441
+ assetSetter;
442
+ ADD_NEXT_ARG_LIST;
443
+ SCRIPT_CONFIG;
444
+ constructor(assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
445
+ this.assetsPrefetcher = assetsPrefetcher;
446
+ this.assetSetter = assetSetter;
447
+ this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
448
+ this.SCRIPT_CONFIG = SCRIPT_CONFIG;
449
+ }
450
+ parse(rawScene, sceneName, sceneUrl) {
451
+ return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
452
+ }
442
453
  }
443
454
 
444
455
  export { SceneParser as default };