webgal-parser 4.6.2 → 4.6.3-beta.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # WebGAL Parser Changelog
2
2
 
3
+ ## [4.6.3-beta.1] - 2026-07-31
4
+
5
+ ### Added
6
+ - 正式启用多行语句支持:`sceneParser` 现在会先经过 `sceneTextPreProcess`,把以空白开头且首字符为 `-` 或 `|` 的续行折叠进上一条语句
7
+ - `ISentence` 新增行范围字段 `startLine` / `endLine`(0-based,含首尾)与 `isLineBreakHolder`,供图形编辑器按行范围整体替换语句
8
+ - 导出 `WEBGAL_LINE_BREAK_MARK` 与 `isLineBreakPlaceholder`
9
+
10
+ ### Fixed
11
+ - `sceneTextPreProcess`:移除逐行 `console.log`;场景首行不再被误判为续行;场景末行以 `\` 结尾时不再丢失整段累积内容
12
+
13
+ ### Technical Notes
14
+ - 续行会被替换成 `;_WEBGAL_LINE_BREAK_` 占位注释行,**预处理前后行数严格一致**。因此「解析后语句 index == 文件行号」这一不变量继续成立,引擎的存读档、Backlog、场景栈、已读记录(都存语句 index)无需改动,旧存档也不会错位
15
+
3
16
  ## [4.5.13] - 2025-07-05
4
17
 
5
18
  ### Fixed
@@ -39,6 +39,7 @@ var commandType;
39
39
  commandType[commandType["applyStyle"] = 32] = "applyStyle";
40
40
  commandType[commandType["wait"] = 33] = "wait";
41
41
  commandType[commandType["callSteam"] = 34] = "callSteam";
42
+ commandType[commandType["return"] = 35] = "return";
42
43
  })(commandType || (commandType = {}));
43
44
 
44
45
  const SCRIPT_CONFIG = [
@@ -80,6 +81,7 @@ const SCRIPT_CONFIG = [
80
81
  { scriptString: 'applyStyle', scriptType: commandType.applyStyle },
81
82
  { scriptString: 'wait', scriptType: commandType.wait },
82
83
  { scriptString: 'callSteam', scriptType: commandType.callSteam },
84
+ { scriptString: 'return', scriptType: commandType.return },
83
85
  ];
84
86
  const ADD_NEXT_ARG_LIST = [
85
87
  commandType.bgm,
@@ -138,7 +140,7 @@ function argsParser(argsRaw, assetSetter) {
138
140
  argValue = undefined;
139
141
  }
140
142
  // 判断是不是语音参数
141
- if (argName.toLowerCase().match(/.ogg|.mp3|.wav/)) {
143
+ if (argName.toLowerCase().match(/.ogg|.mp3|.wav|.opus/)) {
142
144
  returnArrayList.push({
143
145
  key: 'vocal',
144
146
  value: assetSetter(e, fileType.vocal),
@@ -463,7 +465,10 @@ const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG
463
465
  args: [{ key: 'next', value: true }],
464
466
  sentenceAssets: [],
465
467
  subScene: [],
466
- inlineComment: '', // 行内注释
468
+ inlineComment: '',
469
+ startLine: lineNumber,
470
+ endLine: lineNumber,
471
+ isLineBreakHolder: false,
467
472
  };
468
473
  }
469
474
  // 截取命令
@@ -514,92 +519,14 @@ const scriptParser = (sentenceRaw, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG
514
519
  args: args,
515
520
  sentenceAssets: sentenceAssets,
516
521
  subScene: subScene,
517
- inlineComment: sentenceComment.trim(), // 行内注释
522
+ inlineComment: sentenceComment.trim(),
523
+ // 单行语句的行范围就是自己,多行语句的 endLine 由 sceneParser 回填
524
+ startLine: lineNumber,
525
+ endLine: lineNumber,
526
+ isLineBreakHolder: false,
518
527
  };
519
528
  };
520
529
 
521
- /**
522
- * 场景解析器
523
- * @param rawScene 原始场景
524
- * @param sceneName 场景名称
525
- * @param sceneUrl 场景url
526
- * @param assetsPrefetcher
527
- * @param assetSetter
528
- * @param ADD_NEXT_ARG_LIST
529
- * @param SCRIPT_CONFIG_MAP
530
- * @return {IScene} 解析后的场景
531
- */
532
- const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP) => {
533
- const rawSentenceList = rawScene.replaceAll('\r', '').split('\n'); // 原始句子列表
534
- // 去分号留到后面去做了,现在注释要单独处理
535
- const rawSentenceListWithoutEmpty = rawSentenceList;
536
- // .map((sentence) => sentence.split(";")[0])
537
- // .filter((sentence) => sentence.trim() !== "");
538
- let assetsList = []; // 场景资源列表
539
- let subSceneList = []; // 子场景列表
540
- const sentenceList = rawSentenceListWithoutEmpty.map((sentence, index) => {
541
- const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP, index);
542
- // 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
543
- assetsList = [...assetsList, ...returnSentence.sentenceAssets];
544
- subSceneList = [...subSceneList, ...returnSentence.subScene];
545
- return returnSentence;
546
- });
547
- // 开始资源的预加载
548
- assetsList = deduplicateAssets(assetsList);
549
- assetsPrefetcher(assetsList);
550
- return {
551
- sceneName: sceneName,
552
- sceneUrl: sceneUrl,
553
- sentenceList: sentenceList,
554
- assetsList: assetsList,
555
- subSceneList: subSceneList, // 子场景列表
556
- };
557
- };
558
- const deduplicateAssets = (assetsList) => {
559
- const seenAssets = new Set();
560
- return assetsList.filter((asset) => {
561
- if (!asset || typeof asset.url !== 'string' || asset.url === '') {
562
- return false;
563
- }
564
- const assetKey = `${asset.type}:${asset.url}`;
565
- if (seenAssets.has(assetKey)) {
566
- return false;
567
- }
568
- seenAssets.add(assetKey);
569
- return true;
570
- });
571
- };
572
-
573
- function scss2cssinjsParser(scssString) {
574
- const [classNameStyles, others] = parseCSS(scssString);
575
- return {
576
- classNameStyles,
577
- others,
578
- };
579
- }
580
- /**
581
- * GPT 4 写的,临时用,以后要重构!!!
582
- * TODO:用人类智能重构,要是用着一直没问题,也不是不可以 trust AI
583
- * @param css
584
- */
585
- function parseCSS(css) {
586
- const result = {};
587
- let specialRules = '';
588
- let matches;
589
- // 使用非贪婪匹配,尝试正确处理任意层次的嵌套
590
- const classRegex = /\.([^{\s]+)\s*{((?:[^{}]*|{[^}]*})*)}/g;
591
- const specialRegex = /(@[^{]+{\s*(?:[^{}]*{[^}]*}[^{}]*)+\s*})/g;
592
- while ((matches = classRegex.exec(css)) !== null) {
593
- const key = matches[1];
594
- const value = matches[2].trim().replace(/\s*;\s*/g, ';\n');
595
- result[key] = value;
596
- }
597
- while ((matches = specialRegex.exec(css)) !== null) {
598
- specialRules += matches[1].trim() + '\n';
599
- }
600
- return [result, specialRules.trim()];
601
- }
602
-
603
530
  /**
604
531
  * Preprocessor for scene text.
605
532
  *
@@ -629,13 +556,11 @@ function sceneTextPreProcessPassOne(lines) {
629
556
  let lastLineIsMultiline = false;
630
557
  let thisLineIsMultiline = false;
631
558
  for (const line of lines) {
632
- thisLineIsMultiline = false;
633
- if (canBeMultiline(line)) {
634
- thisLineIsMultiline = true;
635
- }
636
- if (shouldNotBeMultiline(line, lastLineIsMultiline)) {
637
- thisLineIsMultiline = false;
638
- }
559
+ // 续行需要接到上一行,所以场景的第一行永远不会是续行。
560
+ thisLineIsMultiline =
561
+ processedLines.length > 0 &&
562
+ canBeMultiline(line) &&
563
+ !shouldNotBeMultiline(line, lastLineIsMultiline);
639
564
  if (thisLineIsMultiline) {
640
565
  processedLines[processedLines.length - 1] += '\\';
641
566
  }
@@ -693,7 +618,6 @@ function sceneTextPreProcessPassTwo(lines) {
693
618
  placeHolderLines.push(placeholderLine(line));
694
619
  }
695
620
  for (const line of lines) {
696
- console.log(line);
697
621
  if (line.endsWith('\\')) {
698
622
  const trueLine = line.slice(0, -1);
699
623
  if (currentMultilineContent === "") {
@@ -717,8 +641,24 @@ function sceneTextPreProcessPassTwo(lines) {
717
641
  }
718
642
  processedLines.push(line);
719
643
  }
644
+ // 场景末行仍以 \ 结尾时循环里没有机会收尾,在这里把累积的内容补出去。
645
+ if (currentMultilineContent !== "") {
646
+ processedLines.push(currentMultilineContent, ...placeHolderLines);
647
+ }
720
648
  return processedLines;
721
649
  }
650
+ /**
651
+ * 占位行的前缀。占位行本身是一条注释,引擎会空跑掉;
652
+ * 它的作用是让预处理后的行数与原始场景一致,从而保持
653
+ * 「解析后语句 index == 文件行号」这一不变量。
654
+ */
655
+ const WEBGAL_LINE_BREAK_MARK = ';_WEBGAL_LINE_BREAK_';
656
+ /**
657
+ * 判断预处理后的某一行是否是占位行,即它是上一条多行语句折叠掉的续行。
658
+ */
659
+ function isLineBreakPlaceholder(processedLine) {
660
+ return processedLine.startsWith(WEBGAL_LINE_BREAK_MARK);
661
+ }
722
662
  /**
723
663
  * Placeholder Line. Adding this line preserves the original number of lines
724
664
  * in the scene text, so that it can be compatible with the graphical editor.
@@ -727,70 +667,116 @@ function sceneTextPreProcessPassTwo(lines) {
727
667
  * @returns The placeholder line
728
668
  */
729
669
  function placeholderLine(content = "") {
730
- return ";_WEBGAL_LINE_BREAK_" + content;
670
+ return WEBGAL_LINE_BREAK_MARK + content;
671
+ }
672
+
673
+ /**
674
+ * 场景解析器
675
+ * @param rawScene 原始场景
676
+ * @param sceneName 场景名称
677
+ * @param sceneUrl 场景url
678
+ * @param assetsPrefetcher
679
+ * @param assetSetter
680
+ * @param ADD_NEXT_ARG_LIST
681
+ * @param SCRIPT_CONFIG_MAP
682
+ * @return {IScene} 解析后的场景
683
+ */
684
+ const sceneParser = (rawScene, sceneName, sceneUrl, assetsPrefetcher, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP) => {
685
+ // 预处理把多行语句折叠进它的首行,并用占位行补齐被折叠掉的行,
686
+ // 因此这里的行数与原始场景严格一致,「语句 index == 文件行号」始终成立。
687
+ const rawSentenceList = sceneTextPreProcess(rawScene).split('\n'); // 原始句子列表
688
+ // 去分号留到后面去做了,现在注释要单独处理
689
+ const rawSentenceListWithoutEmpty = rawSentenceList;
690
+ // .map((sentence) => sentence.split(";")[0])
691
+ // .filter((sentence) => sentence.trim() !== "");
692
+ let assetsList = []; // 场景资源列表
693
+ let subSceneList = []; // 子场景列表
694
+ const sentenceList = rawSentenceListWithoutEmpty.map((sentence, index) => {
695
+ const returnSentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG_MAP, index);
696
+ // 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
697
+ assetsList = [...assetsList, ...returnSentence.sentenceAssets];
698
+ subSceneList = [...subSceneList, ...returnSentence.subScene];
699
+ return returnSentence;
700
+ });
701
+ markMultilineRanges(sentenceList, rawSentenceList);
702
+ // 开始资源的预加载
703
+ assetsList = deduplicateAssets(assetsList);
704
+ assetsPrefetcher(assetsList);
705
+ return {
706
+ sceneName: sceneName,
707
+ sceneUrl: sceneUrl,
708
+ sentenceList: sentenceList,
709
+ assetsList: assetsList,
710
+ subSceneList: subSceneList, // 子场景列表
711
+ };
712
+ };
713
+ /**
714
+ * 回填多行语句的行范围。
715
+ *
716
+ * 预处理后,一条多行语句的完整内容都落在它的首行,紧跟其后的若干占位行
717
+ * 就是被折叠掉的续行。这里把占位行标记出来,并把首行语句的 endLine
718
+ * 推到最后一条续行,图形编辑器据此按整个行范围替换语句。
719
+ *
720
+ * @param sentenceList 解析后的语句列表
721
+ * @param processedLines 预处理后的行,与 sentenceList 一一对应
722
+ */
723
+ const markMultilineRanges = (sentenceList, processedLines) => {
724
+ let headIndex = -1; // 最近一条非占位语句的下标,即当前多行语句的首行
725
+ for (let index = 0; index < sentenceList.length; index++) {
726
+ if (!isLineBreakPlaceholder(processedLines[index])) {
727
+ headIndex = index;
728
+ continue;
729
+ }
730
+ sentenceList[index].isLineBreakHolder = true;
731
+ if (headIndex >= 0) {
732
+ sentenceList[headIndex].endLine = index;
733
+ }
734
+ }
735
+ };
736
+ const deduplicateAssets = (assetsList) => {
737
+ const seenAssets = new Set();
738
+ return assetsList.filter((asset) => {
739
+ if (!asset || typeof asset.url !== 'string' || asset.url === '') {
740
+ return false;
741
+ }
742
+ const assetKey = `${asset.type}:${asset.url}`;
743
+ if (seenAssets.has(assetKey)) {
744
+ return false;
745
+ }
746
+ seenAssets.add(assetKey);
747
+ return true;
748
+ });
749
+ };
750
+
751
+ function scss2cssinjsParser(scssString) {
752
+ const [classNameStyles, others] = parseCSS(scssString);
753
+ return {
754
+ classNameStyles,
755
+ others,
756
+ };
731
757
  }
732
- // export function sceneTextPreProcess(sceneText: string): string {
733
- // const lines = sceneText.replaceAll('\r', '').split('\n');
734
- // const processedLines: string[] = [];
735
- // let lastNonMultilineIndex = -1;
736
- // let isInMultilineSequence = false;
737
- // function isMultiline(line: string): boolean {
738
- // if (!line.startsWith(' ')) return false;
739
- // const trimmedLine = line.trimStart();
740
- // return trimmedLine.startsWith('|') || trimmedLine.startsWith('-');
741
- // }
742
- // for (let i = 0; i < lines.length; i++) {
743
- // const line = lines[i];
744
- // if (line.trim() === '') {
745
- // // Empty line handling
746
- // if (isInMultilineSequence) {
747
- // // Check if the next line is a multiline line
748
- // let isStillInMulti = false;
749
- // for (let j = i + 1; j < lines.length; j++) {
750
- // const lookForwardLine = lines[j] || '';
751
- // // 遇到正常语句了,直接中断
752
- // if (lookForwardLine.trim() !== '' && !isMultiline(lookForwardLine)) {
753
- // isStillInMulti = false;
754
- // break;
755
- // }
756
- // // 必须找到后面接的是参数,并且中间没有遇到任何正常语句才行
757
- // if (lookForwardLine.trim() !== '' && isMultiline(lookForwardLine)) {
758
- // isStillInMulti = true;
759
- // break;
760
- // }
761
- // }
762
- // if (isStillInMulti) {
763
- // // Still within a multiline sequence
764
- // processedLines.push(';_WEBGAL_LINE_BREAK_');
765
- // } else {
766
- // // End of multiline sequence
767
- // isInMultilineSequence = false;
768
- // processedLines.push(line);
769
- // }
770
- // } else {
771
- // // Preserve empty lines outside of multiline sequences
772
- // processedLines.push(line);
773
- // }
774
- // } else if (isMultiline(line)) {
775
- // // Multiline statement handling
776
- // if (lastNonMultilineIndex >= 0) {
777
- // // Concatenate to the previous non-multiline statement
778
- // const trimedLine = line.trimStart();
779
- // const addBlank = trimedLine.startsWith('-') ? ' ' : '';
780
- // processedLines[lastNonMultilineIndex] += addBlank + trimedLine;
781
- // }
782
- // // Add the special comment line
783
- // processedLines.push(';_WEBGAL_LINE_BREAK_' + line);
784
- // isInMultilineSequence = true;
785
- // } else {
786
- // // Non-multiline statement handling
787
- // processedLines.push(line);
788
- // lastNonMultilineIndex = processedLines.length - 1;
789
- // isInMultilineSequence = false;
790
- // }
791
- // }
792
- // return processedLines.join('\n');
793
- // }
758
+ /**
759
+ * GPT 4 写的,临时用,以后要重构!!!
760
+ * TODO:用人类智能重构,要是用着一直没问题,也不是不可以 trust AI
761
+ * @param css
762
+ */
763
+ function parseCSS(css) {
764
+ const result = {};
765
+ let specialRules = '';
766
+ let matches;
767
+ // 使用非贪婪匹配,尝试正确处理任意层次的嵌套
768
+ const classRegex = /\.([^{\s]+)\s*{((?:[^{}]*|{[^}]*})*)}/g;
769
+ const specialRegex = /(@[^{]+{\s*(?:[^{}]*{[^}]*}[^{}]*)+\s*})/g;
770
+ while ((matches = classRegex.exec(css)) !== null) {
771
+ const key = matches[1];
772
+ const value = matches[2].trim().replace(/\s*;\s*/g, ';\n');
773
+ result[key] = value;
774
+ }
775
+ while ((matches = specialRegex.exec(css)) !== null) {
776
+ specialRules += matches[1].trim() + '\n';
777
+ }
778
+ return [result, specialRules.trim()];
779
+ }
794
780
 
795
781
  class SceneParser {
796
782
  assetsPrefetcher;
@@ -837,5 +823,7 @@ class SceneParser {
837
823
 
838
824
  exports.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
839
825
  exports.SCRIPT_CONFIG = SCRIPT_CONFIG;
826
+ exports.WEBGAL_LINE_BREAK_MARK = WEBGAL_LINE_BREAK_MARK;
840
827
  exports.default = SceneParser;
828
+ exports.isLineBreakPlaceholder = isLineBreakPlaceholder;
841
829
  exports.sceneTextPreProcess = sceneTextPreProcess;
@@ -3,7 +3,7 @@ import { WebgalConfig } from './configParser/configParser';
3
3
  import { fileType } from './interface/assets';
4
4
  import { IAsset } from './interface/sceneInterface';
5
5
  import { IWebGALStyleObj } from "./styleParser";
6
- import { sceneTextPreProcess } from "./sceneTextPreProcessor";
6
+ import { isLineBreakPlaceholder, sceneTextPreProcess, WEBGAL_LINE_BREAK_MARK } from "./sceneTextPreProcessor";
7
7
  export default class SceneParser {
8
8
  private readonly assetsPrefetcher;
9
9
  private readonly assetSetter;
@@ -23,4 +23,4 @@ export default class SceneParser {
23
23
  parseScssToWebgalStyleObj(scssString: string): IWebGALStyleObj;
24
24
  }
25
25
  export { ADD_NEXT_ARG_LIST, SCRIPT_CONFIG };
26
- export { sceneTextPreProcess };
26
+ export { sceneTextPreProcess, isLineBreakPlaceholder, WEBGAL_LINE_BREAK_MARK };
@@ -6,6 +6,8 @@ export interface sceneEntry {
6
6
  sceneName: string;
7
7
  sceneUrl: string;
8
8
  continueLine: number;
9
+ locals?: Record<string, any>;
10
+ writeReturnTo?: string;
9
11
  }
10
12
  /**
11
13
  * 场景栈条目接口 (兼容性别名)
@@ -38,7 +38,8 @@ export declare enum commandType {
38
38
  getUserInput = 31,
39
39
  applyStyle = 32,
40
40
  wait = 33,
41
- callSteam = 34
41
+ callSteam = 34,
42
+ return = 35
42
43
  }
43
44
  /**
44
45
  * 单个参数接口
@@ -70,6 +71,15 @@ export interface ISentence {
70
71
  sentenceAssets: Array<IAsset>;
71
72
  subScene: Array<string>;
72
73
  inlineComment: string;
74
+ /**
75
+ * 本语句在原始场景文件中占据的行范围(0-based,含首尾)。
76
+ * 单行语句 startLine === endLine;多行语句 endLine 指向最后一条续行。
77
+ * 图形编辑器据此做「只替换被编辑语句所占的行」。
78
+ */
79
+ startLine: number;
80
+ endLine: number;
81
+ /** 本条是多行语句被折叠掉的续行占位,仅用于补齐行数,不应展示给用户 */
82
+ isLineBreakHolder: boolean;
73
83
  }
74
84
  /**
75
85
  * 场景接口
@@ -9,3 +9,13 @@
9
9
  * @returns The processed scene text
10
10
  */
11
11
  export declare function sceneTextPreProcess(sceneText: string): string;
12
+ /**
13
+ * 占位行的前缀。占位行本身是一条注释,引擎会空跑掉;
14
+ * 它的作用是让预处理后的行数与原始场景一致,从而保持
15
+ * 「解析后语句 index == 文件行号」这一不变量。
16
+ */
17
+ export declare const WEBGAL_LINE_BREAK_MARK = ";_WEBGAL_LINE_BREAK_";
18
+ /**
19
+ * 判断预处理后的某一行是否是占位行,即它是上一条多行语句折叠掉的续行。
20
+ */
21
+ export declare function isLineBreakPlaceholder(processedLine: string): boolean;
@@ -3,7 +3,7 @@ import { WebgalConfig } from './configParser/configParser';
3
3
  import { fileType } from './interface/assets';
4
4
  import { IAsset } from './interface/sceneInterface';
5
5
  import { IWebGALStyleObj } from "./styleParser";
6
- import { sceneTextPreProcess } from "./sceneTextPreProcessor";
6
+ import { isLineBreakPlaceholder, sceneTextPreProcess, WEBGAL_LINE_BREAK_MARK } from "./sceneTextPreProcessor";
7
7
  export default class SceneParser {
8
8
  private readonly assetsPrefetcher;
9
9
  private readonly assetSetter;
@@ -23,4 +23,4 @@ export default class SceneParser {
23
23
  parseScssToWebgalStyleObj(scssString: string): IWebGALStyleObj;
24
24
  }
25
25
  export { ADD_NEXT_ARG_LIST, SCRIPT_CONFIG };
26
- export { sceneTextPreProcess };
26
+ export { sceneTextPreProcess, isLineBreakPlaceholder, WEBGAL_LINE_BREAK_MARK };