|
12 | 12 | #endregion |
13 | 13 |
|
14 | 14 | using System; |
| 15 | +using System.Linq; |
15 | 16 | using System.Reflection; |
16 | | -using FinePrint; |
17 | | -using FinePrint.Contracts.Parameters; |
18 | | -using FinePrint.Utilities; |
19 | 17 | using UnityEngine; |
| 18 | +using Log = KSPBuildTools.Log; |
20 | 19 |
|
21 | 20 | namespace SCANsat.SCAN_Reflection |
22 | 21 | { |
23 | 22 | static class SCANkopernicus |
24 | 23 | { |
25 | | - public const string KOPERNICUSONDEMANDTYPE = "ScaledSpaceOnDemand"; |
26 | | - private const string KOPERNICUSONDEMANDLOAD = "LoadTextures"; |
27 | | - private const string KOPERNICUSONDEMANDUNLOAD = "UnloadTextures"; |
| 24 | + public static bool KopernicusLoaded = false; |
28 | 25 |
|
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) |
30 | 81 | { |
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; |
33 | 83 | } |
34 | 84 |
|
35 | | - internal static void UnloadOnDemand(MonoBehaviour scaledSpaceOnDemand) |
| 85 | + internal static void LoadPQS(CelestialBody body) |
36 | 86 | { |
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); |
39 | 114 | } |
40 | 115 | } |
41 | 116 | } |
0 commit comments