-
Notifications
You must be signed in to change notification settings - Fork 4
/
ScpProxy.cs
398 lines (305 loc) · 10.4 KB
/
ScpProxy.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ScpControl
{
public partial class ScpProxy : Component
{
protected static Char[] m_Delim = new Char[] { '^' };
protected IPEndPoint m_ServerEp = new IPEndPoint(IPAddress.Loopback, 26760);
protected UdpClient m_Server = new UdpClient();
protected IPEndPoint m_ClientEp = new IPEndPoint(IPAddress.Loopback, 26761);
protected UdpClient m_Client = new UdpClient();
protected XmlMapper m_Mapper = new XmlMapper();
protected Boolean m_Active = false;
public event EventHandler<DsPacket> Packet = null;
public virtual XmlMapper Mapper
{
get { return m_Mapper; }
}
public virtual String Active
{
get
{
String Active = String.Empty;
Byte[] Send = { 0, 6 };
if (m_Server.Send(Send, Send.Length, m_ServerEp) == Send.Length)
{
IPEndPoint ReferenceEp = new IPEndPoint(IPAddress.Loopback, 0);
Byte[] Buffer = m_Server.Receive(ref ReferenceEp);
if (Buffer.Length > 0)
{
String Data = Encoding.Unicode.GetString(Buffer);
String[] Split = Data.Split(m_Delim, StringSplitOptions.RemoveEmptyEntries);
Active = Split[0];
}
}
return Active;
}
}
public ScpProxy()
{
InitializeComponent();
}
public ScpProxy(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public virtual Boolean Start()
{
try
{
if (!m_Active)
{
NativeFeed_Worker.RunWorkerAsync();
m_Active = true;
}
}
catch { }
return m_Active;
}
public virtual Boolean Stop()
{
try
{
if (m_Active)
{
NativeFeed_Worker.CancelAsync();
m_Active = false;
}
}
catch { }
return !m_Active;
}
public virtual Boolean Load()
{
Boolean Loaded = false;
try
{
Byte[] Buffer = { 0, 0x08 };
if (m_Server.Send(Buffer, Buffer.Length, m_ServerEp) == Buffer.Length)
{
IPEndPoint ReferenceEp = new IPEndPoint(IPAddress.Loopback, 0);
Buffer = m_Server.Receive(ref ReferenceEp);
if (Buffer.Length > 0)
{
String Data = Encoding.UTF8.GetString(Buffer);
//m_Mapper.Initialize();
}
}
Loaded = true;
}
catch { }
return Loaded;
}
public virtual Boolean Select(Profile Target)
{
Boolean Selected = false;
try
{
if (m_Active)
{
Byte[] Data = Encoding.Unicode.GetBytes(Target.Name);
Byte[] Send = new Byte[Data.Length + 2];
Send[1] = 0x07;
Array.Copy(Data, 0, Send, 2, Data.Length);
m_Server.Send(Send, Send.Length, m_ServerEp);
SetDefault(Target);
Selected = true;
}
}
catch { }
return Selected;
}
public virtual DsDetail Detail(DsPadId Pad)
{
DsDetail Detail = null;
try
{
Byte[] Buffer = { (Byte) Pad, 0x0A };
if (m_Server.Send(Buffer, Buffer.Length, m_ServerEp) == Buffer.Length)
{
IPEndPoint ReferenceEp = new IPEndPoint(IPAddress.Loopback, 0);
Buffer = m_Server.Receive(ref ReferenceEp);
if (Buffer.Length > 0)
{
Byte[] Local = new Byte[6]; Array.Copy(Buffer, 5, Local, 0, Local.Length);
Detail = new DsDetail((DsPadId) Buffer[0], (DsState) Buffer[1], (DsModel) Buffer[2], Local, (DsConnection) Buffer[3], (DsBattery) Buffer[4]);
}
}
}
catch { }
return Detail;
}
public virtual Boolean Rumble(Int32 Pad, Byte Large, Byte Small)
{
Boolean Rumbled = false;
try
{
if (m_Active)
{
Byte[] Buffer = { (Byte) Pad, 0x01, Large, Small };
m_Server.Send(Buffer, Buffer.Length, m_ServerEp);
Rumbled = true;
}
}
catch { }
return Rumbled;
}
public virtual Boolean Remap(String Target, DsPacket Packet)
{
Boolean Remapped = false;
try
{
if (m_Active)
{
Byte[] Output = new Byte[Packet.Native.Length];
switch (Packet.Detail.Model)
{
case DsModel.DS3: if (m_Mapper.RemapDs3(m_Mapper.Map[Target], Packet.Native, Output)) { Array.Copy(Output, Packet.Native, Output.Length); Packet.Remapped(); } break;
}
Remapped = true;
}
}
catch { }
return Remapped;
}
public virtual Boolean SetDefault(Profile Profile)
{
Boolean Set = true;
try
{
foreach (Profile Item in m_Mapper.Map.Values)
{
Item.Default = false;
}
Profile.Default = true;
}
catch { Set = false; }
return Set;
}
protected virtual void NativeFeed_Worker_DoWork(object sender, DoWorkEventArgs e)
{
IPEndPoint Remote = new IPEndPoint(IPAddress.Loopback, 0);
m_Client = new UdpClient(m_ClientEp);
m_Client.Client.ReceiveTimeout = 500;
while(!NativeFeed_Worker.CancellationPending)
{
try
{
Byte[] Buffer = m_Client.Receive(ref Remote);
LogPacket(new DsPacket(Buffer));
}
catch { }
}
m_Client.Close();
e.Cancel = true;
}
protected virtual void LogPacket(DsPacket Data)
{
if (Packet != null)
{
Packet(this, Data);
}
}
}
public class DsPacket : EventArgs
{
protected Int32 m_Packet;
protected DsDetail m_Detail;
protected Byte[] m_Native;
protected Ds3Button m_Ds3Button = Ds3Button.None;
internal DsPacket(Byte[] Native)
{
Byte[] Local = new Byte[6];
Array.Copy(Native, (Int32) DsOffset.Address, Local, 0, Local.Length);
m_Detail = new DsDetail(
(DsPadId) Native[(Int32) DsOffset.Pad ],
(DsState) Native[(Int32) DsOffset.State ],
(DsModel) Native[(Int32) DsOffset.Model ],
Local,
(DsConnection) Native[(Int32) DsOffset.Connection],
(DsBattery) Native[(Int32) DsOffset.Battery ]
);
m_Packet = (Int32)(Native[4] << 0 | Native[5] << 8 | Native[6] << 16 | Native[7] << 24);
m_Native = Native;
switch(m_Detail.Model)
{
case DsModel.DS3: m_Ds3Button = (Ds3Button)((Native[10] << 0) | (Native[11] << 8) | (Native[12] << 16) | (Native[13] << 24)); break;
}
}
internal Byte[] Native
{
get { return m_Native; }
}
internal void Remapped()
{
switch (m_Detail.Model)
{
case DsModel.DS3: m_Ds3Button = (Ds3Button)((Native[10] << 0) | (Native[11] << 8) | (Native[12] << 16) | (Native[13] << 24)); break;
}
}
public DsDetail Detail
{
get { return m_Detail; }
}
public Boolean Button(Ds3Button Flag)
{
if (m_Detail.Model != DsModel.DS3) throw new InvalidEnumArgumentException();
return m_Ds3Button.HasFlag(Flag);
}
public Byte Axis(Ds3Axis Offset)
{
if (m_Detail.Model != DsModel.DS3) throw new InvalidEnumArgumentException();
return Native[(Int32) Offset];
}
}
public class DsDetail
{
protected DsPadId m_Serial;
protected DsModel m_Model;
protected Byte[] m_Local = new Byte[6];
protected DsConnection m_Mode;
protected DsBattery m_Charge;
protected DsState m_State;
internal DsDetail(DsPadId PadId, DsState State, DsModel Model, Byte[] Mac, DsConnection Mode, DsBattery Level)
{
m_Serial = PadId;
m_State = State;
m_Model = Model;
m_Mode = Mode;
m_Charge = Level;
Array.Copy(Mac, m_Local, m_Local.Length);
}
public DsPadId Pad
{
get { return m_Serial; }
}
public DsState State
{
get { return m_State; }
}
public DsModel Model
{
get { return m_Model; }
}
public String Local
{
get { return String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", m_Local[0], m_Local[1], m_Local[2], m_Local[3], m_Local[4], m_Local[5]); }
}
public DsConnection Mode
{
get { return m_Mode; }
}
public DsBattery Charge
{
get { return m_Charge; }
}
}
}