1.解决了Track面向的问题2.新增Surface类型的Spline渲染器3.新增with后缀表达式

This commit is contained in:
2025-10-07 01:45:52 +08:00
parent 60a5347dde
commit 54a28c6671
10 changed files with 1408 additions and 487 deletions

View File

@@ -32,22 +32,93 @@ namespace Demo
public static class ScriptCallUtility
{
// 解析函数调用的方法
public static (string functionName, string[] arguments) ParseFunctionCall(string input)
// 解析函数调用的方法,支持 with 子句
public static (string functionName, string[] arguments, Dictionary<string, string> withVariables) ParseFunctionCall(string input)
{
// 匹配函数名和参数部分
Match match = Regex.Match(input, @"^(\w+)\s*\(\s*(.*?)\s*\)\s*;?$");
// 匹配函数名和参数部分,以及可选的 with 子句
Match match = Regex.Match(input, @"^(\w+)\s*\(\s*(.*?)\s*\)\s*(?:\s+with\s*\(\s*(.*?)\s*\))?\s*;?$");
if (!match.Success)
return (null, new string[0]);
return (null, new string[0], new Dictionary<string, string>());
string functionName = match.Groups[1].Value;
string argumentsString = match.Groups[2].Value;
string withString = match.Groups[3].Value;
// 解析参数数组
string[] arguments = ParseArguments(argumentsString);
return (functionName, arguments);
// 解析 with 子句的变量
Dictionary<string, string> withVariables = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(withString))
{
withVariables = ParseWithClause(withString);
}
return (functionName, arguments, withVariables);
}
// 解析 with 子句的方法
private static Dictionary<string, string> ParseWithClause(string withString)
{
var variables = new Dictionary<string, string>();
if (string.IsNullOrWhiteSpace(withString))
return variables;
int i = 0;
while (i < withString.Length)
{
// 跳过空白字符
while (i < withString.Length && char.IsWhiteSpace(withString[i]))
i++;
if (i >= withString.Length)
break;
// 解析变量名
int nameStart = i;
while (i < withString.Length && (char.IsLetterOrDigit(withString[i]) || withString[i] == '_'))
i++;
if (i == nameStart)
break; // 没有找到有效的变量名
string varName = withString.Substring(nameStart, i - nameStart);
// 跳过空白字符
while (i < withString.Length && char.IsWhiteSpace(withString[i]))
i++;
// 检查等号
if (i >= withString.Length || withString[i] != '=')
break; // 没有找到等号
i++; // 跳过等号
// 跳过空白字符
while (i < withString.Length && char.IsWhiteSpace(withString[i]))
i++;
// 解析变量值
string varValue = ExtractArgument(withString, ref i);
variables[varName] = varValue.Trim();
// 跳过空白字符
while (i < withString.Length && char.IsWhiteSpace(withString[i]))
i++;
// 检查逗号
if (i < withString.Length && withString[i] == ',')
{
i++; // 跳过逗号
}
else if (i < withString.Length)
{
break; // 期望逗号但找到了其他字符
}
}
return variables;
}
// 解析参数的方法
@@ -382,7 +453,14 @@ namespace Demo
}
throw new ArgumentException("value is end by '}' but not match on any invlid parse");
}
return float.Parse(value);
try
{
return float.Parse(value);
}
catch
{
throw new FormatException($"{value} is not support any Parser");
}
}
}
@@ -726,7 +804,7 @@ namespace Demo
}
var exprs = preprocessing.Split(';');
ParseStats ParseCommandAndParamaterWords(int commandIterator, ref string command, ref string[] words)
ParseStats ParseCommandAndParamaterWords(int commandIterator, ref string command, ref string[] words, ref Dictionary<string,string> withVariables)
{
var expr = exprs[commandIterator].Trim();
//空语句
@@ -745,7 +823,7 @@ namespace Demo
MethodInvokerCache.Add(this.GetType(), new());
}
//函数解析
(command, words) = ScriptCallUtility.ParseFunctionCall(expr);
(command, words, withVariables) = ScriptCallUtility.ParseFunctionCall(expr);
if (string.IsNullOrEmpty(command))
return ParseStats.Continue;
for (int i = 0, e = words.Length; i < e; i++)
@@ -763,7 +841,8 @@ namespace Demo
{
string command = null;
string[] words = null;
var stats = ParseCommandAndParamaterWords(commandIterator, ref command, ref words);
Dictionary<string, string> withVariables = new();
var stats = ParseCommandAndParamaterWords(commandIterator, ref command, ref words,ref withVariables);
if (stats == ParseStats.Continue)
continue;
else if (stats == ParseStats.Break)
@@ -781,13 +860,16 @@ namespace Demo
// Main Loop
for (int commandIterator = 0; commandIterator < exprs.Length; commandIterator++)
{
// Run Stats
string command = null;
string[] words = null;
var stats = ParseCommandAndParamaterWords(commandIterator, ref command, ref words);
Dictionary<string, string> withVariables = new();
var stats = ParseCommandAndParamaterWords(commandIterator, ref command, ref words, ref withVariables);
if (stats == ParseStats.Continue)
continue;
else if (stats == ParseStats.Break)
break;
// Logic
if (command == "label")
{
@@ -808,6 +890,8 @@ namespace Demo
continue;
}
}
// Functions
var paramsList = (from word in words where string.IsNullOrEmpty(word.Trim()) == false select (object)word.Trim()).ToArray();
if (MethodInvokerCache[this.GetType()].TryGetValue(command, out MemberInfo commandInfo) == false)
{
@@ -823,6 +907,23 @@ namespace Demo
}
Debug.Log($"in line \"{expr}\" of \"{ScriptPath}\", {command} is try to invoke", this);
IEnumerator resultEnumerator = null;
// 处理 with 子句:保存当前变量状态并设置新变量
Dictionary<string, float> originalVariables = new Dictionary<string, float>();
if (withVariables.Count > 0)
{
foreach (var kvp in withVariables)
{
// 保存原始值(如果存在)
if (ScriptContextSpace.TryGetValue(kvp.Key, out var originalValue))
{
originalVariables[kvp.Key] = originalValue;
}
// 设置新值
ScriptContextSpace[kvp.Key] = Parse(kvp.Value);
}
}
try
{
// 调用成功
@@ -852,7 +953,26 @@ namespace Demo
Debug.LogException(ex, this);
yield break;
}
yield return resultEnumerator;
// 恢复 with 子句中的变量状态
if (withVariables.Count > 0)
{
foreach (var kvp in withVariables)
{
if (originalVariables.ContainsKey(kvp.Key))
{
// 恢复原始值
ScriptContextSpace[kvp.Key] = originalVariables[kvp.Key];
}
else
{
// 删除不存在的变量
ScriptContextSpace.Remove(kvp.Key);
}
}
}
}
}