diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableFieldTypes.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableFieldTypes.cs index dbe5d8ec..2845b563 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableFieldTypes.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableFieldTypes.cs @@ -24,6 +24,10 @@ public enum ForgeAcceptableFieldTypes QUATERNION = 15, COLOR = 16, //OBJECT_ARRAY = 17, //Unsupported - //BYTE_ARRAY = 18 + //BYTE_ARRAY = 18, + DOTNET_VECTOR2 = 19, + DOTNET_VECTOR3 = 20, + DOTNET_VECTOR4 = 21, + DOTNET_QUATERNION = 22, } -} \ No newline at end of file +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableRPCTypes.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableRPCTypes.cs index 16bc1f04..a09ca22e 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableRPCTypes.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeAcceptableRPCTypes.cs @@ -24,6 +24,10 @@ public enum ForgeAcceptableRPCTypes QUATERNION = 15, COLOR = 16, //OBJECT_ARRAY = 17, - BYTE_ARRAY = 18 + BYTE_ARRAY = 18, + DOTNET_VECTOR2 = 19, + DOTNET_VECTOR3 = 20, + DOTNET_VECTOR4 = 21, + DOTNET_QUATERNION = 22, } -} \ No newline at end of file +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldRPCValue.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldRPCValue.cs index 73bf2ac4..521b697d 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldRPCValue.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldRPCValue.cs @@ -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; @@ -85,6 +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(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[])) @@ -133,6 +142,14 @@ public static Type GetTypeFromAcceptable(ForgeAcceptableRPCTypes type) return typeof(Vector3); case ForgeAcceptableRPCTypes.VECTOR4: return typeof(Vector4); + 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: @@ -180,6 +197,14 @@ public static ForgeAcceptableRPCTypes GetTypeFromAcceptable(string val) return ForgeAcceptableRPCTypes.VECTOR3; case "vector4": return ForgeAcceptableRPCTypes.VECTOR4; + 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[]": @@ -196,4 +221,4 @@ public override string ToString() return string.Format("[ Name: {0}, Value: {1}, Type: {2}, IsNetObj: {3}]", FieldRPCName, FieldRPCValue, FieldType, IsNetworkedObject); } } -} \ No newline at end of file +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs index a79a7e36..88855d83 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs @@ -3,6 +3,7 @@ using System; using System.Reflection; using UnityEngine; +using Numerics = System.Numerics; namespace BeardedManStudios.Forge.Networking.UnityEditor { @@ -78,6 +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(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[])) @@ -126,6 +135,14 @@ public static Type GetTypeFromAcceptable(ForgeAcceptableFieldTypes type) return typeof(Vector3); case ForgeAcceptableFieldTypes.VECTOR4: return typeof(Vector4); + 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 @@ -158,6 +175,18 @@ public static string GetInterpolateFromAcceptable(string baseTypeString, ForgeAc case ForgeAcceptableFieldTypes.QUATERNION: returnValue = "InterpolateQuaternion"; break; + case ForgeAcceptableFieldTypes.DOTNET_VECTOR2: + returnValue = "InterpolateDotnetVector2"; + break; + case ForgeAcceptableFieldTypes.DOTNET_VECTOR3: + returnValue = "InterpolateDotnetVector3"; + break; + case ForgeAcceptableFieldTypes.DOTNET_VECTOR4: + returnValue = "InterpolateDotnetVector4"; + break; + case ForgeAcceptableFieldTypes.DOTNET_QUATERNION: + returnValue = "InterpolateDotnetQuaternion"; + break; default: returnValue = "Interpolated<" + baseTypeString + ">"; break; @@ -177,6 +206,10 @@ public static bool IsInterpolatable(ForgeAcceptableFieldTypes type) case ForgeAcceptableFieldTypes.VECTOR3: case ForgeAcceptableFieldTypes.VECTOR4: case ForgeAcceptableFieldTypes.QUATERNION: + case ForgeAcceptableFieldTypes.DOTNET_VECTOR2: + case ForgeAcceptableFieldTypes.DOTNET_VECTOR3: + case ForgeAcceptableFieldTypes.DOTNET_VECTOR4: + case ForgeAcceptableFieldTypes.DOTNET_QUATERNION: returnValue = true; break; } @@ -220,6 +253,14 @@ public static ForgeAcceptableFieldTypes GetTypeFromAcceptable(string val) return ForgeAcceptableFieldTypes.VECTOR3; case "vector4": return ForgeAcceptableFieldTypes.VECTOR4; + 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[]": @@ -237,4 +278,4 @@ public override string ToString() return string.Format("[ Name: {0}, Value: {1}, Type: {2}, IsNetObj: {3}]", FieldName, FieldValue, FieldType, IsNetworkedObject); } } -} \ No newline at end of file +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRPCValue.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRPCValue.cs index 5da35c59..dc3cde23 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRPCValue.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRPCValue.cs @@ -11,6 +11,7 @@ using System.Text; using UnityEditor; using UnityEngine; +using Numerics = System.Numerics; namespace BeardedManStudios.Forge.Networking.UnityEditor { @@ -81,6 +82,14 @@ public static ForgeAcceptableRPCTypes GetATypeFromPInfo(ParameterInfo pInfo) type = ForgeAcceptableRPCTypes.VECTOR3; else if (fieldType == typeof(Vector4)) type = ForgeAcceptableRPCTypes.VECTOR4; + 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[])) @@ -93,4 +102,4 @@ public static ForgeAcceptableRPCTypes GetATypeFromPInfo(ParameterInfo pInfo) return type; } } -} \ No newline at end of file +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRewindValue.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRewindValue.cs index ef893687..b0739e1d 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRewindValue.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeClassRewindValue.cs @@ -11,6 +11,7 @@ using System.Text; using UnityEditor; using UnityEngine; +using Numerics = System.Numerics; namespace BeardedManStudios.Forge.Networking.UnityEditor { @@ -67,6 +68,14 @@ public static ForgeAcceptableRPCTypes GetATypeFromPInfo(ParameterInfo pInfo) type = ForgeAcceptableRPCTypes.VECTOR3; else if (fieldType == typeof(Vector4)) type = ForgeAcceptableRPCTypes.VECTOR4; + 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[])) @@ -79,4 +88,4 @@ public static ForgeAcceptableRPCTypes GetATypeFromPInfo(ParameterInfo pInfo) return type; } } -} \ No newline at end of file +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeNetworkingEditor.cs b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeNetworkingEditor.cs index 90c5fe1f..2cfea816 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeNetworkingEditor.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/ForgeNetworkingEditor.cs @@ -10,6 +10,7 @@ using BeardedManStudios.Templating; using UnityEditor; using UnityEngine; +using Numerics = System.Numerics; namespace BeardedManStudios.Forge.Networking.UnityEditor { @@ -86,7 +87,7 @@ public class ForgeNetworkingEditor : EditorWindow /// private Dictionary _referenceVariables = new Dictionary(); /// - /// This is the bitwise + /// This is the bitwise /// private List _referenceBitWise = new List(); @@ -183,26 +184,30 @@ public void Initialize() _referenceBitWise.Add("0x80"); _referenceVariables = new Dictionary(); - _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(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(); @@ -710,7 +715,14 @@ public void MakeForgeFactory() /// The generated string to save to a file public string SourceCodeNetworkObject(ForgeClassObject cObj, ForgeEditorButton btn, int identity) { - TextAsset asset = Resources.Load(EDITOR_RESOURCES_DIR + "/NetworkObjectTemplate"); + string networkObjectPath = string.Empty; + + if (btn.BaseType == ForgeBaseClassType.NetworkBehavior) + networkObjectPath = EDITOR_RESOURCES_DIR + "/StandAloneNetworkObjectTemplate"; + else + networkObjectPath = EDITOR_RESOURCES_DIR + "/NetworkObjectTemplate"; + + TextAsset asset = Resources.Load(networkObjectPath); TemplateSystem template = new TemplateSystem(asset.text); template.AddVariable("className", btn.StrippedSearchName + "NetworkObject"); @@ -726,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 @@ -806,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 + "\""); } } @@ -892,6 +904,32 @@ public string SourceCodeFactory() return template.Parse(); } + /// + /// Generates the code factory for all our custom network objects for standalone use + /// + /// The string for the save file + public string SourceCodeStandAloneFactory() + { + TextAsset asset = Resources.Load(EDITOR_RESOURCES_DIR + "/StandAloneNetworkObjectFactoryTemplate"); + TemplateSystem template = new TemplateSystem(asset.text); + + List networkObjects = new List(); + for (int i = 0; i < _editorButtons.Count; ++i) + { + if (!_editorButtons[i].IsNetworkObject) + continue; + + string name = _editorButtons[i].StrippedSearchName + "NetworkObject"; + if (networkObjects.Contains(name)) + continue; + + networkObjects.Add(name); + } + + template.AddVariable("networkObjects", networkObjects.ToArray()); + return template.Parse(); + } + /// /// Generates the network manager that will allow the instantiation of these new network objects /// @@ -1045,6 +1083,15 @@ public void Compile() sw.Write(networkManagerData); } + if(ActiveButton.BaseType == ForgeBaseClassType.NetworkBehavior) + { + string standAloneFactoryData = SourceCodeStandAloneFactory(); + using (StreamWriter sw = File.CreateText(Path.Combine(_storingPath, "StandAloneNetworkObjectFactory.cs"))) + { + sw.Write(standAloneFactoryData); + } + } + //IFormatter previousSavedState = new BinaryFormatter(); //using (Stream s = new FileStream(Path.Combine(Application.persistentDataPath, FN_WIZARD_DATA), FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) //{ diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/NetworkObjectTemplate.txt b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/NetworkObjectTemplate.txt index 13a8a14e..f5af05a5 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/NetworkObjectTemplate.txt +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/NetworkObjectTemplate.txt @@ -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 { @@ -57,7 +58,7 @@ namespace BeardedManStudios.Forge.Networking.Generated base.OwnershipChanged(); SnapInterpolations(); } - + public void SnapInterpolations() { >:FOREVERY variables:< @@ -134,7 +135,7 @@ namespace BeardedManStudios.Forge.Networking.Generated return; >:FOREVERY variables:< - if (>:[1]::[1]::[1]::[1]::[1]::[1]::[1]:< = (>:[0]:<)>:[1]::[1]:<(>:[1]::interpolateValues:<]")] + public partial class >:className:< : NetworkObject + { + public const int IDENTITY = >:identity:<; + + private byte[] _dirtyFields = new byte[>:bitwiseSize:<]; + + #pragma warning disable 0067 + public event FieldChangedEvent fieldAltered; + #pragma warning restore 0067 + >:FOREVERY variables:< + [ForgeGeneratedField] + private >:[0]:< _>:[1]:<; + public event FieldEvent<>:[0]:<> >:[1]::[3]:< >:[1]::[3]:<() { LerpT = >:[4]:<, Enabled = >:[2]:< }; + public >:[0]:< >:[1]:< + { + get { return _>:[1]:<; } + set + { + // Don't do anything if the value is the same + if (_>:[1]:< == value) + return; + + // Mark the field as dirty for the network to transmit + _dirtyFields[>:[6]:<] |= >:[5]:<; + _>:[1]:< = value; + hasDirtyFields = true; + } + } + + public void Set>:[1]::[6]:<] |= >:[5]:<; + hasDirtyFields = true; + } + + private void RunChange_>:[1]:<(ulong timestep) + { + if (>:[1]::[1]::[1]:<, timestep); + if (fieldAltered != null) fieldAltered(">:[1]:<", _>:[1]:<, timestep); + } + >:ENDFOREVERY:< + >:FOREVERY rewinds:< + public Rewind<>:[0]:<> rewind>:[1]:< = new Rewind<>:[0]:<>() { rewindTime = >:[2]:< }; + >:ENDFOREVERY:< + + protected override void OwnershipChanged() + { + base.OwnershipChanged(); + SnapInterpolations(); + } + + public void SnapInterpolations() + { + >:FOREVERY variables:< + >:[1]::[1]::ENDFOREVERY:< + } + + public override int UniqueIdentity { get { return IDENTITY; } } + + protected override BMSByte WritePayload(BMSByte data) + { + >:FOREVERY variables:< + ObjectMapper.Instance.MapBytes(data, _>:[1]:<); + >:ENDFOREVERY:< + + return data; + } + + protected override void ReadPayload(BMSByte payload, ulong timestep) + { + >:FOREVERY variables:< + _>:[1]:< = ObjectMapper.Instance.Map<>:[0]:<>(payload); + >:[1]::[1]:<; + >:[1]::[1]:<; + RunChange_>:[1]:<(timestep); + >:ENDFOREVERY:< + } + + protected override BMSByte SerializeDirtyFields() + { + dirtyFieldsData.Clear(); + dirtyFieldsData.Append(_dirtyFields); + + >:FOREVERY variables:< + if ((>:[5]:< & _dirtyFields[>:[6]:<]) != 0) + ObjectMapper.Instance.MapBytes(dirtyFieldsData, _>:[1]:<); + >:ENDFOREVERY:< + + // Reset all the dirty fields + for (int i = 0; i < _dirtyFields.Length; i++) + _dirtyFields[i] = 0; + + return dirtyFieldsData; + } + + protected override void ReadDirtyFields(BMSByte data, ulong timestep) + { + if (readDirtyFlags == null) + Initialize(); + + Buffer.BlockCopy(data.byteArr, data.StartIndex(), readDirtyFlags, 0, readDirtyFlags.Length); + data.MoveStartIndex(readDirtyFlags.Length); + + >:FOREVERY variables:< + if ((>:[5]:< & readDirtyFlags[>:[6]:<]) != 0) + { + if (>:[1]::[1]::[0]:<>(data); + >:[1]::[1]:< = ObjectMapper.Instance.Map<>:[0]:<>(data); + RunChange_>:[1]:<(timestep); + } + } + >:ENDFOREVERY:< + } + + public override void InterpolateUpdate() + { + if (IsOwner) + return; + + >:FOREVERY variables:< + if (>:[1]::[1]::[1]::[1]:< = (>:[0]:<)>:[1]::[1]:<(>:[1]::ENDFOREVERY:< + } + + private void Initialize() + { + if (readDirtyFlags == null) + readDirtyFlags = new byte[>:bitwiseSize:<]; + + >:FOREVERY rewinds:< + rewind>:[1]:<.Time = NetWorker.Time; + >:ENDFOREVERY:< + } + + public >:className:<() : base() { Initialize(); } + public >:className:<(NetWorker networker, INetworkBehavior networkBehavior = null, int createCode = 0, byte[] metadata = null) : base(networker, networkBehavior, createCode, metadata) { Initialize(); } + public >:className:<(NetWorker networker, uint serverId, FrameStream frame) : base(networker, serverId, frame) { Initialize(); } + + // DO NOT TOUCH, THIS GETS GENERATED PLEASE EXTEND THIS CLASS IF YOU WISH TO HAVE CUSTOM CODE ADDITIONS + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandAloneNetworkObjectTemplate.txt.meta b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandAloneNetworkObjectTemplate.txt.meta new file mode 100644 index 00000000..47447f10 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandAloneNetworkObjectTemplate.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: bae1a8a18cd5303429ac2ce07ca31984 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandaloneNetworkObjectFactoryTemplate.txt b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandaloneNetworkObjectFactoryTemplate.txt new file mode 100644 index 00000000..d1ecb880 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandaloneNetworkObjectFactoryTemplate.txt @@ -0,0 +1,42 @@ +using BeardedManStudios.Forge.Networking.Frame; +using System; + +namespace BeardedManStudios.Forge.Networking.Generated +{ + public partial class StandAloneNetworkObjectFactory : INetworkObjectFactory + { + public virtual void NetworkCreateObject(NetWorker networker, int identity, uint id, FrameStream frame, Action callback) + { + if (frame.Sender != null && frame.Sender != networker.Me) + { + if (!ValidateCreateRequest(networker, identity, id, frame)) + return; + } + + bool availableCallback = false; + + NetworkObject obj = null; + + switch (identity) + { + >:FOREACH networkObjects:< + case >:[i]:<.IDENTITY: + availableCallback = true; + obj = new >:[i]:<(networker, id, frame); + break; + >:ENDFOREACH:< + } + + if (!availableCallback && callback != null) + callback(obj); + } + + protected virtual bool ValidateCreateRequest(NetWorker networker, int identity, uint id, FrameStream frame) + { + return true; + } + + // DO NOT TOUCH, THIS GETS GENERATED PLEASE EXTEND THIS CLASS IF YOU WISH TO HAVE CUSTOM CODE ADDITIONS + // WARNING: THIS IS ONLY FOR USE WITH THE STANDALONE SERVER! + } +} \ No newline at end of file diff --git a/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandaloneNetworkObjectFactoryTemplate.txt.meta b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandaloneNetworkObjectFactoryTemplate.txt.meta new file mode 100644 index 00000000..349c941a --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Editor/Resources/BMS_Forge_Editor/StandaloneNetworkObjectFactoryTemplate.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 698ca1e2b870ec14b846161dd33957a7 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/ExampleProximityPlayerNetworkObject.cs b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/ExampleProximityPlayerNetworkObject.cs index fe1f391b..21aca462 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/ExampleProximityPlayerNetworkObject.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/ExampleProximityPlayerNetworkObject.cs @@ -52,7 +52,7 @@ protected override void OwnershipChanged() base.OwnershipChanged(); SnapInterpolations(); } - + public void SnapInterpolations() { positionInterpolation.current = positionInterpolation.target; @@ -118,7 +118,7 @@ public override void InterpolateUpdate() if (IsOwner) return; - if (positionInterpolation.Enabled && !positionInterpolation.current.UnityNear(positionInterpolation.target, 0.0015f)) + if (positionInterpolation.Enabled && !positionInterpolation.current.Near(positionInterpolation.target, 0.0015f)) { _position = (Vector3)positionInterpolation.Interpolate(); //RunChange_position(positionInterpolation.Timestep); diff --git a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/NetworkCameraNetworkObject.cs b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/NetworkCameraNetworkObject.cs index ac9ac439..50c26bdd 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/NetworkCameraNetworkObject.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/NetworkCameraNetworkObject.cs @@ -52,7 +52,7 @@ protected override void OwnershipChanged() base.OwnershipChanged(); SnapInterpolations(); } - + public void SnapInterpolations() { positionInterpolation.current = positionInterpolation.target; @@ -118,7 +118,7 @@ public override void InterpolateUpdate() if (IsOwner) return; - if (positionInterpolation.Enabled && !positionInterpolation.current.UnityNear(positionInterpolation.target, 0.0015f)) + if (positionInterpolation.Enabled && !positionInterpolation.current.Near(positionInterpolation.target, 0.0015f)) { _position = (Vector3)positionInterpolation.Interpolate(); //RunChange_position(positionInterpolation.Timestep); diff --git a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/SomeMoveableNetworkObject.cs b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/SomeMoveableNetworkObject.cs index 313854e1..2131a511 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/SomeMoveableNetworkObject.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/SomeMoveableNetworkObject.cs @@ -52,7 +52,7 @@ protected override void OwnershipChanged() base.OwnershipChanged(); SnapInterpolations(); } - + public void SnapInterpolations() { positionInterpolation.current = positionInterpolation.target; @@ -118,7 +118,7 @@ public override void InterpolateUpdate() if (IsOwner) return; - if (positionInterpolation.Enabled && !positionInterpolation.current.UnityNear(positionInterpolation.target, 0.0015f)) + if (positionInterpolation.Enabled && !positionInterpolation.current.Near(positionInterpolation.target, 0.0015f)) { _position = (Vector3)positionInterpolation.Interpolate(); //RunChange_position(positionInterpolation.Timestep); diff --git a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/TestNetworkObject.cs b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/TestNetworkObject.cs index 8e031875..bf45ac5a 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/TestNetworkObject.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Generated/UserGenerated/TestNetworkObject.cs @@ -393,7 +393,7 @@ protected override void OwnershipChanged() base.OwnershipChanged(); SnapInterpolations(); } - + public void SnapInterpolations() { fieldByteInterpolation.current = fieldByteInterpolation.target; @@ -690,62 +690,62 @@ public override void InterpolateUpdate() if (IsOwner) return; - if (fieldByteInterpolation.Enabled && !fieldByteInterpolation.current.UnityNear(fieldByteInterpolation.target, 0.0015f)) + if (fieldByteInterpolation.Enabled && !fieldByteInterpolation.current.Near(fieldByteInterpolation.target, 0.0015f)) { _fieldByte = (byte)fieldByteInterpolation.Interpolate(); //RunChange_fieldByte(fieldByteInterpolation.Timestep); } - if (fieldCharInterpolation.Enabled && !fieldCharInterpolation.current.UnityNear(fieldCharInterpolation.target, 0.0015f)) + if (fieldCharInterpolation.Enabled && !fieldCharInterpolation.current.Near(fieldCharInterpolation.target, 0.0015f)) { _fieldChar = (char)fieldCharInterpolation.Interpolate(); //RunChange_fieldChar(fieldCharInterpolation.Timestep); } - if (fieldShortInterpolation.Enabled && !fieldShortInterpolation.current.UnityNear(fieldShortInterpolation.target, 0.0015f)) + if (fieldShortInterpolation.Enabled && !fieldShortInterpolation.current.Near(fieldShortInterpolation.target, 0.0015f)) { _fieldShort = (short)fieldShortInterpolation.Interpolate(); //RunChange_fieldShort(fieldShortInterpolation.Timestep); } - if (fieldUShortInterpolation.Enabled && !fieldUShortInterpolation.current.UnityNear(fieldUShortInterpolation.target, 0.0015f)) + if (fieldUShortInterpolation.Enabled && !fieldUShortInterpolation.current.Near(fieldUShortInterpolation.target, 0.0015f)) { _fieldUShort = (ushort)fieldUShortInterpolation.Interpolate(); //RunChange_fieldUShort(fieldUShortInterpolation.Timestep); } - if (fieldBoolInterpolation.Enabled && !fieldBoolInterpolation.current.UnityNear(fieldBoolInterpolation.target, 0.0015f)) + if (fieldBoolInterpolation.Enabled && !fieldBoolInterpolation.current.Near(fieldBoolInterpolation.target, 0.0015f)) { _fieldBool = (bool)fieldBoolInterpolation.Interpolate(); //RunChange_fieldBool(fieldBoolInterpolation.Timestep); } - if (fieldIntInterpolation.Enabled && !fieldIntInterpolation.current.UnityNear(fieldIntInterpolation.target, 0.0015f)) + if (fieldIntInterpolation.Enabled && !fieldIntInterpolation.current.Near(fieldIntInterpolation.target, 0.0015f)) { _fieldInt = (int)fieldIntInterpolation.Interpolate(); //RunChange_fieldInt(fieldIntInterpolation.Timestep); } - if (fieldUIntInterpolation.Enabled && !fieldUIntInterpolation.current.UnityNear(fieldUIntInterpolation.target, 0.0015f)) + if (fieldUIntInterpolation.Enabled && !fieldUIntInterpolation.current.Near(fieldUIntInterpolation.target, 0.0015f)) { _fieldUInt = (uint)fieldUIntInterpolation.Interpolate(); //RunChange_fieldUInt(fieldUIntInterpolation.Timestep); } - if (fieldFloatInterpolation.Enabled && !fieldFloatInterpolation.current.UnityNear(fieldFloatInterpolation.target, 0.0015f)) + if (fieldFloatInterpolation.Enabled && !fieldFloatInterpolation.current.Near(fieldFloatInterpolation.target, 0.0015f)) { _fieldFloat = (float)fieldFloatInterpolation.Interpolate(); //RunChange_fieldFloat(fieldFloatInterpolation.Timestep); } - if (fieldFloatInterpolateInterpolation.Enabled && !fieldFloatInterpolateInterpolation.current.UnityNear(fieldFloatInterpolateInterpolation.target, 0.0015f)) + if (fieldFloatInterpolateInterpolation.Enabled && !fieldFloatInterpolateInterpolation.current.Near(fieldFloatInterpolateInterpolation.target, 0.0015f)) { _fieldFloatInterpolate = (float)fieldFloatInterpolateInterpolation.Interpolate(); //RunChange_fieldFloatInterpolate(fieldFloatInterpolateInterpolation.Timestep); } - if (fieldLongInterpolation.Enabled && !fieldLongInterpolation.current.UnityNear(fieldLongInterpolation.target, 0.0015f)) + if (fieldLongInterpolation.Enabled && !fieldLongInterpolation.current.Near(fieldLongInterpolation.target, 0.0015f)) { _fieldLong = (long)fieldLongInterpolation.Interpolate(); //RunChange_fieldLong(fieldLongInterpolation.Timestep); } - if (fieldULongInterpolation.Enabled && !fieldULongInterpolation.current.UnityNear(fieldULongInterpolation.target, 0.0015f)) + if (fieldULongInterpolation.Enabled && !fieldULongInterpolation.current.Near(fieldULongInterpolation.target, 0.0015f)) { _fieldULong = (ulong)fieldULongInterpolation.Interpolate(); //RunChange_fieldULong(fieldULongInterpolation.Timestep); } - if (fieldDoubleInterpolation.Enabled && !fieldDoubleInterpolation.current.UnityNear(fieldDoubleInterpolation.target, 0.0015f)) + if (fieldDoubleInterpolation.Enabled && !fieldDoubleInterpolation.current.Near(fieldDoubleInterpolation.target, 0.0015f)) { _fieldDouble = (double)fieldDoubleInterpolation.Interpolate(); //RunChange_fieldDouble(fieldDoubleInterpolation.Timestep); diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/BMSUnityExtensions.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/BMSUnityExtensions.cs index 9b79ec5b..92a0f998 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Scripts/BMSUnityExtensions.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/BMSUnityExtensions.cs @@ -4,29 +4,24 @@ namespace BeardedManStudios.Forge.Networking.Unity { public static class BMSUnityExtensions { - public static bool UnityNear(this Vector2 target, Vector2 other, float threshold = 0.0012f) + public static bool Near(this Vector2 target, Vector2 other, float distance = 0.0012f) { - return Vector2.Distance(target, other) <= threshold; + return Vector2.Distance(target, other) <= distance; } - public static bool UnityNear(this Vector3 target, Vector3 other, float threshold = 0.0012f) + public static bool Near(this Vector3 target, Vector3 other, float distance = 0.0012f) { - return Vector3.Distance(target, other) <= threshold; + return Vector3.Distance(target, other) <= distance; } - public static bool UnityNear(this Vector4 target, Vector4 other, float threshold = 0.0012f) + public static bool Near(this Vector4 target, Vector4 other, float distance = 0.0012f) { - return Vector4.Distance(target, other) <= threshold; + return Vector4.Distance(target, other) <= distance; } - public static bool UnityNear(this Quaternion target, Quaternion other, float threshold = 0.0012f) + public static bool Near(this Quaternion target, Quaternion other, float distance = 0.0012f) { - return Quaternion.Angle(target, other) <= threshold; - } - - public static bool UnityNear(this T target, T other, float threshold = 0.0012f) - { - return target.Near(other, threshold); + return Quaternion.Angle(target, other) <= distance; } } } diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs index efc7b741..b7e4a55b 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs @@ -1,4 +1,7 @@ -namespace BeardedManStudios +using System; +using Numerics = System.Numerics; + +namespace BeardedManStudios { public static class BMSExtensions { @@ -57,6 +60,35 @@ public static bool Near(this float target, float other, float distance) return target.Between(other - distance, other + distance); } + public static bool Near(this Numerics.Vector2 target, Numerics.Vector2 other, float distance) + { + return Numerics.Vector2.Distance(target, other) <= distance; + } + + public static bool Near(this Numerics.Vector3 target, Numerics.Vector3 other, float distance) + { + return Numerics.Vector3.Distance(target, other) <= distance; + } + + public static bool Near(this Numerics.Vector4 target, Numerics.Vector4 other, float distance) + { + return Numerics.Vector4.Distance(target, other) <= distance; + } + + public static bool Near(this Numerics.Quaternion target, Numerics.Quaternion other, float distance) + { + const double K_EPSILON = 0.000001; + const double RADIANS_TO_DEGREES = 1.0 / (Math.PI / 180.0); + + float dotProduct = Numerics.Quaternion.Dot(target, other); + + double angle = dotProduct <= 1.0 - K_EPSILON ? + Math.Acos(Math.Min(Math.Abs(dotProduct), 1.0F)) * 2.0 * RADIANS_TO_DEGREES : + 0.0; + + return angle <= distance; + } + public static bool Near(this T target, T other, float distance) { return target.Equals(other); diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/ObjectMapper.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/ObjectMapper.cs index 6cbdee25..7155eccf 100644 --- a/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/ObjectMapper.cs +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/ObjectMapper.cs @@ -5,6 +5,7 @@ using System; using System.IO; using System.Text; +using Numerics = System.Numerics; namespace BeardedManStudios.Forge.Networking { @@ -43,6 +44,14 @@ public virtual object Map(Type type, BMSByte stream) obj = stream.GetBasicType(); else if (type == typeof(Vector)) obj = stream.GetBasicType(); + else if (type == typeof(Numerics.Vector2)) + obj = MapDotnetVector2(stream); + else if (type == typeof(Numerics.Vector3)) + obj = MapDotnetVector3(stream); + else if (type == typeof(Numerics.Vector4)) + obj = MapDotnetVector4(stream); + else if (type == typeof(Numerics.Quaternion)) + obj = MapDotnetQuaternion(stream); else if (type.IsArray) obj = MapArray(type, stream); else if (type == typeof(BMSByte)) @@ -70,6 +79,14 @@ public virtual T Map(BMSByte stream) obj = stream.GetBasicType(); else if (genericType == typeof(Vector)) obj = stream.GetBasicType(); + else if (genericType == typeof(Numerics.Vector2)) + obj = MapDotnetVector2(stream); + else if (genericType == typeof(Numerics.Vector3)) + obj = MapDotnetVector3(stream); + else if (genericType == typeof(Numerics.Vector4)) + obj = MapDotnetVector4(stream); + else if (genericType == typeof(Numerics.Quaternion)) + obj = MapDotnetQuaternion(stream); else if (genericType.IsArray) obj = MapArray(genericType, stream); else if (genericType == typeof(BMSByte)) @@ -244,6 +261,31 @@ protected virtual void GetBytes(object o, Type type, ref BMSByte bytes) bytes.Append(BitConverter.GetBytes(vec.y)); bytes.Append(BitConverter.GetBytes(vec.z)); } + else if (type == typeof(Numerics.Vector2)) + { + bytes.Append(BitConverter.GetBytes(((Numerics.Vector2)o).X)); + bytes.Append(BitConverter.GetBytes(((Numerics.Vector2)o).Y)); + } + else if (type == typeof(Numerics.Vector3)) + { + bytes.Append(BitConverter.GetBytes(((Numerics.Vector3)o).X)); + bytes.Append(BitConverter.GetBytes(((Numerics.Vector3)o).Y)); + bytes.Append(BitConverter.GetBytes(((Numerics.Vector3)o).Z)); + } + else if (type == typeof(Numerics.Vector4)) + { + bytes.Append(BitConverter.GetBytes(((Numerics.Vector4)o).X)); + bytes.Append(BitConverter.GetBytes(((Numerics.Vector4)o).Y)); + bytes.Append(BitConverter.GetBytes(((Numerics.Vector4)o).Z)); + bytes.Append(BitConverter.GetBytes(((Numerics.Vector4)o).W)); + } + else if (type == typeof(Numerics.Quaternion)) + { + bytes.Append(BitConverter.GetBytes(((Numerics.Quaternion)o).X)); + bytes.Append(BitConverter.GetBytes(((Numerics.Quaternion)o).Y)); + bytes.Append(BitConverter.GetBytes(((Numerics.Quaternion)o).Z)); + bytes.Append(BitConverter.GetBytes(((Numerics.Quaternion)o).W)); + } else if (type == null) //TODO: Check if this causes other issues bytes.Append(new byte[1] { 0 }); else if (type == typeof(sbyte)) @@ -357,6 +399,39 @@ protected virtual byte[] GetBytesArray(object o, Type type) Buffer.BlockCopy(BitConverter.GetBytes(vec.z), 0, bytes, sizeof(float) * 2, sizeof(float)); return bytes; } + else if (type == typeof(Numerics.Vector2)) + { + byte[] data = new byte[sizeof(float) * 2]; + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector2)o).X), 0, data, 0, sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector2)o).Y), 0, data, sizeof(float), sizeof(float)); + return data; + } + else if (type == typeof(Numerics.Vector3)) + { + byte[] data = new byte[sizeof(float) * 3]; + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector3)o).X), 0, data, 0, sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector3)o).Y), 0, data, sizeof(float), sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector3)o).Z), 0, data, sizeof(float) * 2, sizeof(float)); + return data; + } + else if (type == typeof(Numerics.Vector4)) + { + byte[] data = new byte[sizeof(float) * 4]; + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector4)o).X), 0, data, 0, sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector4)o).Y), 0, data, sizeof(float), sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector4)o).Z), 0, data, sizeof(float) * 2, sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Vector4)o).W), 0, data, sizeof(float) * 3, sizeof(float)); + return data; + } + else if (type == typeof(Numerics.Quaternion)) + { + byte[] data = new byte[sizeof(float) * 4]; + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Quaternion)o).X), 0, data, 0, sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Quaternion)o).Y), 0, data, sizeof(float), sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Quaternion)o).Z), 0, data, sizeof(float) * 2, sizeof(float)); + Buffer.BlockCopy(BitConverter.GetBytes(((Numerics.Quaternion)o).W), 0, data, sizeof(float) * 3, sizeof(float)); + return data; + } else if (type == null) //TODO: Check if this causes other issues return new byte[1] { 0 }; else if (type == typeof(sbyte)) @@ -466,5 +541,64 @@ public static BMSByte BMSByte(params object[] argsToMap) Instance.MapBytes(data, argsToMap); return data; } + + /// + /// Get a .NET Vector2 out of a FrameStream + /// + /// FrameStream to be used + /// A .NET Vector2 out of the FrameStream + public object MapDotnetVector2(BMSByte stream) + { + return new Numerics.Vector2( + stream.GetBasicType(), + stream.GetBasicType() + ); + } + + /// + /// Get a .NET Vector3 out of a FrameStream + /// + /// FrameStream to be used + /// A .NET Vector3 out of the FrameStream + public object MapDotnetVector3(BMSByte stream) + { + return new Numerics.Vector3( + stream.GetBasicType(), + stream.GetBasicType(), + stream.GetBasicType() + ); + } + + /// + /// Get a .NET Vector4 out of a FrameStream + /// + /// Type of object to be mapped + /// FrameStream to be used + /// A type of .NET Vector4 (Vector4/Color) out of the FrameStream + public object MapDotnetVector4(BMSByte stream) + { + return new Numerics.Vector4( + stream.GetBasicType(), + stream.GetBasicType(), + stream.GetBasicType(), + stream.GetBasicType() + ); + } + + /// + /// Get a .NET Quaternion out of a FrameStream + /// + /// Type of object to be mapped + /// FrameStream to be used + /// A type of .NET Quaternion out of the FrameStream + public object MapDotnetQuaternion(BMSByte stream) + { + return new Numerics.Quaternion( + stream.GetBasicType(), + stream.GetBasicType(), + stream.GetBasicType(), + stream.GetBasicType() + ); + } } } diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone.meta new file mode 100644 index 00000000..ce59cfff --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cf81dd69d72c15642a8849fdc9933aac +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects.meta new file mode 100644 index 00000000..a0565f08 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1657e1b2d258ecb428269241077d74de +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetQuaternion.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetQuaternion.cs new file mode 100644 index 00000000..2246fb26 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetQuaternion.cs @@ -0,0 +1,22 @@ +using BeardedManStudios.Forge.Networking; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public struct InterpolateDotnetQuaternion : IInterpolator + { + public Numerics.Quaternion current; + public Numerics.Quaternion target; + public float LerpT { get; set; } + public bool Enabled { get; set; } + public ulong Timestep { get; set; } + + public Numerics.Quaternion Interpolate() + { + if (!Enabled) return target; + + current = Numerics.Quaternion.Slerp(current, target, LerpT); + return current; + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetQuaternion.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetQuaternion.cs.meta new file mode 100644 index 00000000..06d73b98 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetQuaternion.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c12b064ca5720ab48b3914082dce394e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector2.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector2.cs new file mode 100644 index 00000000..c8bc3375 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector2.cs @@ -0,0 +1,22 @@ +using BeardedManStudios.Forge.Networking; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public struct InterpolateDotnetVector2 : IInterpolator + { + public Numerics.Vector2 current; + public Numerics.Vector2 target; + public float LerpT { get; set; } + public bool Enabled { get; set; } + public ulong Timestep { get; set; } + + public Numerics.Vector2 Interpolate() + { + if (!Enabled) return target; + + current = Numerics.Vector2.Lerp(current, target, LerpT); + return current; + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector2.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector2.cs.meta new file mode 100644 index 00000000..f17feab7 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector2.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5d8f29f9be70c3a4e9c81a969eca280c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector3.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector3.cs new file mode 100644 index 00000000..a6a99fa2 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector3.cs @@ -0,0 +1,22 @@ +using BeardedManStudios.Forge.Networking; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public struct InterpolateDotnetVector3 : IInterpolator + { + public Numerics.Vector3 current; + public Numerics.Vector3 target; + public float LerpT { get; set; } + public bool Enabled { get; set; } + public ulong Timestep { get; set; } + + public Numerics.Vector3 Interpolate() + { + if (!Enabled) return target; + + current = Numerics.Vector3.Lerp(current, target, LerpT); + return current; + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector3.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector3.cs.meta new file mode 100644 index 00000000..0ad8ee44 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector3.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 57d00189c59006d48a08c6c9ed915b29 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector4.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector4.cs new file mode 100644 index 00000000..a1788aa3 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector4.cs @@ -0,0 +1,22 @@ +using BeardedManStudios.Forge.Networking; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public struct InterpolateDotnetVector4 : IInterpolator + { + public Numerics.Vector4 current; + public Numerics.Vector4 target; + public float LerpT { get; set; } + public bool Enabled { get; set; } + public ulong Timestep { get; set; } + + public Numerics.Vector4 Interpolate() + { + if (!Enabled) return target; + + current = Numerics.Vector4.Lerp(current, target, LerpT); + return current; + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector4.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector4.cs.meta new file mode 100644 index 00000000..99ac4c64 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateDotnetVector4.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e8a14353be9c94949a995e640e7743f1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/StandAloneObjectMapper.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/StandAloneObjectMapper.cs.meta new file mode 100644 index 00000000..61a60e90 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/StandAloneObjectMapper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4284964d06d3f2f4a8946c37be2e437b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects.meta new file mode 100644 index 00000000..ea827788 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0a230994537385040a7c245ac5802c13 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetQuaternionUnity.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetQuaternionUnity.cs new file mode 100644 index 00000000..e915946f --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetQuaternionUnity.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public static class DotnetQuaternionUnity + { + public static Numerics.Quaternion ToDotnetQuaternion(this Quaternion v) + { + return new Numerics.Quaternion(v.x, v.y, v.z, v.w); + } + + public static Quaternion ToUnityQuaternion(this Numerics.Quaternion f) + { + return new Quaternion(f.X, f.Y, f.Z, f.W); + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetQuaternionUnity.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetQuaternionUnity.cs.meta new file mode 100644 index 00000000..21587595 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetQuaternionUnity.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e7a4878e99a8bc94bac8c0b958b44381 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector2Unity.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector2Unity.cs new file mode 100644 index 00000000..5aeaa9a2 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector2Unity.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public static class DotnetVector2Unity + { + public static Numerics.Vector2 ToDotnetVector(this Vector2 v) + { + return new Numerics.Vector2(v.x, v.y); + } + + public static Vector2 ToUnityVector(this Numerics.Vector2 f) + { + return new Vector2(f.X, f.Y); + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector2Unity.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector2Unity.cs.meta new file mode 100644 index 00000000..266659e3 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector2Unity.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 152a4431fd306374ba8d73d514a0dc39 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector3Unity.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector3Unity.cs new file mode 100644 index 00000000..1e224507 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector3Unity.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public static class DotnetVector3Unity + { + public static Numerics.Vector3 ToDotnetVector(this Vector3 v) + { + return new Numerics.Vector3(v.x, v.y, v.z); + } + + public static Vector3 ToUnityVector(this Numerics.Vector3 f) + { + return new Vector3(f.X, f.Y, f.Z); + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector3Unity.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector3Unity.cs.meta new file mode 100644 index 00000000..b2389e29 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector3Unity.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2bff69700abd24a4193e65d889857705 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector4Unity.cs b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector4Unity.cs new file mode 100644 index 00000000..d77c8e00 --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector4Unity.cs @@ -0,0 +1,28 @@ +using UnityEngine; +using Numerics = System.Numerics; + +namespace BeardedManStudios +{ + public static class DotnetVector4Unity + { + public static Numerics.Vector4 ToDotnetVector(this Vector4 v) + { + return new Numerics.Vector4(v.x, v.y, v.z, v.w); + } + + public static Vector4 ToUnityVector(this Numerics.Vector4 f) + { + return new Vector4(f.X, f.Y, f.Z, f.W); + } + + public static Numerics.Vector4 ToDotnetVector(this Color v) + { + return new Numerics.Vector4(v.r, v.g, v.b, v.a); + } + + public static Color ToUnityColor(this Numerics.Vector4 f) + { + return new Color(f.X, f.Y, f.Z, f.W); + } + } +} diff --git a/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector4Unity.cs.meta b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector4Unity.cs.meta new file mode 100644 index 00000000..adb3dccc --- /dev/null +++ b/ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/UnityObjects/DotnetVector4Unity.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 04e45afc0c4c12a48a4050d467d7aaae +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: