godotVmcSharp/VmcMessages/VmcExtRcv.cs

118 lines
4.1 KiB
C#
Raw Normal View History

/*
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 <https://www.gnu.org/licenses/>.
*/
using godotOscSharp;
2023-08-17 15:39:28 +02:00
using System.Collections.Generic;
#nullable enable
namespace godotVmcSharp
{
public class VmcExtRcv : VmcMessage
{
2023-09-24 19:06:26 +02:00
public readonly int Enable;
public readonly int Port;
public readonly string? IpAddress;
public static VmcExtRcv CreateFromMessage(OscMessage m)
{
if (m.Data.Count < 2)
{
throw new MissingArgumentsException(m.Address, "{2, 3}", m.Data.Count);
}
var enable = m.Data[0].GetAsInt32();
if (enable < 0 || enable > 1)
{
throw new DataOutOfRangeException(m.Address, 0, "enable", "{0, 1}", enable);
}
var port = m.Data[1].GetAsInt32();
if (port < 0 || port > 65535)
{
throw new DataOutOfRangeException(m.Address, 1, "port", "{0..65535}", port);
}
if (m.Data.Count >= 2) {
return new VmcExtRcv(enable, port);
}
return new VmcExtRcv(enable, port, m.Data[2].GetAsString());
}
public static VmcExtRcv CreateFromParams(int enable, int port)
{
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);
}
return new VmcExtRcv(enable, port);
}
2023-08-17 15:37:17 +02:00
private VmcExtRcv(int enable, int port) : base(new OscAddress("/VMC/Ext/Rcv"))
{
Enable = enable;
Port = port;
}
public static VmcExtRcv CreateFromMessage(int enable, int port, string ipAddress)
2023-08-17 15:37:17 +02:00
{
if (enable < 0 || enable > 1)
2023-08-17 15:37:17 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable);
2023-08-17 15:37:17 +02:00
}
if (port < 0 || port > 65535)
2023-08-17 15:37:17 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port);
2023-08-17 15:37:17 +02:00
}
return new VmcExtRcv(enable, port, ipAddress);
2023-08-17 15:37:17 +02:00
}
private VmcExtRcv(int enable, int port, string ipAddress) : base(new OscAddress("/VMC/Ext/Rcv"))
2023-08-17 15:37:17 +02:00
{
if (enable < 0 || enable > 1)
2023-08-17 15:37:17 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable);
2023-08-17 15:37:17 +02:00
}
if (port < 0 || port > 65535)
2023-08-17 15:37:17 +02:00
{
throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port);
2023-08-17 15:37:17 +02:00
}
Enable = enable;
Port = port;
IpAddress = ipAddress;
2023-08-17 15:37:17 +02:00
}
2023-08-17 15:39:28 +02:00
public new OscMessage ToMessage()
2023-08-17 15:39:28 +02:00
{
if (IpAddress == null)
2023-08-17 15:39:28 +02:00
{
2023-08-18 00:22:27 +02:00
return new OscMessage(Addr, new List<OscArgument>{
OscArgument.CreateFromParams(Enable, 'i')!,
OscArgument.CreateFromParams(Port, 'i')!,
2023-08-17 15:39:28 +02:00
});
}
2023-08-18 00:22:27 +02:00
return new OscMessage(Addr, new List<OscArgument>{
OscArgument.CreateFromParams(Enable, 'i')!,
OscArgument.CreateFromParams(Port, 'i')!,
OscArgument.CreateFromParams(IpAddress, 's')!,
2023-08-17 15:39:28 +02:00
});
}
}
}