-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDVTMPMessage.cs
86 lines (75 loc) · 2.83 KB
/
DVTMPMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Contains definitions for DVTMPMessage and DVTMPMessageUnpacked classes.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace DVTApp
{
public enum MessageAppType { RUCA, RVCA, RHTCA, BTA }
public enum MessageBodyValueType { Bool, Int32, Single, Char, String, Vector3, Vector4, Blob }
public class DVTMPMessage
{
public MessageAppType messageAppType;
public MessagingSystem.MsgEventName messageBodyName;
public MessageBodyValueType messageBodyValueType;
public byte[] messageBodyValue;
}
public struct DVTMPMessageUpacked
{
public MessageAppType messageAppType;
public MessagingSystem.MsgEventName messageBodyName;
public MessageBodyValueType messageBodyValueType;
public int messageBodyValueSize;
public byte[] messageBodyValue;
public static bool UnpackMessage(
byte[] raw_message_bytes, out DVTMPMessageUpacked message)
{
message = new();
if(raw_message_bytes[0] != 2) return false;
// Offset to determine where the message starts in the raw_message_bytes array
int message_start_offset = 5;
message.messageAppType =
(MessageAppType)raw_message_bytes[message_start_offset+0];
message.messageBodyName =
(MessagingSystem.MsgEventName)raw_message_bytes[
message_start_offset+1];
message.messageBodyValueType =
(MessageBodyValueType)raw_message_bytes[message_start_offset+2];
message.messageBodyValueSize = BitConverter.ToInt32(
raw_message_bytes, message_start_offset+3
);
message.messageBodyValue = new byte[message.messageBodyValueSize];
Buffer.BlockCopy(
raw_message_bytes, message_start_offset+7,
message.messageBodyValue, 0, message.messageBodyValueSize
);
return true;
}
public bool GetBool() => BitConverter.ToBoolean(messageBodyValue, 0);
public int GetInt32() => BitConverter.ToInt32(messageBodyValue, 0);
public float GetSingle() => BitConverter.ToSingle(messageBodyValue, 0);
public char GetChar() => BitConverter.ToChar(messageBodyValue, 0);
public string GetString() => ASCIIEncoding.ASCII.GetString(messageBodyValue);
public Vector3 GetVector3() =>
new Vector3(
BitConverter.ToSingle(messageBodyValue, 0),
BitConverter.ToSingle(messageBodyValue, 4),
BitConverter.ToSingle(messageBodyValue, 8)
);
public Vector4 GetVector4() =>
new Vector4(
BitConverter.ToSingle(messageBodyValue, 0),
BitConverter.ToSingle(messageBodyValue, 4),
BitConverter.ToSingle(messageBodyValue, 8),
BitConverter.ToSingle(messageBodyValue, 12)
);
// DEBUG ONLY!!!
public string ToString()
{
return $"";
}
}
}