1
1
using System ;
2
+ using System . Linq ;
2
3
using System . Net ;
3
4
using System . Net . Sockets ;
4
5
using System . Threading ;
5
6
using System . Threading . Tasks ;
6
7
using NetJoy . Core . Config ;
7
8
using NetJoy . Core . NetJoy . Packets ;
9
+ using NetJoy . Core . Utils ;
8
10
using Newtonsoft . Json ;
9
11
using SharpDX . DirectInput ;
10
12
11
13
using Encoding = System . Text . Encoding ;
12
14
13
15
namespace NetJoy . Core . NetJoy . Server
14
16
{
15
- public class NetJoyServer
17
+ public sealed class NetJoyServer
16
18
{
17
19
private Joystick _joystick ; //joystick we're polling for input
18
20
private Socket _clientSocket = null ; //the connected client socket
@@ -45,6 +47,7 @@ await Task.Run(() =>
45
47
var serverListener = StartServerListener ( ) ;
46
48
var controllerListener = StartControllerListener ( ) ;
47
49
50
+
48
51
//wait for all tasks to complete
49
52
Task . WhenAll ( serverListener , controllerListener ) ;
50
53
} ) . ConfigureAwait ( false ) ;
@@ -64,7 +67,7 @@ await Task.Run(() =>
64
67
var localEndPoint = new IPEndPoint ( IPAddress . Any , _configuration . server . port ) ;
65
68
66
69
//log that we started the server
67
- Console . WriteLine ( $ "Started Server @{ ip } :{ _configuration . server . port } ") ;
70
+ Logger . Debug ( $ "Started Server @{ ip } :{ _configuration . server . port } ") ;
68
71
69
72
// Create a TCP/IP socket.
70
73
var listener = new Socket ( ip . AddressFamily ,
@@ -81,7 +84,7 @@ await Task.Run(() =>
81
84
_allDone . Reset ( ) ;
82
85
83
86
// Start an asynchronous socket to listen for connections.
84
- Console . WriteLine ( "Waiting for a connection..." ) ;
87
+ Logger . Debug ( "Waiting for a connection..." ) ;
85
88
listener . BeginAccept (
86
89
AcceptCallback ,
87
90
listener ) ;
@@ -178,27 +181,14 @@ private void Send(string message)
178
181
private void GetJoystick ( )
179
182
{
180
183
var directInput = new DirectInput ( ) ;
181
- var joystickGuid = Guid . Empty ;
182
-
183
- //try to get a gamepad
184
- foreach ( var deviceInstance in directInput . GetDevices ( DeviceType . Gamepad , DeviceEnumerationFlags . AllDevices ) )
185
- {
186
- joystickGuid = deviceInstance . InstanceGuid ;
187
- }
188
-
189
- // If Gamepad not found, look for a Joystick
190
- if ( joystickGuid == Guid . Empty )
191
- {
192
- foreach ( var deviceInstance in directInput . GetDevices ( DeviceType . Joystick , DeviceEnumerationFlags . AllDevices ) )
193
- {
194
- joystickGuid = deviceInstance . InstanceGuid ;
195
- }
196
- }
197
-
184
+
185
+ //get the joystick guid & select a stick
186
+ var joystickGuid = SelectStick ( directInput ) ;
187
+
198
188
// If Joystick not found, throws an error
199
189
if ( joystickGuid == Guid . Empty )
200
190
{
201
- Console . WriteLine ( "No joystick/Gamepad found." ) ;
191
+ Logger . LogError ( "No joystick/Gamepad found." ) ;
202
192
Console . ReadKey ( ) ;
203
193
Environment . Exit ( 1 ) ;
204
194
}
@@ -207,15 +197,15 @@ private void GetJoystick()
207
197
_joystick = new Joystick ( directInput , joystickGuid ) ;
208
198
209
199
//log that we found a joystick
210
- Console . WriteLine ( $ "Found Joystick with GUID: { joystickGuid } ") ;
200
+ Logger . Debug ( $ "Found Joystick with GUID: { joystickGuid } ") ;
211
201
212
202
//query all available effects
213
203
var allEffects = _joystick . GetEffects ( ) ;
214
204
215
205
//log all of the available effects
216
206
foreach ( var effect in allEffects )
217
207
{
218
- Console . WriteLine ( $ "Found Effect: { effect } ") ;
208
+ Logger . Log ( $ "Found Effect: { effect } ") ;
219
209
}
220
210
221
211
//set the buffer size for the joystick
@@ -224,5 +214,59 @@ private void GetJoystick()
224
214
//acquire the joystick
225
215
_joystick . Acquire ( ) ;
226
216
}
217
+
218
+ /// <summary>
219
+ /// Draw menu for selecting a stick
220
+ /// </summary>
221
+ /// <param name="directInput"> stick to select</param>
222
+ /// <returns></returns>
223
+ private Guid SelectStick ( DirectInput directInput )
224
+ {
225
+ do
226
+ {
227
+ //the joysticks to select from
228
+ var joysticks = directInput . GetDevices ( DeviceType . Joystick , DeviceEnumerationFlags . AllDevices ) ;
229
+
230
+ //add all detected gamepads to the joystick list
231
+ foreach ( var pad in directInput . GetDevices ( DeviceType . Gamepad , DeviceEnumerationFlags . AllDevices ) )
232
+ {
233
+ joysticks . Add ( pad ) ;
234
+ }
235
+
236
+ //Log the joysticks
237
+ for ( var index = 0 ; index < joysticks . Count ; index ++ )
238
+ {
239
+ var stick = joysticks [ index ] ;
240
+
241
+ Logger . Log ( $ "{ index + 1 } ) { stick . ProductName } ") ;
242
+ }
243
+
244
+ //Log message depending on joysticks
245
+ Logger . Log ( ! joysticks . Any ( )
246
+ ? "Plug in a joystick & press enter to rescan!"
247
+ : "Enter the ID of the stick you wish to use: " ) ;
248
+
249
+ //check if the input from the console is valid
250
+ var validInput = int . TryParse ( Console . ReadLine ( ) , out var choice ) ;
251
+
252
+ //if the input was invalid continue
253
+ if ( validInput && choice >= 1 && choice <= joysticks . Count )
254
+ {
255
+ //clear the console
256
+ Console . Clear ( ) ;
257
+
258
+ //select the stick and log it
259
+ var stick = joysticks [ choice - 1 ] ;
260
+ Logger . Debug ( $ "Selected Joystick: { stick . ProductName } ") ;
261
+ return stick . InstanceGuid ;
262
+ }
263
+
264
+ //clear the logger
265
+ Logger . Clear ( ) ;
266
+
267
+ //log an error
268
+ Logger . LogError ( "Invalid Input, Please select from the sticks below!" ) ;
269
+ } while ( true ) ;
270
+ }
227
271
}
228
272
}
0 commit comments