/
BaseComputer.cs
225 lines (192 loc) · 7.01 KB
/
BaseComputer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Reflection;
using System.Linq;
using UnityEngine;
namespace Reviva
{
public abstract class BaseComputer : object
{
public void Reboot(ConfigNode updateConfig)
{
if (!this.IsEnabled)
return;
if (updateConfig == null)
{
LogError($"No updateConfig present, cannot reboot {ModuleName}");
return;
}
if (!ValidateComputer())
return;
FindComputerData(updateConfig);
BuildNewConfig();
RecreateComputer();
}
public ModuleIVASwitch ModuleIVASwitch => this.ivaSwitch;
public abstract bool IsEnabled
{
get;
}
public abstract string ModuleName
{
get;
}
/* -------------------------------------------------------------------------------- */
protected BaseComputer(ModuleIVASwitch ivaSwitch)
{
this.ivaSwitch = ivaSwitch;
this.part = this.ivaSwitch?.part;
this.computer = null;
this.computerData = null;
this.computerConfig = null;
Log($"Created {ModuleName} proxy for ModuleIVASwitch");
}
protected abstract ConfigNode CreateDefaultData();
protected static bool DetectAssembly(string assemblyName)
{
var assembly = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name == assemblyName);
bool enabled = (assembly != null);
Log($"Detected {assemblyName}: {enabled}");
return enabled;
}
/* -------------------------------------------------------------------------------- */
private ModuleIVASwitch ivaSwitch;
private Part part;
private PartModule computer;
private ConfigNode computerData; // ConfigNode in updateConfig
private ConfigNode computerConfig; // Duplicated MODULE ConfigNode for computer
/* -------------------------------------------------------------------------------- */
private bool ValidateComputer()
{
if (this.ivaSwitch == null)
{
LogError($"{ModuleName} has null ModuleIVASwitch");
return false;
}
if (this.part == null)
{
LogError($"{ModuleName} has null ModuleIVASwitch part");
return false;
}
if (this.part.partInfo == null)
{
LogError($"{ModuleName} has null ModuleIVASwitch part.partInfo");
return false;
}
if (this.part.partInfo.partConfig == null)
{
LogError($"{ModuleName} has null ModuleIVASwitch part.partInfo.partConfig");
return false;
}
this.computer = this.part.Modules?.GetModule(ModuleName);
if (this.computer == null)
{
Log($"No {ModuleName} found, strange but not impossible, adding one");
// This is ok, will create new one
}
return true;
}
private void FindComputerData(ConfigNode updateConfig)
{
this.computerData = updateConfig.GetNode(this.ModuleName);
if (this.computerData == null)
{
/*
* This means the subtype is not configured, usually stock or does not need
* RPM computer. Assume nothing is needed.
*/
Log($"No {ModuleName} ConfigNode in B9PartSwitch MODULE DATA: assume default");
this.computerData = CreateDefaultData();
}
}
private void BuildNewConfig()
{
this.computerConfig = new ConfigNode("MODULE");
this.computerConfig.AddValue("name", ModuleName);
this.computerData.CopyTo(this.computerConfig);
}
protected virtual void RecreateComputerPreAddHook(Part ownerPart, ConfigNode newConfig)
{
// Sub-classes may hook here to do something before the Computer is added back.
}
private void RecreateComputer()
{
ConfigNode oldConfig = FindModuleConfig(this.computer) ?? new ConfigNode();
ConfigNode newConfig = this.computerConfig;
Log($"Rebooting {ModuleName} with changed configuration");
#if REVIVA_DEBUG
Log($"OldConfig: {oldConfig}");
Log($"NewConifg: {newConfig}");
#endif
int index = -1;
if (this.computer != null)
{
index = DestroyModule(this.computer);
}
// Sub-classes may need to do more with the config: MAS needs to update partInfo.
RecreateComputerPreAddHook(this.part, newConfig);
AddModuleAt(index, newConfig);
}
private ConfigNode FindModuleConfig(PartModule module)
{
if (module == null)
return null;
ConfigNode[] modules = this.part.partInfo.partConfig.GetNodes("MODULE");
if (modules == null)
return null;
foreach (ConfigNode m in modules)
{
if (m.GetValue("name") == module.moduleName)
return m;
}
return null;
}
private int DestroyModule(PartModule module)
{
int index = this.part.Modules.IndexOf(module);
if (index < 0)
{
LogError($"Cannot find {module.moduleName} in partConfig");
return -1;
}
Log($"Destroying {module.moduleName} at index {index}");
this.part.Modules[index] = null;
UnityEngine.Object.DestroyImmediate((UnityEngine.Object)module);
return index;
}
private void AddModuleAt(int index, ConfigNode newConfig)
{
string moduleName = newConfig.GetValue("name");
if (moduleName == null)
{
LogError("Cannot create new module, no name entry in ConfigNode");
return;
}
Log($"Adding new {moduleName}");
PartModule newModule = this.part.AddModule(newConfig, /*forceAwake=*/true);
if (newModule == null)
{
LogError($"Cannot create new {moduleName}");
return;
}
/*
* If PartModule index stability is required (replacing old one), then
* AddModule() just appended to PartModuleList, remove it (note PartModuleList.Remove vs
* Part.RemoveModule) and set the index.
*/
if (index >= 0)
{
Log($"Ensure {moduleName} is at index {index}");
this.part.Modules.Remove(newModule);
this.part.Modules[index] = newModule;
}
}
protected static void Log(string text)
{
Debug.Log($"[Reviva] {text}");
}
protected static void LogError(string text)
{
Debug.LogError($"[Reviva] {text}");
}
}
}