33using System ;
44using System . Collections . Generic ;
55using System . Linq ;
6+ using System . Reflection ;
67using OpenMod . Installer . RocketMod . Jobs ;
78using SDG . Unturned ;
89
@@ -11,6 +12,7 @@ namespace OpenMod.Installer.RocketMod.Commands
1112 public class CommandOpenMod : IRocketCommand
1213 {
1314 private static readonly List < IJob > s_Jobs = new List < IJob > ( ) ;
15+ private static readonly List < CommandWindowInputted > s_RocketModComandWindowsDelegates = new List < CommandWindowInputted > ( ) ;
1416 private static CommandStep s_CurrentStep ;
1517
1618 public void Execute ( IRocketPlayer caller , string [ ] command )
@@ -35,8 +37,9 @@ public void Execute(IRocketPlayer caller, string[] command)
3537 }
3638
3739 Reset ( ) ;
38- Logger . Log ( "Starting OpenMod installation." ) ;
39- Logger . Log ( "Type \" cancel\" to cancel installation." ) ;
40+ Logger . Log ( "Starting OpenMod installation.." , ConsoleColor . DarkCyan ) ;
41+ Logger . Log ( "Visit https://openmod.github.io/openmod-docs for information." , ConsoleColor . DarkCyan ) ;
42+ Logger . Log ( "Type \" cancel\" to cancel." , ConsoleColor . Cyan ) ;
4043
4144 BindCommandInput ( ) ;
4245
@@ -50,20 +53,23 @@ public void Execute(IRocketPlayer caller, string[] command)
5053 private void BindCommandInput ( )
5154 {
5255 CommandWindow . onCommandWindowInputted += OnCommandWindowInputted ;
56+ RemoveRocketModConsoleInput ( ) ;
5357 }
5458
5559 private void UnbindCommandInput ( )
5660 {
5761 CommandWindow . onCommandWindowInputted -= OnCommandWindowInputted ;
62+ RestoreRocketModConsoleInput ( ) ;
5863 }
5964
6065 private void OnCommandWindowInputted ( string text , ref bool shouldexecutecommand )
6166 {
67+ shouldexecutecommand = false ;
6268 text = text . Trim ( ) ;
6369
6470 if ( text . Equals ( "cancel" , StringComparison . OrdinalIgnoreCase ) )
6571 {
66- Logger . Log ( "Aborting OpenMod installation." ) ;
72+ Logger . Log ( "OpenMod installation aborted." , ConsoleColor . DarkRed ) ;
6773 Reset ( ) ;
6874 return ;
6975 }
@@ -77,31 +83,92 @@ private void OnCommandWindowInputted(string text, ref bool shouldexecutecommand)
7783 {
7884 PerformMigration ( ) ;
7985 }
86+ else
87+ {
88+ PrintStep ( s_CurrentStep ) ;
89+ }
8090 }
81- catch ( InvalidOperationException )
91+ catch ( InvalidChoiceException ex )
8292 {
83- Logger . Log ( $ "Invalid choice: { text } ." ) ;
93+ Logger . Log ( $ "Invalid choice: { ex . Choice } . Type \" cancel \" to cancel OpenMod installation." , ConsoleColor . Red ) ;
8494 PrintStep ( s_CurrentStep ) ;
8595 }
8696 }
8797
98+ private void PrintStep ( CommandStep step )
99+ {
100+ Logger . Log ( string . Empty ) ;
101+ Logger . Log ( step . Text , ConsoleColor . DarkCyan ) ;
102+
103+ foreach ( var choice in step . Choices )
104+ {
105+ Logger . Log ( $ " { choice . Name } : { choice . Description } ", ConsoleColor . Cyan ) ;
106+ }
107+ }
108+
88109 private void PerformMigration ( )
89110 {
90- JobsExecutor . Execute ( s_Jobs ) ;
91- Reset ( ) ;
111+ try
112+ {
113+ JobsExecutor . Execute ( s_Jobs ) ;
114+ }
115+ finally
116+ {
117+ Reset ( ) ;
118+ }
92119 }
93120
94- private void PrintStep ( CommandStep step )
121+ private void RemoveRocketModConsoleInput ( )
95122 {
96- Logger . Log ( string . Empty ) ;
97- Logger . Log ( step . Text ) ;
123+ if ( s_RocketModComandWindowsDelegates . Any ( ) )
124+ {
125+ return ;
126+ }
98127
99- foreach ( var choice in step . Choices )
128+ var commandWindowInputedInvocationList = CommandWindow . onCommandWindowInputted . GetInvocationList ( ) ;
129+ foreach ( var @delegate in commandWindowInputedInvocationList )
100130 {
101- Logger . Log ( $ " { choice . Name } : { choice . Description } ") ;
131+ if ( ! IsRocketModDelegate ( @delegate ) )
132+ {
133+ continue ;
134+ }
135+
136+ var rocketModDelegate = ( CommandWindowInputted ) @delegate ;
137+ CommandWindow . onCommandWindowInputted -= rocketModDelegate ;
138+ s_RocketModComandWindowsDelegates . Add ( rocketModDelegate ) ;
102139 }
103140 }
104141
142+ private void RestoreRocketModConsoleInput ( )
143+ {
144+ foreach ( var rocketModDelegate in s_RocketModComandWindowsDelegates )
145+ {
146+ CommandWindow . onCommandWindowInputted += rocketModDelegate ;
147+ }
148+
149+ s_RocketModComandWindowsDelegates . Clear ( ) ;
150+ }
151+
152+ private bool IsRocketModDelegate ( Delegate ? @delegate )
153+ {
154+ if ( @delegate == null )
155+ {
156+ return false ;
157+ }
158+
159+ var methodInfo = @delegate . GetMethodInfo ( ) ;
160+ var assembly = methodInfo ? . DeclaringType ? . Assembly ;
161+
162+ if ( assembly == null )
163+ {
164+ return false ;
165+ }
166+
167+ var assemblyName = assembly . GetName ( ) ;
168+ return assemblyName . Name . Equals ( "Rocket.Unturned" )
169+ || assemblyName . Name . Equals ( "Rocket.Core" ) ;
170+ }
171+
105172 private void Reset ( )
106173 {
107174 s_CurrentStep = null ;
0 commit comments