Made generic API methods

This commit is contained in:
Ben C 2022-10-18 08:56:24 -04:00
parent ac423de88b
commit 728f8d7ffe
4 changed files with 123 additions and 84 deletions

View File

@ -54,11 +54,21 @@ namespace NewHorizons
/// </summary> /// </summary>
object QueryBody(Type outType, string bodyName, string path); object QueryBody(Type outType, string bodyName, string path);
///<summary>
/// Uses JSONPath to query a body
/// </summary>
T QueryBody<T>(string bodyName, string path);
/// <summary> /// <summary>
/// Uses JSONPath to query a system /// Uses JSONPath to query the current star system
/// </summary> /// </summary>
object QuerySystem(Type outType, string path); object QuerySystem(Type outType, string path);
///<summary>
/// Uses JSONPath to query the current star system
///</summary>
T QuerySystem<T>(string path);
/// <summary> /// <summary>
/// Allows you to overwrite the default system. This is where the player is respawned after dying. /// Allows you to overwrite the default system. This is where the player is respawned after dying.
/// </summary> /// </summary>

View File

@ -129,6 +129,16 @@ namespace NewHorizons
: QueryJson(outType, Path.Combine(planet.Mod.ModHelper.Manifest.ModFolderPath, planet.RelativePath), jsonPath); : QueryJson(outType, Path.Combine(planet.Mod.ModHelper.Manifest.ModFolderPath, planet.RelativePath), jsonPath);
} }
public T QueryBody<T>(string bodyName, string jsonPath)
{
var data = QueryBody(typeof(T), bodyName, jsonPath);
if (data is T result) {
return result;
} else {
return default(T);
}
}
public object QuerySystem(Type outType, string jsonPath) public object QuerySystem(Type outType, string jsonPath)
{ {
var system = Main.SystemDict[Main.Instance.CurrentStarSystem]; var system = Main.SystemDict[Main.Instance.CurrentStarSystem];
@ -137,6 +147,15 @@ namespace NewHorizons
: QueryJson(outType, Path.Combine(system.Mod.ModHelper.Manifest.ModFolderPath, system.RelativePath), jsonPath); : QueryJson(outType, Path.Combine(system.Mod.ModHelper.Manifest.ModFolderPath, system.RelativePath), jsonPath);
} }
public T QuerySystem<T>(string jsonPath) {
var data = QuerySystem(typeof(T), jsonPath);
if (data is T result) {
return result;
} else {
return default(T);
}
}
public GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, public GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles,
float scale, bool alignWithNormal) float scale, bool alignWithNormal)
{ {

View File

@ -9,99 +9,109 @@ First create the following interface in your mod:
```cs ```cs
public interface INewHorizons public interface INewHorizons
{ {
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")] [Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
void Create(Dictionary<string, object> config); void Create(Dictionary<string, object> config);
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")] [Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
void Create(Dictionary<string, object> config, IModBehaviour mod); void Create(Dictionary<string, object> config, IModBehaviour mod);
/// <summary> /// <summary>
/// Will load all configs in the regular folders (planets, systems, translations, etc) for this mod. /// Will load all configs in the regular folders (planets, systems, translations, etc) for this mod.
/// The NH addon config template is just a single call to this API method. /// The NH addon config template is just a single call to this API method.
/// </summary> /// </summary>
void LoadConfigs(IModBehaviour mod); void LoadConfigs(IModBehaviour mod);
/// <summary> /// <summary>
/// Retrieve the root GameObject of a custom planet made by creating configs. /// Retrieve the root GameObject of a custom planet made by creating configs.
/// Will only work if the planet has been created (see GetStarSystemLoadedEvent) /// Will only work if the planet has been created (see GetStarSystemLoadedEvent)
/// </summary> /// </summary>
GameObject GetPlanet(string name); GameObject GetPlanet(string name);
/// <summary> /// <summary>
/// The name of the current star system loaded. /// The name of the current star system loaded.
/// </summary> /// </summary>
string GetCurrentStarSystem(); string GetCurrentStarSystem();
/// <summary> /// <summary>
/// An event invoked when the player begins loading the new star system, before the scene starts to load. /// An event invoked when the player begins loading the new star system, before the scene starts to load.
/// Gives the name of the star system being switched to. /// Gives the name of the star system being switched to.
/// </summary> /// </summary>
UnityEvent<string> GetChangeStarSystemEvent(); UnityEvent<string> GetChangeStarSystemEvent();
/// <summary> /// <summary>
/// An event invoked when NH has finished generating all planets for a new star system. /// An event invoked when NH has finished generating all planets for a new star system.
/// Gives the name of the star system that was just loaded. /// Gives the name of the star system that was just loaded.
/// </summary> /// </summary>
UnityEvent<string> GetStarSystemLoadedEvent(); UnityEvent<string> GetStarSystemLoadedEvent();
/// <summary> /// <summary>
/// An event invoked when NH has finished a planet for a star system. /// An event invoked when NH has finished a planet for a star system.
/// Gives the name of the planet that was just loaded. /// Gives the name of the planet that was just loaded.
/// </summary> /// </summary>
UnityEvent<string> GetBodyLoadedEvent(); UnityEvent<string> GetBodyLoadedEvent();
/// <summary> /// <summary>
/// Uses JSONPath to query a body /// Uses JSONPath to query a body
/// </summary> /// </summary>
object QueryBody(Type outType, string bodyName, string path); object QueryBody(Type outType, string bodyName, string path);
/// <summary> ///<summary>
/// Uses JSONPath to query a system /// Uses JSONPath to query a body
/// </summary> /// </summary>
object QuerySystem(Type outType, string path); T QueryBody<T>(string bodyName, string path);
/// <summary> /// <summary>
/// Allows you to overwrite the default system. This is where the player is respawned after dying. /// Uses JSONPath to query the current star system
/// </summary> /// </summary>
bool SetDefaultSystem(string name); object QuerySystem(Type outType, string path);
/// <summary> ///<summary>
/// Allows you to instantly begin a warp to a new star system. /// Uses JSONPath to query the current star system
/// Will return false if that system does not exist (cannot be warped to). ///</summary>
/// </summary> T QuerySystem<T>(string path);
bool ChangeCurrentStarSystem(string name);
/// <summary> /// <summary>
/// Returns the uniqueIDs of each installed NH addon. /// Allows you to overwrite the default system. This is where the player is respawned after dying.
/// </summary> /// </summary>
string[] GetInstalledAddons(); bool SetDefaultSystem(string name);
/// <summary> /// <summary>
/// Allows you to spawn a copy of a prop by specifying its path. /// Allows you to instantly begin a warp to a new star system.
/// This is the same as using Props->details in a config, but also returns the spawned gameObject to you. /// Will return false if that system does not exist (cannot be warped to).
/// </summary> /// </summary>
GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, bool ChangeCurrentStarSystem(string name);
float scale, bool alignWithNormal);
/// <summary> /// <summary>
/// Allows you to spawn an AudioSignal on a planet. /// Returns the uniqueIDs of each installed NH addon.
/// This is the same as using Props->signals in a config, but also returns the spawned AudioSignal to you. /// </summary>
/// This method will not set its position. You will have to do that with the returned object. string[] GetInstalledAddons();
/// </summary>
AudioSignal SpawnSignal(IModBehaviour mod, GameObject root, string audio, string name, string frequency,
float sourceRadius = 1f, float detectionRadius = 20f, float identificationRadius = 10f, bool insideCloak = false,
bool onlyAudibleToScope = true, string reveals = "");
/// <summary> /// <summary>
/// Allows you to spawn character dialogue on a planet. Also returns the RemoteDialogueTrigger if remoteTriggerRadius is specified. /// Allows you to spawn a copy of a prop by specifying its path.
/// This is the same as using Props->dialogue in a config, but also returns the spawned game objects to you. /// This is the same as using Props->details in a config, but also returns the spawned gameObject to you.
/// This method will not set the position of the dialogue or remote trigger. You will have to do that with the returned objects. /// </summary>
/// </summary> GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles,
(CharacterDialogueTree, RemoteDialogueTrigger) SpawnDialogue(IModBehaviour mod, GameObject root, string xmlFile, float radius = 1f, float scale, bool alignWithNormal);
float range = 1f, string blockAfterPersistentCondition = null, float lookAtRadius = 1f, string pathToAnimController = null,
float remoteTriggerRadius = 0f); /// <summary>
} /// Allows you to spawn an AudioSignal on a planet.
/// This is the same as using Props->signals in a config, but also returns the spawned AudioSignal to you.
/// This method will not set its position. You will have to do that with the returned object.
/// </summary>
AudioSignal SpawnSignal(IModBehaviour mod, GameObject root, string audio, string name, string frequency,
float sourceRadius = 1f, float detectionRadius = 20f, float identificationRadius = 10f, bool insideCloak = false,
bool onlyAudibleToScope = true, string reveals = "");
/// <summary>
/// Allows you to spawn character dialogue on a planet. Also returns the RemoteDialogueTrigger if remoteTriggerRadius is specified.
/// This is the same as using Props->dialogue in a config, but also returns the spawned game objects to you.
/// This method will not set the position of the dialogue or remote trigger. You will have to do that with the returned objects.
/// </summary>
(CharacterDialogueTree, RemoteDialogueTrigger) SpawnDialogue(IModBehaviour mod, GameObject root, string xmlFile, float radius = 1f,
float range = 1f, string blockAfterPersistentCondition = null, float lookAtRadius = 1f, string pathToAnimController = null,
float remoteTriggerRadius = 0f);
}
``` ```
In your main `ModBehaviour` class you can get the NewHorizons API like so: In your main `ModBehaviour` class you can get the NewHorizons API like so:

View File

@ -52,9 +52,9 @@ Then, use the `QueryBody` method:
var api = ModHelper.Interactions.TryGetModApi<INewHorizons>("xen.NewHorizons"); var api = ModHelper.Interactions.TryGetModApi<INewHorizons>("xen.NewHorizons");
api.GetBodyLoadedEvent().AddListener((name) => { api.GetBodyLoadedEvent().AddListener((name) => {
ModHelper.Console.WriteLine($"Body: {name} Loaded!"); ModHelper.Console.WriteLine($"Body: {name} Loaded!");
var potentialData = api.QueryBody(typeof(MyCoolExtensionData), "$.extras.myCoolExtensionData", name); var data = api.QueryBody<MyCoolExtensionData>("$.extras.myCoolExtensionData", name);
// Makes sure the module is valid and not null // Makes sure the module is not null
if (potentialData is MyCoolExtensionData data) { if (data != null) {
ModHelper.Console.WriteLine($"myCoolExtensionProperty for {name} is {data.myCoolExtensionProperty}!"); ModHelper.Console.WriteLine($"myCoolExtensionProperty for {name} is {data.myCoolExtensionProperty}!");
} }
}); });
@ -69,6 +69,6 @@ Extending systems is the exact same as extending planets, except you use the `Qu
You can also use the `QueryBody` method to get values of the config outside your extension object You can also use the `QueryBody` method to get values of the config outside your extension object
```csharp ```csharp
var primaryBody = api.QueryBody(typeof(string), "Wetrock", "$.Orbit.primaryBody"); var primaryBody = api.QueryBody<string>("Wetrock", "$.Orbit.primaryBody");
ModHelper.Console.WriteLine($"Primary of {bodyName} is {primaryBody ?? "NULL"}!"); ModHelper.Console.WriteLine($"Primary of {bodyName} is {primaryBody ?? "NULL"}!");
``` ```