From d57031296ad106ab5b4b438592ea689ca41b3cd2 Mon Sep 17 00:00:00 2001 From: jupahe64 Date: Sat, 16 Dec 2023 04:21:37 +0100 Subject: [PATCH] make sure that all numeric dynamic parameter values are either float, int or uint --- Fushigi.Byml/BymlBigDataNode.cs | 4 +++- Fushigi.Byml/BymlTypes.cs | 8 +++++++- Fushigi/param/ParamDB.cs | 4 ++-- Fushigi/util/BymlUtil.cs | 26 +++++++++++++++----------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Fushigi.Byml/BymlBigDataNode.cs b/Fushigi.Byml/BymlBigDataNode.cs index c9c5e024..f15ddcbd 100644 --- a/Fushigi.Byml/BymlBigDataNode.cs +++ b/Fushigi.Byml/BymlBigDataNode.cs @@ -2,11 +2,13 @@ namespace Fushigi.Byml { - public class BymlBigDataNode : IBymlNode + public class BymlBigDataNode : IBymlNode, IBymlValueNode { public BymlNodeId Id { get; } public T Data { get; set; } + object IBymlValueNode.GetValue() => Data!; + public BymlBigDataNode(BymlNodeId id, BinaryReader reader, Func valueReader) { Id = id; diff --git a/Fushigi.Byml/BymlTypes.cs b/Fushigi.Byml/BymlTypes.cs index 9a8b8d7d..77606d7f 100644 --- a/Fushigi.Byml/BymlTypes.cs +++ b/Fushigi.Byml/BymlTypes.cs @@ -68,10 +68,16 @@ public interface IBymlNode public BymlNodeId Id { get; } } - public class BymlNode : IBymlNode + public interface IBymlValueNode + { + public object GetValue(); + } + + public class BymlNode : IBymlNode, IBymlValueNode { public BymlNodeId Id { get; } public T Data; + object IBymlValueNode.GetValue() => Data!; public BymlNode(BymlNodeId id, T data) { diff --git a/Fushigi/param/ParamDB.cs b/Fushigi/param/ParamDB.cs index 5111b0d1..deec670b 100644 --- a/Fushigi/param/ParamDB.cs +++ b/Fushigi/param/ParamDB.cs @@ -44,9 +44,9 @@ private static object CastJsonValue(object jsonValue, string type) { return type switch { - "S16" => Convert.ToInt16(jsonValue)!, + "S16" => Convert.ToInt32(jsonValue)!, "S32" => Convert.ToInt32(jsonValue)!, - "U8" => Convert.ToByte(jsonValue)!, + "U8" => Convert.ToUInt32(jsonValue)!, "U32" => Convert.ToUInt32(jsonValue)!, "F32" => Convert.ToSingle(jsonValue)!, "F64" => Convert.ToDouble(jsonValue)!, diff --git a/Fushigi/util/BymlUtil.cs b/Fushigi/util/BymlUtil.cs index 594be177..2d2a0d56 100644 --- a/Fushigi/util/BymlUtil.cs +++ b/Fushigi/util/BymlUtil.cs @@ -14,14 +14,19 @@ public static class BymlUtil { public static T GetNodeData(IBymlNode node) { - if (node.Id == BymlNodeId.Int64 || node.Id == BymlNodeId.UInt64) + if (node is BymlBigDataNode bigDataNode) { - return ((BymlBigDataNode)node).Data; + return bigDataNode.Data; } return ((BymlNode)node).Data; } + public static object GetNodeValue(IBymlNode node) + { + return ((IBymlValueNode)node).GetValue(); + } + public static T GetNodeFromArray(BymlArrayNode? array, int idx) { return ((BymlNode)array[idx]).Data; @@ -60,24 +65,23 @@ public static System.Numerics.Vector3 GetVector3FromArray(BymlArrayNode? array) public static object GetValueFromDynamicNode(IBymlNode node, ParamDB.ComponentParam param) { - //TODO should we cast to the correct signed-ness? - if (param.IsUnsignedInt() && node.Id == BymlNodeId.Int) - return BymlUtil.GetNodeData(node); - if (param.IsSignedInt() && node.Id == BymlNodeId.UInt) - return BymlUtil.GetNodeData(node); - if (param.IsUnsignedInt()) - return BymlUtil.GetNodeData(node); + return Convert.ToUInt32(BymlUtil.GetNodeValue(node)); if (param.IsSignedInt()) - return BymlUtil.GetNodeData(node); + return Convert.ToInt32(BymlUtil.GetNodeValue(node)); if (param.IsBool()) return BymlUtil.GetNodeData(node); if (param.IsString()) return BymlUtil.GetNodeData(node); if (param.IsFloat()) + { + if (node is BymlBigDataNode doubleNode) + return (float)doubleNode.Data; + return BymlUtil.GetNodeData(node); + } if (param.IsDouble()) - return BymlUtil.GetNodeData(node); + return (float)BymlUtil.GetNodeData(node); return null!; }