2023-08-15 21:36:55 +02:00
|
|
|
/*
|
|
|
|
godotOscSharp
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2023-08-15 21:29:53 +02:00
|
|
|
using System;
|
|
|
|
|
2023-10-21 15:14:35 +02:00
|
|
|
#nullable enable
|
|
|
|
|
2023-08-15 21:29:53 +02:00
|
|
|
namespace godotOscSharp
|
|
|
|
{
|
|
|
|
// A class that represents a DWord
|
|
|
|
public class OscArgument
|
|
|
|
{
|
|
|
|
// The value of the DWord as an unsigned integer
|
|
|
|
public char Type { get; }
|
2023-10-21 15:14:35 +02:00
|
|
|
private object? Value { get; }
|
|
|
|
|
|
|
|
public static OscArgument CreateFromParams(object? value, char type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case 'i':
|
|
|
|
case 'f':
|
|
|
|
case 's':
|
|
|
|
case 'h':
|
|
|
|
case 'd':
|
|
|
|
return new OscArgument(value, type);
|
|
|
|
case 'T':
|
|
|
|
return new OscArgument(true, type);
|
|
|
|
case 'F':
|
|
|
|
return new OscArgument(false, type);
|
|
|
|
case 'N':
|
|
|
|
return new OscArgument(null, type);
|
|
|
|
default:
|
|
|
|
throw new ArgumentException($"Invalid argument type: {type}");
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 21:29:53 +02:00
|
|
|
|
|
|
|
// The constructor that takes an unsigned integer as the value
|
2023-10-21 15:14:35 +02:00
|
|
|
private OscArgument(object? value, char type)
|
2023-08-15 21:29:53 +02:00
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
Type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A method that parses a byte array to a DWord
|
|
|
|
public static OscArgument Parse(byte[] data, ref int index, char type)
|
|
|
|
{
|
|
|
|
// Use BitConverter to get the unsigned integer from the bytes at the given index in little-endian order
|
2023-10-21 15:14:35 +02:00
|
|
|
object? value = null;
|
2023-08-15 21:29:53 +02:00
|
|
|
var start = index;
|
|
|
|
switch (type) {
|
|
|
|
case 'i':
|
|
|
|
value = BitConverter.ToInt32(data, index);
|
|
|
|
index += 4;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
value = BitConverter.ToSingle(data, index);
|
|
|
|
index += 4;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
while (data[index] != 0) // Find the null terminator
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
value = System.Text.Encoding.ASCII.GetString(data, start, index - start);
|
|
|
|
while (data[index] == 0 && index < data.Length)
|
|
|
|
{
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
value = BitConverter.ToInt64(data, index);
|
|
|
|
index += 8;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
value = BitConverter.ToDouble(data, index);
|
|
|
|
index += 8;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
value = true;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
value = false;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
value = null;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-21 15:14:35 +02:00
|
|
|
return new OscArgument(value, type);
|
|
|
|
}
|
2023-08-15 21:29:53 +02:00
|
|
|
|
2023-10-21 15:14:35 +02:00
|
|
|
public Int32 GetAsInt32()
|
|
|
|
{
|
|
|
|
if (Type != 'i') {
|
|
|
|
throw new InvalidDataTypeException('i', Type);
|
|
|
|
}
|
|
|
|
return (Int32)Value!;
|
|
|
|
}
|
2023-08-15 21:29:53 +02:00
|
|
|
|
2023-10-21 15:14:35 +02:00
|
|
|
public float GetAsFloat()
|
|
|
|
{
|
|
|
|
if (Type != 'f') {
|
|
|
|
throw new InvalidDataTypeException('f', Type);
|
|
|
|
}
|
|
|
|
return (float)Value!;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String GetAsString() {
|
|
|
|
if (Type != 's')
|
|
|
|
{
|
|
|
|
throw new InvalidDataTypeException('s', Type);
|
|
|
|
}
|
|
|
|
return (String)Value!;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Int64 GetAsInt64()
|
|
|
|
{
|
|
|
|
if (Type != 'h')
|
|
|
|
{
|
|
|
|
throw new InvalidDataTypeException('h', Type);
|
|
|
|
}
|
|
|
|
return (Int64)Value!;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Double GetAsDouble()
|
|
|
|
{
|
|
|
|
if (Type != 'd')
|
|
|
|
{
|
|
|
|
throw new InvalidDataTypeException('d', Type);
|
|
|
|
}
|
|
|
|
return (Double)Value!;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool GetAsBool() {
|
|
|
|
switch (Type)
|
|
|
|
{
|
|
|
|
case 'T':
|
|
|
|
return true;
|
|
|
|
case 'F':
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
throw new InvalidDataTypeException("{T, F}", Type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public object? GetAsNull()
|
|
|
|
{
|
|
|
|
if (Type != 'N')
|
|
|
|
{
|
|
|
|
throw new InvalidDataTypeException('N', Type);
|
|
|
|
}
|
|
|
|
return null;
|
2023-08-15 21:29:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] ToBytes()
|
|
|
|
{
|
|
|
|
switch (Type) {
|
|
|
|
case 'i':
|
2023-10-21 15:14:35 +02:00
|
|
|
return BitConverter.GetBytes((int)Value!);
|
2023-08-15 21:29:53 +02:00
|
|
|
case 'f':
|
2023-10-21 15:14:35 +02:00
|
|
|
return BitConverter.GetBytes((float)Value!);
|
2023-08-15 21:29:53 +02:00
|
|
|
case 's':
|
|
|
|
var result = new System.Collections.Generic.List<byte>();
|
2023-10-21 15:14:35 +02:00
|
|
|
result.AddRange(System.Text.Encoding.ASCII.GetBytes((string)Value!));
|
2023-08-15 21:29:53 +02:00
|
|
|
result.Add(0);
|
|
|
|
var padding = 4 - (result.Count % 4);
|
|
|
|
for (int i = 0; i < padding; i++)
|
|
|
|
{
|
|
|
|
result.Add(0);
|
|
|
|
}
|
|
|
|
return result.ToArray();
|
|
|
|
case 'h':
|
2023-10-21 15:14:35 +02:00
|
|
|
return BitConverter.GetBytes((long)Value!);
|
2023-08-15 21:29:53 +02:00
|
|
|
case 'd':
|
2023-10-21 15:14:35 +02:00
|
|
|
return BitConverter.GetBytes((double)Value!);
|
2023-08-15 21:29:53 +02:00
|
|
|
}
|
|
|
|
return new byte[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
2023-10-21 15:14:35 +02:00
|
|
|
return Value?.ToString()!;
|
2023-08-15 21:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|