@@ -7,31 +7,35 @@ namespace RemoteTech.FlightComputer
7
7
{
8
8
public class EventCommand : AbstractCommand
9
9
{
10
+ // Guiname of the BaseEvent
10
11
[ Persistent ] public string GUIName ;
11
- [ Persistent ] public int PartId ;
12
+ // flight id of the part by this BaseEvent
13
+ [ Persistent ] public uint flightID ;
14
+ // PartModule of the part by this BaseEvent
12
15
[ Persistent ] public string Module ;
13
-
16
+ // BaseEvent to invoke
14
17
public BaseEvent BaseEvent = null ;
15
18
16
19
public override String Description
17
20
{
18
21
get
19
22
{
20
- return BaseEvent . listParent . part . partInfo . title + ": " + BaseEvent . GUIName +
23
+ return ( ( this . BaseEvent != null ) ? this . BaseEvent . listParent . part . partInfo . title + ": " + this . BaseEvent . GUIName : "none" ) +
21
24
Environment . NewLine + base . Description ;
22
25
}
23
26
}
24
27
public override string ShortName
25
28
{
26
29
get
27
30
{
28
- return BaseEvent . GUIName ;
31
+ return ( this . BaseEvent != null ) ? this . BaseEvent . GUIName : "none" ;
29
32
}
30
33
}
31
34
32
35
public override bool Pop ( FlightComputer f )
33
36
{
34
- BaseEvent . Invoke ( ) ;
37
+ if ( this . BaseEvent != null )
38
+ this . BaseEvent . Invoke ( ) ;
35
39
36
40
return false ;
37
41
}
@@ -41,9 +45,6 @@ public static EventCommand Event(BaseEvent ev)
41
45
return new EventCommand ( )
42
46
{
43
47
BaseEvent = ev ,
44
- GUIName = ev . GUIName ,
45
- PartId = FlightGlobals . ActiveVessel . parts . ToList ( ) . IndexOf ( ev . listParent . part ) ,
46
- Module = ev . listParent . module . ClassName . ToString ( ) ,
47
48
TimeStamp = RTUtil . GameTime ,
48
49
} ;
49
50
}
@@ -55,17 +56,59 @@ public override void Load(ConfigNode n, FlightComputer fc)
55
56
{
56
57
base . Load ( n , fc ) ;
57
58
58
- PartId = int . Parse ( n . GetValue ( "PartId" ) ) ;
59
+ // deprecated since 1.6.2, we need this for upgrading from 1.6.x => 1.6.2
60
+ int PartId = 0 ;
61
+ {
62
+ if ( n . HasValue ( "PartId" ) )
63
+ PartId = int . Parse ( n . GetValue ( "PartId" ) ) ;
64
+ }
65
+
66
+ if ( n . HasValue ( "flightID" ) )
67
+ this . flightID = uint . Parse ( n . GetValue ( "flightID" ) ) ;
68
+
59
69
Module = n . GetValue ( "Module" ) ;
60
70
GUIName = n . GetValue ( "GUIName" ) ;
61
71
62
- Part part = FlightGlobals . ActiveVessel . parts [ PartId ] ;
63
- PartModule partmodule = part . Modules [ Module ] ;
64
- BaseEventList eventlist = new BaseEventList ( part , partmodule ) ;
65
- if ( eventlist . Count > 0 )
72
+ RTLog . Notify ( "Try to load an EventCommand from persistent with {0},{1},{2},{3}" , PartId , flightID , Module , GUIName ) ;
73
+
74
+ Part part = null ;
75
+ var partlist = FlightGlobals . ActiveVessel . parts ;
76
+
77
+ if ( this . flightID == 0 )
78
+ {
79
+ // only look with the partid if we've enough parts
80
+ if ( PartId < partlist . Count )
81
+ part = partlist . ElementAt ( PartId ) ;
82
+ }
83
+ else
84
+ {
85
+ part = partlist . Where ( p => p . flightID == this . flightID ) . FirstOrDefault ( ) ;
86
+ }
87
+
88
+ if ( part != null )
66
89
{
67
- BaseEvent = eventlist . Where ( ba => ba . GUIName == GUIName ) . FirstOrDefault ( ) ;
90
+ PartModule partmodule = part . Modules [ Module ] ;
91
+ if ( partmodule != null )
92
+ {
93
+ BaseEventList eventlist = new BaseEventList ( part , partmodule ) ;
94
+ if ( eventlist . Count > 0 )
95
+ {
96
+ this . BaseEvent = eventlist . Where ( ba => ba . GUIName == this . GUIName ) . FirstOrDefault ( ) ;
97
+ }
98
+ }
68
99
}
69
100
}
101
+
102
+ /// <summary>
103
+ /// Save the BaseEvent to the persistent
104
+ /// </summary>
105
+ public override void Save ( ConfigNode n , FlightComputer fc )
106
+ {
107
+ GUIName = this . BaseEvent . GUIName ;
108
+ flightID = this . BaseEvent . listParent . module . part . flightID ;
109
+ Module = this . BaseEvent . listParent . module . ClassName . ToString ( ) ;
110
+
111
+ base . Save ( n , fc ) ;
112
+ }
70
113
}
71
114
}
0 commit comments