-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathTestFlightFailure_Engine.cs
185 lines (161 loc) · 5.84 KB
/
TestFlightFailure_Engine.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TestFlightAPI;
namespace TestFlight
{
public class TestFlightFailure_Engine : TestFlightFailureBase
{
[KSPField]
public string engineID = "all";
protected class EngineHandler
{
public EngineModuleWrapper.EngineIgnitionState ignitionState;
public EngineModuleWrapper engine;
public bool failEngine;
}
protected List<EngineHandler> engines = new List<EngineHandler>();
protected ITestFlightCore core = null;
protected Dictionary<int, CachedEngineState> engineStates;
public bool IsMajor => string.Equals(severity, "major", StringComparison.OrdinalIgnoreCase);
public new bool TestFlightEnabled
{
get
{
if (core != null && engines.Count > 0)
return core.TestFlightEnabled;
core = TestFlightUtil.GetCore(part, Configuration);
if (core == null)
Log("EngineBase: No TestFlight core found");
else if (engines.Count == 0)
Log("EngineBase: No valid engines found");
return false;
}
}
public override void OnStart(StartState state)
{
base.OnStart(state);
core = TestFlightUtil.GetCore(part, Configuration);
Startup();
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
ConfigNode[] cNodes = node.GetNodes("ENGINESTATE");
if (cNodes.Length > 0 && engineStates == null)
engineStates = new Dictionary<int, CachedEngineState>();
foreach (ConfigNode cNode in cNodes)
{
int id = 0;
if (!cNode.TryGetValue("id", ref id))
{
Debug.LogError("[TestFlight] Invalid ENGINESTATE data in OnLoad:" + cNode);
continue;
}
var state = new CachedEngineState(cNode);
engineStates.Add(id, state);
}
}
public override void OnSave(ConfigNode node)
{
base.OnSave(node);
if (engineStates?.Count > 0)
{
foreach (KeyValuePair<int, CachedEngineState> kvp in engineStates)
{
var cn = new ConfigNode("ENGINESTATE");
kvp.Value.Save(cn);
cn.AddValue("id", kvp.Key);
node.AddNode(cn);
}
}
}
private void AddEngine(EngineModuleWrapper engine)
{
var engineHandler = new EngineHandler();
engineHandler.engine = engine;
engineHandler.ignitionState = engine.IgnitionState;
engines.Add(engineHandler);
}
public virtual void Startup()
{
engines.Clear();
if (!string.IsNullOrEmpty(engineID))
{
if (engineID.Equals("all", StringComparison.OrdinalIgnoreCase))
{
List<ModuleEngines> engineMods = this.part.Modules.GetModules<ModuleEngines>();
foreach (ModuleEngines eng in engineMods)
{
string id = eng.engineID;
EngineModuleWrapper engine = new EngineModuleWrapper();
engine.InitWithEngine(this.part, id);
AddEngine(engine);
}
}
else if (engineID.Contains(","))
{
string[] sEngineIndices = engineID.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string sEngineIndex in sEngineIndices)
{
EngineModuleWrapper engine = new EngineModuleWrapper();
engine.InitWithEngine(this.part, sEngineIndex);
AddEngine(engine);
}
}
else
{
EngineModuleWrapper engine = new EngineModuleWrapper();
engine.InitWithEngine(this.part, engineID);
AddEngine(engine);
}
}
else
{
EngineModuleWrapper engine = new EngineModuleWrapper();
engine.Init(this.part);
AddEngine(engine);
}
}
private void OnEnable()
{
StartCoroutine(UpdateEngineStatus());
}
private void OnDisable()
{
StopCoroutine(UpdateEngineStatus());
}
public override void SetActiveConfig(string alias)
{
base.SetActiveConfig(alias);
if (currentConfig == null) return;
// update current values with those from the current config node
currentConfig.TryGetValue("engineID", ref engineID);
}
public override float DoRepair()
{
foreach (EngineHandler engine in engines)
{
engine.engine.ResetStatus();
}
return base.DoRepair();
}
private readonly WaitForFixedUpdate _wait = new WaitForFixedUpdate();
public IEnumerator UpdateEngineStatus()
{
while (true)
{
yield return _wait;
if (Failed)
{
foreach (var handler in engines)
{
handler.engine.Status = "<color=orange>Failed</color>";
handler.engine.StatusL2 = $"<color=orange>{failureTitle}</color>";
}
}
}
}
}
}