forked from here-and-now/KRPC.MechJeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MechJeb.cs
219 lines (174 loc) · 7.57 KB
/
MechJeb.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
using System;
using System.Collections.Generic;
using System.Reflection;
using KRPC.MechJeb.ExtensionMethods;
using KRPC.Service.Attributes;
using UnityEngine;
namespace KRPC.MechJeb {
/// <summary>
/// This service provides functionality to interact with <a href="https://github.com/MuMech/MechJeb2">MechJeb 2</a>.
/// </summary>
[KRPCService(GameScene = Service.GameScene.Flight)]
public static class MechJeb {
internal const string MechJebType = "MuMech.MechJebCore";
internal static List<string> errors = new List<string>();
private static Type type;
private static MethodInfo getComputerModule;
private static readonly Dictionary<string, Module> modules = new Dictionary<string, Module>();
internal static bool InitTypes() {
try {
// Scan the project assembly for MechJeb 2 reflection classes
Dictionary<string, Type> mechjebTypes = new Dictionary<string, Type>();
foreach(Type t in Assembly.GetExecutingAssembly().GetTypes()) {
FieldInfo mechjebTypeField = t.GetField("MechJebType", BindingFlags.NonPublic | BindingFlags.Static);
if(mechjebTypeField != null) {
string mechjebType = (string)mechjebTypeField.GetValue(null);
Logger.Info("Found class " + t.Name + " wanting to use " + mechjebType);
mechjebTypes.Add(mechjebType, t);
}
}
// Scan all assemblies to match kRPC classes to MechJeb 2
AssemblyLoader.loadedAssemblies.TypeOperation(mechjebType => {
if(mechjebTypes.TryGetValue(mechjebType.FullName, out Type internalType)) {
try {
Logger.Info("Loading class " + internalType.Name + " using " + mechjebType.FullName);
internalType.GetMethod("InitType", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { mechjebType });
mechjebTypes.Remove(mechjebType.FullName);
}
catch(Exception ex) {
string error = "Cannot load class " + internalType.Name;
Logger.Severe(error, ex);
errors.Add(error);
}
}
});
// Check if all classes have been initialized
foreach(KeyValuePair<string, Type> p in mechjebTypes) {
string error = "Cannot load class " + p.Value.Name;
Logger.Severe(error + " because " + p.Key + " was not found");
errors.Add(error);
}
}
catch(Exception ex) {
Logger.Severe("InitTypes() failed", ex);
errors.Clear();
errors.Add("kRPC.MechJeb failed to initialize: " + ex.Message);
type = null;
}
return type != null;
}
internal static void InitType(Type t) {
type = t;
getComputerModule = t.GetCheckedMethod("GetComputerModule", new Type[] { typeof(string) });
// MechJeb found, create module instances
modules.Add("AirplaneAutopilot", new AirplaneAutopilot());
modules.Add("AscentAutopilot", new AscentAutopilot());
modules.Add("DockingAutopilot", new DockingAutopilot());
modules.Add("LandingAutopilot", new LandingAutopilot());
modules.Add("RendezvousAutopilot", new RendezvousAutopilot());
modules.Add("ManeuverPlanner", new ManeuverPlanner());
modules.Add("SmartASS", new SmartASS());
modules.Add("SmartRCS", new SmartRCS());
modules.Add("Translatron", new Translatron());
modules.Add("DeployableAntennaController", new DeployableController());
modules.Add("NodeExecutor", new NodeExecutor());
modules.Add("RCSController", new RCSController());
modules.Add("StagingController", new StagingController());
modules.Add("SolarPanelController", new DeployableController());
modules.Add("TargetController", new TargetController());
modules.Add("ThrustController", new ThrustController());
}
internal static bool InitInstance() {
//assume all MechJeb types are loaded
APIReady = false;
try {
Instance = FlightGlobals.ActiveVessel.GetMasterMechJeb();
if(Instance == null)
return false;
// Set module instances to MechJeb objects
foreach(KeyValuePair<string, Module> p in modules) {
string error = "Cannot initialize class " + p.Value.GetType().Name + " with " + p.Key;
try {
object moduleInstance = GetComputerModule(p.Key);
if(moduleInstance != null)
p.Value.InitInstance(moduleInstance);
else
errors.Add(error);
}
catch(Exception ex) {
Logger.Severe(error, ex);
errors.Add(error);
}
}
APIReady = true;
}
catch(Exception ex) {
Logger.Severe("InitInstance() failed", ex);
errors.Clear();
errors.Add("kRPC.MechJeb failed to initialize: " + ex.Message);
}
return APIReady;
}
internal static void ShowErrors() {
if(errors.Count != 0) {
//PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), "MechJebChecker", "kRPC.MechJeb may not work properly", string.Join("\n", errors.ToArray()), "OK", false, HighLogic.UISkin);
errors.Clear();
}
}
internal static object GetComputerModule(string moduleType) {
object module = getComputerModule.Invoke(Instance, new object[] { "MechJebModule" + moduleType });
if(module == null)
Logger.Severe("MechJeb module " + moduleType + " not found");
return module;
}
internal static PartModule Instance { get; private set; }
public static bool TypesLoaded => type != null;
/// <summary>
/// A value indicating whether the service is available.
/// </summary>
[KRPCProperty]
public static bool APIReady { get; private set; }
// AUTOPILOTS
[KRPCProperty]
public static AirplaneAutopilot AirplaneAutopilot => (AirplaneAutopilot)modules["AirplaneAutopilot"];
[KRPCProperty]
public static AscentAutopilot AscentAutopilot => (AscentAutopilot)modules["AscentAutopilot"];
[KRPCProperty]
public static DockingAutopilot DockingAutopilot => (DockingAutopilot)modules["DockingAutopilot"];
[KRPCProperty]
public static LandingAutopilot LandingAutopilot => (LandingAutopilot)modules["LandingAutopilot"];
[KRPCProperty]
public static RendezvousAutopilot RendezvousAutopilot => (RendezvousAutopilot)modules["RendezvousAutopilot"];
// WINDOWS
[KRPCProperty]
public static ManeuverPlanner ManeuverPlanner => (ManeuverPlanner)modules["ManeuverPlanner"];
[KRPCProperty]
public static SmartASS SmartASS => (SmartASS)modules["SmartASS"];
[KRPCProperty]
public static SmartRCS SmartRCS => (SmartRCS)modules["SmartRCS"];
[KRPCProperty]
public static Translatron Translatron => (Translatron)modules["Translatron"];
// CONTROLLERS
[KRPCProperty]
public static DeployableController AntennaController => (DeployableController)modules["DeployableAntennaController"];
[KRPCProperty]
public static NodeExecutor NodeExecutor => (NodeExecutor)modules["NodeExecutor"];
[KRPCProperty]
public static RCSController RCSController => (RCSController)modules["RCSController"];
[KRPCProperty]
public static StagingController StagingController => (StagingController)modules["StagingController"];
[KRPCProperty]
public static DeployableController SolarPanelController => (DeployableController)modules["SolarPanelController"];
[KRPCProperty]
public static TargetController TargetController => (TargetController)modules["TargetController"];
[KRPCProperty]
public static ThrustController ThrustController => (ThrustController)modules["ThrustController"];
}
/// <summary>
/// General exception for errors in the service.
/// </summary>
[KRPCException(Service = "MechJeb")]
public class MJServiceException : Exception {
public MJServiceException(string message) : base(message) { }
}
}