Skip to content

Commit 65bdcf7

Browse files
committed
Merge pull request RemoteTechnologiesGroup#416 from Peppie23/new_cancel_command
CancelCommand refactoring and changes
2 parents 5413d27 + 3a0c66f commit 65bdcf7

3 files changed

Lines changed: 87 additions & 20 deletions

File tree

src/RemoteTech/FlightComputer/Commands/AbstractCommand.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace RemoteTech.FlightComputer.Commands
55
public abstract class AbstractCommand : ICommand
66
{
77
public double TimeStamp { get; set; }
8+
public Guid CmdGuid { get; private set; }
89
public virtual double ExtraDelay { get; set; }
910
public virtual double Delay { get { return Math.Max(TimeStamp - RTUtil.GameTime, 0); } }
1011
public virtual String Description {
@@ -23,6 +24,14 @@ public virtual String Description {
2324
public abstract String ShortName { get; }
2425
public virtual int Priority { get { return 255; } }
2526

27+
/// <summary>
28+
/// Creates a new Guid for the current command
29+
/// </summary>
30+
public AbstractCommand()
31+
{
32+
this.CmdGuid = Guid.NewGuid();
33+
}
34+
2635
// true: move to active.
2736
public virtual bool Pop(FlightComputer f) { return false; }
2837

@@ -50,15 +59,16 @@ public virtual void Save(ConfigNode node, FlightComputer computer)
5059
}
5160
catch (Exception) {}
5261

53-
if (Delay == 0) {
62+
if (this.Delay == 0) {
5463
// only save the current gametime if we have no signal delay.
5564
// We need this to calculate the correct delta time for the
5665
// ExtraDelay if we come back to this satellite.
57-
TimeStamp = RTUtil.GameTime;
66+
this.TimeStamp = RTUtil.GameTime;
5867
}
5968

60-
node.AddValue("TimeStamp", TimeStamp);
61-
node.AddValue("ExtraDelay", ExtraDelay);
69+
node.AddValue("TimeStamp", this.TimeStamp);
70+
node.AddValue("ExtraDelay", this.ExtraDelay);
71+
node.AddValue("CmdGuid", this.CmdGuid);
6272
}
6373

6474
/// <summary>
@@ -78,6 +88,10 @@ public virtual bool Load(ConfigNode n, FlightComputer fc)
7888
{
7989
ExtraDelay = double.Parse(n.GetValue("ExtraDelay"));
8090
}
91+
if (n.HasValue("CmdGuid"))
92+
{
93+
this.CmdGuid = new Guid(n.GetValue("CmdGuid"));
94+
}
8195

8296
return true;
8397
}
@@ -111,7 +125,7 @@ public static ICommand LoadCommand(ConfigNode n, FlightComputer fc)
111125
ConfigNode.LoadObjectFromConfig(command, n);
112126
// additional loadings
113127
var result = command.Load(n, fc);
114-
RTLog.Verbose("Loading command {0}={1}", RTLogLevel.LVL1, n.name, result);
128+
RTLog.Verbose("Loading command {0}({1})={2}", RTLogLevel.LVL1, n.name, command.CmdGuid, result);
115129
// delete command if we can't load the command correctlys
116130
if (result == false)
117131
command = null;

src/RemoteTech/FlightComputer/Commands/CancelCommand.cs

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/RemoteTech/FlightComputer/Commands/ICommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public interface ICommand : IComparable<ICommand>
66
{
77
double TimeStamp { get; set; }
88
double ExtraDelay { get; set; }
9+
Guid CmdGuid { get; }
910
double Delay { get; }
1011
// The command description displayed in the flight computer
1112
String Description { get; }

0 commit comments

Comments
 (0)