Skip to content

Commit a476486

Browse files
committed
Fix #121: IJSIModule should now be accessible from external mods
1 parent 82b3f5f commit a476486

File tree

9 files changed

+73
-25
lines changed

9 files changed

+73
-25
lines changed

RasterPropMonitor/Core/IJSIModule.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* along with RasterPropMonitor. If not, see <http://www.gnu.org/licenses/>.
2020
****************************************************************************/
2121
using System;
22+
using System.Collections.Generic;
23+
using Log = KSPBuildTools.Log;
2224

2325
namespace JSI
2426
{
@@ -28,6 +30,56 @@ namespace JSI
2830
/// </summary>
2931
public class IJSIModule
3032
{
33+
public IJSIModule(Vessel v)
34+
{
35+
this.vessel = v;
36+
}
37+
3138
public Vessel vessel;
39+
40+
#region Module Registration
41+
42+
static List<Type> x_registeredTypes = new List<Type>();
43+
44+
internal static void CreateJSIModules(List<IJSIModule> modules, Vessel v)
45+
{
46+
foreach (Type t in x_registeredTypes)
47+
{
48+
try
49+
{
50+
modules.Add((IJSIModule)Activator.CreateInstance(t, v));
51+
}
52+
catch (Exception e)
53+
{
54+
Log.Error("Error creating JSI module of type " + t.Name + ": ");
55+
Log.Exception(e);
56+
}
57+
}
58+
}
59+
60+
public static void RegisterModule(Type jsiModuleType)
61+
{
62+
if (x_registeredTypes.IndexOf(jsiModuleType) != -1) return;
63+
if (!typeof(IJSIModule).IsAssignableFrom(jsiModuleType))
64+
{
65+
Log.Error($"Tried to register an ISJIModuleType {jsiModuleType.Name} that does not inherit from IJSIModule");
66+
return;
67+
}
68+
x_registeredTypes.Add(jsiModuleType);
69+
}
70+
71+
// A place to register known modules that might not otherwise have their static constructors called
72+
static IJSIModule()
73+
{
74+
RegisterModule(typeof(JSIParachute));
75+
RegisterModule(typeof(JSIInternalRPMButtons));
76+
if (JSIChatterer.chattererFound) RegisterModule(typeof(JSIChatterer));
77+
if (JSIFAR.farFound) RegisterModule(typeof(JSIFAR));
78+
if (JSIKAC.kacFound) RegisterModule(typeof(JSIKAC));
79+
if (JSIMechJeb.IsInstalled) RegisterModule(typeof(JSIMechJeb));
80+
if (JSIPilotAssistant.paFound) RegisterModule(typeof(JSIPilotAssistant));
81+
}
82+
83+
#endregion
3284
}
3385
}

RasterPropMonitor/Core/RasterPropMonitorComputer.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,11 @@ public void Start()
347347
GameEvents.onVesselChange.Add(onVesselChange);
348348
GameEvents.onVesselCrewWasModified.Add(onVesselCrewWasModified);
349349

350-
installedModules.Add(new JSIParachute(vessel)); // note this handles both stock chutes and realchute
351-
if (JSIMechJeb.IsInstalled) installedModules.Add(new JSIMechJeb(vessel));
352-
installedModules.Add(new JSIInternalRPMButtons(vessel));
353-
if (JSIFAR.farFound) installedModules.Add(new JSIFAR(vessel));
354-
if (JSIKAC.kacFound) installedModules.Add(new JSIKAC(vessel));
350+
IJSIModule.CreateJSIModules(installedModules, vessel);
351+
355352
#if ENABLE_ENGINE_MONITOR
356353
installedModules.Add(new JSIEngine(vessel));
357354
#endif
358-
if (JSIPilotAssistant.paFound) installedModules.Add(new JSIPilotAssistant(vessel));
359-
if (JSIChatterer.chattererFound) installedModules.Add(new JSIChatterer(vessel));
360355

361356
if (string.IsNullOrEmpty(RPMCid))
362357
{
@@ -643,10 +638,7 @@ public void OnDestroy()
643638
localCrew.Clear();
644639
localCrewMedical.Clear();
645640

646-
for (int i = 0; i < installedModules.Count; ++i)
647-
{
648-
installedModules[i].vessel = null;
649-
}
641+
installedModules.Clear();
650642

651643
variableCollection.Clear();
652644
ClearVariables();

RasterPropMonitor/Handlers/JSIChatterer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ static JSIChatterer()
9090
if (chatterer_t != null && chattererStartTalking != null)
9191
{
9292
chattererFound = true;
93+
IJSIModule.RegisterModule(typeof(JSIChatterer));
9394
}
9495
else
9596
{
9697
chattererFound = false;
9798
}
9899
}
99100

100-
public JSIChatterer(Vessel myVessel)
101+
public JSIChatterer(Vessel myVessel) : base(myVessel)
101102
{
102-
vessel = myVessel;
103103
JUtil.LogMessage(this, "A supported version of Chatterer is {0}", (chattererFound) ? "present" : "not available");
104104
lastVessel = Guid.Empty;
105105
}

RasterPropMonitor/Handlers/JSIFAR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ static JSIFAR()
172172
}
173173

174174
farFound = true;
175+
IJSIModule.RegisterModule(typeof(JSIFAR));
175176
}
176177
catch (Exception e)
177178
{
@@ -181,9 +182,8 @@ static JSIFAR()
181182

182183
}
183184

184-
public JSIFAR(Vessel myVessel)
185+
public JSIFAR(Vessel myVessel) : base(myVessel)
185186
{
186-
vessel = myVessel;
187187
JUtil.LogMessage(this, "A supported version of FAR is {0}", (farFound) ? "present" : "not available");
188188
}
189189

RasterPropMonitor/Handlers/JSIInternalRPMButtons.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ namespace JSI
3636
/// </summary>
3737
public class JSIInternalRPMButtons : IJSIModule
3838
{
39-
public JSIInternalRPMButtons(Vessel myVessel)
39+
public JSIInternalRPMButtons(Vessel myVessel) : base(myVessel)
4040
{
41-
vessel = myVessel;
4241
}
4342

4443
/// <summary>

RasterPropMonitor/Handlers/JSIKAC.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ class JSIKAC : IJSIModule
3030
static JSIKAC()
3131
{
3232
kacFound = KACWrapper.InitKACWrapper();
33+
if (kacFound)
34+
{
35+
IJSIModule.RegisterModule(typeof(JSIKAC));
36+
}
3337
}
3438

35-
public JSIKAC(Vessel myVessel)
39+
public JSIKAC(Vessel myVessel) : base(myVessel)
3640
{
37-
vessel = myVessel;
3841
JUtil.LogMessage(this, "A supported version of Kerbal Alarm Clock is {0}", (kacFound) ? "present" : "not available");
3942
}
4043

RasterPropMonitor/Handlers/JSIMechJeb.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,8 @@ static JSIMechJeb()
834834
{
835835
throw new NotImplementedException("mjModuleUsers");
836836
}
837+
838+
IJSIModule.RegisterModule(typeof(JSIMechJeb));
837839
}
838840
catch (Exception e)
839841
{
@@ -853,9 +855,8 @@ static JSIMechJeb()
853855

854856
static public bool IsInstalled => mjFound;
855857

856-
public JSIMechJeb(Vessel myVessel)
858+
public JSIMechJeb(Vessel myVessel) : base(myVessel)
857859
{
858-
vessel = myVessel;
859860
JUtil.LogInfo(this, "A supported version of MechJeb is {0}", (mjFound) ? "present" : "not available");
860861
}
861862

RasterPropMonitor/Handlers/JSIParachute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public enum SafeState
5555

5656
static JSIParachute()
5757
{
58+
IJSIModule.RegisterModule(typeof(JSIParachute));
59+
5860
try
5961
{
6062
Type rcMRC = null;
@@ -156,9 +158,8 @@ static JSIParachute()
156158
}
157159
}
158160

159-
public JSIParachute(Vessel myVessel)
161+
public JSIParachute(Vessel myVessel) : base(myVessel)
160162
{
161-
vessel = myVessel;
162163
JUtil.LogMessage(this, "A supported version of RealChute is {0}", (rcFound) ? "present" : "not available");
163164
}
164165

RasterPropMonitor/Handlers/JSIPilotAssistant.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ static JSIPilotAssistant()
223223
}
224224

225225
paFound = true;
226+
IJSIModule.RegisterModule(typeof(JSIPilotAssistant));
226227
}
227228

228-
public JSIPilotAssistant(Vessel myVessel)
229+
public JSIPilotAssistant(Vessel myVessel) : base(myVessel)
229230
{
230-
vessel = myVessel;
231231
JUtil.LogMessage(this, "A supported version of Pilot Assistant is {0}", (paFound) ? "present" : "not available");
232232
}
233233

0 commit comments

Comments
 (0)