@@ -32,53 +32,73 @@ public static SetJoysticksResult SetJoystickGuids(IList<JoystickInfo> joysticks,
3232 throw new InvalidRyujinxConfigException ( "Failed to deserialize the Ryujinx config json" , ex ) ;
3333 }
3434
35- Log . Information ( "Found {count} input configs in Ryujinx config file" , inputConfigs . Count ) ;
35+ var inputConfigsToProcess = inputConfigs
36+ . Where ( c => c [ "backend" ] ? . ToString ( ) == "GamepadSDL2" )
37+ . OrderBy ( c => ParsePlayerIndex ( c [ "player_index" ] ? . ToString ( ) ) )
38+ . ToList ( ) ;
3639
37- for ( int i = 0 ; i < inputConfigs . Count ; i ++ )
40+ if ( inputConfigsToProcess . Count == 0 )
3841 {
39- var inputConfig = inputConfigs [ i ] ;
40- if ( joysticks . Count - 1 >= i )
42+ Log . Warning ( "No inputs configured with GamepadSDL2 backend type were found in Ryujinx" ) ;
43+ return result ;
44+ }
45+
46+ Log . Information ( "Found {count} SDL2 inputs configured in Ryujinx" , inputConfigsToProcess . Count ) ;
47+
48+ var inputConfigModified = false ;
49+
50+ for ( var i = 0 ; i < inputConfigsToProcess . Count ; i ++ )
51+ {
52+ var inputConfig = inputConfigsToProcess [ i ] ;
53+ var playerIndex = inputConfig [ "player_index" ] ? . ToString ( ) ;
54+
55+ if ( i > joysticks . Count - 1 )
4156 {
42- var oldId = inputConfig [ "id" ] ? . ToString ( ) ;
43- var newId = $ "{ i } -{ joysticks [ i ] . SdlJoystickGuid } ";
57+ Log . Information ( "No connected joystick available to swap for {playerIndex}" , playerIndex ) ;
58+ continue ;
59+ }
4460
45- if ( newId != oldId )
46- {
47- inputConfig [ "id" ] = newId ;
61+ var oldId = inputConfig [ "id" ] ? . ToString ( ) ;
62+ var newId = $ "{ joysticks [ i ] . SdlDeviceIndex } -{ joysticks [ i ] . SdlJoystickGuid } ";
4863
49- if ( IsValidRyujinxInputDeviceId ( oldId ) )
50- {
51- result . SwappedJoysticks . Add ( new SwappedJoystickResult { InputIndex = i , OldInputId = ConvertRyujinxInputDeviceIdToGuid ( oldId ) } ) ;
52- }
64+ if ( newId != oldId )
65+ {
66+ inputConfig [ "id" ] = newId ;
67+ inputConfigModified = true ;
5368
54- Log . Information ( "Swapped input #{index}: {oldId} >> {newId}" , i , oldId , newId ) ;
55- }
56- else
69+ if ( IsValidRyujinxInputDeviceId ( oldId ) )
5770 {
58- Log . Information ( "No change needed for input #{index} ({oldId})" , i , oldId ) ;
71+ result . SwappedJoysticks . Add ( new SwappedJoystickResult { OldRyujinxDeviceId = oldId } ) ;
5972 }
73+
74+ Log . Information ( "Swapped input {playerIndex}: {oldId} >> {newId}" , playerIndex , oldId , newId ) ;
6075 }
6176 else
6277 {
63- Log . Information ( "No connected joystick available to swap for input #{index} " , i ) ;
78+ Log . Information ( "No change needed for input {playerIndex} ({oldId}) " , playerIndex , oldId ) ;
6479 }
6580 }
6681
67- result . ModifiedConfigJson = JsonConvert . SerializeObject ( configData , Formatting . Indented ) ;
82+ if ( inputConfigModified )
83+ {
84+ result . ModifiedConfigJson = JsonConvert . SerializeObject ( configData , Formatting . Indented ) ;
85+ }
6886
6987 return result ;
7088 }
7189
72- public static bool IsValidRyujinxInputDeviceId ( string ? deviceId )
90+ private static int ParsePlayerIndex ( string ? jsonValue )
7391 {
74- return deviceId != null && Regex . IsMatch ( deviceId , @"\d-.+" ) ;
92+ if ( String . IsNullOrEmpty ( jsonValue ) || ! Regex . IsMatch ( jsonValue , @"Player\d" ) )
93+ {
94+ throw new Exception ( $ "PlayerIndex value did not match expected pattern: { jsonValue } ") ;
95+ }
96+
97+ return int . Parse ( Regex . Match ( jsonValue , @"Player(\d)" ) . Groups [ 1 ] . Value ) ;
7598 }
7699
77- public static Guid ConvertRyujinxInputDeviceIdToGuid ( string value )
100+ public static bool IsValidRyujinxInputDeviceId ( string ? deviceId )
78101 {
79- if ( ! RyujinxConfigHelper . IsValidRyujinxInputDeviceId ( value ) )
80- throw new ArgumentException ( $ "OldRyujinxInputId is not in the expected format: { value } ") ;
81-
82- return Guid . Parse ( value . Substring ( 2 ) ) ;
102+ return deviceId != null && Regex . IsMatch ( deviceId , @"\d-.+" ) ;
83103 }
84104}
0 commit comments