webgal-parser 4.4.9 → 4.4.14-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 (48) hide show
  1. package/.prettierrc +19 -0
  2. package/build/cjs/config/scriptConfig.d.ts +6 -1
  3. package/build/cjs/index.cjs +232 -182
  4. package/build/cjs/index.d.ts +15 -6
  5. package/build/cjs/interface/sceneInterface.d.ts +4 -3
  6. package/build/cjs/sceneParser.d.ts +5 -4
  7. package/build/cjs/scriptParser/argsParser.d.ts +2 -2
  8. package/build/cjs/scriptParser/commandParser.d.ts +4 -3
  9. package/build/cjs/scriptParser/contentParser.d.ts +1 -1
  10. package/build/cjs/scriptParser/scriptParser.d.ts +4 -3
  11. package/build/cjs/scriptParser/subSceneScanner.d.ts +1 -1
  12. package/build/cjs/styleParser/index.d.ts +5 -0
  13. package/build/es/config/scriptConfig.d.ts +6 -1
  14. package/build/es/index.d.ts +15 -6
  15. package/build/es/index.js +232 -182
  16. package/build/es/interface/sceneInterface.d.ts +4 -3
  17. package/build/es/sceneParser.d.ts +5 -4
  18. package/build/es/scriptParser/argsParser.d.ts +2 -2
  19. package/build/es/scriptParser/commandParser.d.ts +4 -3
  20. package/build/es/scriptParser/contentParser.d.ts +1 -1
  21. package/build/es/scriptParser/scriptParser.d.ts +4 -3
  22. package/build/es/scriptParser/subSceneScanner.d.ts +1 -1
  23. package/build/es/styleParser/index.d.ts +5 -0
  24. package/build/types/config/scriptConfig.d.ts +6 -1
  25. package/build/types/index.d.ts +15 -6
  26. package/build/types/interface/sceneInterface.d.ts +4 -3
  27. package/build/types/sceneParser.d.ts +5 -4
  28. package/build/types/scriptParser/argsParser.d.ts +2 -2
  29. package/build/types/scriptParser/commandParser.d.ts +4 -3
  30. package/build/types/scriptParser/contentParser.d.ts +1 -1
  31. package/build/types/scriptParser/scriptParser.d.ts +4 -3
  32. package/build/types/scriptParser/subSceneScanner.d.ts +1 -1
  33. package/build/types/styleParser/index.d.ts +5 -0
  34. package/build/umd/index.global.js +232 -182
  35. package/package.json +2 -2
  36. package/src/config/scriptConfig.ts +9 -5
  37. package/src/configParser/configParser.ts +37 -25
  38. package/src/index.ts +66 -30
  39. package/src/interface/sceneInterface.ts +4 -3
  40. package/src/parser4/index.ts +1 -1
  41. package/src/sceneParser.ts +36 -17
  42. package/src/scriptParser/argsParser.ts +17 -14
  43. package/src/scriptParser/assetsScanner.ts +5 -1
  44. package/src/scriptParser/commandParser.ts +31 -23
  45. package/src/scriptParser/contentParser.ts +14 -10
  46. package/src/scriptParser/scriptParser.ts +38 -20
  47. package/src/scriptParser/subSceneScanner.ts +11 -5
  48. package/src/styleParser/index.ts +39 -0
@@ -35,63 +35,54 @@ var webgalParser = (function (exports) {
35
35
  commandType[commandType["setTransform"] = 29] = "setTransform";
36
36
  commandType[commandType["setTransition"] = 30] = "setTransition";
37
37
  commandType[commandType["getUserInput"] = 31] = "getUserInput";
38
+ commandType[commandType["applyStyle"] = 32] = "applyStyle";
38
39
  })(commandType || (commandType = {}));
39
40
 
40
- /**
41
- * 处理命令
42
- * @param commandRaw
43
- * @param ADD_NEXT_ARG_LIST
44
- * @param SCRIPT_CONFIG
45
- * @return {parsedCommand} 处理后的命令
46
- */
47
- const commandParser = (commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
48
- let returnCommand = {
49
- type: commandType.say,
50
- additionalArgs: []
51
- };
52
- // 开始处理命令内容
53
- const type = getCommandType(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
54
- returnCommand.type = type;
55
- // 如果是对话,加上额外的参数
56
- if (type === commandType.say && commandRaw !== 'say') {
57
- returnCommand.additionalArgs.push({
58
- key: "speaker",
59
- value: commandRaw
60
- });
61
- }
62
- returnCommand = addNextArg(returnCommand, type, ADD_NEXT_ARG_LIST);
63
- return returnCommand;
64
- };
65
- /**
66
- * 根据command原始值判断是什么命令
67
- * @param command command原始值
68
- * @param ADD_NEXT_ARG_LIST
69
- * @param SCRIPT_CONFIG
70
- * @return {commandType} 得到的command类型
71
- */
72
- function getCommandType(command, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
73
- // if (command.match(/if/)) {
74
- // return commandType.if;
75
- // }
76
- const commandMap = new Map();
77
- SCRIPT_CONFIG.forEach((e) => {
78
- commandMap.set(e.scriptString, e.scriptType);
79
- });
80
- if (commandMap.has(command)) {
81
- return commandMap.get(command);
82
- }
83
- else
84
- return commandType.say;
85
- }
86
- function addNextArg(commandToParse, thisCommandType, ADD_NEXT_ARG_LIST) {
87
- if (ADD_NEXT_ARG_LIST.includes(thisCommandType)) {
88
- commandToParse.additionalArgs.push({
89
- key: "next",
90
- value: true
91
- });
92
- }
93
- return commandToParse;
94
- }
41
+ const SCRIPT_CONFIG = [
42
+ { scriptString: 'intro', scriptType: commandType.intro },
43
+ { scriptString: 'changeBg', scriptType: commandType.changeBg },
44
+ { scriptString: 'changeFigure', scriptType: commandType.changeFigure },
45
+ { scriptString: 'miniAvatar', scriptType: commandType.miniAvatar },
46
+ { scriptString: 'changeScene', scriptType: commandType.changeScene },
47
+ { scriptString: 'choose', scriptType: commandType.choose },
48
+ { scriptString: 'end', scriptType: commandType.end },
49
+ { scriptString: 'bgm', scriptType: commandType.bgm },
50
+ { scriptString: 'playVideo', scriptType: commandType.video },
51
+ {
52
+ scriptString: 'setComplexAnimation',
53
+ scriptType: commandType.setComplexAnimation,
54
+ },
55
+ { scriptString: 'setFilter', scriptType: commandType.setFilter },
56
+ { scriptString: 'pixiInit', scriptType: commandType.pixiInit },
57
+ { scriptString: 'pixiPerform', scriptType: commandType.pixi },
58
+ { scriptString: 'label', scriptType: commandType.label },
59
+ { scriptString: 'jumpLabel', scriptType: commandType.jumpLabel },
60
+ { scriptString: 'setVar', scriptType: commandType.setVar },
61
+ { scriptString: 'callScene', scriptType: commandType.callScene },
62
+ { scriptString: 'showVars', scriptType: commandType.showVars },
63
+ { scriptString: 'unlockCg', scriptType: commandType.unlockCg },
64
+ { scriptString: 'unlockBgm', scriptType: commandType.unlockBgm },
65
+ { scriptString: 'say', scriptType: commandType.say },
66
+ { scriptString: 'filmMode', scriptType: commandType.filmMode },
67
+ { scriptString: 'callScene', scriptType: commandType.callScene },
68
+ { scriptString: 'setTextbox', scriptType: commandType.setTextbox },
69
+ { scriptString: 'setAnimation', scriptType: commandType.setAnimation },
70
+ { scriptString: 'playEffect', scriptType: commandType.playEffect },
71
+ { scriptString: 'applyStyle', scriptType: commandType.applyStyle },
72
+ ];
73
+ const ADD_NEXT_ARG_LIST = [
74
+ commandType.bgm,
75
+ commandType.pixi,
76
+ commandType.pixiInit,
77
+ commandType.label,
78
+ commandType.if,
79
+ commandType.miniAvatar,
80
+ commandType.setVar,
81
+ commandType.unlockBgm,
82
+ commandType.unlockCg,
83
+ commandType.filmMode,
84
+ commandType.playEffect,
85
+ ];
95
86
 
96
87
  /**
97
88
  * 内置资源类型的枚举
@@ -117,12 +108,12 @@ var webgalParser = (function (exports) {
117
108
  const returnArrayList = [];
118
109
  // 处理参数
119
110
  // 不要去空格
120
- let newArgsRaw = argsRaw.replace(/ /g, " ");
111
+ let newArgsRaw = argsRaw.replace(/ /g, ' ');
121
112
  // 分割参数列表
122
- let rawArgsList = newArgsRaw.split(" -");
113
+ let rawArgsList = newArgsRaw.split(' -');
123
114
  // 去除空字符串
124
115
  rawArgsList = rawArgsList.filter((e) => {
125
- return e !== "";
116
+ return e !== '';
126
117
  });
127
118
  rawArgsList.forEach((e) => {
128
119
  const equalSignIndex = e.indexOf('=');
@@ -135,8 +126,8 @@ var webgalParser = (function (exports) {
135
126
  // 判断是不是语音参数
136
127
  if (argName.toLowerCase().match(/.ogg|.mp3|.wav/)) {
137
128
  returnArrayList.push({
138
- key: "vocal",
139
- value: assetSetter(e, fileType.vocal)
129
+ key: 'vocal',
130
+ value: assetSetter(e, fileType.vocal),
140
131
  });
141
132
  }
142
133
  else {
@@ -144,15 +135,15 @@ var webgalParser = (function (exports) {
144
135
  if (argValue === undefined) {
145
136
  returnArrayList.push({
146
137
  key: argName,
147
- value: true
138
+ value: true,
148
139
  });
149
140
  }
150
141
  else {
151
142
  // 是字符串描述的布尔值
152
- if (argValue === "true" || argValue === "false") {
143
+ if (argValue === 'true' || argValue === 'false') {
153
144
  returnArrayList.push({
154
145
  key: argName,
155
- value: argValue === "true"
146
+ value: argValue === 'true',
156
147
  });
157
148
  }
158
149
  else {
@@ -160,14 +151,14 @@ var webgalParser = (function (exports) {
160
151
  if (!isNaN(Number(argValue))) {
161
152
  returnArrayList.push({
162
153
  key: argName,
163
- value: Number(argValue)
154
+ value: Number(argValue),
164
155
  });
165
156
  }
166
157
  else {
167
158
  // 是普通参数
168
159
  returnArrayList.push({
169
160
  key: argName,
170
- value: argValue
161
+ value: argValue,
171
162
  });
172
163
  }
173
164
  }
@@ -177,6 +168,102 @@ var webgalParser = (function (exports) {
177
168
  return returnArrayList;
178
169
  }
179
170
 
171
+ function configLineParser(inputLine) {
172
+ const options = [];
173
+ let command;
174
+ let newSentenceRaw = inputLine.split(';')[0];
175
+ if (newSentenceRaw === '') {
176
+ // 注释提前返回
177
+ return {
178
+ command: '',
179
+ args: [],
180
+ options: [],
181
+ };
182
+ }
183
+ // 截取命令
184
+ const getCommandResult = /:/.exec(newSentenceRaw);
185
+ // 没有command
186
+ if (getCommandResult === null) {
187
+ command = '';
188
+ }
189
+ else {
190
+ command = newSentenceRaw.substring(0, getCommandResult.index);
191
+ // 划分命令区域和content区域
192
+ newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
193
+ }
194
+ // 截取 Options 区域
195
+ const getOptionsResult = / -/.exec(newSentenceRaw);
196
+ // 获取到参数
197
+ if (getOptionsResult) {
198
+ const optionsRaw = newSentenceRaw.substring(getOptionsResult.index, newSentenceRaw.length);
199
+ newSentenceRaw = newSentenceRaw.substring(0, getOptionsResult.index);
200
+ for (const e of argsParser(optionsRaw, (name, _) => {
201
+ return name;
202
+ })) {
203
+ options.push(e);
204
+ }
205
+ }
206
+ return {
207
+ command,
208
+ args: newSentenceRaw
209
+ .split('|')
210
+ .map((e) => e.trim())
211
+ .filter((e) => e !== ''),
212
+ options,
213
+ };
214
+ }
215
+ function configParser(configText) {
216
+ const configLines = configText.replaceAll(`\r`, '').split('\n');
217
+ return configLines
218
+ .map((e) => configLineParser(e))
219
+ .filter((e) => e.command !== '');
220
+ }
221
+
222
+ /**
223
+ * 处理命令
224
+ * @param commandRaw
225
+ * @param ADD_NEXT_ARG_LIST
226
+ * @param SCRIPT_CONFIG_MAP
227
+ * @return {parsedCommand} 处理后的命令
228
+ */
229
+ const commandParser = (commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP) => {
230
+ let returnCommand = {
231
+ type: commandType.say,
232
+ additionalArgs: [],
233
+ };
234
+ // 开始处理命令内容
235
+ const type = getCommandType(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP);
236
+ returnCommand.type = type;
237
+ // 如果是对话,加上额外的参数
238
+ if (type === commandType.say && commandRaw !== 'say') {
239
+ returnCommand.additionalArgs.push({
240
+ key: 'speaker',
241
+ value: commandRaw,
242
+ });
243
+ }
244
+ returnCommand = addNextArg(returnCommand, type, ADD_NEXT_ARG_LIST);
245
+ return returnCommand;
246
+ };
247
+ /**
248
+ * 根据command原始值判断是什么命令
249
+ * @param command command原始值
250
+ * @param ADD_NEXT_ARG_LIST
251
+ * @param SCRIPT_CONFIG_MAP
252
+ * @return {commandType} 得到的command类型
253
+ */
254
+ function getCommandType(command, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP) {
255
+ return SCRIPT_CONFIG_MAP.get(command)?.scriptType ?? commandType.say;
256
+ }
257
+ function addNextArg(commandToParse, thisCommandType, ADD_NEXT_ARG_LIST) {
258
+ if (ADD_NEXT_ARG_LIST.includes(thisCommandType)) {
259
+ commandToParse.additionalArgs.push({
260
+ key: 'next',
261
+ value: true,
262
+ });
263
+ }
264
+ return commandToParse;
265
+ }
266
+
180
267
  /**
181
268
  * 解析语句内容的函数,主要作用是把文件名改为绝对地址或相对地址(根据使用情况而定)
182
269
  * @param contentRaw 原始语句内容
@@ -185,8 +272,8 @@ var webgalParser = (function (exports) {
185
272
  * @return {string} 解析后的语句内容
186
273
  */
187
274
  const contentParser = (contentRaw, type, assetSetter) => {
188
- if (contentRaw === "none" || contentRaw === "") {
189
- return "";
275
+ if (contentRaw === 'none' || contentRaw === '') {
276
+ return '';
190
277
  }
191
278
  switch (type) {
192
279
  case commandType.playEffect:
@@ -216,12 +303,12 @@ var webgalParser = (function (exports) {
216
303
  }
217
304
  };
218
305
  function getChooseContent(contentRaw, assetSetter) {
219
- const chooseList = contentRaw.split("|");
306
+ const chooseList = contentRaw.split('|');
220
307
  const chooseKeyList = [];
221
308
  const chooseValueList = [];
222
309
  for (const e of chooseList) {
223
- chooseKeyList.push(e.split(":")[0] ?? "");
224
- chooseValueList.push(e.split(":")[1] ?? "");
310
+ chooseKeyList.push(e.split(':')[0] ?? '');
311
+ chooseValueList.push(e.split(':')[1] ?? '');
225
312
  }
226
313
  const parsedChooseList = chooseValueList.map((e) => {
227
314
  if (e.match(/\./)) {
@@ -231,10 +318,10 @@ var webgalParser = (function (exports) {
231
318
  return e;
232
319
  }
233
320
  });
234
- let ret = "";
321
+ let ret = '';
235
322
  for (let i = 0; i < chooseKeyList.length; i++) {
236
323
  if (i !== 0) {
237
- ret = ret + "|";
324
+ ret = ret + '|';
238
325
  }
239
326
  ret = ret + `${chooseKeyList[i]}:${parsedChooseList[i]}`;
240
327
  }
@@ -316,12 +403,13 @@ var webgalParser = (function (exports) {
316
403
  */
317
404
  const subSceneScanner = (command, content) => {
318
405
  const subSceneList = [];
319
- if (command === commandType.changeScene || command === commandType.callScene) {
406
+ if (command === commandType.changeScene ||
407
+ command === commandType.callScene) {
320
408
  subSceneList.push(content);
321
409
  }
322
410
  if (command === commandType.choose) {
323
- const chooseList = content.split("|");
324
- const chooseValue = chooseList.map((e) => e.split(":")[1] ?? "");
411
+ const chooseList = content.split('|');
412
+ const chooseValue = chooseList.map((e) => e.split(':')[1] ?? '');
325
413
  chooseValue.forEach((e) => {
326
414
  if (e.match(/\./)) {
327
415
  subSceneList.push(e);
@@ -336,9 +424,9 @@ var webgalParser = (function (exports) {
336
424
  * @param sentenceRaw 原始语句
337
425
  * @param assetSetter
338
426
  * @param ADD_NEXT_ARG_LIST
339
- * @param SCRIPT_CONFIG
427
+ * @param SCRIPT_CONFIG_MAP
340
428
  */
341
- const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
429
+ const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP) => {
342
430
  let command; // 默认为对话
343
431
  let content; // 语句内容
344
432
  let subScene; // 语句携带的子场景(可能没有)
@@ -348,16 +436,16 @@ var webgalParser = (function (exports) {
348
436
  let commandRaw;
349
437
  // 正式开始解析
350
438
  // 去分号,前面已做,这里不再需要
351
- let newSentenceRaw = sentenceRaw.split(";")[0];
352
- if (newSentenceRaw === "") {
439
+ let newSentenceRaw = sentenceRaw.split(';')[0];
440
+ if (newSentenceRaw === '') {
353
441
  // 注释提前返回
354
442
  return {
355
443
  command: commandType.comment,
356
- commandRaw: "comment",
357
- content: sentenceRaw.split(";")[1] ?? "",
358
- args: [{ key: "next", value: true }],
444
+ commandRaw: 'comment',
445
+ content: sentenceRaw.split(';')[1] ?? '',
446
+ args: [{ key: 'next', value: true }],
359
447
  sentenceAssets: [],
360
- subScene: [] // 语句携带的子场景
448
+ subScene: [], // 语句携带的子场景
361
449
  };
362
450
  }
363
451
  // 截取命令
@@ -368,11 +456,11 @@ var webgalParser = (function (exports) {
368
456
  // 没有command,说明这是一条连续对话或单条语句
369
457
  if (getCommandResult === null) {
370
458
  commandRaw = newSentenceRaw;
371
- parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
459
+ parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP);
372
460
  command = parsedCommand.type;
373
461
  for (const e of parsedCommand.additionalArgs) {
374
462
  // 由于是连续对话,所以我们去除 speaker 参数。
375
- if (command === commandType.say && e.key === "speaker") {
463
+ if (command === commandType.say && e.key === 'speaker') {
376
464
  continue;
377
465
  }
378
466
  args.push(e);
@@ -382,7 +470,7 @@ var webgalParser = (function (exports) {
382
470
  commandRaw = newSentenceRaw.substring(0, getCommandResult.index);
383
471
  // 划分命令区域和content区域
384
472
  newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
385
- parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
473
+ parsedCommand = commandParser(commandRaw, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP);
386
474
  command = parsedCommand.type;
387
475
  for (const e of parsedCommand.additionalArgs) {
388
476
  args.push(e);
@@ -407,7 +495,7 @@ var webgalParser = (function (exports) {
407
495
  content: content,
408
496
  args: args,
409
497
  sentenceAssets: sentenceAssets,
410
- subScene: subScene // 语句携带的子场景
498
+ subScene: subScene, // 语句携带的子场景
411
499
  };
412
500
  };
413
501
 
@@ -1667,11 +1755,11 @@ var webgalParser = (function (exports) {
1667
1755
  * @param assetsPrefetcher
1668
1756
  * @param assetSetter
1669
1757
  * @param ADD_NEXT_ARG_LIST
1670
- * @param SCRIPT_CONFIG
1758
+ * @param SCRIPT_CONFIG_MAP
1671
1759
  * @return {IScene} 解析后的场景
1672
1760
  */
1673
- const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) => {
1674
- const rawSentenceList = rawScene.split("\n"); // 原始句子列表
1761
+ const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP) => {
1762
+ const rawSentenceList = rawScene.split('\n'); // 原始句子列表
1675
1763
  // 去分号留到后面去做了,现在注释要单独处理
1676
1764
  const rawSentenceListWithoutEmpty = rawSentenceList;
1677
1765
  // .map((sentence) => sentence.split(";")[0])
@@ -1679,7 +1767,7 @@ var webgalParser = (function (exports) {
1679
1767
  let assetsList = []; // 场景资源列表
1680
1768
  let subSceneList = []; // 子场景列表
1681
1769
  const sentenceList = rawSentenceListWithoutEmpty.map((sentence) => {
1682
- const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
1770
+ const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP);
1683
1771
  // 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
1684
1772
  assetsList = [...assetsList, ...returnSentence.sentenceAssets];
1685
1773
  subSceneList = [...subSceneList, ...returnSentence.subScene];
@@ -1693,119 +1781,81 @@ var webgalParser = (function (exports) {
1693
1781
  sceneUrl: sceneUrl,
1694
1782
  sentenceList: sentenceList,
1695
1783
  assetsList: assetsList,
1696
- subSceneList: subSceneList // 子场景列表
1784
+ subSceneList: subSceneList, // 子场景列表
1697
1785
  };
1698
1786
  };
1699
1787
 
1700
- const SCRIPT_CONFIG = [
1701
- { scriptString: 'intro', scriptType: commandType.intro },
1702
- { scriptString: 'changeBg', scriptType: commandType.changeBg },
1703
- { scriptString: 'changeFigure', scriptType: commandType.changeFigure },
1704
- { scriptString: 'miniAvatar', scriptType: commandType.miniAvatar },
1705
- { scriptString: 'changeScene', scriptType: commandType.changeScene },
1706
- { scriptString: 'choose', scriptType: commandType.choose },
1707
- { scriptString: 'end', scriptType: commandType.end },
1708
- { scriptString: 'bgm', scriptType: commandType.bgm },
1709
- { scriptString: 'playVideo', scriptType: commandType.video },
1710
- {
1711
- scriptString: 'setComplexAnimation',
1712
- scriptType: commandType.setComplexAnimation,
1713
- },
1714
- { scriptString: 'setFilter', scriptType: commandType.setFilter },
1715
- { scriptString: 'pixiInit', scriptType: commandType.pixiInit },
1716
- { scriptString: 'pixiPerform', scriptType: commandType.pixi },
1717
- { scriptString: 'label', scriptType: commandType.label },
1718
- { scriptString: 'jumpLabel', scriptType: commandType.jumpLabel },
1719
- { scriptString: 'setVar', scriptType: commandType.setVar },
1720
- { scriptString: 'callScene', scriptType: commandType.callScene },
1721
- { scriptString: 'showVars', scriptType: commandType.showVars },
1722
- { scriptString: 'unlockCg', scriptType: commandType.unlockCg },
1723
- { scriptString: 'unlockBgm', scriptType: commandType.unlockBgm },
1724
- { scriptString: 'say', scriptType: commandType.say },
1725
- { scriptString: 'filmMode', scriptType: commandType.filmMode },
1726
- { scriptString: 'callScene', scriptType: commandType.callScene },
1727
- { scriptString: 'setTextbox', scriptType: commandType.setTextbox },
1728
- { scriptString: 'setAnimation', scriptType: commandType.setAnimation },
1729
- { scriptString: 'playEffect', scriptType: commandType.playEffect },
1730
- ];
1731
- const ADD_NEXT_ARG_LIST = [
1732
- commandType.bgm,
1733
- commandType.pixi,
1734
- commandType.pixiInit,
1735
- commandType.label,
1736
- commandType.if,
1737
- commandType.miniAvatar,
1738
- commandType.setVar,
1739
- commandType.unlockBgm,
1740
- commandType.unlockCg,
1741
- commandType.filmMode,
1742
- commandType.playEffect,
1743
- ];
1744
-
1745
- function configLineParser(inputLine) {
1746
- const options = [];
1747
- let command;
1748
- let newSentenceRaw = inputLine.split(";")[0];
1749
- if (newSentenceRaw === "") {
1750
- // 注释提前返回
1751
- return {
1752
- command: '',
1753
- args: [],
1754
- options: []
1755
- };
1756
- }
1757
- // 截取命令
1758
- const getCommandResult = /:/.exec(newSentenceRaw);
1759
- // 没有command
1760
- if (getCommandResult === null) {
1761
- command = '';
1762
- }
1763
- else {
1764
- command = newSentenceRaw.substring(0, getCommandResult.index);
1765
- // 划分命令区域和content区域
1766
- newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
1767
- }
1768
- // 截取 Options 区域
1769
- const getOptionsResult = / -/.exec(newSentenceRaw);
1770
- // 获取到参数
1771
- if (getOptionsResult) {
1772
- const optionsRaw = newSentenceRaw.substring(getOptionsResult.index, newSentenceRaw.length);
1773
- newSentenceRaw = newSentenceRaw.substring(0, getOptionsResult.index);
1774
- for (const e of argsParser(optionsRaw, (name, _) => { return name; })) {
1775
- options.push(e);
1776
- }
1777
- }
1788
+ function scss2cssinjsParser(scssString) {
1789
+ const [classNameStyles, others] = parseCSS(scssString);
1778
1790
  return {
1779
- command,
1780
- args: newSentenceRaw.split('|').map(e => e.trim()).filter(e => e !== ''),
1781
- options
1791
+ classNameStyles,
1792
+ others,
1782
1793
  };
1783
1794
  }
1784
- function configParser(configText) {
1785
- const configLines = configText.replaceAll(`\r`, '').split('\n');
1786
- return configLines.map(e => configLineParser(e)).filter(e => e.command !== '');
1795
+ /**
1796
+ * GPT 4 写的,临时用,以后要重构!!!
1797
+ * TODO:用人类智能重构,要是用着一直没问题,也不是不可以 trust AI
1798
+ * @param css
1799
+ */
1800
+ function parseCSS(css) {
1801
+ const result = {};
1802
+ let specialRules = '';
1803
+ let matches;
1804
+ // 匹配类选择器和其内容,包括一层嵌套
1805
+ const classRegex = /\.([^{\s]+)\s*{([^}]*?(?:\{[^}]*?\}[^}]*?)?)}/g;
1806
+ // 更新特殊规则正则表达式以正确处理一层嵌套,并确保大括号成对出现
1807
+ const specialRegex = /(@[^{]+{\s*(?:[^{}]*{[^}]*}[^{}]*)+\s*})/g;
1808
+ // 提取类和样式
1809
+ while ((matches = classRegex.exec(css)) !== null) {
1810
+ result[matches[1]] = matches[2].trim().replace(/\s*;\s*/g, ';\n');
1811
+ }
1812
+ // 提取特殊规则,并确保包含所有的大括号
1813
+ while ((matches = specialRegex.exec(css)) !== null) {
1814
+ specialRules += matches[1].trim() + '\n';
1815
+ }
1816
+ return [result, specialRules.trim()];
1787
1817
  }
1788
1818
 
1789
1819
  class SceneParser {
1790
1820
  assetsPrefetcher;
1791
1821
  assetSetter;
1792
1822
  ADD_NEXT_ARG_LIST;
1793
- SCRIPT_CONFIG;
1794
- constructor(assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG) {
1823
+ SCRIPT_CONFIG_MAP;
1824
+ constructor(assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_INPUT) {
1795
1825
  this.assetsPrefetcher = assetsPrefetcher;
1796
1826
  this.assetSetter = assetSetter;
1797
1827
  this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
1798
- this.SCRIPT_CONFIG = SCRIPT_CONFIG;
1828
+ if (Array.isArray(SCRIPT_CONFIG_INPUT)) {
1829
+ this.SCRIPT_CONFIG_MAP = new Map();
1830
+ SCRIPT_CONFIG_INPUT.forEach((config) => {
1831
+ this.SCRIPT_CONFIG_MAP.set(config.scriptString, config);
1832
+ });
1833
+ }
1834
+ else {
1835
+ this.SCRIPT_CONFIG_MAP = SCRIPT_CONFIG_INPUT;
1836
+ }
1799
1837
  }
1838
+ /**
1839
+ * 解析场景
1840
+ * @param rawScene 原始场景
1841
+ * @param sceneName 场景名称
1842
+ * @param sceneUrl 场景url
1843
+ * @return 解析后的场景
1844
+ */
1800
1845
  parse(rawScene, sceneName, sceneUrl) {
1801
- return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
1846
+ return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG_MAP);
1802
1847
  }
1803
1848
  parseConfig(configText) {
1804
1849
  return configParser(configText);
1805
1850
  }
1806
1851
  stringifyConfig(config) {
1807
- return config
1808
- .reduce((previousValue, curr) => (previousValue + `${curr.command}:${curr.args.join('|')}${curr.options.length <= 0 ? '' : curr.options.reduce((p, c) => (p + ' -' + c.key + '=' + c.value), '')};\n`), '');
1852
+ return config.reduce((previousValue, curr) => previousValue +
1853
+ `${curr.command}:${curr.args.join('|')}${curr.options.length <= 0
1854
+ ? ''
1855
+ : curr.options.reduce((p, c) => p + ' -' + c.key + '=' + c.value, '')};\n`, '');
1856
+ }
1857
+ parseScssToWebgalStyleObj(scssString) {
1858
+ return scss2cssinjsParser(scssString);
1809
1859
  }
1810
1860
  }
1811
1861
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webgal-parser",
3
- "version": "4.4.9",
3
+ "version": "4.4.14-1",
4
4
  "description": "WebGAL script parser",
5
5
  "scripts": {
6
6
  "test": "vitest",
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "types": "./build/types/index.d.ts",
13
13
  "module": "./build/es/index.js",
14
- "main": "./build/cjs/index.js",
14
+ "main": "./build/cjs/index.cjs",
15
15
  "author": "Mahiru <Mahiru_@outlook.com>",
16
16
  "license": "MPL-2.0",
17
17
  "dependencies": {
@@ -1,4 +1,4 @@
1
- import { commandType } from "../interface/sceneInterface";
1
+ import {commandType} from '../interface/sceneInterface';
2
2
 
3
3
  export const SCRIPT_CONFIG = [
4
4
  { scriptString: 'intro', scriptType: commandType.intro },
@@ -6,7 +6,7 @@ export const SCRIPT_CONFIG = [
6
6
  { scriptString: 'changeFigure', scriptType: commandType.changeFigure },
7
7
  { scriptString: 'miniAvatar', scriptType: commandType.miniAvatar },
8
8
  { scriptString: 'changeScene', scriptType: commandType.changeScene },
9
- { scriptString: 'choose', scriptType: commandType.choose},
9
+ { scriptString: 'choose', scriptType: commandType.choose },
10
10
  { scriptString: 'end', scriptType: commandType.end },
11
11
  { scriptString: 'bgm', scriptType: commandType.bgm },
12
12
  { scriptString: 'playVideo', scriptType: commandType.video },
@@ -17,8 +17,8 @@ export const SCRIPT_CONFIG = [
17
17
  { scriptString: 'setFilter', scriptType: commandType.setFilter },
18
18
  { scriptString: 'pixiInit', scriptType: commandType.pixiInit },
19
19
  { scriptString: 'pixiPerform', scriptType: commandType.pixi },
20
- { scriptString: 'label', scriptType: commandType.label},
21
- { scriptString: 'jumpLabel', scriptType: commandType.jumpLabel},
20
+ { scriptString: 'label', scriptType: commandType.label },
21
+ { scriptString: 'jumpLabel', scriptType: commandType.jumpLabel },
22
22
  { scriptString: 'setVar', scriptType: commandType.setVar },
23
23
  { scriptString: 'callScene', scriptType: commandType.callScene },
24
24
  { scriptString: 'showVars', scriptType: commandType.showVars },
@@ -27,9 +27,10 @@ export const SCRIPT_CONFIG = [
27
27
  { scriptString: 'say', scriptType: commandType.say },
28
28
  { scriptString: 'filmMode', scriptType: commandType.filmMode },
29
29
  { scriptString: 'callScene', scriptType: commandType.callScene },
30
- { scriptString: 'setTextbox', scriptType: commandType.setTextbox},
30
+ { scriptString: 'setTextbox', scriptType: commandType.setTextbox },
31
31
  { scriptString: 'setAnimation', scriptType: commandType.setAnimation },
32
32
  { scriptString: 'playEffect', scriptType: commandType.playEffect },
33
+ { scriptString: 'applyStyle', scriptType: commandType.applyStyle },
33
34
  ];
34
35
  export const ADD_NEXT_ARG_LIST = [
35
36
  commandType.bgm,
@@ -44,3 +45,6 @@ export const ADD_NEXT_ARG_LIST = [
44
45
  commandType.filmMode,
45
46
  commandType.playEffect,
46
47
  ];
48
+
49
+ export type ConfigMap = Map<string, ConfigItem>;
50
+ export type ConfigItem = { scriptString: string; scriptType: commandType };