forked from markusa380/PersistentRotation
/
MechJebWrapper.cs
221 lines (201 loc) · 7.7 KB
/
MechJebWrapper.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
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace PersistentRotation
{
public static class MechJebWrapper
{
private static Type mjCore_t;
private static FieldInfo saTarget_t;
private static Type mjVesselExtensions_t;
private static Reflection.DynamicMethod<Vessel> GetMasterMechJeb;
private static Reflection.DynamicMethod<object, string> GetComputerModule;
private static Reflection.DynamicMethodBool<object> ModuleEnabled;
static internal bool mjAvailable;
#region ### MechJeb Enum Imports ###
public enum SATarget
{
OFF = 0,
KILLROT = 1,
NODE = 2,
SURFACE = 3,
PROGRADE = 4,
RETROGRADE = 5,
NORMAL_PLUS = 6,
NORMAL_MINUS = 7,
RADIAL_PLUS = 8,
RADIAL_MINUS = 9,
RELATIVE_PLUS = 10,
RELATIVE_MINUS = 11,
TARGET_PLUS = 12,
TARGET_MINUS = 13,
PARALLEL_PLUS = 14,
PARALLEL_MINUS = 15,
ADVANCED = 16,
AUTO = 17,
SURFACE_PROGRADE = 18,
SURFACE_RETROGRADE = 19,
HORIZONTAL_PLUS = 20,
HORIZONTAL_MINUS = 21,
VERTICAL_PLUS = 22,
}
static public Dictionary<int, SATarget> saTargetMap = new Dictionary<int, SATarget>
{
{ 0, SATarget.OFF },
{ 1, SATarget.KILLROT },
{ 2, SATarget.NODE },
{ 3, SATarget.SURFACE },
{ 4, SATarget.PROGRADE },
{ 5, SATarget.RETROGRADE },
{ 6, SATarget.NORMAL_PLUS },
{ 7, SATarget.NORMAL_MINUS },
{ 8, SATarget.RADIAL_PLUS },
{ 9, SATarget.RADIAL_MINUS },
{10, SATarget.RELATIVE_PLUS },
{11, SATarget.RELATIVE_MINUS },
{12, SATarget.TARGET_PLUS },
{13, SATarget.TARGET_MINUS },
{14, SATarget.PARALLEL_PLUS },
{15, SATarget.PARALLEL_MINUS },
{16, SATarget.ADVANCED },
{17, SATarget.AUTO },
{18, SATarget.SURFACE_PROGRADE },
{19, SATarget.SURFACE_RETROGRADE },
{20, SATarget.HORIZONTAL_PLUS },
{21, SATarget.HORIZONTAL_MINUS },
{22, SATarget.VERTICAL_PLUS }
};
#endregion
/* MONOBEHAVIOUR METHODS */
public static void Initialize()
{
Debug.Log("[PR] Initializing MechJeb wrapper...");
mjAvailable = false;
try
{
mjCore_t = Reflection.GetExportedType("MechJeb2", "MuMech.MechJebCore");
if (mjCore_t == null)
{
return;
}
mjVesselExtensions_t = Reflection.GetExportedType("MechJeb2", "MuMech.VesselExtensions");
if (mjVesselExtensions_t == null)
{
return;
}
Type mjModuleSmartass_t = Reflection.GetExportedType("MechJeb2", "MuMech.MechJebModuleSmartASS");
if (mjModuleSmartass_t == null)
{
return;
}
saTarget_t = mjModuleSmartass_t.GetField("target", BindingFlags.Instance | BindingFlags.Public);
if (saTarget_t == null)
{
return;
}
MethodInfo GetMasterMechJeb_t = mjVesselExtensions_t.GetMethod("GetMasterMechJeb", BindingFlags.Static | BindingFlags.Public);
if (GetMasterMechJeb_t == null)
{
return;
}
GetMasterMechJeb = Reflection.CreateFunc<Vessel>(GetMasterMechJeb_t);
if (GetMasterMechJeb == null)
{
return;
}
MethodInfo GetComputerModule_t = mjCore_t.GetMethod("GetComputerModule", new Type[] { typeof(string) });
if (GetComputerModule_t == null)
{
return;
}
GetComputerModule = Reflection.CreateFunc<object, string>(GetComputerModule_t);
if (GetComputerModule == null)
{
return;
}
Type mjComputerModule_t = Reflection.GetExportedType("MechJeb2", "MuMech.ComputerModule");
if (mjComputerModule_t == null)
{
return;
}
PropertyInfo mjModuleEnabledProperty = mjComputerModule_t.GetProperty("enabled", BindingFlags.Instance | BindingFlags.Public);
MethodInfo mjModuleEnabled = null;
if (mjModuleEnabledProperty != null)
{
mjModuleEnabled = mjModuleEnabledProperty.GetGetMethod();
}
if (mjModuleEnabled == null)
{
return;
}
ModuleEnabled = Reflection.CreateFuncBool<object>(mjModuleEnabled);
mjAvailable = true;
Debug.Log("[PR] MechJeb reflection successfull!");
}
catch
{
Debug.LogWarning("[PR] MechJeb exception.");
}
}
/* PUBLIC METHODS */
public static SATarget GetMode(Vessel vessel)
{
object masterMechJeb;
object smartAss;
SATarget saTarget;
if (mjAvailable)
{
masterMechJeb = GetMasterMechJeb(vessel);
if(masterMechJeb == null)
{
return SATarget.OFF;
}
smartAss = GetComputerModule(masterMechJeb, "MechJebModuleSmartASS");
object activeSATarget = saTarget_t.GetValue(smartAss);
saTarget = saTargetMap[(int)activeSATarget];
return saTarget;
}
else
{
return SATarget.OFF;
}
}
public static bool Active(Vessel vessel)
{
object masterMechJeb;
object ascentAutopilot;
object landingAutopilot;
object nodeExecutor;
object rendezvousAutopilot;
if (mjAvailable)
{
masterMechJeb = GetMasterMechJeb(vessel);
if (masterMechJeb == null)
{
return false;
}
ascentAutopilot = GetComputerModule(masterMechJeb, "MechJebModuleAscentAutopilot");
landingAutopilot = GetComputerModule(masterMechJeb, "MechJebModuleLandingAutopilot");
nodeExecutor = GetComputerModule(masterMechJeb, "MechJebModuleNodeExecutor");
rendezvousAutopilot = GetComputerModule(masterMechJeb, "MechJebModuleRendezvousAutopilot");
Debug.Log("[PR] MechJeb wrapper.Active 1");
if (ModuleEnabled(ascentAutopilot)) { }
Debug.Log("[PR] MechJeb wrapper.Active 2");
if (ModuleEnabled(landingAutopilot)) { }
Debug.Log("[PR] MechJeb wrapper.Active 3");
if (ModuleEnabled(nodeExecutor)) { }
Debug.Log("[PR] MechJeb wrapper.Active 4");
if (ModuleEnabled(rendezvousAutopilot)) { }
Debug.Log("[PR] MechJeb wrapper.Active 5");
if (ModuleEnabled(ascentAutopilot) || ModuleEnabled(landingAutopilot) || ModuleEnabled(nodeExecutor) || ModuleEnabled(rendezvousAutopilot))
return true;
return false;
}
else
{
return false;
}
}
}
}