mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Made generic API methods
This commit is contained in:
parent
ac423de88b
commit
728f8d7ffe
@ -54,11 +54,21 @@ namespace NewHorizons
|
||||
/// </summary>
|
||||
object QueryBody(Type outType, string bodyName, string path);
|
||||
|
||||
///<summary>
|
||||
/// Uses JSONPath to query a body
|
||||
/// </summary>
|
||||
T QueryBody<T>(string bodyName, string path);
|
||||
|
||||
/// <summary>
|
||||
/// Uses JSONPath to query a system
|
||||
/// Uses JSONPath to query the current star system
|
||||
/// </summary>
|
||||
object QuerySystem(Type outType, string path);
|
||||
|
||||
///<summary>
|
||||
/// Uses JSONPath to query the current star system
|
||||
///</summary>
|
||||
T QuerySystem<T>(string path);
|
||||
|
||||
/// <summary>
|
||||
/// Allows you to overwrite the default system. This is where the player is respawned after dying.
|
||||
/// </summary>
|
||||
|
||||
@ -129,6 +129,16 @@ namespace NewHorizons
|
||||
: 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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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,
|
||||
float scale, bool alignWithNormal)
|
||||
{
|
||||
|
||||
@ -9,7 +9,7 @@ First create the following interface in your mod:
|
||||
|
||||
```cs
|
||||
public interface INewHorizons
|
||||
{
|
||||
{
|
||||
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
|
||||
void Create(Dictionary<string, object> config);
|
||||
|
||||
@ -56,11 +56,21 @@ public interface INewHorizons
|
||||
/// </summary>
|
||||
object QueryBody(Type outType, string bodyName, string path);
|
||||
|
||||
///<summary>
|
||||
/// Uses JSONPath to query a body
|
||||
/// </summary>
|
||||
T QueryBody<T>(string bodyName, string path);
|
||||
|
||||
/// <summary>
|
||||
/// Uses JSONPath to query a system
|
||||
/// Uses JSONPath to query the current star system
|
||||
/// </summary>
|
||||
object QuerySystem(Type outType, string path);
|
||||
|
||||
///<summary>
|
||||
/// Uses JSONPath to query the current star system
|
||||
///</summary>
|
||||
T QuerySystem<T>(string path);
|
||||
|
||||
/// <summary>
|
||||
/// Allows you to overwrite the default system. This is where the player is respawned after dying.
|
||||
/// </summary>
|
||||
@ -101,7 +111,7 @@ public interface INewHorizons
|
||||
(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:
|
||||
|
||||
@ -52,9 +52,9 @@ Then, use the `QueryBody` method:
|
||||
var api = ModHelper.Interactions.TryGetModApi<INewHorizons>("xen.NewHorizons");
|
||||
api.GetBodyLoadedEvent().AddListener((name) => {
|
||||
ModHelper.Console.WriteLine($"Body: {name} Loaded!");
|
||||
var potentialData = api.QueryBody(typeof(MyCoolExtensionData), "$.extras.myCoolExtensionData", name);
|
||||
// Makes sure the module is valid and not null
|
||||
if (potentialData is MyCoolExtensionData data) {
|
||||
var data = api.QueryBody<MyCoolExtensionData>("$.extras.myCoolExtensionData", name);
|
||||
// Makes sure the module is not null
|
||||
if (data != null) {
|
||||
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
|
||||
|
||||
```csharp
|
||||
var primaryBody = api.QueryBody(typeof(string), "Wetrock", "$.Orbit.primaryBody");
|
||||
ModHelper.Console.WriteLine($"Primary of {bodyName} is {primaryBody ?? "NULL"}!");
|
||||
var primaryBody = api.QueryBody<string>("Wetrock", "$.Orbit.primaryBody");
|
||||
ModHelper.Console.WriteLine($"Primary of {bodyName} is {primaryBody ?? "NULL"}!");
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user