Skip to content

Commit a98ff4d

Browse files
committed
Small fixes
I changed the saving method for each active and queued command. We always passed the complete `flighcomputer`-Node to each command for saving. So this can cause problems by saving a command-data to his own node. The `ExternalApiCommand` will now save the passed externalData ConfigNode from the API call to the persistent correctly.
1 parent 4828ed7 commit a98ff4d

4 files changed

Lines changed: 81 additions & 42 deletions

File tree

src/RemoteTech/FlightComputer/Commands/AbstractCommand.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ public int CompareTo(ICommand dc)
3939
/// <summary>
4040
/// Save the basic informations for every command.
4141
/// </summary>
42-
/// <param name="n">Node to save in</param>
43-
/// <param name="fc">Current flightcomputer</param>
44-
public virtual void Save(ConfigNode n, FlightComputer fc)
42+
/// <param name="node">Node to save in</param>
43+
/// <param name="computer">Current flightcomputer</param>
44+
public virtual void Save(ConfigNode node, FlightComputer computer)
4545
{
46-
ConfigNode save = new ConfigNode(this.GetType().Name);
4746
try
4847
{
4948
// try to serialize 'this'
50-
ConfigNode.CreateConfigFromObject(this, 0, save);
49+
ConfigNode.CreateConfigFromObject(this, 0, node);
5150
}
5251
catch (Exception) {}
5352

@@ -58,9 +57,8 @@ public virtual void Save(ConfigNode n, FlightComputer fc)
5857
TimeStamp = RTUtil.GameTime;
5958
}
6059

61-
save.AddValue("TimeStamp", TimeStamp);
62-
save.AddValue("ExtraDelay", ExtraDelay);
63-
n.AddNode(save);
60+
node.AddValue("TimeStamp", TimeStamp);
61+
node.AddValue("ExtraDelay", ExtraDelay);
6462
}
6563

6664
/// <summary>

src/RemoteTech/FlightComputer/Commands/ExternalAPICommand.cs

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ namespace RemoteTech.FlightComputer.Commands
55
{
66
public class ExternalAPICommand : AbstractCommand
77
{
8+
/// <summary>original ConfigNode object passed from the api</summary>
9+
private ConfigNode externalData;
810
/// <summary>Name of the mod who passed this command</summary>
9-
[Persistent] private string Executor;
11+
private string Executor;
1012
/// <summary>Label for this command on the queue</summary>
11-
[Persistent] private string QueueLabel;
13+
private string QueueLabel;
1214
/// <summary>Label for this command if its active</summary>
13-
[Persistent] private string ActiveLabel;
15+
private string ActiveLabel;
1416
/// <summary>Label for alert on no power</summary>
15-
[Persistent] private string ShortLabel;
17+
private string ShortLabel;
1618
/// <summary>The ReflectionType for the methods to invoke</summary>
17-
[Persistent] private string ReflectionType;
19+
private string ReflectionType;
1820
/// <summary>Name of the Pop-method on the ReflectionType</summary>
19-
[Persistent] private string ReflectionPopMethod = "";
21+
private string ReflectionPopMethod = "";
2022
/// <summary>Name of the Execution-method on the ReflectionType</summary>
21-
[Persistent] private string ReflectionExecuteMethod = "";
23+
private string ReflectionExecuteMethod = "";
2224
/// <summary>Name of the Abort-method on the ReflectionType</summary>
23-
[Persistent] private string ReflectionAbortMethod = "";
25+
private string ReflectionAbortMethod = "";
2426
/// <summary>GUID of the vessel</summary>
25-
[Persistent] private string GUIDString;
27+
private string GUIDString;
2628
/// <summary>true - when this command will be aborted</summary>
2729
private bool AbortCommand = false;
2830

@@ -127,45 +129,73 @@ public static ExternalAPICommand FromExternal(ConfigNode externalData)
127129
{
128130
ExternalAPICommand command = new ExternalAPICommand();
129131
command.TimeStamp = RTUtil.GameTime;
130-
command.Executor = externalData.GetValue("Executor");
131-
command.ReflectionType = externalData.GetValue("ReflectionType");
132-
command.GUIDString = externalData.GetValue("GUIDString");
132+
command.ConfigNodeToObject(command,externalData);
133133

134-
if (externalData.HasValue("QueueLabel"))
135-
command.QueueLabel = externalData.GetValue("QueueLabel");
136-
if (externalData.HasValue("ActiveLabel"))
137-
command.ActiveLabel = externalData.GetValue("ActiveLabel");
138-
if (externalData.HasValue("ShortLabel"))
139-
command.ShortLabel = externalData.GetValue("ShortLabel");
134+
return command;
135+
}
140136

137+
/// <summary>
138+
/// Maps the ConfigNode object passed from the api or loading to an ExternalAPICommand.
139+
/// </summary>
140+
/// <param name="command">Map the data to this object</param>
141+
/// <param name="data">Data to map onto the command</param>
142+
private void ConfigNodeToObject(ExternalAPICommand command, ConfigNode data)
143+
{
144+
command.externalData = new ConfigNode("ExternalData").AddNode(data);
145+
command.Executor = data.GetValue("Executor");
146+
command.ReflectionType = data.GetValue("ReflectionType");
147+
command.GUIDString = data.GetValue("GUIDString");
148+
149+
if (data.HasValue("QueueLabel"))
150+
command.QueueLabel = data.GetValue("QueueLabel");
151+
if (data.HasValue("ActiveLabel"))
152+
command.ActiveLabel = data.GetValue("ActiveLabel");
153+
if (data.HasValue("ShortLabel"))
154+
command.ShortLabel = data.GetValue("ShortLabel");
155+
156+
if (data.HasValue("ReflectionPopMethod"))
157+
command.ReflectionPopMethod = data.GetValue("ReflectionPopMethod");
158+
if (data.HasValue("ReflectionExecuteMethod"))
159+
command.ReflectionExecuteMethod = data.GetValue("ReflectionExecuteMethod");
160+
if (data.HasValue("ReflectionAbortMethod"))
161+
command.ReflectionAbortMethod = data.GetValue("ReflectionAbortMethod");
162+
}
141163

142-
if (externalData.HasValue("ReflectionPopMethod"))
143-
command.ReflectionPopMethod = externalData.GetValue("ReflectionPopMethod");
144-
if (externalData.HasValue("ReflectionExecuteMethod"))
145-
command.ReflectionExecuteMethod = externalData.GetValue("ReflectionExecuteMethod");
146-
if (externalData.HasValue("ReflectionAbortMethod"))
147-
command.ReflectionAbortMethod = externalData.GetValue("ReflectionAbortMethod");
164+
/// <summary>
165+
/// Saves the original configNode <see cref="externalData"/> passed from the api
166+
/// to the persistent
167+
/// </summary>
168+
/// <param name="node">Node with the command infos to save in</param>
169+
/// <param name="computer">Current flightcomputer</param>
170+
public override void Save(ConfigNode node, FlightComputer computer)
171+
{
172+
base.Save(node, computer);
148173

149-
return command;
174+
node.AddNode("ExternalData");
175+
node.SetNode("ExternalData", this.externalData);
150176
}
151177

152178
/// <summary>
153179
/// Loads the ExternalAPICommand from the persistent file. If the loading
154180
/// can't find the <see cref="ReflectionType"/> we'll notify a ScreenMessage
155181
/// and remove the command.
156182
/// </summary>
157-
/// <param name="n">Node with the command infos</param>
183+
/// <param name="node">Node with the command infos to load</param>
158184
/// <param name="computer">Current flightcomputer</param>
159185
/// <returns>true - loaded successfull</returns>
160-
public override bool Load(ConfigNode n, FlightComputer computer)
186+
public override bool Load(ConfigNode node, FlightComputer computer)
161187
{
162188
try
163189
{
164-
if(base.Load(n, computer))
190+
if(base.Load(node, computer))
165191
{
166-
// try loading the reflectionType
167-
this.getReflectionType(this.ReflectionType);
168-
return true;
192+
if (node.HasNode("ExternalData"))
193+
{
194+
this.ConfigNodeToObject(this, node.GetNode("ExternalData"));
195+
// try loading the reflectionType
196+
this.getReflectionType(this.ReflectionType);
197+
return true;
198+
}
169199
}
170200
}
171201
catch(Exception)
@@ -181,7 +211,8 @@ public override bool Load(ConfigNode n, FlightComputer computer)
181211
/// <returns>ConfigNode to pass over to the reflection methods</returns>
182212
private ConfigNode prepareDataForExternalMod()
183213
{
184-
ConfigNode externalData = ConfigNode.CreateConfigFromObject(this);
214+
ConfigNode externalData = new ConfigNode();
215+
this.externalData.CopyTo(externalData);
185216
externalData.AddValue("AbortCommand",this.AbortCommand);
186217

187218
return externalData;

src/RemoteTech/FlightComputer/FlightComputer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,19 @@ public void Save(ConfigNode n)
489489

490490
foreach (KeyValuePair<int, ICommand> cmd in mActiveCommands)
491491
{
492-
cmd.Value.Save(ActiveCommands, this);
492+
// Save each active command on his own node
493+
ConfigNode activeCommandNode = new ConfigNode(cmd.Value.GetType().Name);
494+
cmd.Value.Save(activeCommandNode, this);
495+
ActiveCommands.AddNode(activeCommandNode);
493496
}
494497

495498
foreach (ICommand cmd in mCommandQueue)
496-
cmd.Save(Commands, this);
499+
{
500+
// Save each command on his own node
501+
ConfigNode commandNode = new ConfigNode(cmd.GetType().Name);
502+
cmd.Save(commandNode, this);
503+
Commands.AddNode(commandNode);
504+
}
497505

498506
ConfigNode FlightNode = new ConfigNode("FlightComputer");
499507
FlightNode.AddValue("TotalDelay", TotalDelay);

src/RemoteTech/UI/DebugWindow.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ private void drawAPITester()
433433
dataNode.AddValue("ReflectionExecuteMethod", "ReceiveDataExec");
434434
dataNode.AddValue("ReflectionAbortMethod", "ReceiveDataAbort");
435435
dataNode.AddValue("GUIDString", this.ReceivDataVesselGuidInput);
436+
dataNode.AddValue("YourData1", "RemoteTech");
437+
dataNode.AddValue("YourDataN", "TechRemote");
436438

437439
var result = RemoteTech.API.API.QueueCommandToFlightComputer(dataNode);
438440

0 commit comments

Comments
 (0)