@@ -58,22 +58,56 @@ public static bool AutoStartFsuipc()
5858 }
5959 else
6060 {
61- string pattern = @"^RunIf(\d+).*WorkingTitle2GSX\.exe" ;
62- if ( Regex . IsMatch ( fileContent , pattern , RegexOptions . Compiled | RegexOptions . Multiline ) )
61+ RegexOptions regOptions = RegexOptions . Compiled | RegexOptions . Multiline ;
62+ var runMatches = Regex . Matches ( fileContent , @"[;]{0,1}Run(\d+).*" , regOptions ) ;
63+ int lastRun = 0 ;
64+ if ( runMatches . Count > 0 && runMatches [ runMatches . Count - 1 ] . Groups . Count == 2 )
65+ lastRun = Convert . ToInt32 ( runMatches [ runMatches . Count - 1 ] . Groups [ 1 ] . Value ) ;
66+
67+ var runIfMatches = Regex . Matches ( fileContent , @"[;]{0,1}RunIf(\d+).*" , regOptions ) ;
68+ int lastRunIf = 0 ;
69+ if ( runIfMatches . Count > 0 && runIfMatches [ runIfMatches . Count - 1 ] . Groups . Count == 2 )
70+ lastRunIf = Convert . ToInt32 ( runIfMatches [ runIfMatches . Count - 1 ] . Groups [ 1 ] . Value ) ;
71+
72+ if ( Regex . IsMatch ( fileContent , @"^[;]{0,1}Run(\d+).*WorkingTitle2GSX\.exe" , regOptions ) )
6373 {
64- fileContent = Regex . Replace ( fileContent , pattern , $ "RunIf$1 ={ programParam } ,KILL,{ Parameters . binPath } ", RegexOptions . Compiled | RegexOptions . Multiline ) ;
74+ fileContent = Regex . Replace ( fileContent , @"^[;]{0,1}Run(\d+).*WorkingTitle2GSX\.exe" , $ "RunIf{ lastRunIf + 1 } ={ programParam } ,KILL,{ Parameters . binPath } ", regOptions ) ;
6575 File . WriteAllText ( regPath , fileContent , Encoding . Default ) ;
6676 result = true ;
6777 }
68- else
78+ else if ( Regex . IsMatch ( fileContent , @"^[;]{0,1}RunIf(\d+).*WorkingTitle2GSX\.exe" , regOptions ) )
6979 {
70- int posLastRunBegin = fileContent . LastIndexOf ( "RunIf" ) ;
71- int posLastRunEnd = fileContent . IndexOf ( '\n ' , posLastRunBegin ) ;
72- int lastIndex = Convert . ToInt32 ( fileContent . Substring ( posLastRunBegin + 5 , 1 ) ) ;
73- fileContent = fileContent . Insert ( posLastRunEnd + 1 , $ "RunIf{ lastIndex + 1 } ={ programParam } ,KILL,{ Parameters . binPath } \r \n ") ;
80+ fileContent = Regex . Replace ( fileContent , @"^[;]{0,1}RunIf(\d+).*WorkingTitle2GSX\.exe" , $ "RunIf$1={ programParam } ,KILL,{ Parameters . binPath } ", regOptions ) ;
7481 File . WriteAllText ( regPath , fileContent , Encoding . Default ) ;
7582 result = true ;
7683 }
84+ else
85+ {
86+ int index = - 1 ;
87+ if ( runIfMatches . Count > 0 && runMatches . Count > 0 )
88+ {
89+ index = runIfMatches [ runIfMatches . Count - 1 ] . Index + runIfMatches [ runIfMatches . Count - 1 ] . Length ;
90+ if ( runMatches [ runMatches . Count - 1 ] . Index > runIfMatches [ runIfMatches . Count - 1 ] . Index )
91+ index = runMatches [ runMatches . Count - 1 ] . Index + runMatches [ runMatches . Count - 1 ] . Length ;
92+ }
93+ else if ( runIfMatches . Count > 0 )
94+ index = runIfMatches [ runIfMatches . Count - 1 ] . Index + runIfMatches [ runIfMatches . Count - 1 ] . Length ;
95+ else if ( runMatches . Count > 0 )
96+ index = runMatches [ runMatches . Count - 1 ] . Index + runMatches [ runMatches . Count - 1 ] . Length ;
97+
98+ if ( index > 0 )
99+ {
100+ fileContent = fileContent . Insert ( index + 1 , $ "RunIf{ lastRunIf + 1 } ={ programParam } ,KILL,{ Parameters . binPath } \r \n ") ;
101+ File . WriteAllText ( regPath , fileContent , Encoding . Default ) ;
102+ result = true ;
103+ }
104+ else
105+ {
106+ fileContent = Regex . Replace ( fileContent , @"^\[Programs\]\r\n" , $ "[Programs]\r \n RunIf{ lastRunIf + 1 } ={ programParam } ,KILL,{ Parameters . binPath } \r \n ", regOptions ) ;
107+ File . WriteAllText ( regPath , fileContent , Encoding . Default ) ;
108+ result = true ;
109+ }
110+ }
77111 }
78112 }
79113 }
0 commit comments