@@ -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 ;
0 commit comments