diff --git a/ControlCore.csproj b/ControlCore.csproj index 9f3c622..feb0b62 100644 --- a/ControlCore.csproj +++ b/ControlCore.csproj @@ -3,17 +3,12 @@ false false - net9.0 - enable - enable + net5.0 - - - - - + + diff --git a/LinearAlgebra/Direction.cs b/LinearAlgebra/Direction.cs index 9ab56fd..bd7477f 100644 --- a/LinearAlgebra/Direction.cs +++ b/LinearAlgebra/Direction.cs @@ -14,12 +14,12 @@ namespace Passer.LinearAlgebra { //Normalize(); } - public readonly static Direction forward = new(0, 0); - public readonly static Direction backward = new(-180, 0); - public readonly static Direction up = new(0, 90); - public readonly static Direction down = new(0, -90); - public readonly static Direction left = new(-90, 0); - public readonly static Direction right = new(90, 0); + public readonly static Direction forward = new Direction(0, 0); + public readonly static Direction backward = new Direction(-180, 0); + public readonly static Direction up = new Direction(0, 90); + public readonly static Direction down = new Direction(0, -90); + public readonly static Direction left = new Direction(-90, 0); + public readonly static Direction right = new Direction(90, 0); public void Normalize() { if (this.vertical > 90 || this.vertical < -90) { diff --git a/LinearAlgebra/Quat32.cs b/LinearAlgebra/Quat32.cs index b839b0e..6bebd04 100644 --- a/LinearAlgebra/Quat32.cs +++ b/LinearAlgebra/Quat32.cs @@ -42,7 +42,7 @@ namespace Passer.LinearAlgebra float yawOver2 = yaw * 0.5f; float sinYawOver2 = (float)Math.Sin((float)yawOver2); float cosYawOver2 = (float)Math.Cos((float)yawOver2); - Quat32 result = new() + Quat32 result = new Quat32() { w = cosYawOver2 * cosPitchOver2 * cosRollOver2 + sinYawOver2 * sinPitchOver2 * sinRollOver2, diff --git a/LinearAlgebra/Spherical.cs b/LinearAlgebra/Spherical.cs index 5670c6b..be0c38a 100644 --- a/LinearAlgebra/Spherical.cs +++ b/LinearAlgebra/Spherical.cs @@ -8,8 +8,8 @@ namespace Passer.LinearAlgebra { public float distance; public Direction direction; - public static Spherical zero = new(0, 0, 0); - public static Spherical forward = new(1, 0, 0); + public static Spherical zero = new Spherical(0, 0, 0); + public static Spherical forward = new Spherical(1, 0, 0); public Spherical(float distance, float horizontal, float vertical) { this.distance = distance; diff --git a/LinearAlgebra/SwingTwist.cs b/LinearAlgebra/SwingTwist.cs index 61774d1..7f4bbfd 100644 --- a/LinearAlgebra/SwingTwist.cs +++ b/LinearAlgebra/SwingTwist.cs @@ -6,7 +6,7 @@ namespace Passer.LinearAlgebra public Direction swing; public float twist; - public static readonly SwingTwist zero = new(0, 0, 0); + public static readonly SwingTwist zero = new SwingTwist(0, 0, 0); public SwingTwist(Direction swing, float twist) { @@ -24,7 +24,7 @@ namespace Passer.LinearAlgebra // UnityEngine.Quaternion q = new(q32.x, q32.y, q32.z, q32.w); // SwingTwist r = new(q.eulerAngles.y, q.eulerAngles.x, q.eulerAngles.z); q32.ToAngles(out float right, out float up, out float forward); - SwingTwist r = new(up, right, forward); + SwingTwist r = new SwingTwist(up, right, forward); return r; } } diff --git a/LinearAlgebra/Vector3.cs b/LinearAlgebra/Vector3.cs index 4b629ac..9349b99 100644 --- a/LinearAlgebra/Vector3.cs +++ b/LinearAlgebra/Vector3.cs @@ -1,3 +1,5 @@ +#if !UNITY_5_3_OR_NEWER +using System; #if !UNITY_5_3_OR_NEWER namespace Passer.LinearAlgebra { @@ -17,9 +19,11 @@ namespace Passer.LinearAlgebra { // } } - public class Vector3Int(int x, int y, int z) : Vector3Of(x, y, z) { + public class Vector3Int : Vector3Of { + public Vector3Int(int x, int y, int z) : base(x, y, z) { } } - public class Vector3Float(float x, float y, float z) : Vector3Of(x, y, z) { + public class Vector3Float : Vector3Of { + public Vector3Float(float x, float y, float z) : base(x, y, z) { } public float magnitude { get => (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z); diff --git a/Messages/CustomMsg.cs b/Messages/CustomMsg.cs index d089b73..cf06fff 100644 --- a/Messages/CustomMsg.cs +++ b/Messages/CustomMsg.cs @@ -1,4 +1,3 @@ -#nullable enable namespace Passer.Control.Core { public class CustomMsg : IMessage { diff --git a/Messages/DestroyMsg.cs b/Messages/DestroyMsg.cs index 1298113..9a07eda 100644 --- a/Messages/DestroyMsg.cs +++ b/Messages/DestroyMsg.cs @@ -21,7 +21,7 @@ namespace Passer.Control.Core { return false; byte[] buffer = await Receive(dataStream, packetSize); - DestroyMsg msg = new(buffer); + DestroyMsg msg = new DestroyMsg(buffer); client.messageQueue.Enqueue(msg); return true; diff --git a/Messages/InvestigateMsg.cs b/Messages/InvestigateMsg.cs index 39d4c2b..b6f9765 100644 --- a/Messages/InvestigateMsg.cs +++ b/Messages/InvestigateMsg.cs @@ -37,7 +37,7 @@ namespace Passer.Control.Core { return false; byte[] buffer = await Receive(dataStream, packetSize); - InvestigateMsg msg = new(buffer); + InvestigateMsg msg = new InvestigateMsg(buffer); //UnityEngine.Debug.Log($"Receive investigate [{msg.networkId}/{msg.thingId}]"); client.messageQueue.Enqueue(msg); diff --git a/Messages/LowLevelMessages.cs b/Messages/LowLevelMessages.cs index 055096b..591a25d 100644 --- a/Messages/LowLevelMessages.cs +++ b/Messages/LowLevelMessages.cs @@ -17,7 +17,7 @@ namespace Passer.Control.Core float distance = ReceiveFloat16(data, ref ix); float horizontal = ReceiveAngle8(data, ref ix); float vertical = ReceiveAngle8(data, ref ix); - Spherical v = new(distance, horizontal, vertical); + Spherical v = new Spherical(distance, horizontal, vertical); return v; } @@ -48,7 +48,7 @@ namespace Passer.Control.Core } public static Quat32 ReceiveQuat32(byte[] data, ref byte ix) { - Quat32 q = new( + Quat32 q = new Quat32( (data[ix++] - 128.0F) / 127.0F, (data[ix++] - 128.0F) / 127.0F, (data[ix++] - 128.0F) / 127.0F, @@ -84,7 +84,7 @@ namespace Passer.Control.Core public static void SendFloat16(byte[] data, ref byte ix, float f) { - float16 f16 = new(f); + float16 f16 = new float16(f); ushort binary = f16.GetBinary(); data[ix++] = (byte)(binary >> 8); data[ix++] = (byte)(binary & 255); @@ -99,7 +99,7 @@ namespace Passer.Control.Core public static float ReceiveFloat16(byte[] data, ref byte ix) { ushort value = (ushort)(data[ix++] << 8 | data[ix++]); - float16 f16 = new(); + float16 f16 = new float16(); f16.SetBinary(value); float f = f16.toFloat(); return f; diff --git a/Messages/NameMsg.cs b/Messages/NameMsg.cs index cb709d2..70c8d9d 100644 --- a/Messages/NameMsg.cs +++ b/Messages/NameMsg.cs @@ -1,5 +1,3 @@ -#nullable enable - namespace Passer.Control.Core { public class NameMsg : IMessage { diff --git a/Messages/PoseMsg.cs b/Messages/PoseMsg.cs index 6cdcb0c..14c0a34 100644 --- a/Messages/PoseMsg.cs +++ b/Messages/PoseMsg.cs @@ -70,12 +70,12 @@ namespace Passer.Control.Core { } public static bool Send(Participant client, byte thingId, Spherical position, SwingTwist orientation) { - PoseMsg msg = new(client.networkId, thingId, position, orientation); + PoseMsg msg = new PoseMsg(client.networkId, thingId, position, orientation); return SendMsg(client, msg); } public static async Task Receive(Stream dataStream, Participant client, byte packetSize) { byte[] buffer = await Receive(dataStream, packetSize); - PoseMsg msg = new(buffer); + PoseMsg msg = new PoseMsg(buffer); // Do no process poses with nwid == 0 (== local) if (msg.networkId == 0) diff --git a/Messages/TextMsg.cs b/Messages/TextMsg.cs index b28ac57..3959116 100644 --- a/Messages/TextMsg.cs +++ b/Messages/TextMsg.cs @@ -16,7 +16,7 @@ namespace Passer.Control.Core { public static async Task Receive(Stream dataStream, Participant client, byte packetSize) { byte[] buffer = await Receive(dataStream, packetSize); - TextMsg msg = new(buffer); + TextMsg msg = new TextMsg(buffer); client.messageQueue.Enqueue(msg); return true; diff --git a/ModelUrlMsg.cs b/ModelUrlMsg.cs index a3c02ba..b1c712c 100644 --- a/ModelUrlMsg.cs +++ b/ModelUrlMsg.cs @@ -1,5 +1,3 @@ -#nullable enable - namespace Passer.Control.Core { public class ModelUrlMsg : IMessage { @@ -7,7 +5,7 @@ namespace Passer.Control.Core { public const byte length = 4; public byte networkId; public byte thingId; - public string? url = null; + public string url = null; public ModelUrlMsg(byte networkId, Thing thing) { this.networkId = networkId; diff --git a/Participant.cs b/Participant.cs index 2e19888..a8a650d 100644 --- a/Participant.cs +++ b/Participant.cs @@ -1,4 +1,3 @@ -#nullable enable using System; using System.Collections.Generic; using System.Collections.Concurrent; @@ -14,11 +13,11 @@ namespace Passer.Control.Core { //public byte networkId = 0; public string name = "Participant"; - public IPEndPoint? endPoint = null; - public UdpClient? udpClient = null; + public IPEndPoint endPoint = null; + public UdpClient udpClient = null; public string broadcastIpAddress = "255.255.255.255"; - public readonly ConcurrentQueue messageQueue = new(); + public readonly ConcurrentQueue messageQueue = new ConcurrentQueue(); #region Init @@ -46,10 +45,12 @@ namespace Passer.Control.Core { this.ipAddress = ipAddress; this.port = port; - this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending + this.udpClient = new UdpClient(); + this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); // local port + // this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending - this.udpClient = new UdpClient(port); // for receiving - this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); + // this.udpClient = new UdpClient(port); // for receiving + // this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); } @@ -63,9 +64,9 @@ namespace Passer.Control.Core { this.port = port; } - public List senders = new(); + public List senders = new List(); - public RemoteParticipant? GetParticipant(string ipAddress, int port) { + public RemoteParticipant GetParticipant(string ipAddress, int port) { //Console.WriteLine($"Get Participant {ipAddress}:{port}"); foreach (RemoteParticipant sender in senders) { if (sender.ipAddress == ipAddress && sender.port == port) @@ -75,14 +76,14 @@ namespace Passer.Control.Core { } public RemoteParticipant AddParticipant(string ipAddress, int port) { // Console.WriteLine($"New Participant {ipAddress}:{port}"); - RemoteParticipant participant = new(ipAddress, port) { + RemoteParticipant participant = new RemoteParticipant(ipAddress, port) { networkId = (byte)this.senders.Count }; senders.Add(participant); return participant; } - protected readonly Dictionary> thingMsgProcessors = new(); + protected readonly Dictionary> thingMsgProcessors = new Dictionary>(); public delegate Thing ThingConstructor(byte networkId, byte thingId); public void Register(byte thingType, ThingConstructor constr) { @@ -94,7 +95,7 @@ namespace Passer.Control.Core { } public void Register(byte thingType) where ThingClass : Thing { - thingMsgProcessors[thingType] = static (byte networkId, byte thingId) => + thingMsgProcessors[thingType] = (byte networkId, byte thingId) => Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass; Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); } @@ -115,13 +116,13 @@ namespace Passer.Control.Core { // We can receive our own publish (broadcast) packages. How do we recognize them???? // It is hard to determine our source port string ipAddress = this.endPoint.Address.ToString(); - RemoteParticipant? remoteParticipant = GetParticipant(ipAddress, this.endPoint.Port); + RemoteParticipant remoteParticipant = GetParticipant(ipAddress, this.endPoint.Port); if (remoteParticipant == null) remoteParticipant = AddParticipant(ipAddress, this.endPoint.Port); ReceiveData(data, remoteParticipant); - udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); + udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null); } protected ulong nextPublishMe = 0; @@ -177,7 +178,7 @@ namespace Passer.Control.Core { if (bufferSize <= 0) return true; - IPEndPoint participantEndpoint = new(IPAddress.Parse(remoteParticipant.ipAddress), remoteParticipant.port); + IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(remoteParticipant.ipAddress), remoteParticipant.port); Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); return true; @@ -289,7 +290,7 @@ namespace Passer.Control.Core { protected virtual void Process(RemoteParticipant sender, NameMsg msg) { // Console.WriteLine($"Participant: Process name [{msg.networkId}/{msg.thingId}] {msg.name}"); - Thing? thing = sender.Get(msg.networkId, msg.thingId); + Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) thing.name = msg.name; } @@ -302,7 +303,7 @@ namespace Passer.Control.Core { protected virtual void Process(RemoteParticipant sender, CustomMsg msg) { // Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]"); - Thing? thing = sender.Get(msg.networkId, msg.thingId); + Thing thing = sender.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.bytes); } diff --git a/RemoteParticipant.cs b/RemoteParticipant.cs index 8d8341f..0a7ee81 100644 --- a/RemoteParticipant.cs +++ b/RemoteParticipant.cs @@ -18,10 +18,10 @@ namespace Passer.Control.Core { this.port = port; } - protected readonly List things = new(); + protected readonly List things = new List(); - public Thing? Get(byte networkId, byte thingId) { - Thing? thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); + public Thing Get(byte networkId, byte thingId) { + Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); return thing; } @@ -30,7 +30,7 @@ namespace Passer.Control.Core { public void Add(Thing thing, bool invokeEvent = true) { // Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]"); - Thing? foundThing = Get(thing.networkId, thing.id); + Thing foundThing = Get(thing.networkId, thing.id); if (foundThing == null) { things.Add(thing); diff --git a/SiteServer.cs b/SiteServer.cs index 8c80049..3282444 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -20,7 +20,7 @@ namespace Passer.Control.Core { this.udpClient = new UdpClient(port); // for receiving this.udpClient.BeginReceive( new AsyncCallback(result => ReceiveUDP(result)), - new Tuple(this.udpClient, new(IPAddress.Any, port))); + new Tuple(this.udpClient, new IPEndPoint(IPAddress.Any, port))); } public void Close() { @@ -41,14 +41,15 @@ namespace Passer.Control.Core { protected override void Process(RemoteParticipant sender, ThingMsg msg) { //Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); - Thing? thing = sender.Get(msg.networkId, msg.thingId); + Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing == null) { - Thing? newThing = null; - if (thingMsgProcessors.TryGetValue(msg.thingType, out Func? value)) { + Thing newThing = null; + if (thingMsgProcessors.TryGetValue(msg.thingType, out Func value)) { if (value != null) newThing = value(msg.networkId, msg.thingId); } - newThing ??= new Thing(sender, msg.networkId, msg.thingId, msg.thingType); + if (newThing == null) + newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); sender.Add(newThing); } diff --git a/Thing.cs b/Thing.cs index 3570382..fcf85e6 100644 --- a/Thing.cs +++ b/Thing.cs @@ -43,8 +43,8 @@ namespace Passer.Control.Core { public byte id; public event ChangeHandler OnParentChanged = delegate {}; - private Thing? _parent; - public Thing? parent { + private Thing _parent; + public Thing parent { get => _parent; set { if (_parent == value) diff --git a/float16.cs b/float16.cs index b934651..c1885c3 100644 --- a/float16.cs +++ b/float16.cs @@ -1,10 +1,8 @@ using System; -namespace Passer.LinearAlgebra -{ +namespace Passer.LinearAlgebra { - public class float16 - { + public class float16 { // // FILE: float16.cpp // AUTHOR: Rob Tillaart @@ -16,13 +14,11 @@ namespace Passer.LinearAlgebra public float16() { _value = 0; } - public float16(float f) - { + public float16(float f) { _value = f32tof16(f); } - public float toFloat() - { + public float toFloat() { return f16tof32(_value); } @@ -156,8 +152,7 @@ namespace Passer.LinearAlgebra // // CORE CONVERSION // - float f16tof32(ushort _value) - { + float f16tof32(ushort _value) { //ushort sgn; ushort man; int exp; @@ -172,13 +167,11 @@ namespace Passer.LinearAlgebra //Debug.Log($"{sgn} {exp} {man}"); // ZERO - if ((_value & 0x7FFF) == 0) - { + if ((_value & 0x7FFF) == 0) { return sgn ? -0 : 0; } // NAN & INF - if (exp == 0x001F) - { + if (exp == 0x001F) { if (man == 0) return sgn ? float.NegativeInfinity : float.PositiveInfinity; //-INFINITY : INFINITY; else @@ -192,44 +185,46 @@ namespace Passer.LinearAlgebra f = 1; // PROCESS MANTISSE - for (int i = 9; i >= 0; i--) - { + for (int i = 9; i >= 0; i--) { f *= 2; if ((man & (1 << i)) != 0) f = f + 1; } //Debug.Log($"{f}"); f = f * (float)Math.Pow(2.0f, exp - 25); - if (exp == 0) - { + if (exp == 0) { f = f * (float)Math.Pow(2.0f, -13); // 5.96046447754e-8; } //Debug.Log($"{f}"); return sgn ? -f : f; } - ushort f32tof16(float f) - { + public static uint SingleToInt32Bits(float value) { + byte[] bytes = BitConverter.GetBytes(value); + if (BitConverter.IsLittleEndian) + Array.Reverse(bytes); // If the system is little-endian, reverse the byte order + return BitConverter.ToUInt32(bytes, 0); + } + + ushort f32tof16(float f) { //uint t = *(uint*)&f; - uint t = (uint)BitConverter.SingleToInt32Bits(f); + //uint t = (uint)BitConverter.SingleToInt32Bits(f); + uint t = SingleToInt32Bits(f); // man bits = 10; but we keep 11 for rounding ushort man = (ushort)((t & 0x007FFFFF) >> 12); short exp = (short)((t & 0x7F800000) >> 23); bool sgn = (t & 0x80000000) != 0; // handle 0 - if ((t & 0x7FFFFFFF) == 0) - { + if ((t & 0x7FFFFFFF) == 0) { return sgn ? (ushort)0x8000 : (ushort)0x0000; } // denormalized float32 does not fit in float16 - if (exp == 0x00) - { + if (exp == 0x00) { return sgn ? (ushort)0x8000 : (ushort)0x0000; } // handle infinity & NAN - if (exp == 0x00FF) - { + if (exp == 0x00FF) { if (man != 0) return 0xFE00; // NAN return sgn ? (ushort)0xFC00 : (ushort)0x7C00; // -INF : INF @@ -238,13 +233,11 @@ namespace Passer.LinearAlgebra // normal numbers exp = (short)(exp - 127 + 15); // overflow does not fit => INF - if (exp > 30) - { + if (exp > 30) { return sgn ? (ushort)0xFC00 : (ushort)0x7C00; // -INF : INF } // subnormal numbers - if (exp < -38) - { + if (exp < -38) { return sgn ? (ushort)0x8000 : (ushort)0x0000; // -0 or 0 ? just 0 ? } if (exp <= 0) // subnormal diff --git a/test/UnitTest1.cs b/test/UnitTest1.cs index 51c6baa..66633d7 100644 --- a/test/UnitTest1.cs +++ b/test/UnitTest1.cs @@ -13,7 +13,7 @@ namespace ControlCore.test { [Test] public void Test_Participant() { - Participant participant = new("127.0.0.1", 7681); + Participant participant = new Participant("127.0.0.1", 7682); ulong milliseconds = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); ulong startTime = milliseconds; @@ -29,7 +29,7 @@ namespace ControlCore.test { [Test] public void Test_SiteServer() { - SiteServer siteServer = new(7681); + SiteServer siteServer = new SiteServer(7681); ulong milliseconds = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); ulong startTime = milliseconds; @@ -45,8 +45,8 @@ namespace ControlCore.test { [Test] public void Test_SiteParticipant() { - SiteServer siteServer = new(7681); - Participant participant = new("127.0.0.1", 7681); + SiteServer siteServer = new SiteServer(7681); + Participant participant = new Participant("127.0.0.1", 7681); ulong milliseconds = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); ulong startTime = milliseconds; @@ -63,9 +63,9 @@ namespace ControlCore.test { [Test] public void Test_ThingMsg() { - SiteServer siteServer = new(); - Participant participant = new("127.0.0.1"); - Thing thing = new(participant) { + SiteServer siteServer = new SiteServer(); + Participant participant = new Participant("127.0.0.1"); + Thing thing = new Thing(participant) { name = "First Thing", modelUrl = "https://passer.life/extras/ant.jpg" }; diff --git a/test/test.csproj b/test/test.csproj index b0f97b9..1c81788 100644 --- a/test/test.csproj +++ b/test/test.csproj @@ -1,24 +1,15 @@  - net9.0 - latest - enable - enable + net5.0 false true - - - - - - - - - + + +