webgal-parser 4.4.3 → 4.4.4

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/package.json CHANGED
@@ -1,40 +1,40 @@
1
- {
2
- "name": "webgal-parser",
3
- "version": "4.4.3",
4
- "description": "WebGAL script parser",
5
- "scripts": {
6
- "test": "vitest",
7
- "coverage": "vitest run --coverage",
8
- "build": "rimraf -rf ./build && rollup --config",
9
- "debug": "tsx test/debug.ts"
10
- },
11
- "types": "./build/types/index.d.ts",
12
- "module": "./build/es/index.js",
13
- "main": "./build/cjs/index.js",
14
- "author": "Mahiru <Mahiru_@outlook.com>",
15
- "license": "MPL-2.0",
16
- "dependencies": {
17
- "chevrotain": "^10.5.0",
18
- "cloudlogjs": "^1.0.11",
19
- "lodash": "^4.17.21",
20
- "tsx": "^3.12.7"
21
- },
22
- "devDependencies": {
23
- "@rollup/plugin-commonjs": "^23.0.2",
24
- "@rollup/plugin-json": "^5.0.1",
25
- "@rollup/plugin-node-resolve": "^15.0.1",
26
- "@rollup/plugin-typescript": "^9.0.2",
27
- "@types/lodash": "^4.14.189",
28
- "@types/node": "^18.14.0",
29
- "@vitest/coverage-c8": "^0.28.5",
30
- "rollup": "^3.3.0",
31
- "rollup-plugin-babel": "^4.4.0",
32
- "rollup-plugin-terser": "^7.0.2",
33
- "rollup-plugin-typescript2": "^0.34.1",
34
- "ts-node": "^10.9.1",
35
- "tslib": "^2.4.1",
36
- "typescript": "^4.9.3",
37
- "vitest": "^0.28.5"
38
- },
39
- "type": "module"
40
- }
1
+ {
2
+ "name": "webgal-parser",
3
+ "version": "4.4.4",
4
+ "description": "WebGAL script parser",
5
+ "scripts": {
6
+ "test": "vitest",
7
+ "coverage": "vitest run --coverage",
8
+ "build": "rimraf -rf ./build && rollup --config",
9
+ "debug": "tsx test/debug.ts"
10
+ },
11
+ "types": "./build/types/index.d.ts",
12
+ "module": "./build/es/index.js",
13
+ "main": "./build/cjs/index.js",
14
+ "author": "Mahiru <Mahiru_@outlook.com>",
15
+ "license": "MPL-2.0",
16
+ "dependencies": {
17
+ "chevrotain": "^10.5.0",
18
+ "cloudlogjs": "^1.0.11",
19
+ "lodash": "^4.17.21",
20
+ "tsx": "^3.12.7"
21
+ },
22
+ "devDependencies": {
23
+ "@rollup/plugin-commonjs": "^23.0.2",
24
+ "@rollup/plugin-json": "^5.0.1",
25
+ "@rollup/plugin-node-resolve": "^15.0.1",
26
+ "@rollup/plugin-typescript": "^9.0.2",
27
+ "@types/lodash": "^4.14.189",
28
+ "@types/node": "^18.14.0",
29
+ "@vitest/coverage-c8": "^0.28.5",
30
+ "rollup": "^3.3.0",
31
+ "rollup-plugin-babel": "^4.4.0",
32
+ "rollup-plugin-terser": "^7.0.2",
33
+ "rollup-plugin-typescript2": "^0.34.1",
34
+ "ts-node": "^10.9.1",
35
+ "tslib": "^2.4.1",
36
+ "typescript": "^4.9.3",
37
+ "vitest": "^0.28.5"
38
+ },
39
+ "type": "module"
40
+ }
@@ -1,4 +1,60 @@
1
- export function configParser(configText:string){
2
- // TODO
3
- return configText
1
+ import {argsParser} from "../scriptParser/argsParser";
2
+
3
+ interface IOptionItem{
4
+ key:string;
5
+ value:string | number | boolean;
6
+ }
7
+ interface IConfigItem{
8
+ command:string;
9
+ args:string[]
10
+ options:IOptionItem[];
11
+ }
12
+
13
+ export type WebgalConfig = IConfigItem[]
14
+
15
+
16
+ function configLineParser(inputLine:string):IConfigItem{
17
+ const options:Array<IOptionItem> = [];
18
+ let command: string;
19
+
20
+ let newSentenceRaw = inputLine.split(";")[0];
21
+ if (newSentenceRaw === "") {
22
+ // 注释提前返回
23
+ return {
24
+ command:'',
25
+ args:[],
26
+ options:[]
27
+ };
28
+ }
29
+ // 截取命令
30
+ const getCommandResult = /:/.exec(newSentenceRaw);
31
+
32
+ // 没有command
33
+ if (getCommandResult === null) {
34
+ command = '';
35
+ } else {
36
+ command = newSentenceRaw.substring(0, getCommandResult.index);
37
+ // 划分命令区域和content区域
38
+ newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
39
+ }
40
+ // 截取 Options 区域
41
+ const getOptionsResult = / -/.exec(newSentenceRaw);
42
+ // 获取到参数
43
+ if (getOptionsResult) {
44
+ const optionsRaw = newSentenceRaw.substring(getOptionsResult.index, newSentenceRaw.length);
45
+ newSentenceRaw = newSentenceRaw.substring(0, getOptionsResult.index);
46
+ for (const e of argsParser(optionsRaw, (name,_)=>{return name})) {
47
+ options.push(e);
48
+ }
49
+ }
50
+ return {
51
+ command,
52
+ args:newSentenceRaw.split('|').map(e=>e.trim()).filter(e=>e!==''),
53
+ options
54
+ };
55
+ }
56
+
57
+ export function configParser(configText:string):WebgalConfig{
58
+ const configLines = configText.replaceAll(`\r`,'').split('\n');
59
+ return configLines.map(e=>configLineParser(e)).filter(e=>e.command!=='');
4
60
  }
package/src/index.ts CHANGED
@@ -1,33 +1,42 @@
1
- import {commandType, IAsset} from "./interface/sceneInterface";
2
- import {fileType} from "./interface/assets";
3
- import {sceneParser} from "./sceneParser";
4
- import {ADD_NEXT_ARG_LIST, SCRIPT_CONFIG} from "./config/scriptConfig";
5
- import {configParser} from "@/configParser/configParser";
6
-
7
- export default class SceneParser {
8
-
9
- private readonly assetsPrefetcher;
10
- private readonly assetSetter;
11
- private readonly ADD_NEXT_ARG_LIST;
12
- private readonly SCRIPT_CONFIG;
13
-
14
- constructor(assetsPrefetcher: ((assetList: Array<IAsset>) => void),
15
- assetSetter: (fileName: string, assetType: fileType) => string,
16
- ADD_NEXT_ARG_LIST: Array<number>, SCRIPT_CONFIG: Array<any>) {
17
- this.assetsPrefetcher = assetsPrefetcher;
18
- this.assetSetter = assetSetter;
19
- this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
20
- this.SCRIPT_CONFIG = SCRIPT_CONFIG;
21
- }
22
-
23
- parse(rawScene: string, sceneName: string, sceneUrl: string
24
- ) {
25
- return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
26
- }
27
-
28
- parseConfig(configText: string) {
29
- return configParser(configText)
30
- }
31
- }
32
-
33
- export {ADD_NEXT_ARG_LIST, SCRIPT_CONFIG};
1
+ import {commandType, IAsset} from "./interface/sceneInterface";
2
+ import {fileType} from "./interface/assets";
3
+ import {sceneParser} from "./sceneParser";
4
+ import {ADD_NEXT_ARG_LIST, SCRIPT_CONFIG} from "./config/scriptConfig";
5
+ import {configParser, WebgalConfig} from "./configParser/configParser";
6
+
7
+ export default class SceneParser {
8
+
9
+ private readonly assetsPrefetcher;
10
+ private readonly assetSetter;
11
+ private readonly ADD_NEXT_ARG_LIST;
12
+ private readonly SCRIPT_CONFIG;
13
+
14
+ constructor(assetsPrefetcher: ((assetList: Array<IAsset>) => void),
15
+ assetSetter: (fileName: string, assetType: fileType) => string,
16
+ ADD_NEXT_ARG_LIST: Array<number>, SCRIPT_CONFIG: Array<any>) {
17
+ this.assetsPrefetcher = assetsPrefetcher;
18
+ this.assetSetter = assetSetter;
19
+ this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
20
+ this.SCRIPT_CONFIG = SCRIPT_CONFIG;
21
+ }
22
+
23
+ parse(rawScene: string, sceneName: string, sceneUrl: string
24
+ ) {
25
+ return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
26
+ }
27
+
28
+ parseConfig(configText: string) {
29
+ return configParser(configText)
30
+ }
31
+
32
+ stringifyConfig(config: WebgalConfig) {
33
+ return config
34
+ .reduce(
35
+ (previousValue, curr) =>
36
+ (previousValue + `${curr.command}:${curr.args.join('|')}${curr.options.length <= 0 ? '' : curr.options.reduce((p, c) => (p + ' -' + c.key + '=' + c.value), '')};\n`),
37
+ ''
38
+ )
39
+ }
40
+ }
41
+
42
+ export {ADD_NEXT_ARG_LIST, SCRIPT_CONFIG};
@@ -1,103 +1,103 @@
1
- /**
2
- * 语句类型
3
- */
4
- import {sceneEntry} from "./runtimeInterface";
5
- import {fileType} from "./assets";
6
-
7
- export enum commandType {
8
- say, // 对话
9
- changeBg, // 更改背景
10
- changeFigure, // 更改立绘
11
- bgm, // 更改背景音乐
12
- video, // 播放视频
13
- pixi, // pixi演出
14
- pixiInit, // pixi初始化
15
- intro, // 黑屏文字演示
16
- miniAvatar, // 小头像
17
- changeScene, // 切换场景
18
- choose, // 分支选择
19
- end, // 结束游戏
20
- setComplexAnimation, // 动画演出
21
- setFilter, // 设置效果
22
- label, // 标签
23
- jumpLabel, // 跳转标签
24
- chooseLabel, // 选择标签
25
- setVar, // 设置变量
26
- if, // 条件跳转
27
- callScene, // 调用场景
28
- showVars,
29
- unlockCg,
30
- unlockBgm,
31
- filmMode,
32
- setTextbox,
33
- setAnimation,
34
- playEffect,
35
- setTempAnimation,
36
- comment,
37
- setTransform,
38
- setTransition,
39
- }
40
-
41
- /**
42
- * 单个参数接口
43
- * @interface arg
44
- */
45
- export interface arg {
46
- key: string; // 参数键
47
- value: string | boolean | number; // 参数值
48
- }
49
-
50
- /**
51
- * 资源接口
52
- * @interface IAsset
53
- */
54
- export interface IAsset {
55
- name: string; // 资源名称
56
- type: fileType; // 资源类型
57
- url: string; // 资源url
58
- lineNumber: number; // 触发资源语句的行号
59
- }
60
-
61
- /**
62
- * 单条语句接口
63
- * @interface ISentence
64
- */
65
- export interface ISentence {
66
- command: commandType; // 语句类型
67
- commandRaw: string; // 命令的原始内容,方便调试
68
- content: string; // 语句内容
69
- args: Array<arg>; // 参数列表
70
- sentenceAssets: Array<IAsset>; // 语句携带的资源列表
71
- subScene: Array<string>; // 语句包含子场景列表
72
- }
73
-
74
- /**
75
- * 场景接口
76
- * @interface IScene
77
- */
78
- export interface IScene {
79
- sceneName: string; // 场景名称
80
- sceneUrl: string; // 场景url
81
- sentenceList: Array<ISentence>; // 语句列表
82
- assetsList: Array<IAsset>; // 资源列表
83
- subSceneList: Array<string>; // 子场景的url列表
84
- }
85
-
86
- /**
87
- * 当前的场景数据
88
- * @interface ISceneData
89
- */
90
- export interface ISceneData {
91
- currentSentenceId: number; // 当前语句ID
92
- sceneStack: Array<sceneEntry>; // 场景栈
93
- currentScene: IScene; // 当前场景数据
94
- }
95
-
96
- /**
97
- * 处理后的命令接口
98
- * @interface parsedCommand
99
- */
100
- export interface parsedCommand {
101
- type: commandType;
102
- additionalArgs: Array<arg>;
103
- }
1
+ /**
2
+ * 语句类型
3
+ */
4
+ import {sceneEntry} from "./runtimeInterface";
5
+ import {fileType} from "./assets";
6
+
7
+ export enum commandType {
8
+ say, // 对话
9
+ changeBg, // 更改背景
10
+ changeFigure, // 更改立绘
11
+ bgm, // 更改背景音乐
12
+ video, // 播放视频
13
+ pixi, // pixi演出
14
+ pixiInit, // pixi初始化
15
+ intro, // 黑屏文字演示
16
+ miniAvatar, // 小头像
17
+ changeScene, // 切换场景
18
+ choose, // 分支选择
19
+ end, // 结束游戏
20
+ setComplexAnimation, // 动画演出
21
+ setFilter, // 设置效果
22
+ label, // 标签
23
+ jumpLabel, // 跳转标签
24
+ chooseLabel, // 选择标签
25
+ setVar, // 设置变量
26
+ if, // 条件跳转
27
+ callScene, // 调用场景
28
+ showVars,
29
+ unlockCg,
30
+ unlockBgm,
31
+ filmMode,
32
+ setTextbox,
33
+ setAnimation,
34
+ playEffect,
35
+ setTempAnimation,
36
+ comment,
37
+ setTransform,
38
+ setTransition,
39
+ }
40
+
41
+ /**
42
+ * 单个参数接口
43
+ * @interface arg
44
+ */
45
+ export interface arg {
46
+ key: string; // 参数键
47
+ value: string | boolean | number; // 参数值
48
+ }
49
+
50
+ /**
51
+ * 资源接口
52
+ * @interface IAsset
53
+ */
54
+ export interface IAsset {
55
+ name: string; // 资源名称
56
+ type: fileType; // 资源类型
57
+ url: string; // 资源url
58
+ lineNumber: number; // 触发资源语句的行号
59
+ }
60
+
61
+ /**
62
+ * 单条语句接口
63
+ * @interface ISentence
64
+ */
65
+ export interface ISentence {
66
+ command: commandType; // 语句类型
67
+ commandRaw: string; // 命令的原始内容,方便调试
68
+ content: string; // 语句内容
69
+ args: Array<arg>; // 参数列表
70
+ sentenceAssets: Array<IAsset>; // 语句携带的资源列表
71
+ subScene: Array<string>; // 语句包含子场景列表
72
+ }
73
+
74
+ /**
75
+ * 场景接口
76
+ * @interface IScene
77
+ */
78
+ export interface IScene {
79
+ sceneName: string; // 场景名称
80
+ sceneUrl: string; // 场景url
81
+ sentenceList: Array<ISentence>; // 语句列表
82
+ assetsList: Array<IAsset>; // 资源列表
83
+ subSceneList: Array<string>; // 子场景的url列表
84
+ }
85
+
86
+ /**
87
+ * 当前的场景数据
88
+ * @interface ISceneData
89
+ */
90
+ export interface ISceneData {
91
+ currentSentenceId: number; // 当前语句ID
92
+ sceneStack: Array<sceneEntry>; // 场景栈
93
+ currentScene: IScene; // 当前场景数据
94
+ }
95
+
96
+ /**
97
+ * 处理后的命令接口
98
+ * @interface parsedCommand
99
+ */
100
+ export interface parsedCommand {
101
+ type: commandType;
102
+ additionalArgs: Array<arg>;
103
+ }