@@ -6,22 +6,24 @@ namespace RemoteTech.FlightComputer.Commands
66 public class CancelCommand : AbstractCommand
77 {
88 public override double ExtraDelay { get { return base . ExtraDelay ; } set { return ; } }
9- public ICommand Command ;
10- [ Persistent ] public int queueIndex ;
9+ private Guid CancelCmdGuid ;
1110
1211 public override string Description { get { return "Cancelling a command." + Environment . NewLine + base . Description ; } }
1312 public override string ShortName { get { return "Cancel command" ; } }
1413
15- public override bool Pop ( FlightComputer f )
14+ public override bool Pop ( FlightComputer computer )
1615 {
17- if ( Command == null )
16+ if ( this . CancelCmdGuid != Guid . Empty )
1817 {
19- f . Reset ( ) ;
18+ this . cancelQueuedCommand ( this . CancelCmdGuid , computer ) ;
2019 }
2120 else
2221 {
23- f . Remove ( Command ) ;
22+ // we've no CancelCmdGuid for an active command. But
23+ // maybe we'll use this later
24+ this . cancelActiveCommand ( this . CancelCmdGuid , computer ) ;
2425 }
26+
2527 return false ;
2628 }
2729
@@ -30,7 +32,7 @@ public static CancelCommand WithCommand(ICommand cmd)
3032
3133 return new CancelCommand ( )
3234 {
33- Command = cmd ,
35+ CancelCmdGuid = cmd . CmdGuid ,
3436 TimeStamp = RTUtil . GameTime ,
3537 } ;
3638 }
@@ -39,7 +41,7 @@ public static CancelCommand ResetActive()
3941 {
4042 return new CancelCommand ( )
4143 {
42- Command = null ,
44+ CancelCmdGuid = Guid . Empty ,
4345 TimeStamp = RTUtil . GameTime ,
4446 } ;
4547 }
@@ -48,23 +50,73 @@ public static CancelCommand ResetActive()
4850 /// Load the saved CancelCommand and find the element to cancel, based on the saved queue position
4951 /// </summary>
5052 /// <returns>true - loaded successfull</returns>
51- public override bool Load ( ConfigNode n , FlightComputer fc )
53+ public override bool Load ( ConfigNode n , FlightComputer computer )
5254 {
53- if ( base . Load ( n , fc ) )
55+ if ( base . Load ( n , computer ) )
5456 {
55- Command = fc . QueuedCommands . ElementAt ( queueIndex ) ;
56- return true ;
57+ if ( n . HasValue ( "CancelCmdGuid" ) )
58+ {
59+ this . CancelCmdGuid = new Guid ( n . GetValue ( "CancelCmdGuid" ) ) ;
60+ }
61+
62+ // old way to cancel a command
63+ if ( n . HasValue ( "queueIndex" ) )
64+ {
65+ try
66+ {
67+ int queueIndex = int . Parse ( n . GetValue ( "queueIndex" ) ) ;
68+ // try to find the command to cancel
69+ this . CancelCmdGuid = computer . QueuedCommands . ElementAt ( queueIndex ) . CmdGuid ;
70+ }
71+ catch ( Exception )
72+ { }
73+ }
74+
75+ // loaded successfull
76+ if ( this . CancelCmdGuid != Guid . Empty )
77+ return true ;
5778 }
5879 return false ;
5980 }
6081
6182 /// <summary>
6283 /// Saves the queue index for this command to the persist
6384 /// </summary>
64- public override void Save ( ConfigNode n , FlightComputer fc )
85+ public override void Save ( ConfigNode n , FlightComputer computer )
86+ {
87+ base . Save ( n , computer ) ;
88+ n . AddValue ( "CancelCmdGuid" , this . CancelCmdGuid ) ;
89+ }
90+
91+ /// <summary>
92+ /// Cancels a queued command by it's guid
93+ /// </summary>
94+ /// <param name="cmdGuid">Guid for the command to cancel</param>
95+ /// <param name="computer">Current flightcomputer</param>
96+ /// <returns>True if we canceld the command</returns>
97+ private bool cancelQueuedCommand ( Guid cmdGuid , FlightComputer computer )
98+ {
99+ ICommand searchCmd = computer . QueuedCommands . Where ( cmd => cmd . CmdGuid == cmdGuid ) . FirstOrDefault ( ) ;
100+ if ( searchCmd != null )
101+ {
102+ computer . Remove ( searchCmd ) ;
103+ return true ;
104+ }
105+
106+ return false ;
107+ }
108+
109+ /// <summary>
110+ /// Cancels the current active command.
111+ /// </summary>
112+ /// <param name="cmdGuid">Unused right now</param>
113+ /// <param name="computer">Current flightcomputer</param>
114+ /// <returns></returns>
115+ private bool cancelActiveCommand ( Guid cmdGuid , FlightComputer computer )
65116 {
66- queueIndex = fc . QueuedCommands . ToList ( ) . IndexOf ( Command ) ;
67- base . Save ( n , fc ) ;
117+ computer . Reset ( ) ;
118+
119+ return true ;
68120 }
69121 }
70122}
0 commit comments