From 87d903619e1e19233f3f7d6279ba8eaa1e6198e9 Mon Sep 17 00:00:00 2001 From: Cassandra de la Cruz-Munoz Date: Sat, 21 Oct 2023 09:15:37 -0400 Subject: [PATCH] feat!: add exceptions and support type safe getting OscArgument values --- Exceptions/DataOutOfRangeException.cs | 17 +++ Exceptions/MissingArgumentsException.cs | 16 +++ InvalidType.cs | 32 ----- Marionette.cs | 44 +++---- Performer.cs | 22 ++-- VmcMessages/VmcExtBlendVal.cs | 77 +++++------- VmcMessages/VmcExtBonePos.cs | 68 ++-------- VmcMessages/VmcExtCam.cs | 74 ++--------- VmcMessages/VmcExtCon.cs | 83 ++++--------- VmcMessages/VmcExtConfig.cs | 19 +-- VmcMessages/VmcExtDevicePos.cs | 68 ++-------- VmcMessages/VmcExtKey.cs | 46 +++---- VmcMessages/VmcExtLight.cs | 100 +++------------ VmcMessages/VmcExtMidiCcBit.cs | 38 +++--- VmcMessages/VmcExtMidiCcVal.cs | 22 ++-- VmcMessages/VmcExtMidiNote.cs | 54 +++----- VmcMessages/VmcExtOk.cs | 158 +++++++++--------------- VmcMessages/VmcExtOpt.cs | 19 +-- VmcMessages/VmcExtRcv.cs | 98 +++++++-------- VmcMessages/VmcExtRemote.cs | 27 ++-- VmcMessages/VmcExtRootPos.cs | 151 ++++------------------ VmcMessages/VmcExtSetCalibExec.cs | 35 +++--- VmcMessages/VmcExtSetConfig.cs | 19 +-- VmcMessages/VmcExtSetEye.cs | 32 ++--- VmcMessages/VmcExtSetPeriod.cs | 59 ++------- VmcMessages/VmcExtSetRes.cs | 19 +-- VmcMessages/VmcExtSetShortcut.cs | 19 +-- VmcMessages/VmcExtSettingColor.cs | 39 ++---- VmcMessages/VmcExtSettingWin.cs | 108 +++++++--------- VmcMessages/VmcExtT.cs | 19 +-- VmcMessages/VmcExtVrm.cs | 61 +++------ 31 files changed, 517 insertions(+), 1126 deletions(-) create mode 100644 Exceptions/DataOutOfRangeException.cs create mode 100644 Exceptions/MissingArgumentsException.cs delete mode 100644 InvalidType.cs diff --git a/Exceptions/DataOutOfRangeException.cs b/Exceptions/DataOutOfRangeException.cs new file mode 100644 index 0000000..dd0421c --- /dev/null +++ b/Exceptions/DataOutOfRangeException.cs @@ -0,0 +1,17 @@ +using System; +using godotOscSharp; + +namespace godotVmcSharp +{ + public class DataOutOfRangeException : ArgumentOutOfRangeException + { + public DataOutOfRangeException(String address, int index, String name, String expected, object actual): base($"{address}[{index}] {name}", actual, expected) + {} + public DataOutOfRangeException(String address, int index, String name, String expected, object actual, Exception inner): base($"{address}[{index}] {name} has data out of range. Received '{actual}', expected within range '{expected}'.", inner) + {} + public DataOutOfRangeException(OscAddress address, int index, String name, String expected, object actual): base($"{address.ToString()}[{index}] {name}", actual, expected) + {} + public DataOutOfRangeException(OscAddress address, int index, String name, String expected, object actual, Exception inner): base($"{address.ToString()}[{index}] {name} has data out of range. Received '{actual}', expected within range '{expected}'.", inner) + {} + } +} diff --git a/Exceptions/MissingArgumentsException.cs b/Exceptions/MissingArgumentsException.cs new file mode 100644 index 0000000..01fec91 --- /dev/null +++ b/Exceptions/MissingArgumentsException.cs @@ -0,0 +1,16 @@ +using System; +using godotOscSharp; + +namespace godotVmcSharp +{ + public class MissingArgumentsException : ArgumentNullException + { + public MissingArgumentsException() : base("Missing parameters.") {} + public MissingArgumentsException(String message): base(message) {} + public MissingArgumentsException(String message, Exception inner): base(message, inner) {} + public MissingArgumentsException(String address, String expected, int actual) : base($"{address}", "Missing parameters: expecting {expected}, received {actual}.") {} + public MissingArgumentsException(String address, String expected, int actual, Exception inner) : base($"Missing parameters for {address}: expecting {expected}, received {actual}.", inner) {} + public MissingArgumentsException(OscAddress address, String expected, int actual) : base($"{address.ToString()}", "Missing parameters: expecting {expected}, received {actual}.") {} + public MissingArgumentsException(OscAddress address, String expected, int actual, Exception inner) : base($"Missing parameters for {address.ToString()}: expecting {expected}, received {actual}.", inner) {} + } +} diff --git a/InvalidType.cs b/InvalidType.cs deleted file mode 100644 index 8b58e01..0000000 --- a/InvalidType.cs +++ /dev/null @@ -1,32 +0,0 @@ -/* - godotVmcSharp - Copyright (C) 2023 Cassandra de la Cruz-Munoz - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - */ - -namespace godotVmcSharp -{ - public class InvalidArgumentType - { - public static string GetErrorString(string address, string name, char expected, char actual) - { - return $"Invalid argument type for argument \"{name}\" of \"{address}\". Expected {expected}, received {actual}."; - } - public static string GetErrorString(godotOscSharp.OscAddress address, string name, char expected, char actual) - { - return GetErrorString(address.ToString(), name, expected, actual); - } - } -} diff --git a/Marionette.cs b/Marionette.cs index 8749e5f..96894ce 100644 --- a/Marionette.cs +++ b/Marionette.cs @@ -20,12 +20,14 @@ using System.Net; using godotOscSharp; using Godot; +#nullable enable + namespace godotVmcSharp { public class Marionette { private OscReceiver receiver; - private OscSender sender; + private OscSender? sender; private CameraReceiver cam; private DeviceReceiver devices; private DirectionalLightReceiver lights; @@ -55,40 +57,40 @@ namespace godotVmcSharp switch (m.Address.ToString()) { case "/VMC/Ext/OK": - new VmcExtOk(m); + VmcExtOk.CreateFromMessage(m); break; case "/VMC/Ext/T": - new VmcExtT(m); + VmcExtT.CreateFromMessage(m); break; case "/VMC/Ext/Root/Pos": - new VmcExtRootPos(m); + VmcExtRootPos.CreateFromMessage(m); break; case "/VMC/Ext/Bone/Pos": - new VmcExtBonePos(m); + VmcExtBonePos.CreateFromMessage(m); break; case "/VMC/Ext/Blend/Val": - new VmcExtBlendVal(m); + VmcExtBlendVal.CreateFromMessage(m); break; case "/VMC/Ext/Blend/Apply": new VmcMessage(m.Address); break; case "/VMC/Ext/Cam": - this.cam.ProcessMessage(new VmcExtCam(m)); + this.cam.ProcessMessage(VmcExtCam.CreateFromMessage(m)); break; case "/VMC/Ext/Con": - new VmcExtCon(m); + VmcExtCon.CreateFromMessage(m); break; case "/VMC/Ext/Key": - new VmcExtKey(m); + VmcExtKey.CreateFromMessage(m); break; case "/VMC/Ext/Midi/Note": - new VmcExtMidiNote(m); + VmcExtMidiNote.CreateFromMessage(m); break; case "/VMC/Ext/Midi/CC/Val": - new VmcExtMidiCcVal(m); + VmcExtMidiCcVal.CreateFromMessage(m); break; case "/VMC/Ext/Midi/CC/Bit": - new VmcExtMidiCcBit(m); + VmcExtMidiCcBit.CreateFromMessage(m); break; case "/VMC/Ext/Hmd/Pos": case "/VMC/Ext/Con/Pos": @@ -96,31 +98,31 @@ namespace godotVmcSharp case "/VMC/Ext/Hmd/Pos/Local": case "/VMC/Ext/Con/Pos/Local": case "/VMC/Ext/Tra/Pos/Local": - this.devices.ProcessMessage(new VmcExtDevicePos(m)); + this.devices.ProcessMessage(VmcExtDevicePos.CreateFromMessage(m)); break; case "/VMC/Ext/Rcv": - new VmcExtRcv(m); + VmcExtRcv.CreateFromMessage(m); break; case "/VMC/Ext/Light": - this.lights.ProcessMessage(new VmcExtLight(m)); + this.lights.ProcessMessage(VmcExtLight.CreateFromMessage(m)); break; case "/VMC/Ext/VRM": - new VmcExtVrm(m); + VmcExtVrm.CreateFromMessage(m); break; case "/VMC/Ext/Remote": - new VmcExtRemote(m); + VmcExtRemote.CreateFromMessage(m); break; case "/VMC/Ext/Opt": - new VmcExtOpt(m); + VmcExtOpt.CreateFromMessage(m); break; case "/VMC/Ext/Setting/Color": - new VmcExtSettingColor(m); + VmcExtSettingColor.CreateFromMessage(m); break; case "/VMC/Ext/Setting/Win": - new VmcExtSettingWin(m); + VmcExtSettingWin.CreateFromMessage(m); break; case "/VMC/Ext/Config": - new VmcExtConfig(m); + VmcExtConfig.CreateFromMessage(m); break; } } diff --git a/Performer.cs b/Performer.cs index d806a83..d9fa7c8 100644 --- a/Performer.cs +++ b/Performer.cs @@ -55,46 +55,46 @@ namespace godotVmcSharp case "/VMC/Ext/Hmd/Pos": case "/VMC/Ext/Con/Pos": case "/VMC/Ext/Tra/Pos": - this.devices.ProcessMessage(new VmcExtDevicePos(m)); + this.devices.ProcessMessage(VmcExtDevicePos.CreateFromMessage(m)); break; case "/VMC/Ext/Set/Period": - new VmcExtSetPeriod(m); + VmcExtSetPeriod.CreateFromMessage(m); break; case "/VMC/Ext/Midi/CC/Val": - new VmcExtMidiCcVal(m); + VmcExtMidiCcVal.CreateFromMessage(m); break; case "/VMC/Ext/Cam": - this.cam.ProcessMessage(new VmcExtCam(m)); + this.cam.ProcessMessage(VmcExtCam.CreateFromMessage(m)); break; case "/VMC/Ext/Blend/Val": - new VmcExtBlendVal(m); + VmcExtBlendVal.CreateFromMessage(m); break; case "/VMC/Ext/Blend/Apply": new VmcMessage(m.Address); break; case "/VMC/Ext/Set/Eye": - new VmcExtSetEye(m); + VmcExtSetEye.CreateFromMessage(m); break; case "/VMC/Ext/Set/Req": new VmcMessage(m.Address); break; case "/VMC/Ext/Set/Res": - new VmcExtSetRes(m); + VmcExtSetRes.CreateFromMessage(m); break; case "/VMC/Ext/Set/Calib/Ready": new VmcMessage(m.Address); break; case "/VMC/Ext/Set/Calib/Exec": - new VmcExtSetCalibExec(m); + VmcExtSetCalibExec.CreateFromMessage(m); break; case "/VMC/Ext/Set/Config": - new VmcExtSetConfig(m); + VmcExtSetConfig.CreateFromMessage(m); break; case "/VMC/Ext/Light": - this.lights.ProcessMessage(new VmcExtLight(m)); + this.lights.ProcessMessage(VmcExtLight.CreateFromMessage(m)); break; case "/VMC/Ext/Set/Shortcut": - new VmcExtSetShortcut(m); + VmcExtSetShortcut.CreateFromMessage(m); break; } } diff --git a/VmcMessages/VmcExtBlendVal.cs b/VmcMessages/VmcExtBlendVal.cs index 4a2f5f6..65914b9 100644 --- a/VmcMessages/VmcExtBlendVal.cs +++ b/VmcMessages/VmcExtBlendVal.cs @@ -16,92 +16,83 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; +using System; + +#nullable enable namespace godotVmcSharp { public class VmcExtBlendVal : VmcMessage { - public readonly string Name; + public readonly string? Name; public readonly float Value; - public VmcExtBlendVal(OscMessage m) : base(m.Address) + public static VmcExtBlendVal CreateFromMessage(OscMessage m) { - if (m.Data.Count != 2) + if (m.Data.Count <= 2) { - GD.Print($"Invalid number of arguments for /VMC/Ext/Blend/Val. Expected 2, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "2", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', m.Data[0].Type)); - return; + var blendShape = m.Data[0].GetAsString(); + var value = m.Data[1].GetAsFloat(); + if (value < 0 || value > 1) { + throw new DataOutOfRangeException(m.Address, 1, "value", "0 <= value <= 1", value); } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "value", 'f', m.Data[1].Type)); - return; - } - var blendShape = (string)m.Data[0].Value; if (IsVrm0BlendShape(blendShape)) { - Name = blendShape; - Value = (float)m.Data[1].Value; - return; + return new VmcExtBlendVal(blendShape, value); } if (IsVrm1Expression(blendShape)) { - Name = blendShape; - Value = (float)m.Data[1].Value; - return; + return new VmcExtBlendVal(blendShape, value); } if (IsArkitBlendShape(blendShape)) { - Name = blendShape; - Value = (float)m.Data[1].Value; - return; + return new VmcExtBlendVal(blendShape, value); } - GD.Print($"Invalid argument for {Addr}. BlendShape \"{blendShape}\" not in list."); + throw new DataOutOfRangeException(m.Address, 0, "name", "name in valid blendshape names", blendShape); } - public VmcExtBlendVal(string name, float value) : base(new OscAddress("VMC/Ext/Blend/Val")) + public static VmcExtBlendVal CreateFromParams(string name, float value) { + if (value < 0 || value > 1) { + throw new DataOutOfRangeException("VMC/Ext/Blend/Val", 1, "value", "0 <= value <= 1", value); + } if (IsVrm0BlendShape(name)) { - Name = name; - Value = value; - return; + return new VmcExtBlendVal(name, value); } if (IsVrm1Expression(name)) { - Name = name; - Value = value; - return; + return new VmcExtBlendVal(name, value); } if (IsArkitBlendShape(name)) { - Name = name; - Value = value; - return; + return new VmcExtBlendVal(name, value); } - GD.Print($"Invalid argument for {Addr}. BlendShape \"{name}\" not in list."); + throw new DataOutOfRangeException("VMC/Ext/Blend/Val", 0, "name", "name in valid blendshape names", name); } - private bool IsVrm0BlendShape(string name) + private VmcExtBlendVal(String name, float value): base(new OscAddress("VMC/Ext/Blend/Val")) { + Name = name; + Value = value; + } + + private static bool IsVrm0BlendShape(string name) { return name == "Joy" || name == "Angry" || name == "Sorrow" || name == "Fun" || name == "A" || name == "I" || name == "U" || name == "E" || name == "O" || name == "Blink_L" || name == "Blink_R"; } - private bool IsVrm1Expression(string name) + private static bool IsVrm1Expression(string name) { return name == "happy" || name == "angry" || name == "sad" || name == "relaxed" || name == "aa" || name == "ih" || name == "ou" || name == "ee" || name == "oh" || name == "blinkLeft" || name == "blinkRight"; } - private bool IsArkitBlendShape(string name) + private static bool IsArkitBlendShape(string name) { return name == "browInnerUp" || name == "browDownLeft" || name == "browDownRight" || @@ -135,9 +126,9 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List { - new OscArgument(Name, 's'), - new OscArgument(Value, 'f') + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Value, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtBonePos.cs b/VmcMessages/VmcExtBonePos.cs index 38842ee..fe41961 100644 --- a/VmcMessages/VmcExtBonePos.cs +++ b/VmcMessages/VmcExtBonePos.cs @@ -26,55 +26,13 @@ namespace godotVmcSharp public readonly string Name; public readonly Transform3D Transform; - public VmcExtBonePos(OscMessage m) : base(m.Address) + public static VmcExtBonePos CreateFromMessage(OscMessage m) { - if (m.Data.Count != 8) + if (m.Data.Count < 8) { - GD.Print($"Invalid number of arguments for {Addr}. Expected 8, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "8", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.x", 'f', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.y", 'f', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.z", 'f', m.Data[3].Type)); - return; - } - if (m.Data[4].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.x", 'f', m.Data[4].Type)); - return; - } - if (m.Data[5].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.y", 'f', m.Data[5].Type)); - return; - } - if (m.Data[6].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.z", 'f', m.Data[6].Type)); - return; - } - if (m.Data[7].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.w", 'f', m.Data[7].Type)); - return; - } - Name = (string)m.Data[0].Value; - Transform = new Transform3D(new Basis(new Quaternion((float)m.Data[4].Value, (float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value)), new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value)); + return new VmcExtBonePos((string)m.Data[0].GetAsString(), new Transform3D(new Basis(new Quaternion(m.Data[4].GetAsFloat(), m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat()))); } public VmcExtBonePos(string name, Transform3D transform) : base(new OscAddress("/VMC/Ext/Bone/Pos")) @@ -87,15 +45,15 @@ namespace godotVmcSharp { var quat = Transform.Basis.GetRotationQuaternion(); return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Name, 's'), - new OscArgument(Transform.Origin.X, 'f'), - new OscArgument(Transform.Origin.Y, 'f'), - new OscArgument(Transform.Origin.Z, 'f'), - new OscArgument(quat.X, 'f'), - new OscArgument(quat.Y, 'f'), - new OscArgument(quat.Z, 'f'), - new OscArgument(quat.W, 'f'), + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Transform.Origin.X, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Y, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Z, 'f')!, + OscArgument.CreateFromParams(quat.X, 'f')!, + OscArgument.CreateFromParams(quat.Y, 'f')!, + OscArgument.CreateFromParams(quat.Z, 'f')!, + OscArgument.CreateFromParams(quat.W, 'f')!, }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtCam.cs b/VmcMessages/VmcExtCam.cs index dbe1a88..5b7bdc0 100644 --- a/VmcMessages/VmcExtCam.cs +++ b/VmcMessages/VmcExtCam.cs @@ -28,61 +28,13 @@ namespace godotVmcSharp public readonly Transform3D Transform; public readonly float Fov; - public VmcExtCam(OscMessage m) : base(m.Address) + public static VmcExtCam CreateFromMessage(OscMessage m) { - if (m.Data.Count != 9) + if (m.Data.Count < 9) { - GD.Print($"Invalid number of arguments for /VMC/Ext/Cam. Expected 9, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "9", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.x", 'f', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.y", 'f', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.z", 'f', m.Data[3].Type)); - return; - } - if (m.Data[4].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.x", 'f', m.Data[4].Type)); - return; - } - if (m.Data[5].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.y", 'f', m.Data[5].Type)); - return; - } - if (m.Data[6].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.z", 'f', m.Data[6].Type)); - return; - } - if (m.Data[7].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.w", 'f', m.Data[7].Type)); - return; - } - if (m.Data[8].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString("VMC/Ext/Cam", "fov", 'f', m.Data[8].Type)); - return; - } - Name = (string)m.Data[0].Value; - Transform = new Transform3D(new Basis(new Quaternion((float)m.Data[4].Value, (float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value)), new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value)); - Fov = (float)m.Data[8].Value; + return new VmcExtCam(m.Data[0].GetAsString(), new Transform3D(new Basis(new Quaternion(m.Data[4].GetAsFloat(), m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat())), m.Data[8].GetAsFloat()); } public VmcExtCam(string name, Transform3D transform, float fov) : base(new OscAddress("/VMC/Ext/Cam")) @@ -96,15 +48,15 @@ namespace godotVmcSharp { var quat = Transform.Basis.GetRotationQuaternion(); return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Name, 's'), - new OscArgument(Transform.Origin.X, 'f'), - new OscArgument(Transform.Origin.Y, 'f'), - new OscArgument(Transform.Origin.Z, 'f'), - new OscArgument(quat.X, 'f'), - new OscArgument(quat.Y, 'f'), - new OscArgument(quat.Z, 'f'), - new OscArgument(quat.W, 'f'), - new OscArgument(Fov, 'f') + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Transform.Origin.X, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Y, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Z, 'f')!, + OscArgument.CreateFromParams(quat.X, 'f')!, + OscArgument.CreateFromParams(quat.Y, 'f')!, + OscArgument.CreateFromParams(quat.Z, 'f')!, + OscArgument.CreateFromParams(quat.W, 'f')!, + OscArgument.CreateFromParams(Fov, 'f')! }); } } diff --git a/VmcMessages/VmcExtCon.cs b/VmcMessages/VmcExtCon.cs index 16b4f86..3fba6f7 100644 --- a/VmcMessages/VmcExtCon.cs +++ b/VmcMessages/VmcExtCon.cs @@ -30,67 +30,31 @@ namespace godotVmcSharp public readonly int IsAxis; public readonly Vector3 Axis; - public VmcExtCon(OscMessage m) : base(m.Address) + public static VmcExtCon CreateFromMessage(OscMessage m) { - if (m.Data.Count != 8) + if (m.Data.Count < 8) { - GD.Print($"Invalid number of arguments for /VMC/Ext/Con. Expected 8, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "8", m.Data.Count); } - if (m.Data[0].Type != 'i') + var active = m.Data[0].GetAsInt32(); + if (active < 0 || active > 2) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "active", 'i', m.Data[0].Type)); - return; + throw new DataOutOfRangeException(m.Address, 0, "active", "{0, 1, 2}", active); } - if (m.Data[1].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "IsLeft", 'i', m.Data[2].Type)); - } - if (m.Data[3].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "IsTouch", 'i', m.Data[3].Type)); - } - if (m.Data[4].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "IsAxis", 'i', m.Data[4].Type)); - } - if (m.Data[5].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "Axis.x", 'f', m.Data[5].Type)); - } - if (m.Data[6].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "Axis.y", 'f', m.Data[6].Type)); - } - if (m.Data[7].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "Axis.z", 'f', m.Data[7].Type)); - } - if ((int)m.Data[0].Value < 0 || (int)m.Data[0].Value > 2) - { - GD.Print($"Invalid value for \"active\" 'i' argument of /VMC/Ext/Con. Expected 0-2, received {(int)m.Data[0].Value}"); - return; - } - Active = (int)m.Data[0].Value; - Name = (string)m.Data[1].Value; - IsLeft = (int)m.Data[2].Value; - IsTouch = (int)m.Data[3].Value; - IsAxis = (int)m.Data[4].Value; - Axis = new Vector3((float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value); + return new VmcExtCon(active, m.Data[1].GetAsString(), m.Data[2].GetAsInt32(), m.Data[3].GetAsInt32(), m.Data[4].GetAsInt32(), new Vector3(m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())); } - public VmcExtCon(int active, string name, int isLeft, int isTouch, int isAxis, Vector3 axis) : base(new OscAddress("/VMC/Ext/Con")) + public static VmcExtCon CreateFromParams(int active, string name, int isLeft, int isTouch, int isAxis, Vector3 axis) { if (active < 0 || active > 2) { - GD.Print($"Invalid value for \"active\" 'i' argument of {Addr}. Expected 0-2, received {active}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/Con", 0, "active", "{0, 1, 2}", active); } + return new VmcExtCon(active, name, isLeft, isTouch, isAxis, axis); + } + + private VmcExtCon(int active, string name, int isLeft, int isTouch, int isAxis, Vector3 axis) : base(new OscAddress("/VMC/Ext/Con")) + { Active = active; Name = name; IsLeft = isLeft; @@ -99,18 +63,19 @@ namespace godotVmcSharp Axis = axis; } + public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Active, 'i'), - new OscArgument(Name, 's'), - new OscArgument(IsLeft, 'i'), - new OscArgument(IsTouch, 'i'), - new OscArgument(IsAxis, 'i'), - new OscArgument(Axis.X, 'i'), - new OscArgument(Axis.Y, 'i'), - new OscArgument(Axis.Z, 'i'), + OscArgument.CreateFromParams(Active, 'i')!, + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(IsLeft, 'i')!, + OscArgument.CreateFromParams(IsTouch, 'i')!, + OscArgument.CreateFromParams(IsAxis, 'i')!, + OscArgument.CreateFromParams(Axis.X, 'i')!, + OscArgument.CreateFromParams(Axis.Y, 'i')!, + OscArgument.CreateFromParams(Axis.Z, 'i')!, }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtConfig.cs b/VmcMessages/VmcExtConfig.cs index fef5c03..b9e9131 100644 --- a/VmcMessages/VmcExtConfig.cs +++ b/VmcMessages/VmcExtConfig.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,19 +24,13 @@ namespace godotVmcSharp { public readonly string Path; - public VmcExtConfig(OscMessage m) : base(m.Address) + public static VmcExtConfig CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 1, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "1", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "path", 's', m.Data[0].Type)); - return; - } - Path = (string)m.Data[0].Value; + return new VmcExtConfig(m.Data[0].GetAsString()); } public VmcExtConfig(string path) : base(new OscAddress("/VMC/Ext/Config")) @@ -47,7 +40,7 @@ namespace godotVmcSharp public new OscMessage ToMessage() { - return new OscMessage(Addr, new System.Collections.Generic.List { new OscArgument(Path, 's') }); + return new OscMessage(Addr, new System.Collections.Generic.List { OscArgument.CreateFromParams(Path, 's') }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtDevicePos.cs b/VmcMessages/VmcExtDevicePos.cs index abbcf3f..9a3eba2 100644 --- a/VmcMessages/VmcExtDevicePos.cs +++ b/VmcMessages/VmcExtDevicePos.cs @@ -26,55 +26,13 @@ namespace godotVmcSharp public readonly string Serial; public readonly Transform3D Transform; - public VmcExtDevicePos(OscMessage m) : base(m.Address) + public static VmcExtDevicePos CreateFromMessage(OscMessage m) { - if (m.Data.Count != 8) + if (m.Data.Count < 8) { - GD.Print($"Invalid number of arguments for {base.Addr}. Expected 8, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "{8}", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "serial", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.x", 'f', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.y", 'f', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.z", 'f', m.Data[3].Type)); - return; - } - if (m.Data[4].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.x", 'f', m.Data[4].Type)); - return; - } - if (m.Data[5].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.y", 'f', m.Data[5].Type)); - return; - } - if (m.Data[6].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.z", 'f', m.Data[6].Type)); - return; - } - if (m.Data[7].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.w", 'f', m.Data[7].Type)); - return; - } - Serial = (string)m.Data[0].Value; - Transform = new Transform3D(new Basis(new Quaternion((float)m.Data[4].Value, (float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value)), new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value)); + return new VmcExtDevicePos(m.Address, m.Data[0].GetAsString(), new Transform3D(new Basis(new Quaternion(m.Data[4].GetAsFloat(), m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat()))); } public VmcExtDevicePos(OscAddress Addr, string serial, Transform3D transform) : base(Addr) @@ -87,15 +45,15 @@ namespace godotVmcSharp { var quat = Transform.Basis.GetRotationQuaternion(); return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Serial, 's'), - new OscArgument(Transform.Origin.X, 'f'), - new OscArgument(Transform.Origin.Y, 'f'), - new OscArgument(Transform.Origin.Z, 'f'), - new OscArgument(quat.X, 'f'), - new OscArgument(quat.Y, 'f'), - new OscArgument(quat.Z, 'f'), - new OscArgument(quat.W, 'f') + OscArgument.CreateFromParams(Serial, 's')!, + OscArgument.CreateFromParams(Transform.Origin.X, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Y, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Z, 'f')!, + OscArgument.CreateFromParams(quat.X, 'f')!, + OscArgument.CreateFromParams(quat.Y, 'f')!, + OscArgument.CreateFromParams(quat.Z, 'f')!, + OscArgument.CreateFromParams(quat.W, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtKey.cs b/VmcMessages/VmcExtKey.cs index 86aae04..80fe00a 100644 --- a/VmcMessages/VmcExtKey.cs +++ b/VmcMessages/VmcExtKey.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -26,40 +25,27 @@ namespace godotVmcSharp public readonly int Active; public readonly string Name; public readonly int Keycode; - public VmcExtKey(OscMessage m) : base(m.Address) + public static VmcExtKey CreateFromMessage(OscMessage m) { - if (m.Data[0].Type != 'i') + var active = m.Data[0].GetAsInt32(); + if (active < 0 || active > 1) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "active", 'i', m.Data[0].Type)); - return; + throw new DataOutOfRangeException(m.Address, 0, "active", "{0, 1}", active); } - if (m.Data[1].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "keycode", 'i', m.Data[2].Type)); - return; - } - if ((int)m.Data[0].Value < 0 || (int)m.Data[0].Value > 1) - { - GD.Print($"Invalid value for \"active\" 'i' argument of {Addr}. Expected 0 or 1, received {(int)m.Data[0].Value}"); - return; - } - Active = (int)m.Data[0].Value; - Name = (string)m.Data[1].Value; - Keycode = (int)m.Data[2].Value; + return new VmcExtKey(active, m.Data[1].GetAsString(), m.Data[2].GetAsInt32()); } - public VmcExtKey(int active, string name, int keycode) : base(new OscAddress("/VMC/Ext/Key")) + public static VmcExtKey CreateFromParams(int active, string name, int keycode) { if (active < 0 || active > 1) { - GD.Print($"Invalid value for \"active\" 'i' argument of {Addr}. Expected 0 or 1, received {active}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/Key", 0, "active", "{0, 1}", active); } + return new VmcExtKey(active, name, keycode); + } + + private VmcExtKey(int active, string name, int keycode) : base(new OscAddress("/VMC/Ext/Key")) + { Active = active; Name = name; Keycode = keycode; @@ -68,10 +54,10 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Active, 'i'), - new OscArgument(Name, 's'), - new OscArgument(Keycode, 'i') + OscArgument.CreateFromParams(Active, 'i')!, + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Keycode, 'i')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtLight.cs b/VmcMessages/VmcExtLight.cs index 4bdb007..6e8f551 100644 --- a/VmcMessages/VmcExtLight.cs +++ b/VmcMessages/VmcExtLight.cs @@ -18,7 +18,6 @@ using Godot; using godotOscSharp; -using System.Collections.Generic; namespace godotVmcSharp { @@ -28,76 +27,13 @@ namespace godotVmcSharp public readonly Transform3D Transform; public readonly Color Color; - public VmcExtLight(OscMessage m) : base(m.Address) + public static VmcExtLight CreateFromMessage(OscMessage m) { - if (m.Data.Count != 12) + if (m.Data.Count < 12) { - GD.Print($"Invalid number of arguments for {base.Addr}. Expected 12, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "12", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.x", 'f', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.y", 'f', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.z", 'f', m.Data[3].Type)); - return; - } - if (m.Data[4].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.x", 'f', m.Data[4].Type)); - return; - } - if (m.Data[5].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.y", 'f', m.Data[5].Type)); - return; - } - if (m.Data[6].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.z", 'f', m.Data[6].Type)); - return; - } - if (m.Data[7].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.w", 'f', m.Data[7].Type)); - return; - } - if (m.Data[8].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "color.red", 'f', m.Data[8].Type)); - return; - } - if (m.Data[9].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "color.blue", 'f', m.Data[9].Type)); - return; - } - if (m.Data[10].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "color.green", 'f', m.Data[10].Type)); - return; - } - if (m.Data[11].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "color.alpha", 'f', m.Data[11].Type)); - return; - } - Name = (string)m.Data[0].Value; - Transform = new Transform3D(new Basis(new Quaternion((float)m.Data[4].Value, (float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value)), new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value)); - Color = new Color((float)m.Data[8].Value, (float)m.Data[9].Value, (float)m.Data[10].Value, (float)m.Data[11].Value); + return new VmcExtLight(m.Data[0].GetAsString(), new Transform3D(new Basis(new Quaternion(m.Data[4].GetAsFloat(), m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat())), new Color(m.Data[8].GetAsFloat(), m.Data[9].GetAsFloat(), m.Data[10].GetAsFloat(), m.Data[11].GetAsFloat())); } public VmcExtLight(string name, Transform3D transform, Color color) : base(new OscAddress("/VMC/Ext/Light")) @@ -110,20 +46,20 @@ namespace godotVmcSharp public new OscMessage ToMessage() { var quat = Transform.Basis.GetRotationQuaternion(); - return new OscMessage(Addr, new List{ - new OscArgument(Name, 's'), - new OscArgument(Transform.Origin.X, 'f'), - new OscArgument(Transform.Origin.Y, 'f'), - new OscArgument(Transform.Origin.Z, 'f'), - new OscArgument(quat.X, 'f'), - new OscArgument(quat.Y, 'f'), - new OscArgument(quat.Z, 'f'), - new OscArgument(quat.W, 'f'), - new OscArgument(Color.R, 'f'), - new OscArgument(Color.G, 'f'), - new OscArgument(Color.B, 'f'), - new OscArgument(Color.A, 'f') + return new OscMessage(Addr, new System.Collections.Generic.List{ + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Transform.Origin.X, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Y, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Z, 'f')!, + OscArgument.CreateFromParams(quat.X, 'f')!, + OscArgument.CreateFromParams(quat.Y, 'f')!, + OscArgument.CreateFromParams(quat.Z, 'f')!, + OscArgument.CreateFromParams(quat.W, 'f')!, + OscArgument.CreateFromParams(Color.R, 'f')!, + OscArgument.CreateFromParams(Color.G, 'f')!, + OscArgument.CreateFromParams(Color.B, 'f')!, + OscArgument.CreateFromParams(Color.A, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtMidiCcBit.cs b/VmcMessages/VmcExtMidiCcBit.cs index 8574740..fb7e6c3 100644 --- a/VmcMessages/VmcExtMidiCcBit.cs +++ b/VmcMessages/VmcExtMidiCcBit.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,34 +24,27 @@ namespace godotVmcSharp { public readonly int Knob; public readonly int Active; - public VmcExtMidiCcBit(OscMessage m) : base(m.Address) + public static VmcExtMidiCcBit CreateFromMessage(OscMessage m) { - if (m.Data[0].Type != 'i') + var active = m.Data[1].GetAsInt32(); + if (active < 0 || active > 1) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "knob", 'i', m.Data[0].Type)); - return; + throw new DataOutOfRangeException(m.Address, 1, "active", "{0, 1}", active); } - if (m.Data[1].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "active", 'i', m.Data[1].Type)); - return; - } - if ((int)m.Data[1].Value < 0 || (int)m.Data[1].Value > 1) - { - GD.Print($"Invalid value for \"active\" argument of {Addr}. Expected 0 or 1, received {(int)m.Data[1].Value}."); - return; - } - Knob = (int)m.Data[0].Value; - Active = (int)m.Data[1].Value; + return new VmcExtMidiCcBit(m.Data[0].GetAsInt32(), active); } - public VmcExtMidiCcBit(int knob, int active) : base(new OscAddress("/VMC/Ext/Midi/CC/Bit")) + public static VmcExtMidiCcBit CreateFromParams(int knob, int active) { if (active < 0 || active > 1) { - GD.Print($"Invalid value for \"active\" argument of {Addr}. Expected 0 or 1, received {active}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/Midi/CC/Bit", 1, "active", "{0, 1}", active); } + return new VmcExtMidiCcBit(knob, active); + } + + private VmcExtMidiCcBit(int knob, int active) : base(new OscAddress("/VMC/Ext/Midi/CC/Bit")) + { Knob = knob; Active = active; } @@ -60,9 +52,9 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Knob, 'i'), - new OscArgument(Active, 'i') + OscArgument.CreateFromParams(Knob, 'i')!, + OscArgument.CreateFromParams(Active, 'i')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtMidiCcVal.cs b/VmcMessages/VmcExtMidiCcVal.cs index 1f97f3c..57f5693 100644 --- a/VmcMessages/VmcExtMidiCcVal.cs +++ b/VmcMessages/VmcExtMidiCcVal.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,20 +24,13 @@ namespace godotVmcSharp { public readonly int Knob; public readonly float Value; - public VmcExtMidiCcVal(OscMessage m) : base(m.Address) + public static VmcExtMidiCcVal CreateFromMessage(OscMessage m) { - if (m.Data[0].Type != 'i') + if (m.Data.Count < 2) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "knob", 'i', m.Data[0].Type)); - return; + throw new MissingArgumentsException(m.Address, "{2}", m.Data.Count); } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "value", 'f', m.Data[1].Type)); - return; - } - Knob = (int)m.Data[0].Value; - Value = (int)m.Data[1].Value; + return new VmcExtMidiCcVal(m.Data[0].GetAsInt32(), m.Data[1].GetAsFloat()); } public VmcExtMidiCcVal(int knob, float value) : base(new OscAddress("/VMC/Ext/Midi/CC/Val")) @@ -50,9 +42,9 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Knob, 'i'), - new OscArgument(Value, 'f') + OscArgument.CreateFromParams(Knob, 'i')!, + OscArgument.CreateFromParams(Value, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtMidiNote.cs b/VmcMessages/VmcExtMidiNote.cs index d55697a..d797d6b 100644 --- a/VmcMessages/VmcExtMidiNote.cs +++ b/VmcMessages/VmcExtMidiNote.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -28,46 +27,31 @@ namespace godotVmcSharp public readonly int Note; public readonly float Velocity; - public VmcExtMidiNote(OscMessage m) : base(m.Address) + public static VmcExtMidiNote CreateFromMessage(OscMessage m) { - if (m.Data[0].Type != 'i') + if (m.Data.Count < 4) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "active", 'i', m.Data[0].Type)); - return; + throw new MissingArgumentsException(m.Address, "{4}", m.Data.Count); } - if (m.Data[1].Type != 'i') + var active = m.Data[0].GetAsInt32(); + if (active < 0 || active > 1) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "channel", 'i', m.Data[1].Type)); - return; + throw new DataOutOfRangeException(m.Address, 0, "active", "{0, 1}", active); } - if (m.Data[2].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "note", 'i', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "velocity", 'f', m.Data[4].Type)); - return; - } - if ((int)m.Data[0].Value < 0 || (int)m.Data[0].Value > 1) - { - GD.Print($"Invalid value for \"active\" 'i' argument of {Addr}. Expected 0 or 1, received {(int)m.Data[0].Value}"); - return; - } - Active = (int)m.Data[0].Value; - Channel = (int)m.Data[1].Value; - Note = (int)m.Data[2].Value; - Velocity = (int)m.Data[3].Value; + return new VmcExtMidiNote(active, m.Data[1].GetAsInt32(), m.Data[2].GetAsInt32(), m.Data[3].GetAsFloat()); } - public VmcExtMidiNote(int active, int channel, int note, float velocity) : base(new OscAddress("/VMC/Ext/Midi/Note")) + public static VmcExtMidiNote CreateFromParams(int active, int channel, int note, float velocity) { if (active < 0 || active > 1) { - GD.Print($"Invalid value for \"active\" 'i' argument of {Addr}. Expected 0 or 1, received {Active}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/Midi/Note", 0, "active", "{0, 1}", active); } + return new VmcExtMidiNote(active, channel, note, velocity); + } + + private VmcExtMidiNote(int active, int channel, int note, float velocity) : base(new OscAddress("/VMC/Ext/Midi/Note")) + { Active = active; Channel = channel; Note = note; @@ -77,11 +61,11 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Active, 'i'), - new OscArgument(Channel, 'i'), - new OscArgument(Note, 'i'), - new OscArgument(Velocity, 'f') + OscArgument.CreateFromParams(Active, 'i')!, + OscArgument.CreateFromParams(Channel, 'i')!, + OscArgument.CreateFromParams(Note, 'i')!, + OscArgument.CreateFromParams(Velocity, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtOk.cs b/VmcMessages/VmcExtOk.cs index be3b045..f229157 100644 --- a/VmcMessages/VmcExtOk.cs +++ b/VmcMessages/VmcExtOk.cs @@ -17,7 +17,6 @@ */ using godotOscSharp; -using Godot; using System.Collections.Generic; namespace godotVmcSharp @@ -29,163 +28,120 @@ namespace godotVmcSharp public readonly int? CalibrationMode; public readonly int? TrackingStatus; - public VmcExtOk(int loaded) : base(new OscAddress("/VMC/Ext/OK")) + public static VmcExtOk CreateFromParams(int loaded) { if (loaded < 0 || loaded > 1) { - GD.Print($"Invalid value for loaded status. Expected 0 or 1, received {loaded}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded); } + return new VmcExtOk(loaded); + } + + private VmcExtOk(int loaded): base(new OscAddress("/VMC/Ext/OK")) + { Loaded = loaded; } - public VmcExtOk(int loaded, int calibrationState, int calibrationMode) : base(new OscAddress("/VMC/Ext/OK")) + public static VmcExtOk CreateFromParams(int loaded, int calibrationState, int calibrationMode) { if (loaded < 0 || loaded > 1) { - GD.Print($"Invalid value for loaded status. Expected 0 or 1, received {loaded}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded); } if (calibrationState < 0 || calibrationState > 3) { - GD.Print($"Invalid value for calibration state. Expected 0-3, received {calibrationState}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState); } if (calibrationMode < 0 || calibrationMode > 2) { - GD.Print($"Invalid value for calibration mode. Expected 0-2, received {calibrationMode}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode); } + return new VmcExtOk(loaded, calibrationState, calibrationMode); + } + + private VmcExtOk(int loaded, int calibrationState, int calibrationMode) : base(new OscAddress("/VMC/Ext/OK")) + { Loaded = loaded; CalibrationState = calibrationState; CalibrationMode = calibrationMode; } - public VmcExtOk(int loaded, int calibrationState, int calibrationMode, int trackingStatus) : base(new OscAddress("/VMC/Ext/OK")) + public static VmcExtOk CreateFromParams(int loaded, int calibrationState, int calibrationMode, int trackingStatus) { if (loaded < 0 || loaded > 1) { - GD.Print($"Invalid value for loaded status. Expected 0 or 1, received {loaded}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded); } if (calibrationState < 0 || calibrationState > 3) { - GD.Print($"Invalid value for calibration state. Expected 0-3, received {calibrationState}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState); } if (calibrationMode < 0 || calibrationMode > 2) { - GD.Print($"Invalid value for calibration mode. Expected 0-2, received {calibrationMode}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode); } if (trackingStatus < 0 || trackingStatus > 1) { - GD.Print($"Invalid value for tracking status. Expected 0-1, received {trackingStatus}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 3, "trackingStatus", "{0, 1}", trackingStatus); } + return new VmcExtOk(loaded, calibrationState, calibrationMode, trackingStatus); + } + private VmcExtOk(int loaded, int calibrationState, int calibrationMode, int trackingStatus) : base(new OscAddress("/VMC/Ext/OK")) + { Loaded = loaded; CalibrationState = calibrationState; CalibrationMode = calibrationMode; TrackingStatus = trackingStatus; } - public VmcExtOk(OscMessage m) : base(m.Address) + public static VmcExtOk CreateFromMessage(OscMessage m) { switch (m.Data.Count) { case 1: - if (!OkParam0(m.Data[0])) - { - return; - } - Loaded = (int)m.Data[0].Value; - break; + return new VmcExtOk(getLoaded(m)); case 3: - if (!OkParam1And2(m.Data[0], m.Data[1], m.Data[2])) - { - return; - } - Loaded = (int)m.Data[0].Value; - CalibrationState = (int)m.Data[1].Value; - CalibrationMode = (int)m.Data[2].Value; - break; + return new VmcExtOk(getLoaded(m), getCalibrationState(m), getCalibrationMode(m)); case 4: - if (!OkParam3(m.Data[0], m.Data[1], m.Data[2], m.Data[3])) + var trackingStatus = m.Data[3].GetAsInt32(); + if (trackingStatus < 0 || trackingStatus > 1) { - return; + throw new DataOutOfRangeException("/VMC/Ext/OK", 3, "trackingStatus", "{0, 1}", trackingStatus); } - Loaded = (int)m.Data[0].Value; - CalibrationState = (int)m.Data[1].Value; - CalibrationMode = (int)m.Data[2].Value; - TrackingStatus = (int)m.Data[3].Value; - break; + return new VmcExtOk(getLoaded(m), getCalibrationState(m), getCalibrationMode(m), trackingStatus); default: - GD.Print($"Invalid number of arguments for /VMC/Ext/OK message. Expected 1, 3, or 4 but received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{1, 3, 4}", m.Data.Count); } } - private bool OkParam0(OscArgument arg) + private static int getLoaded(OscMessage m) { - if (arg.Type != 'i') + var loaded = m.Data[0].GetAsInt32(); + if (loaded < 0 || loaded > 1) { - GD.Print($"Invalid argument type for /VMC/Ext/OK message. Expected int in argument 0, received {arg.Type}"); - return false; + throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded); } - if ((int)arg.Value < 0 && (int)arg.Value > 1) - { - GD.Print($"Invalid value for loaded status. Expected 0-1, received {(int)arg.Value}"); - return false; - } - return true; + return loaded; } - private bool OkParam1And2(OscArgument arg0, OscArgument arg1, OscArgument arg2) + private static int getCalibrationState(OscMessage m) { - if (!OkParam0(arg0)) + var calibrationState = m.Data[1].GetAsInt32(); + if (calibrationState < 0 || calibrationState > 3) { - return false; + throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState); } - if (arg1.Type != 'i') - { - GD.Print($"Invalid argument type for /VMC/Ext/OK message. Expected int in argument 1, received {arg1.Type}"); - return false; - } - if (arg2.Type != 'i') - { - GD.Print($"Invalid argument type for /VMC/Ext/OK message. Expected int in argument 2, received {arg2.Type}"); - return false; - } - if ((int)arg1.Value < 0 && (int)arg1.Value > 3) - { - GD.Print($"Invalid value for calibration state. Expected 0-3, received {(int)arg1.Value}"); - return false; - } - if ((int)arg2.Value < 0 && (int)arg2.Value > 2) - { - GD.Print($"Invalid value for calibration mode. Expected 0-2, received {(int)arg2.Value}"); - return false; - } - return true; + return calibrationState; } - private bool OkParam3(OscArgument arg0, OscArgument arg1, OscArgument arg2, OscArgument arg) + private static int getCalibrationMode(OscMessage m) { - if (!OkParam1And2(arg0, arg1, arg2)) + var calibrationMode = m.Data[2].GetAsInt32(); + if (calibrationMode < 0 || calibrationMode > 2) { - return false; + throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode); } - if (arg.Type != 'i') - { - GD.Print($"Invalid argument type for /VMC/Ext/OK message. Expected int in argument 3, received {arg.Type}"); - return false; - } - if ((int)arg.Value < 0 && (int)arg.Value > 1) - { - GD.Print($"Invalid value for tracking status. Expected 0-1, received {(int)arg.Value}"); - return false; - } - return true; + return calibrationMode; } public new OscMessage ToMessage() @@ -193,23 +149,23 @@ namespace godotVmcSharp if (CalibrationState == null) { return new OscMessage(Addr, new List{ - new OscArgument(Loaded, 'i') + OscArgument.CreateFromParams(Loaded, 'i')! }); } if (TrackingStatus == null) { return new OscMessage(Addr, new List{ - new OscArgument(Loaded, 'i'), - new OscArgument(CalibrationState, 'i'), - new OscArgument(CalibrationMode, 'i') + OscArgument.CreateFromParams(Loaded, 'i')!, + OscArgument.CreateFromParams(CalibrationState, 'i')!, + OscArgument.CreateFromParams(CalibrationMode, 'i')! }); } return new OscMessage(Addr, new List{ - new OscArgument(Loaded, 'i'), - new OscArgument(CalibrationState, 'i'), - new OscArgument(CalibrationMode, 'i'), - new OscArgument(TrackingStatus, 'i') + OscArgument.CreateFromParams(Loaded, 'i')!, + OscArgument.CreateFromParams(CalibrationState, 'i')!, + OscArgument.CreateFromParams(CalibrationMode, 'i')!, + OscArgument.CreateFromParams(TrackingStatus, 'i')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtOpt.cs b/VmcMessages/VmcExtOpt.cs index 59dff8d..895770e 100644 --- a/VmcMessages/VmcExtOpt.cs +++ b/VmcMessages/VmcExtOpt.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,19 +24,13 @@ namespace godotVmcSharp { public readonly string option; - public VmcExtOpt(OscMessage m) : base(m.Address) + public static VmcExtOpt CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 1, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "1", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "option", 's', m.Data[0].Type)); - return; - } - option = (string)m.Data[0].Value; + return new VmcExtOpt(m.Data[0].GetAsString()); } public VmcExtOpt(string _option) : base(new OscAddress("/VMC/Ext/Opt")) @@ -47,7 +40,7 @@ namespace godotVmcSharp public new OscMessage ToMessage() { - return new OscMessage(Addr, new System.Collections.Generic.List{new OscArgument(option, 's')}); + return new OscMessage(Addr, new System.Collections.Generic.List{OscArgument.CreateFromParams(option, 's')!}); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtRcv.cs b/VmcMessages/VmcExtRcv.cs index 8ca3dbf..5a8da8f 100644 --- a/VmcMessages/VmcExtRcv.cs +++ b/VmcMessages/VmcExtRcv.cs @@ -16,88 +16,82 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; using System.Collections.Generic; +#nullable enable + namespace godotVmcSharp { public class VmcExtRcv : VmcMessage { public readonly int Enable; public readonly int Port; - public readonly string IpAddress; + public readonly string? IpAddress; - public VmcExtRcv(OscMessage m) : base(m.Address) + public static VmcExtRcv CreateFromMessage(OscMessage m) { - if (m.Data.Count > 3 || m.Data.Count < 2) + if (m.Data.Count < 2) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 2 or 3, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{2, 3}", m.Data.Count); } - if (m.Data[0].Type != 'i') + var enable = m.Data[0].GetAsInt32(); + if (enable < 0 || enable > 1) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "enable", 'i', m.Data[0].Type)); - return; + throw new DataOutOfRangeException(m.Address, 0, "enable", "{0, 1}", enable); } - if (m.Data[1].Type != 'i') + var port = m.Data[1].GetAsInt32(); + if (port < 0 || port > 65535) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "port", 'i', m.Data[0].Type)); - return; + throw new DataOutOfRangeException(m.Address, 1, "port", "{0..65535}", port); } - if ((int)m.Data[0].Value < 0 || (int)m.Data[0].Value > 1) - { - GD.Print($"Invalid value for \"enable\" argument of {Addr}. Expected 0 or 1, received {(int)m.Data[0].Value}."); - return; + if (m.Data.Count >= 2) { + return new VmcExtRcv(enable, port); } - if ((int)m.Data[1].Value < 0 || (int)m.Data[1].Value > 65535) - { - GD.Print($"Invalid value for \"port\" argument of {Addr}. Expected 0-65535, received {(int)m.Data[1].Value}."); - return; - } - Enable = (int)m.Data[0].Value; - Port = (int)m.Data[1].Value; - if (m.Data.Count != 3) - { - IpAddress = ""; - return; - } - if (m.Data[2].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "IpAddress", 's', m.Data[2].Type)); - return; - } - IpAddress = (string)m.Data[2].Value; + return new VmcExtRcv(enable, port, m.Data[2].GetAsString()); } - public VmcExtRcv(int enable, int port) : base(new OscAddress("/VMC/Ext/Rcv")) + public static VmcExtRcv CreateFromParams(int enable, int port) { if (enable < 0 || enable > 1) { - GD.Print($"Invalid value for \"enable\" argument of {Addr}. Expected 0 or 1, received {enable}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable); } if (port < 0 || port > 65535) { - GD.Print($"Invalid value for \"port\" argument of {Addr}. Expected 0-65535, received {port}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port); } + return new VmcExtRcv(enable, port); + } + + private VmcExtRcv(int enable, int port) : base(new OscAddress("/VMC/Ext/Rcv")) + { Enable = enable; Port = port; - IpAddress = ""; } - public VmcExtRcv(int enable, int port, string ipAddress) : base(new OscAddress("/VMC/Ext/Rcv")) + public static VmcExtRcv CreateFromMessage(int enable, int port, string ipAddress) { if (enable < 0 || enable > 1) { - GD.Print($"Invalid value for \"enable\" argument of {Addr}. Expected 0 or 1, received {enable}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable); } if (port < 0 || port > 65535) { - GD.Print($"Invalid value for \"port\" argument of {Addr}. Expected 0-65535, received {port}."); - return; + throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port); + } + return new VmcExtRcv(enable, port, ipAddress); + } + + private VmcExtRcv(int enable, int port, string ipAddress) : base(new OscAddress("/VMC/Ext/Rcv")) + { + if (enable < 0 || enable > 1) + { + throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable); + } + if (port < 0 || port > 65535) + { + throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port); } Enable = enable; Port = port; @@ -106,18 +100,18 @@ namespace godotVmcSharp public new OscMessage ToMessage() { - if (IpAddress == "") + if (IpAddress == null) { return new OscMessage(Addr, new List{ - new OscArgument(Enable, 'i'), - new OscArgument(Port, 'i'), + OscArgument.CreateFromParams(Enable, 'i')!, + OscArgument.CreateFromParams(Port, 'i')!, }); } return new OscMessage(Addr, new List{ - new OscArgument(Enable, 'i'), - new OscArgument(Port, 'i'), - new OscArgument(IpAddress, 's'), + OscArgument.CreateFromParams(Enable, 'i')!, + OscArgument.CreateFromParams(Port, 'i')!, + OscArgument.CreateFromParams(IpAddress, 's')!, }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtRemote.cs b/VmcMessages/VmcExtRemote.cs index 3bf6dff..8fc0e54 100644 --- a/VmcMessages/VmcExtRemote.cs +++ b/VmcMessages/VmcExtRemote.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -26,25 +25,13 @@ namespace godotVmcSharp public readonly string Service; public readonly string Json; - public VmcExtRemote(OscMessage m) : base(m.Address) + public static VmcExtRemote CreateFromMessage(OscMessage m) { - if (m.Data.Count != 2) + if (m.Data.Count < 2) { - GD.Print($"Invalid number of arguments for {Addr} message. Expected 2 but received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "2", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "service", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "json", 's', m.Data[1].Type)); - return; - } - Service = (string)m.Data[0].Value; - Json = (string)m.Data[1].Value; + return new VmcExtRemote(m.Data[0].GetAsString(), m.Data[1].GetAsString()); } public VmcExtRemote(string service, string json) : base(new OscAddress("/VMC/Ext/Remote")) @@ -56,9 +43,9 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Service, 's'), - new OscArgument(Json, 's') + OscArgument.CreateFromParams(Service, 's')!, + OscArgument.CreateFromParams(Json, 's')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtRootPos.cs b/VmcMessages/VmcExtRootPos.cs index 15d249d..9acd74c 100644 --- a/VmcMessages/VmcExtRootPos.cs +++ b/VmcMessages/VmcExtRootPos.cs @@ -30,31 +30,16 @@ namespace godotVmcSharp public readonly Vector3? Scale; public readonly Vector3? Offset; - public VmcExtRootPos(OscMessage m) : base(m.Address) + public static VmcExtRootPos CreateFromMessage(OscMessage m) { switch (m.Data.Count) { case 8: - if (!Transform8(m.Data)) - { - return; - } - Name = (string)m.Data[0].Value; - Transform = new Transform3D(new Basis(new Quaternion((float)m.Data[4].Value, (float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value)), new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value)); - break; + return new VmcExtRootPos(m.Data[0].GetAsString(), new Transform3D(new Basis(new Quaternion(m.Data[4].GetAsFloat(), m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat()))); case 14: - if (!Transform14(m.Data)) - { - return; - } - Name = (string)m.Data[0].Value; - Transform = new Transform3D(new Basis(new Quaternion((float)m.Data[4].Value, (float)m.Data[5].Value, (float)m.Data[6].Value, (float)m.Data[7].Value)), new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value)); - Scale = new Vector3((float)m.Data[8].Value, (float)m.Data[9].Value, (float)m.Data[10].Value); - Offset = new Vector3((float)m.Data[11].Value, (float)m.Data[12].Value, (float)m.Data[13].Value); - break; + return new VmcExtRootPos(m.Data[0].GetAsString(), new Transform3D(new Basis(new Quaternion(m.Data[4].GetAsFloat(), m.Data[5].GetAsFloat(), m.Data[6].GetAsFloat(), m.Data[7].GetAsFloat())), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat())), new Vector3(m.Data[8].GetAsFloat(), m.Data[9].GetAsFloat(), m.Data[10].GetAsFloat()), new Vector3(m.Data[11].GetAsFloat(), m.Data[12].GetAsFloat(), m.Data[13].GetAsFloat())); default: - GD.Print($"Invalid number of arguments for {Addr}. Expected 8 or 14, received {m.Data.Count}."); - return; + throw new MissingArgumentsException(m.Address, "{8, 14}", m.Data.Count); } } @@ -72,121 +57,37 @@ namespace godotVmcSharp Offset = offset; } - private bool Transform8(List data) - { - if (data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "name", 's', data[0].Type)); - return false; - } - if (data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.x", 'f', data[1].Type)); - return false; - } - if (data[2].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.y", 'f', data[2].Type)); - return false; - } - if (data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.z", 'f', data[3].Type)); - return false; - } - if (data[4].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.x", 'f', data[4].Type)); - return false; - } - if (data[5].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.y", 'f', data[5].Type)); - return false; - } - if (data[6].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.z", 'f', data[6].Type)); - return false; - } - if (data[7].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "q.w", 'f', data[7].Type)); - return false; - } - return true; - } - - private bool Transform14(List data) - { - if (!Transform8(data)) - { - return false; - } - if (data[8].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "s.x", 'f', data[8].Type)); - return false; - } - if (data[9].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "s.y", 'f', data[9].Type)); - return false; - } - if (data[10].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "s.z", 'f', data[10].Type)); - return false; - } - if (data[11].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "o.x", 'f', data[11].Type)); - return false; - } - if (data[12].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "o.y", 'f', data[12].Type)); - return false; - } - if (data[13].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "o.z", 'f', data[13].Type)); - return false; - } - return true; - } - public new OscMessage ToMessage() { var quat = Transform.Basis.GetRotationQuaternion(); if (!Scale.HasValue) { return new OscMessage(Addr, new List{ - new OscArgument(Name, 's'), - new OscArgument(Transform.Origin.X, 'f'), - new OscArgument(Transform.Origin.Y, 'f'), - new OscArgument(Transform.Origin.Z, 'f'), - new OscArgument(quat.X, 'f'), - new OscArgument(quat.Y, 'f'), - new OscArgument(quat.Z, 'f'), - new OscArgument(quat.W, 'f'), + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Transform.Origin.X, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Y, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Z, 'f')!, + OscArgument.CreateFromParams(quat.X, 'f')!, + OscArgument.CreateFromParams(quat.Y, 'f')!, + OscArgument.CreateFromParams(quat.Z, 'f')!, + OscArgument.CreateFromParams(quat.W, 'f')!, }); } return new OscMessage(Addr, new List{ - new OscArgument(Name, 's'), - new OscArgument(Transform.Origin.X, 'f'), - new OscArgument(Transform.Origin.Y, 'f'), - new OscArgument(Transform.Origin.Z, 'f'), - new OscArgument(quat.X, 'f'), - new OscArgument(quat.Y, 'f'), - new OscArgument(quat.Z, 'f'), - new OscArgument(quat.W, 'f'), - new OscArgument(Scale.Value.X, 'f'), - new OscArgument(Scale.Value.Y, 'f'), - new OscArgument(Scale.Value.Z, 'f'), - new OscArgument(Offset.Value.X, 'f'), - new OscArgument(Offset.Value.Y, 'f'), - new OscArgument(Offset.Value.Z, 'f'), + OscArgument.CreateFromParams(Name, 's')!, + OscArgument.CreateFromParams(Transform.Origin.X, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Y, 'f')!, + OscArgument.CreateFromParams(Transform.Origin.Z, 'f')!, + OscArgument.CreateFromParams(quat.X, 'f')!, + OscArgument.CreateFromParams(quat.Y, 'f')!, + OscArgument.CreateFromParams(quat.Z, 'f')!, + OscArgument.CreateFromParams(quat.W, 'f')!, + OscArgument.CreateFromParams(Scale!.Value.X, 'f')!, + OscArgument.CreateFromParams(Scale!.Value.Y, 'f')!, + OscArgument.CreateFromParams(Scale!.Value.Z, 'f')!, + OscArgument.CreateFromParams(Offset!.Value.X, 'f')!, + OscArgument.CreateFromParams(Offset!.Value.Y, 'f')!, + OscArgument.CreateFromParams(Offset!.Value.Z, 'f')!, }); } } diff --git a/VmcMessages/VmcExtSetCalibExec.cs b/VmcMessages/VmcExtSetCalibExec.cs index f503412..d7f6ef1 100644 --- a/VmcMessages/VmcExtSetCalibExec.cs +++ b/VmcMessages/VmcExtSetCalibExec.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,39 +24,37 @@ namespace godotVmcSharp { public readonly int Mode; - public VmcExtSetCalibExec(OscMessage m) : base(m.Address) + public static VmcExtSetCalibExec CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 1, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{1}", m.Data.Count); } - if (m.Data[0].Type != 'i') + var mode = m.Data[0].GetAsInt32(); + if (mode < 0 || mode > 2) { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "mode", 'i', m.Data[0].Type)); - return; + throw new DataOutOfRangeException(m.Address, 0, "mode", "{0..2}", mode); } - if ((int)m.Data[0].Value < 0 || (int)m.Data[0].Value > 2) - { - GD.Print($"Invalid value for argument \"mode\" of \"/VMC/Ext/Set/Calib/Exec\". Expected in range 0-2, received {(int)m.Data[0].Value}"); - return; - } - Mode = (int)m.Data[0].Value; + return new VmcExtSetCalibExec(mode); } - public VmcExtSetCalibExec(int mode) : base(new OscAddress("/VMC/Ext/Set/Calib/Exec")) + public static VmcExtSetCalibExec CreateFromParams(int mode) { if (mode < 0 || mode > 2) { - GD.Print($"Invalid value for argument \"mode\" of \"/VMC/Ext/Set/Calib/Exec\". Expected in range 0-2, received {mode}"); - return; + throw new DataOutOfRangeException("/VMC/Ext/Set/Calib/Exec", 0, "mode", "{0..2}", mode); } + return new VmcExtSetCalibExec(mode); + } + + private VmcExtSetCalibExec(int mode) : base(new OscAddress("/VMC/Ext/Set/Calib/Exec")) + { Mode = mode; } public new OscMessage ToMessage() { - return new OscMessage(Addr, new System.Collections.Generic.List{new OscArgument(Mode, 'i')}); + return new OscMessage(Addr, new System.Collections.Generic.List{OscArgument.CreateFromParams(Mode, 'i')!}); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSetConfig.cs b/VmcMessages/VmcExtSetConfig.cs index 66b7f71..7b17301 100644 --- a/VmcMessages/VmcExtSetConfig.cs +++ b/VmcMessages/VmcExtSetConfig.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,19 +24,13 @@ namespace godotVmcSharp { public readonly string Path; - public VmcExtSetConfig(OscMessage m) : base(m.Address) + public static VmcExtSetConfig CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 1, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{1}", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "path", 's', m.Data[0].Type)); - return; - } - Path = (string)m.Data[0].Value; + return new VmcExtSetConfig(m.Data[0].GetAsString()); } public VmcExtSetConfig(string path) : base(new OscAddress("/VMC/Ext/Set/Config")) @@ -47,7 +40,7 @@ namespace godotVmcSharp public new OscMessage ToMessage() { - return new OscMessage(Addr, new System.Collections.Generic.List{new OscArgument(Path, 's')}); + return new OscMessage(Addr, new System.Collections.Generic.List{OscArgument.CreateFromParams(Path, 's')!}); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSetEye.cs b/VmcMessages/VmcExtSetEye.cs index 5c29a25..e8ebd8d 100644 --- a/VmcMessages/VmcExtSetEye.cs +++ b/VmcMessages/VmcExtSetEye.cs @@ -26,35 +26,29 @@ namespace godotVmcSharp public readonly int Enable; public readonly Vector3 Position; - public VmcExtSetEye(OscMessage m) : base(m.Address) + public static VmcExtSetEye CreateFromMessage(OscMessage m) { - if (m.Data.Count != 4) + if (m.Data.Count < 4) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 4, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{4}", m.Data.Count); } if (m.Data[0].Type != 'i') { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "enable", 'i', m.Data[0].Type)); - return; + throw new InvalidDataTypeException(m.Address, 0, "enable", 'i', m.Data[0].Type); } if (m.Data[1].Type != 'f') { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.x", 'f', m.Data[1].Type)); - return; + throw new InvalidDataTypeException(m.Address, 1, "p.x", 'f', m.Data[1].Type); } if (m.Data[2].Type != 'f') { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.y", 'f', m.Data[2].Type)); - return; + throw new InvalidDataTypeException(m.Address, 2, "p.y", 'f', m.Data[2].Type); } if (m.Data[3].Type != 'f') { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "p.z", 'f', m.Data[3].Type)); - return; + throw new InvalidDataTypeException(m.Address, 3, "p.z", 'f', m.Data[3].Type); } - Enable = (int)m.Data[0].Value; - Position = new Vector3((float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value); + return new VmcExtSetEye(m.Data[0].GetAsInt32(), new Vector3(m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat())); } public VmcExtSetEye(int enable, Vector3 position) : base(new OscAddress("/VMC/Ext/Set/Eye")) @@ -66,11 +60,11 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Enable, 'i'), - new OscArgument(Position.X, 'f'), - new OscArgument(Position.Y, 'f'), - new OscArgument(Position.Z, 'f'), + OscArgument.CreateFromParams(Enable, 'i')!, + OscArgument.CreateFromParams(Position.X, 'f')!, + OscArgument.CreateFromParams(Position.Y, 'f')!, + OscArgument.CreateFromParams(Position.Z, 'f')!, }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSetPeriod.cs b/VmcMessages/VmcExtSetPeriod.cs index a3cec60..6ff258a 100644 --- a/VmcMessages/VmcExtSetPeriod.cs +++ b/VmcMessages/VmcExtSetPeriod.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -30,49 +29,13 @@ namespace godotVmcSharp public readonly int Camera; public readonly int Devices; - public VmcExtSetPeriod(OscMessage m) : base(m.Address) + public static VmcExtSetPeriod CreateFromMessage(OscMessage m) { - if (m.Data.Count != 6) + if (m.Data.Count < 6) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 6, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{6}", m.Data.Count); } - if (m.Data[0].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "status", 'i', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "root", 'i', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "bone", 'i', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "blendShape", 'i', m.Data[3].Type)); - return; - } - if (m.Data[4].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "camera", 'i', m.Data[4].Type)); - return; - } - if (m.Data[5].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "devices", 'i', m.Data[5].Type)); - return; - } - Status = (int)m.Data[0].Value; - Root = (int)m.Data[1].Value; - Bone = (int)m.Data[2].Value; - BlendShape = (int)m.Data[3].Value; - Camera = (int)m.Data[4].Value; - Devices = (int)m.Data[5].Value; + return new VmcExtSetPeriod(m.Data[0].GetAsInt32(), m.Data[1].GetAsInt32(), m.Data[2].GetAsInt32(), m.Data[3].GetAsInt32(), m.Data[4].GetAsInt32(), m.Data[5].GetAsInt32()); } public VmcExtSetPeriod(int status, int root, int bone, int blendShape, int camera, int devices) : base(new OscAddress("/VMC/Ext/Set/Period")) @@ -88,13 +51,13 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Status, 'i'), - new OscArgument(Root, 'i'), - new OscArgument(Bone, 'i'), - new OscArgument(BlendShape, 'i'), - new OscArgument(Camera, 'i'), - new OscArgument(Devices, 'i'), + OscArgument.CreateFromParams(Status, 'i')!, + OscArgument.CreateFromParams(Root, 'i')!, + OscArgument.CreateFromParams(Bone, 'i')!, + OscArgument.CreateFromParams(BlendShape, 'i')!, + OscArgument.CreateFromParams(Camera, 'i')!, + OscArgument.CreateFromParams(Devices, 'i')!, }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSetRes.cs b/VmcMessages/VmcExtSetRes.cs index e5e295c..2e909a2 100644 --- a/VmcMessages/VmcExtSetRes.cs +++ b/VmcMessages/VmcExtSetRes.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; @@ -26,19 +25,13 @@ namespace godotVmcSharp { public readonly string Response; - public VmcExtSetRes(OscMessage m) : base(m.Address) + public static VmcExtSetRes CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 1, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{1}", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "response", 's', m.Data[0].Type)); - return; - } - Response = (string)m.Data[0].Value; + return new VmcExtSetRes(m.Data[0].GetAsString()); } public VmcExtSetRes(string response) : base(new OscAddress("/VMC/Ext/Set/Res")) @@ -48,7 +41,7 @@ namespace godotVmcSharp public new OscMessage ToMessage() { - return new OscMessage(Addr, new System.Collections.Generic.List{new OscArgument(Response, 's')}); + return new OscMessage(Addr, new System.Collections.Generic.List{OscArgument.CreateFromParams(Response, 's')!}); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSetShortcut.cs b/VmcMessages/VmcExtSetShortcut.cs index e7cf77a..4e6ef0f 100644 --- a/VmcMessages/VmcExtSetShortcut.cs +++ b/VmcMessages/VmcExtSetShortcut.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -25,19 +24,13 @@ namespace godotVmcSharp { public readonly string Shortcut; - public VmcExtSetShortcut(OscMessage m) : base(m.Address) + public static VmcExtSetShortcut CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 1, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{1}", m.Data.Count); } - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "shortcut", 's', m.Data[0].Type)); - return; - } - Shortcut = (string)m.Data[0].Value; + return new VmcExtSetShortcut(m.Data[0].GetAsString()); } public VmcExtSetShortcut(string shortcut) : base(new OscAddress("/VMC/Ext/Set/Shortcut")) @@ -47,7 +40,7 @@ namespace godotVmcSharp public new OscMessage ToMessage() { - return new OscMessage(Addr, new System.Collections.Generic.List{new OscArgument(Shortcut, 's')}); + return new OscMessage(Addr, new System.Collections.Generic.List{OscArgument.CreateFromParams(Shortcut, 's')!}); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSettingColor.cs b/VmcMessages/VmcExtSettingColor.cs index bbf3ed1..1e99116 100644 --- a/VmcMessages/VmcExtSettingColor.cs +++ b/VmcMessages/VmcExtSettingColor.cs @@ -25,34 +25,13 @@ namespace godotVmcSharp { public readonly Color Color; - public VmcExtSettingColor(OscMessage m) : base(m.Address) + public static VmcExtSettingColor CreateFromMessage(OscMessage m) { - if (m.Data.Count != 4) + if (m.Data.Count < 4) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 4, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "{4}", m.Data.Count); } - if (m.Data[0].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "r", 'f', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "g", 'f', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "b", 'f', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'f') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "a", 'f', m.Data[3].Type)); - return; - } - Color = new Godot.Color((float)m.Data[0].Value, (float)m.Data[1].Value, (float)m.Data[2].Value, (float)m.Data[3].Value); + return new VmcExtSettingColor(new Godot.Color(m.Data[0].GetAsFloat(), m.Data[1].GetAsFloat(), m.Data[2].GetAsFloat(), m.Data[3].GetAsFloat())); } public VmcExtSettingColor(Color color) : base(new OscAddress("/VMC/Ext/Setting/Color")) @@ -63,11 +42,11 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Color.R, 'f'), - new OscArgument(Color.G, 'f'), - new OscArgument(Color.B, 'f'), - new OscArgument(Color.A, 'f') + OscArgument.CreateFromParams(Color.R, 'f')!, + OscArgument.CreateFromParams(Color.G, 'f')!, + OscArgument.CreateFromParams(Color.B, 'f')!, + OscArgument.CreateFromParams(Color.A, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtSettingWin.cs b/VmcMessages/VmcExtSettingWin.cs index 1f5042b..c86dc55 100644 --- a/VmcMessages/VmcExtSettingWin.cs +++ b/VmcMessages/VmcExtSettingWin.cs @@ -16,7 +16,6 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; namespace godotVmcSharp @@ -28,81 +27,58 @@ namespace godotVmcSharp public readonly int WindowClickThrough; public readonly int HideBorder; - public VmcExtSettingWin(OscMessage m) : base(m.Address) + public static VmcExtSettingWin CreateFromMessage(OscMessage m) { - if (m.Data.Count != 4) + if (m.Data.Count < 4) { - GD.Print($"Invalid number of arguments for {Addr}. Expecting 4, received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "4", m.Data.Count); } - if (m.Data[0].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "isTopMost", 'i', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "isTransparent", 'i', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "windowClickThrough", 'i', m.Data[2].Type)); - return; - } - if (m.Data[3].Type != 'i') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "hideBorder", 'i', m.Data[3].Type)); - return; - } - if ((int)m.Data[0].Value < 0 || (int)m.Data[0].Value > 1) - { - GD.Print($"Invalid value for \"isTopMost\" 'i' argument of {Addr}. Expected 0 or 1, received {(int)m.Data[0].Value}"); - return; - } - if ((int)m.Data[1].Value < 0 || (int)m.Data[1].Value > 1) - { - GD.Print($"Invalid value for \"isTransparent\" 'i' argument of {Addr}. Expected 0 or 1, received {(int)m.Data[1].Value}"); - return; - } - if ((int)m.Data[2].Value < 0 || (int)m.Data[2].Value > 1) - { - GD.Print($"Invalid value for \"windowClickThrough\" 'i' argument of {Addr}. Expected 0 or 1, received {(int)m.Data[2].Value}"); - return; - } - if ((int)m.Data[3].Value < 0 || (int)m.Data[3].Value > 1) - { - GD.Print($"Invalid value for \"hideBorder\" 'i' argument of {Addr}. Expected 0 or 1, received {(int)m.Data[3].Value}"); - return; - } - IsTopMost = (int)m.Data[0].Value; - IsTransparent = (int)m.Data[1].Value; - WindowClickThrough = (int)m.Data[2].Value; - HideBorder = (int)m.Data[3].Value; - } - - public VmcExtSettingWin(int isTopMost, int isTransparent, int windowClickThrough, int hideBorder) : base(new OscAddress("/VMC/Ext/Setting/Win")) - { + var isTopMost = m.Data[0].GetAsInt32(); + var isTransparent = m.Data[1].GetAsInt32(); + var windowClickThrough = m.Data[2].GetAsInt32(); + var hideBorder = m.Data[3].GetAsInt32(); if (isTopMost < 0 || isTopMost > 1) { - GD.Print($"Invalid value for \"isTopMost\" 'i' argument of {Addr}. Expected 0 or 1, received {isTopMost}"); - return; + throw new DataOutOfRangeException(m.Address, 0, "isTopMost", "{0, 1}", isTopMost); } if (isTransparent < 0 || isTransparent > 1) { - GD.Print($"Invalid value for \"isTransparent\" 'i' argument of {Addr}. Expected 0 or 1, received {isTransparent}"); - return; + throw new DataOutOfRangeException(m.Address, 1, "isTransparent", "{0, 1}", isTransparent); } if (windowClickThrough < 0 || windowClickThrough > 1) { - GD.Print($"Invalid value for \"windowClickThrough\" 'i' argument of {Addr}. Expected 0 or 1, received {windowClickThrough}"); - return; + throw new DataOutOfRangeException(m.Address, 2, "windowClickThrough", "{0, 1}", windowClickThrough); } if (hideBorder < 0 || hideBorder > 1) { - GD.Print($"Invalid value for \"hideBorder\" 'i' argument of {Addr}. Expected 0 or 1, received {hideBorder}"); - return; + throw new DataOutOfRangeException(m.Address, 3, "hideBorder", "{0, 1}", hideBorder); } + return new VmcExtSettingWin(isTopMost, isTransparent, windowClickThrough, hideBorder); + } + + public static VmcExtSettingWin CreateFromParams(int isTopMost, int isTransparent, int windowClickThrough, int hideBorder) + { + if (isTopMost < 0 || isTopMost > 1) + { + throw new DataOutOfRangeException("/VMC/Ext/Setting/Win", 0, "isTopMost", "{0, 1}", isTopMost); + } + if (isTransparent < 0 || isTransparent > 1) + { + throw new DataOutOfRangeException("/VMC/Ext/Setting/Win", 1, "isTransparent", "{0, 1}", isTransparent); + } + if (windowClickThrough < 0 || windowClickThrough > 1) + { + throw new DataOutOfRangeException("/VMC/Ext/Setting/Win", 2, "windowClickThrough", "{0, 1}", windowClickThrough); + } + if (hideBorder < 0 || hideBorder > 1) + { + throw new DataOutOfRangeException("/VMC/Ext/Setting/Win", 3, "hideBorder", "{0, 1}", hideBorder); + } + return new VmcExtSettingWin(isTopMost, isTransparent, windowClickThrough, hideBorder); + } + + private VmcExtSettingWin(int isTopMost, int isTransparent, int windowClickThrough, int hideBorder) : base(new OscAddress("/VMC/Ext/Setting/Win")) + { IsTopMost = isTopMost; IsTransparent = isTransparent; WindowClickThrough = windowClickThrough; @@ -112,11 +88,11 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(IsTopMost, 'i'), - new OscArgument(IsTransparent, 'i'), - new OscArgument(WindowClickThrough, 'i'), - new OscArgument(HideBorder, 'i') + OscArgument.CreateFromParams(IsTopMost, 'i')!, + OscArgument.CreateFromParams(IsTransparent, 'i')!, + OscArgument.CreateFromParams(WindowClickThrough, 'i')!, + OscArgument.CreateFromParams(HideBorder, 'i')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtT.cs b/VmcMessages/VmcExtT.cs index 906ebdd..3004070 100644 --- a/VmcMessages/VmcExtT.cs +++ b/VmcMessages/VmcExtT.cs @@ -17,26 +17,19 @@ */ using godotOscSharp; -using Godot; namespace godotVmcSharp { public class VmcExtT : VmcMessage { public readonly float Time; - public VmcExtT(OscMessage m) : base(m.Address) + public static VmcExtT CreateFromMessage(OscMessage m) { - if (m.Data.Count != 1) + if (m.Data.Count < 1) { - GD.Print($"Invalid number of arguments for /VMC/Ext/T message. Expected 1 but received {m.Data.Count}"); - return; + throw new MissingArgumentsException(m.Address, "1", m.Data.Count); } - if (m.Data[0].Type != 'f') - { - InvalidArgumentType.GetErrorString(m.Address.ToString(), "time", 'f', m.Data[0].Type); - return; - } - Time = (float)m.Data[0].Value; + return new VmcExtT(m.Data[0].GetAsFloat()); } public VmcExtT(float time) : base(new OscAddress("/VMC/Ext/T")) @@ -47,8 +40,8 @@ namespace godotVmcSharp public new OscMessage ToMessage() { return new OscMessage(Addr, new System.Collections.Generic.List{ - new OscArgument(Time, 'f') + OscArgument.CreateFromParams(Time, 'f')! }); } } -} \ No newline at end of file +} diff --git a/VmcMessages/VmcExtVrm.cs b/VmcMessages/VmcExtVrm.cs index 784f5d9..464c30f 100644 --- a/VmcMessages/VmcExtVrm.cs +++ b/VmcMessages/VmcExtVrm.cs @@ -16,60 +16,30 @@ along with this program. If not, see . */ -using Godot; using godotOscSharp; using System.Collections.Generic; +#nullable enable + namespace godotVmcSharp { public class VmcExtVrm : VmcMessage { public readonly string Path; public readonly string Title; - public readonly string Hash; + public readonly string? Hash; - public VmcExtVrm(OscMessage m) : base(m.Address) + public static VmcExtVrm? CreateFromMessage(OscMessage m) { switch (m.Data.Count) { case 2: - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "path", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "title", 's', m.Data[1].Type)); - return; - } - Path = (string)m.Data[0].Value; - Title = (string)m.Data[1].Value; - Hash = ""; - break; - case 3: - if (m.Data[0].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "path", 's', m.Data[0].Type)); - return; - } - if (m.Data[1].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "title", 's', m.Data[1].Type)); - return; - } - if (m.Data[2].Type != 's') - { - GD.Print(InvalidArgumentType.GetErrorString(Addr, "hash", 's', m.Data[1].Type)); - return; - } - Path = (string)m.Data[0].Value; - Title = (string)m.Data[1].Value; - Hash = (string)m.Data[2].Value; - break; + return new VmcExtVrm(m.Data[0].GetAsString(), m.Data[1].GetAsString()); + case 1: + case 0: + throw new MissingArgumentsException(m.Address, "2 <= count <= 3", m.Data.Count); default: - GD.Print($"Invalid number of arguments for {Addr} message. Expected 2 or 3 but received {m.Data.Count}"); - return; + return new VmcExtVrm(m.Data[0].GetAsString(), m.Data[1].GetAsString(), m.Data[2].GetAsString()); } } @@ -77,7 +47,6 @@ namespace godotVmcSharp { Path = path; Title = title; - Hash = ""; } public VmcExtVrm(string path, string title, string hash) : base(new OscAddress("/VMC/Ext/VRM")) @@ -92,15 +61,15 @@ namespace godotVmcSharp if (Hash == null) { return new OscMessage(Addr, new List{ - new OscArgument(Path, 's'), - new OscArgument(Title, 's') + OscArgument.CreateFromParams(Path, 's')!, + OscArgument.CreateFromParams(Title, 's')! }); } return new OscMessage(Addr, new List{ - new OscArgument(Path, 's'), - new OscArgument(Title, 's'), - new OscArgument(Hash, 's') + OscArgument.CreateFromParams(Path, 's')!, + OscArgument.CreateFromParams(Title, 's')!, + OscArgument.CreateFromParams(Hash, 's')! }); } } -} \ No newline at end of file +}