forked from 9E4ECDDE/MultiplayerDynamicBonesMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPCSupport.cs
284 lines (267 loc) · 12 KB
/
IPCSupport.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
namespace ExternalDynamicBoneEditor
{
namespace IPCSupport
{
public enum Message
{
SetBoneDamping,
SetBoneElasticity,
SetBoneStiffness,
SetBoneInert,
SetBoneRadius,
SetBoneEndLength,
SetBoneEndOffset,
SetBoneGravity,
SetBoneForce,
SetBoneData
}
[StructLayout(LayoutKind.Sequential)]
public struct float3
{
public float x, y, z;
public float3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
public struct AvatarBones
{
public string name;
public int boneCount;
public SerializedBoneData[] bones;
public byte[] ToByteArray()
{
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter binaryWriter = new BinaryWriter(ms))
{
binaryWriter.Write(name);
binaryWriter.Write(boneCount);
List<byte[]> bonesBytes = new List<byte[]>(bones.Select((b) => b.ToByteArray()));
foreach (byte[] boneByteData in bonesBytes)
{
binaryWriter.Write(boneByteData.Length);
binaryWriter.Write(boneByteData);
}
return ms.ToArray();
}
}
}
public static AvatarBones FromByteArray(byte[] array)
{
using (MemoryStream memoryStream = new MemoryStream(array))
{
using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))
{
AvatarBones boneList = new AvatarBones();
boneList.name = binaryReader.ReadString();
boneList.boneCount = binaryReader.ReadInt32();
boneList.bones = new SerializedBoneData[boneList.boneCount];
for (int i = 0; i < boneList.boneCount; i++)
{
boneList.bones[i] = SerializedBoneData.FromByteArray(binaryReader.ReadBytes(binaryReader.ReadInt32()));
}
return boneList;
}
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SerializedBoneData
{
public string name;
public float damping;
public float elasticity;
public float stiffness;
public float inert;
public float radius;
public float endLength;
public float3 endOffset;
public float3 gravity;
public float3 force;
public static SerializedBoneData FromByteArray(byte[] array)
{
using (MemoryStream memoryStream = new MemoryStream(array))
{
using (BinaryReader binaryReader = new BinaryReader(memoryStream, Encoding.UTF8))
{
SerializedBoneData boneData = new SerializedBoneData();
boneData.name = binaryReader.ReadString();
boneData.damping = binaryReader.ReadSingle();
boneData.elasticity = binaryReader.ReadSingle();
boneData.stiffness = binaryReader.ReadSingle();
boneData.inert = binaryReader.ReadSingle();
boneData.radius = binaryReader.ReadSingle();
boneData.endLength = binaryReader.ReadSingle();
boneData.endOffset = new float3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
boneData.gravity = new float3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
boneData.force = new float3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
return boneData;
}
}
}
public unsafe byte[] ToByteArray()
{
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter binaryWriter = new BinaryWriter(ms))
{
binaryWriter.Write(name);
binaryWriter.Write(damping);
binaryWriter.Write(elasticity);
binaryWriter.Write(stiffness);
binaryWriter.Write(inert);
binaryWriter.Write(radius);
binaryWriter.Write(endLength);
binaryWriter.Write(endOffset.x);
binaryWriter.Write(endOffset.y);
binaryWriter.Write(endOffset.z);
binaryWriter.Write(gravity.x);
binaryWriter.Write(gravity.y);
binaryWriter.Write(gravity.z);
binaryWriter.Write(force.x);
binaryWriter.Write(force.y);
binaryWriter.Write(force.z);
return ms.ToArray();
}
}
}
}
public class IPCHandler
{
private PipeStream pipe;
private BinaryWriter pipeWriter;
private BinaryReader pipeReader;
public bool IsConnected
{
get
{
return pipe != null && pipe.IsConnected;
}
}
public bool IsServer
{
get
{
return pipe != null && pipe is NamedPipeServerStream;
}
}
public IPCHandler(PipeStream pipeResource)
{
pipe = pipeResource;
pipeReader = new BinaryReader(pipe, Encoding.UTF8);
pipeWriter = new BinaryWriter(pipe, Encoding.UTF8);
}
public Message Receive(out byte[] data)
{
if (!IsConnected) throw new InvalidOperationException("Tried to receive a message but the pipe is disconnected.");
Message message = (Message)pipeReader.ReadInt32();
data = pipeReader.ReadBytes(pipeReader.ReadInt32());
return message;
}
public void Send(Message messageType, string boneName, object data)
{
if (!IsConnected) throw new InvalidOperationException("Tried to send a message but the pipe is disconnected.");
switch (messageType)
{
case Message.SetBoneData:
{
if (!IsServer) throw new InvalidOperationException("GetBoneData can only be called from the server.");
pipeWriter.Write((int)Message.SetBoneData);
pipeWriter.Write((byte[])data);
break;
}
case Message.SetBoneDamping:
{
if (IsServer) throw new InvalidOperationException("SetBoneDamping can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneDamping);
pipeWriter.Write(boneName);
pipeWriter.Write((float)data);
break;
}
case Message.SetBoneElasticity:
{
if (IsServer) throw new InvalidOperationException("SetBoneElasticity can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneElasticity);
pipeWriter.Write(boneName);
pipeWriter.Write((float)data);
break;
}
case Message.SetBoneStiffness:
{
if (IsServer) throw new InvalidOperationException("SetBoneStiffness can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneStiffness);
pipeWriter.Write(boneName);
pipeWriter.Write((float)data);
break;
}
case Message.SetBoneInert:
{
if (IsServer) throw new InvalidOperationException("SetBoneInert can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneInert);
pipeWriter.Write(boneName);
pipeWriter.Write((float)data);
break;
}
case Message.SetBoneRadius:
{
if (IsServer) throw new InvalidOperationException("SetBoneRadius can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneRadius);
pipeWriter.Write(boneName);
pipeWriter.Write((float)data);
break;
}
case Message.SetBoneEndLength:
{
if (IsServer) throw new InvalidOperationException("SetBoneEndLength can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneEndLength);
pipeWriter.Write(boneName);
pipeWriter.Write((float)data);
break;
}
case Message.SetBoneEndOffset:
{
if (IsServer) throw new InvalidOperationException("SetBoneEndOffset can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneInert);
pipeWriter.Write(boneName);
pipeWriter.Write(((float3)data).x);
pipeWriter.Write(((float3)data).y);
pipeWriter.Write(((float3)data).z);
break;
}
case Message.SetBoneGravity:
{
if (IsServer) throw new InvalidOperationException("SetBoneGravity can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneGravity);
pipeWriter.Write(boneName);
pipeWriter.Write(((float3)data).x);
pipeWriter.Write(((float3)data).y);
pipeWriter.Write(((float3)data).z);
break;
}
case Message.SetBoneForce:
{
if (IsServer) throw new InvalidOperationException("SetBoneForce can only be called from the client.");
pipeWriter.Write((int)Message.SetBoneInert);
pipeWriter.Write(boneName);
pipeWriter.Write(((float3)data).x);
pipeWriter.Write(((float3)data).y);
pipeWriter.Write(((float3)data).z);
break;
}
}
}
}
}
}