Skip to content

Commit 6361144

Browse files
committed
Working test version
0 parents  commit 6361144

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1656
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
.obj/

Core/Config/ConfigService.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.IO;
3+
using Nett;
4+
5+
namespace NetJoy.Core.Config
6+
{
7+
public class ConfigService
8+
{
9+
10+
//the filename for the toml file
11+
private static readonly string fileName = "config.toml";
12+
13+
public ConfigService(){}
14+
15+
public static Configuration ReadConfig()
16+
{
17+
try
18+
{
19+
20+
//If the file exists, just read it
21+
if (File.Exists(fileName))
22+
{
23+
return Toml.ReadFile<Configuration>(fileName);
24+
}
25+
26+
//write the default config file
27+
Console.WriteLine("No config file detected, making one with default settings");
28+
Toml.WriteFile(new DefaultConfig(), fileName);
29+
30+
return Toml.ReadFile<Configuration>(fileName);
31+
}
32+
catch(Exception e)
33+
{
34+
Console.WriteLine(e);
35+
Console.WriteLine("Error reading config file, maybe it's corrupted?");
36+
}
37+
38+
return new Configuration();
39+
}
40+
}
41+
}

Core/Config/Configuration.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace NetJoy.Core.Config
2+
{
3+
public class Configuration
4+
{
5+
public bool isServer { get; set; }
6+
public Client server{ get; set; }
7+
8+
public class Client
9+
{
10+
public string address { get; set; }
11+
public int port { get; set; }
12+
}
13+
}
14+
}

Core/Config/DefaultConfig.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Dynamic;
2+
3+
namespace NetJoy.Core.Config
4+
{
5+
public class DefaultConfig
6+
{
7+
public bool isServer { get; set; } = false;
8+
public Client server{ get; set; } = new Client();
9+
}
10+
11+
public class Client
12+
{
13+
public string address { get; set; } = "127.0.0.1";
14+
public int port { get; set; } = 6069;
15+
}
16+
}
+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
using System;
2+
using vJoyInterfaceWrap;
3+
4+
namespace NetJoy.Core.NetJoy.Client.Handling
5+
{
6+
7+
// Don't forget to add this
8+
public class JoyHandler
9+
{
10+
//the port for the controller
11+
private readonly uint _port;
12+
private readonly vJoy _joystick;
13+
14+
public JoyHandler(uint port)
15+
{
16+
17+
//set the joystick port
18+
_port = port;
19+
20+
//create a new vJoy instance
21+
_joystick = new vJoy();
22+
23+
//if vJoy is not enabled
24+
if (!_joystick.vJoyEnabled())
25+
{
26+
//if vJoy is not enabled throw an exception
27+
throw new Exception("vJoy is not enabled!");
28+
}
29+
30+
//log information about the joystick driver
31+
LogJoystickInfo(_joystick);
32+
33+
//check whether the dll version and the driver version match
34+
CheckVersions(_joystick);
35+
36+
//check whether the device is available
37+
var available = IsDeviceAvailable(_joystick, _port);
38+
39+
//if the joystick is not available we can't use it
40+
if (!available)
41+
{
42+
throw new Exception("Joystick is not available and therefore cannot be used! Free the joystick before restarting the program!");
43+
}
44+
45+
//try to acquire the device
46+
var acquired = AcquireDevice(_joystick, port);
47+
48+
//if we failed to acquire the device, throw an exception
49+
if (!acquired)
50+
{
51+
throw new Exception($"Failed to acquire device @ port {_port}");
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Try to set the button with the given id to the given sate
57+
/// </summary>
58+
/// <param name="id">of button to change state of</param>
59+
/// <param name="pressed">whether the button should be pressed</param>
60+
public void setButton(int id, bool pressed)
61+
{
62+
try
63+
{
64+
_joystick.SetBtn(pressed, _port, (uint) id);
65+
}
66+
catch
67+
{
68+
//ignored
69+
}
70+
}
71+
/// <summary>
72+
/// Convert a double percentage to an axis value
73+
/// </summary>
74+
/// <param name="percent"></param>
75+
/// <returns>The Joystick value from 0 - 65536</returns>
76+
private int PercentToAxisValue(double percent)
77+
{
78+
return (int) (short.MaxValue * percent);
79+
}
80+
81+
/// <summary>
82+
/// Acquire the device to be fed from joystick input
83+
/// </summary>
84+
/// <param name="joy">driver to use</param>
85+
/// <param name="port">to acquire</param>
86+
/// <returns></returns>
87+
private bool AcquireDevice(vJoy joy, uint port)
88+
{
89+
//get the status of the joystick @ the given port
90+
var status = joy.GetVJDStatus(port);
91+
92+
// Acquire the target
93+
if (status == VjdStat.VJD_STAT_OWN ||
94+
(status == VjdStat.VJD_STAT_FREE) && (!joy.AcquireVJD(port)))
95+
{
96+
Console.WriteLine($"Failed to acquire vJoy device number {port}.");
97+
return false;
98+
}
99+
100+
Console.WriteLine($"Acquired: vJoy device number {port}.");
101+
return true;
102+
103+
}
104+
105+
/// <summary>
106+
/// Check whether the given device is available or not
107+
/// </summary>
108+
/// <param name="joy">to check</param>
109+
/// <param name="id">of the device in question</param>
110+
/// <returns></returns>
111+
private bool IsDeviceAvailable(vJoy joy, uint id)
112+
{
113+
// Get the state of the requested device
114+
var status = joy.GetVJDStatus(id);
115+
switch (status)
116+
{
117+
case VjdStat.VJD_STAT_FREE:
118+
Console.WriteLine("vJoy Device {0} is free\n", id);
119+
return true;
120+
case VjdStat.VJD_STAT_OWN:
121+
Console.WriteLine("vJoy Device {0} is already owned by this feeder\n", id);
122+
return true;
123+
case VjdStat.VJD_STAT_BUSY:
124+
Console.WriteLine(
125+
"vJoy Device {0} is already owned by another feeder\nCannot continue\n", id);
126+
break;
127+
case VjdStat.VJD_STAT_MISS:
128+
Console.WriteLine(
129+
"vJoy Device {0} is not installed or disabled\nCannot continue\n", id);
130+
break;
131+
case VjdStat.VJD_STAT_UNKN:
132+
break;
133+
default:
134+
Console.WriteLine("vJoy Device {0} general error\nCannot continue\n", id);
135+
break;
136+
};
137+
138+
return false;
139+
}
140+
141+
/// <summary>
142+
/// Log information about the joystick driver
143+
/// </summary>
144+
/// <param name="joy"></param>
145+
private void LogJoystickInfo(vJoy joy)
146+
{
147+
//log vJoy information
148+
Console.WriteLine("vJoy Information:\nVendor: {0}\nProduct: {1}\nVersion Number: {2}\n",
149+
joy.GetvJoyManufacturerString(),
150+
joy.GetvJoyProductString(),
151+
joy.GetvJoySerialNumberString());
152+
}
153+
154+
/// <summary>
155+
/// Check whether the versions of the driver and the dll used match
156+
/// </summary>
157+
/// <param name="joy">The vJoy instance to check</param>
158+
private void CheckVersions(vJoy joy)
159+
{
160+
//Check if dll and driver version match
161+
uint dllVer = 0, drvVer = 0;
162+
var match = joy.DriverMatch(ref dllVer, ref drvVer);
163+
164+
//Log data depending on whether they matched or not
165+
if (match)
166+
{
167+
Console.WriteLine("Version of Driver Matches DLL Version ({0:X})\n", dllVer);
168+
}
169+
else
170+
{
171+
Console.WriteLine("Version of Driver ({0:X}) does NOT match DLL Version ({1:X})\nIf you experience errors please upgrade/downgrade accordingly!",
172+
drvVer, dllVer);
173+
}
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)