Skip to content

Commit cadfe12

Browse files
committed
Merge pull request RemoteTechnologiesGroup#321 from Peppie23/fix_eventcommand_load_and_save
Fix EventCommand load/save
2 parents add9635 + ca0483f commit cadfe12

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src/RemoteTech/FlightComputer/EventCommand.cs

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,35 @@ namespace RemoteTech.FlightComputer
77
{
88
public class EventCommand : AbstractCommand
99
{
10+
// Guiname of the BaseEvent
1011
[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
1215
[Persistent] public string Module;
13-
16+
// BaseEvent to invoke
1417
public BaseEvent BaseEvent = null;
1518

1619
public override String Description
1720
{
1821
get
1922
{
20-
return BaseEvent.listParent.part.partInfo.title + ": " + BaseEvent.GUIName +
23+
return ((this.BaseEvent != null) ? this.BaseEvent.listParent.part.partInfo.title + ": " + this.BaseEvent.GUIName : "none") +
2124
Environment.NewLine + base.Description;
2225
}
2326
}
2427
public override string ShortName
2528
{
2629
get
2730
{
28-
return BaseEvent.GUIName;
31+
return (this.BaseEvent != null) ? this.BaseEvent.GUIName : "none";
2932
}
3033
}
3134

3235
public override bool Pop(FlightComputer f)
3336
{
34-
BaseEvent.Invoke();
37+
if (this.BaseEvent != null)
38+
this.BaseEvent.Invoke();
3539

3640
return false;
3741
}
@@ -41,9 +45,6 @@ public static EventCommand Event(BaseEvent ev)
4145
return new EventCommand()
4246
{
4347
BaseEvent = ev,
44-
GUIName = ev.GUIName,
45-
PartId = FlightGlobals.ActiveVessel.parts.ToList().IndexOf(ev.listParent.part),
46-
Module = ev.listParent.module.ClassName.ToString(),
4748
TimeStamp = RTUtil.GameTime,
4849
};
4950
}
@@ -55,17 +56,59 @@ public override void Load(ConfigNode n, FlightComputer fc)
5556
{
5657
base.Load(n, fc);
5758

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+
5969
Module = n.GetValue("Module");
6070
GUIName = n.GetValue("GUIName");
6171

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)
6689
{
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+
}
6899
}
69100
}
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+
}
70113
}
71114
}

0 commit comments

Comments
 (0)