Skip to content

Commit

Permalink
make sure that all numeric dynamic parameter values are either float,…
Browse files Browse the repository at this point in the history
… int or uint
  • Loading branch information
jupahe64 committed Dec 16, 2023
1 parent 9880b6c commit d570312
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Fushigi.Byml/BymlBigDataNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Fushigi.Byml
{
public class BymlBigDataNode<T> : IBymlNode
public class BymlBigDataNode<T> : IBymlNode, IBymlValueNode
{
public BymlNodeId Id { get; }
public T Data { get; set; }

object IBymlValueNode.GetValue() => Data!;

public BymlBigDataNode(BymlNodeId id, BinaryReader reader, Func<BinaryReader, T> valueReader)
{
Id = id;
Expand Down
8 changes: 7 additions & 1 deletion Fushigi.Byml/BymlTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ public interface IBymlNode
public BymlNodeId Id { get; }
}

public class BymlNode<T> : IBymlNode
public interface IBymlValueNode
{
public object GetValue();
}

public class BymlNode<T> : IBymlNode, IBymlValueNode
{
public BymlNodeId Id { get; }
public T Data;
object IBymlValueNode.GetValue() => Data!;

public BymlNode(BymlNodeId id, T data)
{
Expand Down
4 changes: 2 additions & 2 deletions Fushigi/param/ParamDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)!,
Expand Down
26 changes: 15 additions & 11 deletions Fushigi/util/BymlUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ public static class BymlUtil
{
public static T GetNodeData<T>(IBymlNode node)
{
if (node.Id == BymlNodeId.Int64 || node.Id == BymlNodeId.UInt64)
if (node is BymlBigDataNode<T> bigDataNode)
{
return ((BymlBigDataNode<T>)node).Data;
return bigDataNode.Data;
}

return ((BymlNode<T>)node).Data;
}

public static object GetNodeValue(IBymlNode node)
{
return ((IBymlValueNode)node).GetValue();
}

public static T GetNodeFromArray<T>(BymlArrayNode? array, int idx)
{
return ((BymlNode<T>)array[idx]).Data;
Expand Down Expand Up @@ -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<int>(node);
if (param.IsSignedInt() && node.Id == BymlNodeId.UInt)
return BymlUtil.GetNodeData<uint>(node);

if (param.IsUnsignedInt())
return BymlUtil.GetNodeData<uint>(node);
return Convert.ToUInt32(BymlUtil.GetNodeValue(node));
if (param.IsSignedInt())
return BymlUtil.GetNodeData<int>(node);
return Convert.ToInt32(BymlUtil.GetNodeValue(node));
if (param.IsBool())
return BymlUtil.GetNodeData<bool>(node);
if (param.IsString())
return BymlUtil.GetNodeData<string>(node);
if (param.IsFloat())
{
if (node is BymlBigDataNode<double> doubleNode)
return (float)doubleNode.Data;

return BymlUtil.GetNodeData<float>(node);
}
if (param.IsDouble())
return BymlUtil.GetNodeData<double>(node);
return (float)BymlUtil.GetNodeData<double>(node);

return null!;
}
Expand Down

0 comments on commit d570312

Please sign in to comment.