/* 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 . */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Godot; namespace godotOscSharp { public class OscMessage { public OscAddress Address { get; } public List Data { get; } public OscMessage(OscAddress address) { Address = address; } public OscMessage(OscAddress address, List data) { Address = address; Data = data; } public static OscMessage Parse(byte[] data) { var index = 0; var address = OscAddress.Parse(data, ref index); var start = index; while (data[index] != 0) { index++; } var pattern = System.Text.Encoding.ASCII.GetString(data, start, index - start); while (data[index] == 0) { index++; } var dataList = new List(); for (var items = 1; items < pattern.Length; items++) { dataList.Add(OscArgument.Parse(data, ref index, pattern[items])); } return new OscMessage(address, dataList); } public override string ToString() { var sb = new StringBuilder(); sb.Append(Address.ToString()); foreach (var d in Data) { sb.Append(" "); sb.Append(d.ToString()); } return sb.ToString(); } public byte[] ToBytes() { var result = new System.Collections.Generic.List(); result.AddRange(Address.ToBytes().ToList()); result.Add(0x2c); for (var i = 0; i < Data.Count(); i++) { result.Add(BitConverter.GetBytes(Data[i].Type).ToList()[0]); } var padding = 4 - (result.Count % 4); for (int i = 0; i < padding; i++) { result.Add(0); } for (var i = 0; i < Data.Count(); i++) { result.AddRange(Data[i].ToBytes().ToList()); } return result.ToArray(); } } }