Skip to content

Instantly share code, notes, and snippets.

@StollD
Created October 12, 2018 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StollD/cdf365e1a07de379cddfacbeada6486b to your computer and use it in GitHub Desktop.
Save StollD/cdf365e1a07de379cddfacbeada6486b to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Object = System.Object;
// Provides Access to Kopernicus ScaledSpace OnDemand loading without referencing it
public class ScaledSpaceODWrapper
{
// Cached fields
private static FieldInfo _isLoaded;
private static MethodInfo _load;
private static MethodInfo _unload;
private static Type _odComponent;
static ScaledSpaceODWrapper()
{
_odComponent = AssemblyLoader.loadedTypes.FirstOrDefault(t =>
t.Name == "ScaledSpaceOnDemand" && t.Namespace == "Kopernicus.OnDemand");
// If the type was not found this means Kopernicus is not loaded
if (_odComponent == null)
{
return;
}
// Grab the other methods and fields we need
_isLoaded = _odComponent.GetField("isLoaded");
_load = _odComponent.GetMethod("LoadTextures");
_unload = _odComponent.GetMethod("UnloadTextures");
}
/// <summary>
/// Checks whether the scaled space textures of a body are loaded
/// </summary>
public static Boolean IsLoaded(CelestialBody body)
{
// If Kopernicus wasn't found no textures are managed
if (_odComponent == null)
{
return true;
}
// Get the scaled space component
Component od = body.scaledBody.GetComponent(_odComponent);
return (Boolean) _isLoaded.GetValue(od);
}
/// <summary>
/// Loads the scaled space textures of a body
/// </summary>
public static void LoadTextures(CelestialBody body)
{
// If Kopernicus wasn't found no textures are managed
if (_odComponent == null)
{
return;
}
// Get the scaled space component
Component od = body.scaledBody.GetComponent(_odComponent);
_load.Invoke(od, new Object[0]);
}
/// <summary>
/// Unload the scaled space textures of a body
/// </summary>
public static void UnloadTextures(CelestialBody body)
{
// If Kopernicus wasn't found no textures are managed
if (_odComponent == null)
{
return;
}
// Get the scaled space component
Component od = body.scaledBody.GetComponent(_odComponent);
_unload.Invoke(od, new Object[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment