-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathevalSentenceFn.ts
More file actions
82 lines (77 loc) · 2.35 KB
/
evalSentenceFn.ts
File metadata and controls
82 lines (77 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { webgalStore } from '@/store/store';
import { random } from 'lodash';
import { WebGAL } from '../WebGAL';
import expression from 'angular-expressions';
import { logger } from '@/Core/util/logger';
// 是否是函数调用
export const isFunctionCall = (valExp: string) => {
return /^\s*[a-zA-Z_$][\w$]*\s*\(.*\)\s*$/.test(valExp);
};
export interface EvaluateExpressionOptions {
/**
* 当是无效值 `null | undefined | 报错` 时返回原值还是返回{...}包裹
* @default block {...}包裹
*/
InvalidValueReturns?: 'origin' | 'block';
/**
* 当是表达式报错时,是否返回布尔值
*/
ErrorReturnsBoolean?: boolean;
}
/**
* 执行运行时表达式
*/
export const EvaluateExpression = (val: string, options: EvaluateExpressionOptions = {}) => {
const sceneUrl = WebGAL.sceneManager.sceneData.currentScene.sceneUrl;
const sceneArguments = webgalStore.getState().stage.sceneArguments;
const stage = webgalStore.getState().stage;
const userData = webgalStore.getState().userData;
const globalVars = userData.globalGameVar;
const localVars = stage.GameVar;
const _Merge = { $stage: stage, $userData: userData }; // 不要直接合并到一起,防止可能的键冲突
try {
const instance = expression.compile(val);
const evalResult = instance({
// 注入变量
...globalVars,
...localVars,
..._Merge,
// 随机函数
random(...args: any[]) {
return args.length ? random(...args) : Math.random();
},
// 获取场景调用参数
getArg(key: string) {
const target = sceneArguments[sceneUrl];
if (target) {
return target.find((item) => item.key === key)?.value ?? null;
}
return null;
},
});
if ((evalResult === null || evalResult === undefined) && options) {
switch (options.InvalidValueReturns) {
case 'block':
return `{${val}}`;
case 'origin':
return val;
default:
return evalResult;
}
}
return evalResult;
} catch {
logger.warn('EvaluateExpression throw error, expr = ' + val);
if (options.ErrorReturnsBoolean) {
return false;
}
switch (options.InvalidValueReturns) {
case 'block':
return `{${val}}`;
case 'origin':
return val;
default:
return `{${val}}`;
}
}
};