forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventCommand.cs
More file actions
132 lines (114 loc) · 4.36 KB
/
EventCommand.cs
File metadata and controls
132 lines (114 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Linq;
using RemoteTech.FlightComputer.Commands;
namespace RemoteTech.FlightComputer
{
public class EventCommand : AbstractCommand
{
// Guiname of the BaseEvent
[Persistent] public string GUIName;
// Name of the BaseEvent
[Persistent] public string Name;
// flight id of the part by this BaseEvent
[Persistent] public uint flightID;
// PartModule of the part by this BaseEvent
[Persistent] public string Module;
// BaseEvent to invoke
public BaseEvent BaseEvent = null;
public override String Description
{
get
{
return ((this.BaseEvent != null) ? this.BaseEvent.listParent.part.partInfo.title + ": " + this.GUIName : "none") +
Environment.NewLine + base.Description;
}
}
public override string ShortName
{
get
{
return (this.BaseEvent != null) ? this.BaseEvent.GUIName : "none";
}
}
public override bool Pop(FlightComputer f)
{
if (this.BaseEvent != null)
{
try
{
// invoke the baseevent
this.BaseEvent.Invoke();
}
catch (Exception invokeException)
{
RTLog.Notify("BaseEvent invokeException by '{0}' with message: {1}",
RTLogLevel.LVL1, this.BaseEvent.guiName, invokeException.Message);
}
}
return false;
}
public static EventCommand Event(BaseEvent ev)
{
return new EventCommand()
{
BaseEvent = ev,
GUIName = ev.GUIName,
TimeStamp = RTUtil.GameTime,
};
}
/// <summary>
/// Load infos into this object and create a new BaseEvent
/// </summary>
/// <returns>true - loaded successfull</returns>
public override bool Load(ConfigNode n, FlightComputer fc)
{
if(base.Load(n, fc))
{
// deprecated since 1.6.2, we need this for upgrading from 1.6.x => 1.6.2
int PartId = 0;
{
if (n.HasValue("PartId"))
PartId = int.Parse(n.GetValue("PartId"));
}
if (n.HasValue("flightID"))
this.flightID = uint.Parse(n.GetValue("flightID"));
this.Module = n.GetValue("Module");
this.GUIName = n.GetValue("GUIName");
this.Name = n.GetValue("Name");
RTLog.Notify("Try to load an EventCommand from persistent with {0},{1},{2},{3},{4}",
PartId, this.flightID, this.Module, this.GUIName, this.Name);
Part part = null;
var partlist = FlightGlobals.ActiveVessel.parts;
if (this.flightID == 0)
{
// only look with the partid if we've enough parts
if (PartId < partlist.Count)
part = partlist.ElementAt(PartId);
}
else
{
part = partlist.Where(p => p.flightID == this.flightID).FirstOrDefault();
}
if (part == null) return false;
PartModule partmodule = part.Modules[Module];
if (partmodule == null) return false;
BaseEventList eventlist = new BaseEventList(part, partmodule);
if (eventlist.Count <= 0) return false;
this.BaseEvent = eventlist.Where(ba => (ba.GUIName == this.GUIName || ba.name == this.Name)).FirstOrDefault();
return true;
}
return false;
}
/// <summary>
/// Save the BaseEvent to the persistent
/// </summary>
public override void Save(ConfigNode n, FlightComputer fc)
{
this.GUIName = this.BaseEvent.GUIName;
this.flightID = this.BaseEvent.listParent.module.part.flightID;
this.Module = this.BaseEvent.listParent.module.ClassName.ToString();
this.Name = this.BaseEvent.name;
base.Save(n, fc);
}
}
}