-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
125 lines (114 loc) · 5.51 KB
/
Program.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
using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.WinUsb;
using System;
using System.Collections.Generic;
using System.Text;
using Nefarius;
using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Targets;
using Nefarius.ViGEm.Client.Targets.Xbox360;
namespace GHL_HIDEmulator
{
class Program
{
public static void Main(string[] args)
{
Console.Title = "GHL HID Emulator";
UsbDevice guitar = null;
bool isPS4 = false;
foreach(WinUsbRegistry device in LibUsbDotNet.UsbDevice.AllWinUsbDevices)
{
// USB\VID_12BA&PID_074B
if ((device.Vid == 0x12BA && device.Pid == 0x074B) || (device.Vid == 0x1430 && device.Pid == 0x07BB))
{
guitar = device.Device;
isPS4 = (device.Pid == 0x07BB);
}
}
if (guitar == null)
{
Console.WriteLine("Could not find any Guitar Hero Live guitars.");
Console.WriteLine("Make sure you are using a PS3/Wii U/PS4 Guitar Hero Live dongle with the WinUSB driver installed.");
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
return;
}
// Set up Virtual Xbox 360 controller
ViGEmClient client;
try
{
client = new ViGEmClient();
} catch (Exception)
{
Console.WriteLine("Failed to initialise ViGEm Client. Make sure you have the ViGEm bus driver installed.");
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
return;
}
IXbox360Controller controller = client.CreateXbox360Controller();
controller.Connect();
Console.WriteLine($"Found a Guitar Hero Live guitar!");
var reader = guitar.OpenEndpointReader(ReadEndpointID.Ep01);
byte[] readBuffer = new byte[100];
int runner = 0;
while (true)
{
if (runner == 0)
{
// Send control packet (to enable strumming)
byte[] buffer;
if (isPS4)
buffer = new byte[9] { 0x30, 0x02, 0x08, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00 };
else
buffer = new byte[9] { 0x02, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int bytesWrote;
UsbSetupPacket setupPacket = new UsbSetupPacket(0x21, 0x09, 0x0201, 0x0000, 0x0008);
guitar.ControlTransfer(ref setupPacket, buffer, 0x0008, out bytesWrote);
}
runner++;
if (runner > 500) runner = 0;
int bytesRead;
reader.Read(readBuffer, 100, out bytesRead);
Console.SetCursorPosition(0, 1);
Console.WriteLine("Frets: " + BitConverter.ToString(new byte[] { readBuffer[0] }));
Console.WriteLine("Buttons: " + BitConverter.ToString(new byte[] { readBuffer[1] }));
Console.WriteLine("Tilt: " + BitConverter.ToString(new byte[] { readBuffer[19] }));
Console.WriteLine("Whammy: " + BitConverter.ToString(new byte[] { readBuffer[6] }));
Console.WriteLine("Strum: " + (readBuffer[4] == 0x00 || readBuffer[4] == 0xFF) + " ");
Console.WriteLine("Raw Data: " + BitConverter.ToString(readBuffer));
byte frets = readBuffer[0];
controller.SetButtonState(Xbox360Button.A, (frets & 0x02) != 0x00); // B1
controller.SetButtonState(Xbox360Button.B, (frets & 0x04) != 0x00); // B2
controller.SetButtonState(Xbox360Button.Y, (frets & 0x08) != 0x00); // B3
controller.SetButtonState(Xbox360Button.X, (frets & 0x01) != 0x00); // W1
controller.SetButtonState(Xbox360Button.LeftShoulder, (frets & 0x10) != 0x00); // W2
controller.SetButtonState(Xbox360Button.RightShoulder, (frets & 0x20) != 0x00); // W3
byte strum = readBuffer[4];
if (strum == 0xFF)
{
// Strum Down
controller.SetButtonState(Xbox360Button.Down, true);
controller.SetButtonState(Xbox360Button.Up, false);
} else if (strum == 0x00)
{
// Strum Up
controller.SetButtonState(Xbox360Button.Down, false);
controller.SetButtonState(Xbox360Button.Up, true);
} else
{
// No Strum
controller.SetButtonState(Xbox360Button.Down, false);
controller.SetButtonState(Xbox360Button.Up, false);
}
byte buttons = readBuffer[1];
controller.SetButtonState(Xbox360Button.Start, (buttons & 0x02) != 0x00); // Pause
controller.SetButtonState(Xbox360Button.Back, (buttons & 0x01) != 0x00); // Hero Power
// ViGEm isn't co-operating here - setting to some weird value causes an issue.
//controller.SetAxisValue(Xbox360Axis.RightThumbY, (short)(~readBuffer[6]-128));
//controller.SetAxisValue(Xbox360Axis.RightThumbX, readBuffer[19]);
Console.WriteLine("Emulating as Xbox 360 Controller " + (controller.UserIndex + 1));
}
}
}
}