Skip to content

Commit b3ddd73

Browse files
committed
refactor more of the kopernicus on-demand loading stuff into the kopernicus file
1 parent fead7f5 commit b3ddd73

File tree

3 files changed

+95
-71
lines changed

3 files changed

+95
-71
lines changed

SCANsat/SCAN_Reflection/SCANkopernicus.cs

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,105 @@
1212
#endregion
1313

1414
using System;
15+
using System.Linq;
1516
using System.Reflection;
16-
using FinePrint;
17-
using FinePrint.Contracts.Parameters;
18-
using FinePrint.Utilities;
1917
using UnityEngine;
18+
using Log = KSPBuildTools.Log;
2019

2120
namespace SCANsat.SCAN_Reflection
2221
{
2322
static class SCANkopernicus
2423
{
25-
public const string KOPERNICUSONDEMANDTYPE = "ScaledSpaceOnDemand";
26-
private const string KOPERNICUSONDEMANDLOAD = "LoadTextures";
27-
private const string KOPERNICUSONDEMANDUNLOAD = "UnloadTextures";
24+
public static bool KopernicusLoaded = false;
2825

29-
internal static void LoadOnDemand(MonoBehaviour scaledSpaceOnDemand)
26+
private static Type x_ScaledSpaceOnDemand_Type = null;
27+
private static MethodInfo x_ScaledSpaceOnDemand_LoadTextures = null;
28+
private static MethodInfo x_ScaledSpaceOnDemand_UnloadTextures = null;
29+
30+
private static Type x_PQSMod_OnDemandHandler_Type = null;
31+
32+
internal static void Initialize(AssemblyLoader.LoadedAssembly kopernicusAssembly)
33+
{
34+
KopernicusLoaded = true;
35+
36+
try
37+
{
38+
x_ScaledSpaceOnDemand_Type = kopernicusAssembly.assembly.GetType("ScaledSpaceOnDemand");
39+
x_ScaledSpaceOnDemand_LoadTextures = x_ScaledSpaceOnDemand_Type.GetMethod("LoadTextures", BindingFlags.Instance | BindingFlags.Public);
40+
x_ScaledSpaceOnDemand_UnloadTextures = x_ScaledSpaceOnDemand_Type.GetMethod("UnloadTextures", BindingFlags.Instance | BindingFlags.Public);
41+
42+
x_PQSMod_OnDemandHandler_Type = kopernicusAssembly.assembly.GetType("PQSMod_OnDemandHandler");
43+
}
44+
catch(Exception e)
45+
{
46+
Log.Exception(e);
47+
}
48+
49+
if (x_ScaledSpaceOnDemand_Type == null || x_ScaledSpaceOnDemand_LoadTextures == null || x_ScaledSpaceOnDemand_UnloadTextures == null || x_PQSMod_OnDemandHandler_Type == null)
50+
{
51+
Log.Error("SCANsat: Unable to reflect Kopernicus OnDemand methods");
52+
KopernicusLoaded = false;
53+
}
54+
}
55+
56+
internal static void LoadOnDemand(CelestialBody body)
57+
{
58+
if (!KopernicusLoaded) return;
59+
60+
Component scaledSpaceOnDemand = body.scaledBody.GetComponent(x_ScaledSpaceOnDemand_Type);
61+
62+
if (scaledSpaceOnDemand == null) return;
63+
64+
SCANUtil.SCANlog("Loading Kopernicus On Demand Scaled Space Map For {0}", body.bodyName);
65+
x_ScaledSpaceOnDemand_LoadTextures.Invoke(scaledSpaceOnDemand, null);
66+
}
67+
68+
internal static void UnloadOnDemand(CelestialBody body)
69+
{
70+
if (!KopernicusLoaded) return;
71+
72+
Component scaledSpaceOnDemand = body.scaledBody.GetComponent(x_ScaledSpaceOnDemand_Type);
73+
74+
if (scaledSpaceOnDemand == null) return;
75+
76+
SCANUtil.SCANlog("Unloading Kopernicus On Demand Scaled Space Map For {0}", body.bodyName);
77+
x_ScaledSpaceOnDemand_UnloadTextures.Invoke(scaledSpaceOnDemand, null);
78+
}
79+
80+
private static PQSMod GetOnDemandHandlerPQSMod(CelestialBody body)
3081
{
31-
scaledSpaceOnDemand.GetType().InvokeMember(KOPERNICUSONDEMANDLOAD
32-
, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreReturn | BindingFlags.InvokeMethod, null, scaledSpaceOnDemand, null);
82+
return body.GetComponentsInChildren(x_PQSMod_OnDemandHandler_Type, true).FirstOrDefault() as PQSMod;
3383
}
3484

35-
internal static void UnloadOnDemand(MonoBehaviour scaledSpaceOnDemand)
85+
internal static void LoadPQS(CelestialBody body)
3686
{
37-
scaledSpaceOnDemand.GetType().InvokeMember(KOPERNICUSONDEMANDUNLOAD
38-
, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreReturn | BindingFlags.InvokeMethod, null, scaledSpaceOnDemand, null);
87+
if (!KopernicusLoaded) return;
88+
89+
PQSMod KopernicusOnDemand = GetOnDemandHandlerPQSMod(body);
90+
91+
if (KopernicusOnDemand == null)
92+
{
93+
return;
94+
}
95+
96+
SCANUtil.SCANlog("Loading Kopernicus On Demand PQSMod For {0}", body.bodyName);
97+
KopernicusOnDemand.OnQuadPreBuild(null);
98+
}
99+
100+
internal static void UnloadPQS(CelestialBody body)
101+
{
102+
if (!KopernicusLoaded) return;
103+
104+
PQSMod KopernicusOnDemand = GetOnDemandHandlerPQSMod(body);
105+
106+
if (KopernicusOnDemand == null)
107+
{
108+
return;
109+
}
110+
111+
KopernicusOnDemand.OnSphereInactive();
112+
113+
SCANUtil.SCANlog("Unloading Kopernicus On Demand PQSMod For {0}", body.bodyName);
39114
}
40115
}
41116
}

SCANsat/SCANcontroller.cs

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ private void onSurvey(Vessel v, CelestialBody b)
12421242

12431243
internal void loadPQS(CelestialBody b, mapSource s = mapSource.Data)
12441244
{
1245-
if (!SCANmainMenuLoader.KopernicusLoaded)
1245+
if (!SCANkopernicus.KopernicusLoaded)
12461246
{
12471247
return;
12481248
}
@@ -1316,23 +1316,12 @@ internal void loadPQS(CelestialBody b, mapSource s = mapSource.Data)
13161316
return;
13171317
}
13181318

1319-
PQSMod KopernicusOnDemand = b.GetComponentsInChildren<PQSMod>(true).Where(p => p.GetType().Name == "PQSMod_OnDemandHandler").FirstOrDefault();
1320-
1321-
if (KopernicusOnDemand == null)
1322-
{
1323-
return;
1324-
}
1325-
1326-
KopernicusOnDemand.OnQuadPreBuild(null);
1327-
1328-
KopernicusOnDemand = null;
1329-
1330-
SCANUtil.SCANlog("Loading Kopernicus On Demand PQSMod For {0}", b.bodyName);
1319+
SCANkopernicus.LoadPQS(b);
13311320
}
13321321

13331322
internal void unloadPQS(CelestialBody b, mapSource s = mapSource.Data)
13341323
{
1335-
if (!SCANmainMenuLoader.KopernicusLoaded)
1324+
if (!SCANkopernicus.KopernicusLoaded)
13361325
{
13371326
return;
13381327
}
@@ -1422,27 +1411,11 @@ internal void unloadPQS(CelestialBody b, mapSource s = mapSource.Data)
14221411
return;
14231412
}
14241413

1425-
PQSMod KopernicusOnDemand = b.GetComponentsInChildren<PQSMod>(true).Where(p => p.GetType().Name == "PQSMod_OnDemandHandler").FirstOrDefault();
1426-
1427-
if (KopernicusOnDemand == null)
1428-
{
1429-
return;
1430-
}
1431-
1432-
KopernicusOnDemand.OnSphereInactive();
1433-
1434-
KopernicusOnDemand = null;
1435-
1436-
SCANUtil.SCANlog("Unloading Kopernicus On Demand PQSMod For {0}", b.bodyName);
1414+
SCANkopernicus.UnloadPQS(b);
14371415
}
14381416

14391417
internal void loadOnDemandScaledSpace(CelestialBody b, mapSource s)
14401418
{
1441-
if (!SCANmainMenuLoader.KopernicusLoaded)
1442-
{
1443-
return;
1444-
}
1445-
14461419
if (!SCAN_Settings_Config.Instance.VisibleMapsActive)
14471420
{
14481421
return;
@@ -1490,23 +1463,12 @@ internal void loadOnDemandScaledSpace(CelestialBody b, mapSource s)
14901463
break;
14911464
}
14921465

1493-
MonoBehaviour kopernicusScaledSpaceLoader = b.scaledBody.GetComponents<MonoBehaviour>().Where(p => p.GetType().Name == SCANkopernicus.KOPERNICUSONDEMANDTYPE).FirstOrDefault();
1494-
1495-
if (kopernicusScaledSpaceLoader == null)
1496-
{
1497-
return;
1498-
}
1499-
1500-
SCANkopernicus.LoadOnDemand(kopernicusScaledSpaceLoader);
1501-
1502-
kopernicusScaledSpaceLoader = null;
1503-
1504-
SCANUtil.SCANlog("Loading Kopernicus On Demand Scaled Space Map For {0}", b.bodyName);
1466+
SCANkopernicus.LoadOnDemand(b);
15051467
}
15061468

15071469
internal void unloadOnDemandScaledSpace(CelestialBody b, mapSource s)
15081470
{
1509-
if (!SCANmainMenuLoader.KopernicusLoaded)
1471+
if (!SCANkopernicus.KopernicusLoaded)
15101472
{
15111473
return;
15121474
}
@@ -1568,18 +1530,7 @@ internal void unloadOnDemandScaledSpace(CelestialBody b, mapSource s)
15681530
return;
15691531
}
15701532

1571-
MonoBehaviour kopernicusScaledSpaceLoader = b.scaledBody.GetComponents<MonoBehaviour>().Where(p => p.GetType().Name == SCANkopernicus.KOPERNICUSONDEMANDTYPE).FirstOrDefault();
1572-
1573-
if (kopernicusScaledSpaceLoader == null)
1574-
{
1575-
return;
1576-
}
1577-
1578-
SCANkopernicus.UnloadOnDemand(kopernicusScaledSpaceLoader);
1579-
1580-
kopernicusScaledSpaceLoader = null;
1581-
1582-
SCANUtil.SCANlog("Unloading Kopernicus On Demand Scaled Space Map For {0}", b.bodyName);
1533+
SCANkopernicus.UnloadOnDemand(b);
15831534
}
15841535

15851536
private bool InCurrentBodyFamily(CelestialBody b)

SCANsat/SCANmainMenuLoader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class SCANmainMenuLoader : MonoBehaviour
3030
internal static string SCANsatVersion = "";
3131
public static bool MechJebLoaded = false;
3232
public static bool MMLoaded = false;
33-
public static bool KopernicusLoaded = false;
3433

3534
private static Texture2D orbitIconsMap;
3635

@@ -101,8 +100,7 @@ private void findAssemblies(string[] assemblies)
101100
}
102101
else if (alog.name == "Kopernicus")
103102
{
104-
KopernicusLoaded = true;
105-
//SCANreflection.LoadKopernicusReflection();
103+
SCANkopernicus.Initialize(assembly);
106104
}
107105
}
108106
}

0 commit comments

Comments
 (0)