fix: handle Yarn initial variable storage fallback
This commit is contained in:
@@ -146,10 +146,45 @@ namespace AibisDream
|
||||
}
|
||||
}
|
||||
|
||||
if (TryGetInitialValue(variableName, out result))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryGetInitialValue<T>(string variableName, out T result)
|
||||
{
|
||||
result = default;
|
||||
|
||||
if (Program == null || !Program.InitialValues.TryGetValue(variableName, out var initialValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!InitialValueMatchesType<T>(initialValue.ValueCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Program.TryGetInitialValue(variableName, out result);
|
||||
}
|
||||
|
||||
private static bool InitialValueMatchesType<T>(Yarn.Operand.ValueOneofCase valueCase)
|
||||
{
|
||||
var targetType = typeof(T);
|
||||
|
||||
return valueCase switch
|
||||
{
|
||||
Yarn.Operand.ValueOneofCase.BoolValue => targetType == typeof(bool),
|
||||
Yarn.Operand.ValueOneofCase.StringValue => targetType == typeof(string),
|
||||
Yarn.Operand.ValueOneofCase.FloatValue => targetType == typeof(float),
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 序列化
|
||||
@@ -365,4 +400,4 @@ namespace AibisDream
|
||||
public string variableName;
|
||||
public object value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user