Skip to content

Commit

Permalink
Merge pull request #456 from yuyi919/refactoring
Browse files Browse the repository at this point in the history
refactor: 一些重构优化/格式化操作
  • Loading branch information
MakinoharaShoko committed Jan 28, 2024
2 parents 322df19 + 0b20eab commit d6cfa92
Show file tree
Hide file tree
Showing 16 changed files with 386 additions and 256 deletions.
19 changes: 19 additions & 0 deletions packages/parser/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "always",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto"
}
13 changes: 8 additions & 5 deletions packages/parser/src/config/scriptConfig.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { commandType } from "../interface/sceneInterface";
import { commandType } from '../interface/sceneInterface';

export const SCRIPT_CONFIG = [
{ scriptString: 'intro', scriptType: commandType.intro },
{ scriptString: 'changeBg', scriptType: commandType.changeBg },
{ scriptString: 'changeFigure', scriptType: commandType.changeFigure },
{ scriptString: 'miniAvatar', scriptType: commandType.miniAvatar },
{ scriptString: 'changeScene', scriptType: commandType.changeScene },
{ scriptString: 'choose', scriptType: commandType.choose},
{ scriptString: 'choose', scriptType: commandType.choose },
{ scriptString: 'end', scriptType: commandType.end },
{ scriptString: 'bgm', scriptType: commandType.bgm },
{ scriptString: 'playVideo', scriptType: commandType.video },
Expand All @@ -17,8 +17,8 @@ export const SCRIPT_CONFIG = [
{ scriptString: 'setFilter', scriptType: commandType.setFilter },
{ scriptString: 'pixiInit', scriptType: commandType.pixiInit },
{ scriptString: 'pixiPerform', scriptType: commandType.pixi },
{ scriptString: 'label', scriptType: commandType.label},
{ scriptString: 'jumpLabel', scriptType: commandType.jumpLabel},
{ scriptString: 'label', scriptType: commandType.label },
{ scriptString: 'jumpLabel', scriptType: commandType.jumpLabel },
{ scriptString: 'setVar', scriptType: commandType.setVar },
{ scriptString: 'callScene', scriptType: commandType.callScene },
{ scriptString: 'showVars', scriptType: commandType.showVars },
Expand All @@ -27,7 +27,7 @@ export const SCRIPT_CONFIG = [
{ scriptString: 'say', scriptType: commandType.say },
{ scriptString: 'filmMode', scriptType: commandType.filmMode },
{ scriptString: 'callScene', scriptType: commandType.callScene },
{ scriptString: 'setTextbox', scriptType: commandType.setTextbox},
{ scriptString: 'setTextbox', scriptType: commandType.setTextbox },
{ scriptString: 'setAnimation', scriptType: commandType.setAnimation },
{ scriptString: 'playEffect', scriptType: commandType.playEffect },
];
Expand All @@ -44,3 +44,6 @@ export const ADD_NEXT_ARG_LIST = [
commandType.filmMode,
commandType.playEffect,
];

export type ConfigMap = Map<string, ConfigItem>;
export type ConfigItem = { scriptString: string; scriptType: commandType };
62 changes: 37 additions & 25 deletions packages/parser/src/configParser/configParser.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import {argsParser} from "../scriptParser/argsParser";
import { argsParser } from '../scriptParser/argsParser';

interface IOptionItem{
key:string;
value:string | number | boolean;
interface IOptionItem {
key: string;
value: string | number | boolean;
}
interface IConfigItem{
command:string;
args:string[]
options:IOptionItem[];
interface IConfigItem {
command: string;
args: string[];
options: IOptionItem[];
}

export type WebgalConfig = IConfigItem[]
export type WebgalConfig = IConfigItem[];


function configLineParser(inputLine:string):IConfigItem{
const options:Array<IOptionItem> = [];
function configLineParser(inputLine: string): IConfigItem {
const options: Array<IOptionItem> = [];
let command: string;

let newSentenceRaw = inputLine.split(";")[0];
if (newSentenceRaw === "") {
let newSentenceRaw = inputLine.split(';')[0];
if (newSentenceRaw === '') {
// 注释提前返回
return {
command:'',
args:[],
options:[]
command: '',
args: [],
options: [],
};
}
// 截取命令
Expand All @@ -35,26 +34,39 @@ function configLineParser(inputLine:string):IConfigItem{
} else {
command = newSentenceRaw.substring(0, getCommandResult.index);
// 划分命令区域和content区域
newSentenceRaw = newSentenceRaw.substring(getCommandResult.index + 1, newSentenceRaw.length);
newSentenceRaw = newSentenceRaw.substring(
getCommandResult.index + 1,
newSentenceRaw.length,
);
}
// 截取 Options 区域
const getOptionsResult = / -/.exec(newSentenceRaw);
// 获取到参数
if (getOptionsResult) {
const optionsRaw = newSentenceRaw.substring(getOptionsResult.index, newSentenceRaw.length);
const optionsRaw = newSentenceRaw.substring(
getOptionsResult.index,
newSentenceRaw.length,
);
newSentenceRaw = newSentenceRaw.substring(0, getOptionsResult.index);
for (const e of argsParser(optionsRaw, (name,_)=>{return name})) {
for (const e of argsParser(optionsRaw, (name, _) => {
return name;
})) {
options.push(e);
}
}
return {
command,
args:newSentenceRaw.split('|').map(e=>e.trim()).filter(e=>e!==''),
options
args: newSentenceRaw
.split('|')
.map((e) => e.trim())
.filter((e) => e !== ''),
options,
};
}

export function configParser(configText:string):WebgalConfig{
const configLines = configText.replaceAll(`\r`,'').split('\n');
return configLines.map(e=>configLineParser(e)).filter(e=>e.command!=='');
export function configParser(configText: string): WebgalConfig {
const configLines = configText.replaceAll(`\r`, '').split('\n');
return configLines
.map((e) => configLineParser(e))
.filter((e) => e.command !== '');
}
90 changes: 60 additions & 30 deletions packages/parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,72 @@
import {commandType, IAsset} from "./interface/sceneInterface";
import {fileType} from "./interface/assets";
import {sceneParser} from "./sceneParser";
import {ADD_NEXT_ARG_LIST, SCRIPT_CONFIG} from "./config/scriptConfig";
import {configParser, WebgalConfig} from "./configParser/configParser";
import {
ADD_NEXT_ARG_LIST,
SCRIPT_CONFIG,
ConfigMap,
ConfigItem,
} from './config/scriptConfig';
import { configParser, WebgalConfig } from './configParser/configParser';
import { fileType } from './interface/assets';
import { IAsset } from './interface/sceneInterface';
import { sceneParser } from './sceneParser';

export default class SceneParser {

private readonly assetsPrefetcher;
private readonly assetSetter;
private readonly ADD_NEXT_ARG_LIST;
private readonly SCRIPT_CONFIG;

constructor(assetsPrefetcher: ((assetList: Array<IAsset>) => void),
assetSetter: (fileName: string, assetType: fileType) => string,
ADD_NEXT_ARG_LIST: Array<number>, SCRIPT_CONFIG: Array<any>) {
this.assetsPrefetcher = assetsPrefetcher;
this.assetSetter = assetSetter;
this.ADD_NEXT_ARG_LIST = ADD_NEXT_ARG_LIST;
this.SCRIPT_CONFIG = SCRIPT_CONFIG;
}

parse(rawScene: string, sceneName: string, sceneUrl: string
private readonly SCRIPT_CONFIG_MAP: ConfigMap;
constructor(
private readonly assetsPrefetcher: (assetList: IAsset[]) => void,
private readonly assetSetter: (
fileName: string,
assetType: fileType,
) => string,
private readonly ADD_NEXT_ARG_LIST: number[],
SCRIPT_CONFIG_INPUT: ConfigItem[] | ConfigMap,
) {
return sceneParser(rawScene, sceneName, sceneUrl, this.assetsPrefetcher, this.assetSetter, this.ADD_NEXT_ARG_LIST, this.SCRIPT_CONFIG);
if (Array.isArray(SCRIPT_CONFIG_INPUT)) {
this.SCRIPT_CONFIG_MAP = new Map();
SCRIPT_CONFIG_INPUT.forEach((config) => {
this.SCRIPT_CONFIG_MAP.set(config.scriptString, config);
});
} else {
this.SCRIPT_CONFIG_MAP = SCRIPT_CONFIG_INPUT;
}
}
/**
* 解析场景
* @param rawScene 原始场景
* @param sceneName 场景名称
* @param sceneUrl 场景url
* @return 解析后的场景
*/
parse(rawScene: string, sceneName: string, sceneUrl: string) {
return sceneParser(
rawScene,
sceneName,
sceneUrl,
this.assetsPrefetcher,
this.assetSetter,
this.ADD_NEXT_ARG_LIST,
this.SCRIPT_CONFIG_MAP,
);
}

parseConfig(configText: string) {
return configParser(configText)
return configParser(configText);
}

stringifyConfig(config: WebgalConfig) {
return config
.reduce(
(previousValue, curr) =>
(previousValue + `${curr.command}:${curr.args.join('|')}${curr.options.length <= 0 ? '' : curr.options.reduce((p, c) => (p + ' -' + c.key + '=' + c.value), '')};\n`),
''
)
return config.reduce(
(previousValue, curr) =>
previousValue +
`${curr.command}:${curr.args.join('|')}${
curr.options.length <= 0
? ''
: curr.options.reduce(
(p, c) => p + ' -' + c.key + '=' + c.value,
'',
)
};\n`,
'',
);
}
}

export {ADD_NEXT_ARG_LIST, SCRIPT_CONFIG};
export { ADD_NEXT_ARG_LIST, SCRIPT_CONFIG };
6 changes: 3 additions & 3 deletions packages/parser/src/interface/sceneInterface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* 语句类型
*/
import {sceneEntry} from "./runtimeInterface";
import {fileType} from "./assets";
import { sceneEntry } from './runtimeInterface';
import { fileType } from './assets';

export enum commandType {
say, // 对话
Expand Down Expand Up @@ -36,7 +36,7 @@ export enum commandType {
comment,
setTransform,
setTransition,
getUserInput
getUserInput,
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/parser4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@
// defaultRule: "scene"
// };
// }());
export const parser4 = 'unreleased'
export const parser4 = 'unreleased';
53 changes: 36 additions & 17 deletions packages/parser/src/sceneParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { IAsset, IScene, ISentence } from "./interface/sceneInterface";
import { scriptParser } from "./scriptParser/scriptParser";
import uniqWith from "lodash/uniqWith";
import { fileType } from "./interface/assets";
import {
commandType,
IAsset,
IScene,
ISentence,
} from './interface/sceneInterface';
import { scriptParser } from './scriptParser/scriptParser';
import uniqWith from 'lodash/uniqWith';
import { fileType } from './interface/assets';
import { ConfigMap } from './config/scriptConfig';

/**
* 场景解析器
Expand All @@ -11,27 +17,40 @@ import { fileType } from "./interface/assets";
* @param assetsPrefetcher
* @param assetSetter
* @param ADD_NEXT_ARG_LIST
* @param SCRIPT_CONFIG
* @param SCRIPT_CONFIG_MAP
* @return {IScene} 解析后的场景
*/
export const sceneParser = (rawScene: string, sceneName: string, sceneUrl: string
, assetsPrefetcher: ((assetList: Array<IAsset>) => void), assetSetter: (fileName: string, assetType: fileType) => string
, ADD_NEXT_ARG_LIST: any, SCRIPT_CONFIG: any): IScene => {
const rawSentenceList = rawScene.split("\n"); // 原始句子列表
export const sceneParser = (
rawScene: string,
sceneName: string,
sceneUrl: string,
assetsPrefetcher: (assetList: Array<IAsset>) => void,
assetSetter: (fileName: string, assetType: fileType) => string,
ADD_NEXT_ARG_LIST: commandType[],
SCRIPT_CONFIG_MAP: ConfigMap,
): IScene => {
const rawSentenceList = rawScene.split('\n'); // 原始句子列表

// 去分号留到后面去做了,现在注释要单独处理
const rawSentenceListWithoutEmpty = rawSentenceList;
// .map((sentence) => sentence.split(";")[0])
// .filter((sentence) => sentence.trim() !== "");
let assetsList: Array<IAsset> = []; // 场景资源列表
let subSceneList: Array<string> = []; // 子场景列表
const sentenceList: Array<ISentence> = rawSentenceListWithoutEmpty.map((sentence) => {
const returnSentence: ISentence = scriptParser(sentence, assetSetter, ADD_NEXT_ARG_LIST, SCRIPT_CONFIG);
// 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
assetsList = [...assetsList, ...returnSentence.sentenceAssets];
subSceneList = [...subSceneList, ...returnSentence.subScene];
return returnSentence;
});
const sentenceList: Array<ISentence> = rawSentenceListWithoutEmpty.map(
(sentence) => {
const returnSentence: ISentence = scriptParser(
sentence,
assetSetter,
ADD_NEXT_ARG_LIST,
SCRIPT_CONFIG_MAP,
);
// 在这里解析出语句可能携带的资源和场景,合并到 assetsList 和 subSceneList
assetsList = [...assetsList, ...returnSentence.sentenceAssets];
subSceneList = [...subSceneList, ...returnSentence.subScene];
return returnSentence;
},
);

// 开始资源的预加载
assetsList = uniqWith(assetsList); // 去重
Expand All @@ -42,6 +61,6 @@ export const sceneParser = (rawScene: string, sceneName: string, sceneUrl: strin
sceneUrl: sceneUrl,
sentenceList: sentenceList, // 语句列表
assetsList: assetsList, // 资源列表
subSceneList: subSceneList // 子场景列表
subSceneList: subSceneList, // 子场景列表
};
};
Loading

0 comments on commit d6cfa92

Please sign in to comment.