Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Commit

Permalink
Replace FloatX types with .NET System.Numeric types
Browse files Browse the repository at this point in the history
This reduces the amount of custom code we need. .NET's Numeric classes
also have many more built-in convenience functions.

This also supports .NET's Quaternion next to .NET's Vector4, which will
use Slerp instead of Lerp to fix interpolation issues.
  • Loading branch information
NoTuxNoBux committed Jan 8, 2021
1 parent 5b43b1d commit 96ee91d
Show file tree
Hide file tree
Showing 37 changed files with 449 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public enum ForgeAcceptableFieldTypes
COLOR = 16,
//OBJECT_ARRAY = 17, //Unsupported
//BYTE_ARRAY = 18,
FLOAT2 = 19,
FLOAT3 = 20,
FLOAT4 = 21,
DOTNET_VECTOR2 = 19,
DOTNET_VECTOR3 = 20,
DOTNET_VECTOR4 = 21,
DOTNET_QUATERNION = 22,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public enum ForgeAcceptableRPCTypes
COLOR = 16,
//OBJECT_ARRAY = 17,
BYTE_ARRAY = 18,
FLOAT2 = 19,
FLOAT3 = 20,
FLOAT4 = 21,
DOTNET_VECTOR2 = 19,
DOTNET_VECTOR3 = 20,
DOTNET_VECTOR4 = 21,
DOTNET_QUATERNION = 22,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using BeardedManStudios.Templating;
using SimpleJSONEditor;
using System;
using Numerics = System.Numerics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -85,12 +86,14 @@ public static ForgeClassFieldRPCValue GetClassField(FieldInfo field, Type t, boo
type = ForgeAcceptableRPCTypes.VECTOR3;
else if (fieldType == typeof(Vector4))
type = ForgeAcceptableRPCTypes.VECTOR4;
else if (fieldType == typeof(Float2))
type = ForgeAcceptableRPCTypes.FLOAT2;
else if (fieldType == typeof(Float3))
type = ForgeAcceptableRPCTypes.FLOAT3;
else if (fieldType == typeof(Float4))
type = ForgeAcceptableRPCTypes.FLOAT4;
else if (fieldType == typeof(Numerics.Vector2))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR2;
else if (fieldType == typeof(Numerics.Vector3))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR3;
else if (fieldType == typeof(Numerics.Vector4))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR4;
else if (fieldType == typeof(Numerics.Quaternion))
type = ForgeAcceptableRPCTypes.DOTNET_QUATERNION;
else if (fieldType == typeof(string))
type = ForgeAcceptableRPCTypes.STRING;
//else if (fieldType == typeof(object[]))
Expand Down Expand Up @@ -139,12 +142,14 @@ public static Type GetTypeFromAcceptable(ForgeAcceptableRPCTypes type)
return typeof(Vector3);
case ForgeAcceptableRPCTypes.VECTOR4:
return typeof(Vector4);
case ForgeAcceptableRPCTypes.FLOAT2:
return typeof(Float2);
case ForgeAcceptableRPCTypes.FLOAT3:
return typeof(Float3);
case ForgeAcceptableRPCTypes.FLOAT4:
return typeof(Float4);
case ForgeAcceptableRPCTypes.DOTNET_VECTOR2:
return typeof(Numerics.Vector2);
case ForgeAcceptableRPCTypes.DOTNET_VECTOR3:
return typeof(Numerics.Vector3);
case ForgeAcceptableRPCTypes.DOTNET_VECTOR4:
return typeof(Numerics.Vector4);
case ForgeAcceptableRPCTypes.DOTNET_QUATERNION:
return typeof(Numerics.Quaternion);
case ForgeAcceptableRPCTypes.STRING:
return typeof(string);
//case ForgeAcceptableRPCTypes.OBJECT_ARRAY:
Expand Down Expand Up @@ -192,12 +197,14 @@ public static ForgeAcceptableRPCTypes GetTypeFromAcceptable(string val)
return ForgeAcceptableRPCTypes.VECTOR3;
case "vector4":
return ForgeAcceptableRPCTypes.VECTOR4;
case "float2":
return ForgeAcceptableRPCTypes.FLOAT2;
case "float3":
return ForgeAcceptableRPCTypes.FLOAT3;
case "float4":
return ForgeAcceptableRPCTypes.FLOAT4;
case "dotnetvector2":
return ForgeAcceptableRPCTypes.DOTNET_VECTOR2;
case "dotnetvector3":
return ForgeAcceptableRPCTypes.DOTNET_VECTOR3;
case "dotnetvector4":
return ForgeAcceptableRPCTypes.DOTNET_VECTOR4;
case "dotnetquaternion":
return ForgeAcceptableRPCTypes.DOTNET_QUATERNION;
case "string":
return ForgeAcceptableRPCTypes.STRING;
//case "object[]":
Expand Down
65 changes: 38 additions & 27 deletions ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Reflection;
using UnityEngine;
using Numerics = System.Numerics;

namespace BeardedManStudios.Forge.Networking.UnityEditor
{
Expand Down Expand Up @@ -78,12 +79,14 @@ public static ForgeClassFieldValue GetClassField(FieldInfo field, Type t, bool i
type = ForgeAcceptableFieldTypes.VECTOR3;
else if (fieldType == typeof(Vector4))
type = ForgeAcceptableFieldTypes.VECTOR4;
else if (fieldType == typeof(Float2))
type = ForgeAcceptableFieldTypes.FLOAT2;
else if (fieldType == typeof(Float3))
type = ForgeAcceptableFieldTypes.FLOAT3;
else if (fieldType == typeof(Float4))
type = ForgeAcceptableFieldTypes.FLOAT4;
else if (fieldType == typeof(Numerics.Vector2))
type = ForgeAcceptableFieldTypes.DOTNET_VECTOR2;
else if (fieldType == typeof(Numerics.Vector3))
type = ForgeAcceptableFieldTypes.DOTNET_VECTOR3;
else if (fieldType == typeof(Numerics.Vector4))
type = ForgeAcceptableFieldTypes.DOTNET_VECTOR4;
else if (fieldType == typeof(Numerics.Quaternion))
type = ForgeAcceptableFieldTypes.DOTNET_QUATERNION;
//else if (fieldType == typeof(string))
// type = ForgeAcceptableFieldTypes.STRING; //Unsupported
//else if (fieldType == typeof(object[]))
Expand Down Expand Up @@ -132,12 +135,14 @@ public static Type GetTypeFromAcceptable(ForgeAcceptableFieldTypes type)
return typeof(Vector3);
case ForgeAcceptableFieldTypes.VECTOR4:
return typeof(Vector4);
case ForgeAcceptableFieldTypes.FLOAT2:
return typeof(Float2);
case ForgeAcceptableFieldTypes.FLOAT3:
return typeof(Float3);
case ForgeAcceptableFieldTypes.FLOAT4:
return typeof(Float4);
case ForgeAcceptableFieldTypes.DOTNET_VECTOR2:
return typeof(Numerics.Vector2);
case ForgeAcceptableFieldTypes.DOTNET_VECTOR3:
return typeof(Numerics.Vector3);
case ForgeAcceptableFieldTypes.DOTNET_VECTOR4:
return typeof(Numerics.Vector4);
case ForgeAcceptableFieldTypes.DOTNET_QUATERNION:
return typeof(Numerics.Quaternion);
//case ForgeAcceptableFieldTypes.STRING: //Unsupported
// return typeof(string);
//case ForgeAcceptableFieldTypes.OBJECT_ARRAY: //Unsupported
Expand Down Expand Up @@ -170,14 +175,17 @@ public static string GetInterpolateFromAcceptable(string baseTypeString, ForgeAc
case ForgeAcceptableFieldTypes.QUATERNION:
returnValue = "InterpolateQuaternion";
break;
case ForgeAcceptableFieldTypes.FLOAT2:
returnValue = "InterpolateFloat2";
case ForgeAcceptableFieldTypes.DOTNET_VECTOR2:
returnValue = "InterpolateDotnetVector2";
break;
case ForgeAcceptableFieldTypes.FLOAT3:
returnValue = "InterpolateFloat3";
case ForgeAcceptableFieldTypes.DOTNET_VECTOR3:
returnValue = "InterpolateDotnetVector3";
break;
case ForgeAcceptableFieldTypes.FLOAT4:
returnValue = "InterpolateFloat4";
case ForgeAcceptableFieldTypes.DOTNET_VECTOR4:
returnValue = "InterpolateDotnetVector4";
break;
case ForgeAcceptableFieldTypes.DOTNET_QUATERNION:
returnValue = "InterpolateDotnetQuaternion";
break;
default:
returnValue = "Interpolated<" + baseTypeString + ">";
Expand All @@ -197,10 +205,11 @@ public static bool IsInterpolatable(ForgeAcceptableFieldTypes type)
case ForgeAcceptableFieldTypes.VECTOR2:
case ForgeAcceptableFieldTypes.VECTOR3:
case ForgeAcceptableFieldTypes.VECTOR4:
case ForgeAcceptableFieldTypes.FLOAT2:
case ForgeAcceptableFieldTypes.FLOAT3:
case ForgeAcceptableFieldTypes.FLOAT4:
case ForgeAcceptableFieldTypes.QUATERNION:
case ForgeAcceptableFieldTypes.DOTNET_VECTOR2:
case ForgeAcceptableFieldTypes.DOTNET_VECTOR3:
case ForgeAcceptableFieldTypes.DOTNET_VECTOR4:
case ForgeAcceptableFieldTypes.DOTNET_QUATERNION:
returnValue = true;
break;
}
Expand Down Expand Up @@ -244,12 +253,14 @@ public static ForgeAcceptableFieldTypes GetTypeFromAcceptable(string val)
return ForgeAcceptableFieldTypes.VECTOR3;
case "vector4":
return ForgeAcceptableFieldTypes.VECTOR4;
case "float2":
return ForgeAcceptableFieldTypes.FLOAT2;
case "float3":
return ForgeAcceptableFieldTypes.FLOAT3;
case "float4":
return ForgeAcceptableFieldTypes.FLOAT4;
case "dotnetvector2":
return ForgeAcceptableFieldTypes.DOTNET_VECTOR2;
case "dotnetvector3":
return ForgeAcceptableFieldTypes.DOTNET_VECTOR3;
case "dotnetvector4":
return ForgeAcceptableFieldTypes.DOTNET_VECTOR4;
case "dotnetquaternion":
return ForgeAcceptableFieldTypes.DOTNET_QUATERNION;
//case "string":
// return ForgeAcceptableFieldTypes.STRING; //Unsupported
//case "object[]":
Expand Down
15 changes: 9 additions & 6 deletions ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRPCValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using UnityEditor;
using UnityEngine;
using Numerics = System.Numerics;

namespace BeardedManStudios.Forge.Networking.UnityEditor
{
Expand Down Expand Up @@ -81,12 +82,14 @@ public static ForgeAcceptableRPCTypes GetATypeFromPInfo(ParameterInfo pInfo)
type = ForgeAcceptableRPCTypes.VECTOR3;
else if (fieldType == typeof(Vector4))
type = ForgeAcceptableRPCTypes.VECTOR4;
else if (fieldType == typeof(Float2))
type = ForgeAcceptableRPCTypes.FLOAT2;
else if (fieldType == typeof(Float3))
type = ForgeAcceptableRPCTypes.FLOAT3;
else if (fieldType == typeof(Float4))
type = ForgeAcceptableRPCTypes.FLOAT4;
else if (fieldType == typeof(Numerics.Vector2))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR2;
else if (fieldType == typeof(Numerics.Vector3))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR3;
else if (fieldType == typeof(Numerics.Vector4))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR4;
else if (fieldType == typeof(Numerics.Quaternion))
type = ForgeAcceptableRPCTypes.DOTNET_QUATERNION;
else if (fieldType == typeof(string))
type = ForgeAcceptableRPCTypes.STRING;
//else if (fieldType == typeof(object[]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using UnityEditor;
using UnityEngine;
using Numerics = System.Numerics;

namespace BeardedManStudios.Forge.Networking.UnityEditor
{
Expand Down Expand Up @@ -67,12 +68,14 @@ public static ForgeAcceptableRPCTypes GetATypeFromPInfo(ParameterInfo pInfo)
type = ForgeAcceptableRPCTypes.VECTOR3;
else if (fieldType == typeof(Vector4))
type = ForgeAcceptableRPCTypes.VECTOR4;
else if (fieldType == typeof(Float2))
type = ForgeAcceptableRPCTypes.FLOAT2;
else if (fieldType == typeof(Float3))
type = ForgeAcceptableRPCTypes.FLOAT3;
else if (fieldType == typeof(Float4))
type = ForgeAcceptableRPCTypes.FLOAT4;
else if (fieldType == typeof(Numerics.Vector2))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR2;
else if (fieldType == typeof(Numerics.Vector3))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR3;
else if (fieldType == typeof(Numerics.Vector4))
type = ForgeAcceptableRPCTypes.DOTNET_VECTOR4;
else if (fieldType == typeof(Numerics.Quaternion))
type = ForgeAcceptableRPCTypes.DOTNET_QUATERNION;
else if (fieldType == typeof(string))
type = ForgeAcceptableRPCTypes.STRING;
//else if (fieldType == typeof(object[]))
Expand Down
64 changes: 33 additions & 31 deletions ForgeUnity/Assets/BeardedManStudios/Editor/ForgeNetworkingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using BeardedManStudios.Templating;
using UnityEditor;
using UnityEngine;
using Numerics = System.Numerics;

namespace BeardedManStudios.Forge.Networking.UnityEditor
{
Expand Down Expand Up @@ -86,7 +87,7 @@ public class ForgeNetworkingEditor : EditorWindow
/// </summary>
private Dictionary<object, string> _referenceVariables = new Dictionary<object, string>();
/// <summary>
/// This is the bitwise
/// This is the bitwise
/// </summary>
private List<string> _referenceBitWise = new List<string>();

Expand Down Expand Up @@ -183,29 +184,30 @@ public void Initialize()
_referenceBitWise.Add("0x80");

_referenceVariables = new Dictionary<object, string>();
_referenceVariables.Add(typeof(bool).Name, "bool");
_referenceVariables.Add(typeof(byte).Name, "byte");
_referenceVariables.Add(typeof(char).Name, "char");
_referenceVariables.Add(typeof(short).Name, "short");
_referenceVariables.Add(typeof(ushort).Name, "ushort");
_referenceVariables.Add(typeof(int).Name, "int");
_referenceVariables.Add(typeof(uint).Name, "uint");
_referenceVariables.Add(typeof(float).Name, "float");
_referenceVariables.Add(typeof(long).Name, "long");
_referenceVariables.Add(typeof(ulong).Name, "ulong");
_referenceVariables.Add(typeof(double).Name, "double");
_referenceVariables.Add(typeof(string).Name, "string");
_referenceVariables.Add(typeof(Vector2).Name, "Vector2");
_referenceVariables.Add(typeof(Vector3).Name, "Vector3");
_referenceVariables.Add(typeof(Vector4).Name, "Vector4");
_referenceVariables.Add(typeof(Quaternion).Name, "Quaternion");
_referenceVariables.Add(typeof(Color).Name, "Color");
_referenceVariables.Add(typeof(Float2).Name, "Float2");
_referenceVariables.Add(typeof(Float3).Name, "Float3");
_referenceVariables.Add(typeof(Float4).Name, "Float4");
_referenceVariables.Add(typeof(object).Name, "object");
_referenceVariables.Add(typeof(object[]).Name, "object[]");
_referenceVariables.Add(typeof(byte[]).Name, "byte[]");
_referenceVariables.Add(typeof(bool).FullName, "bool");
_referenceVariables.Add(typeof(byte).FullName, "byte");
_referenceVariables.Add(typeof(char).FullName, "char");
_referenceVariables.Add(typeof(short).FullName, "short");
_referenceVariables.Add(typeof(ushort).FullName, "ushort");
_referenceVariables.Add(typeof(int).FullName, "int");
_referenceVariables.Add(typeof(uint).FullName, "uint");
_referenceVariables.Add(typeof(float).FullName, "float");
_referenceVariables.Add(typeof(long).FullName, "long");
_referenceVariables.Add(typeof(ulong).FullName, "ulong");
_referenceVariables.Add(typeof(double).FullName, "double");
_referenceVariables.Add(typeof(string).FullName, "string");
_referenceVariables.Add(typeof(Vector2).FullName, "Vector2");
_referenceVariables.Add(typeof(Vector3).FullName, "Vector3");
_referenceVariables.Add(typeof(Vector4).FullName, "Vector4");
_referenceVariables.Add(typeof(Quaternion).FullName, "Quaternion");
_referenceVariables.Add(typeof(Color).FullName, "Color");
_referenceVariables.Add(typeof(Numerics.Vector2).FullName, "Numerics.Vector2");
_referenceVariables.Add(typeof(Numerics.Vector3).FullName, "Numerics.Vector3");
_referenceVariables.Add(typeof(Numerics.Vector4).FullName, "Numerics.Vector4");
_referenceVariables.Add(typeof(Numerics.Quaternion).FullName, "Numerics.Quaternion");
_referenceVariables.Add(typeof(object).FullName, "object");
_referenceVariables.Add(typeof(object[]).FullName, "object[]");
_referenceVariables.Add(typeof(byte[]).FullName, "byte[]");

_scrollView = Vector2.zero;
_editorButtons = new List<ForgeEditorButton>();
Expand Down Expand Up @@ -736,14 +738,14 @@ public string SourceCodeNetworkObject(ForgeClassObject cObj, ForgeEditorButton b
for (i = 0, j = 0; i < btn.ClassVariables.Count; ++i)
{
Type t = ForgeClassFieldValue.GetTypeFromAcceptable(btn.ClassVariables[i].FieldType);
interpolateType = ForgeClassFieldValue.GetInterpolateFromAcceptable(_referenceVariables[t.Name], btn.ClassVariables[i].FieldType);
interpolateType = ForgeClassFieldValue.GetInterpolateFromAcceptable(_referenceVariables[t.FullName], btn.ClassVariables[i].FieldType);

if (i != 0 && i % 8 == 0)
j++;

object[] fieldData = new object[]
{
_referenceVariables[t.Name], // Data type
_referenceVariables[t.FullName], // Data type
btn.ClassVariables[i].FieldName.Replace(" ", string.Empty), // Field name
btn.ClassVariables[i].Interpolate, // Interpolated
interpolateType, // Interpolate type
Expand Down Expand Up @@ -816,20 +818,20 @@ public string SourceCodeNetworkBehavior(ForgeClassObject cObj, ForgeEditorButton
{
Type t = ForgeClassFieldRPCValue.GetTypeFromAcceptable(btn.RPCVariables[i].FieldTypes[x].Type);

helperNames.AppendLine("\t\t/// " + _referenceVariables[t.Name] + " " + btn.RPCVariables[i].FieldTypes[x].HelperName);
helperNames.AppendLine("\t\t/// " + _referenceVariables[t.FullName] + " " + btn.RPCVariables[i].FieldTypes[x].HelperName);

string fieldHelper = btn.RPCVariables[i].FieldTypes[x].HelperName;
if (x + 1 < btn.RPCVariables[i].ArgumentCount)
{
innerTypes.Append(", typeof(" + _referenceVariables[t.Name] + ")");
innerJSON.Append("\"" + _referenceVariables[t.Name] + "\", ");
innerTypes.Append(", typeof(" + _referenceVariables[t.FullName] + ")");
innerJSON.Append("\"" + _referenceVariables[t.FullName] + "\", ");
innerHelperTypesJSON.Append("\"" + fieldHelper + "\", ");

}
else
{
innerTypes.Append(", typeof(" + _referenceVariables[t.Name] + ")");
innerJSON.Append("\"" + _referenceVariables[t.Name] + "\"");
innerTypes.Append(", typeof(" + _referenceVariables[t.FullName] + ")");
innerJSON.Append("\"" + _referenceVariables[t.FullName] + "\"");
innerHelperTypesJSON.Append("\"" + fieldHelper + "\"");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using BeardedManStudios.Forge.Networking.Frame;
using BeardedManStudios.Forge.Networking.Unity;
using System;
using UnityEngine;
using Numerics = System.Numerics;

namespace BeardedManStudios.Forge.Networking.Generated
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Unity;
using BeardedManStudios.Forge.Networking.StandAlone;
using Numerics = System.Numerics;

namespace BeardedManStudios.Forge.Networking.Generated
{
Expand Down
Loading

0 comments on commit 96ee91d

Please sign in to comment.