Fix documentation error and add log for future debuging for QueryBody; Set SolarSystem relative path if a mod adds a .json file to overwrite it. (#926)

<!-- Be sure to reference the existing issue if it exists -->

## Bug fixes

- Added log message in the event the QueryBody could not find a body
- Fixed documentation to match API
- Discovered that if you modify the default SolarySystem, `QuerySystem`
will fail due to an IO error as the relative path is not set. While not
an ideal solution, we have settled on just setting the relativePath if a
mod adjusts the system. This will mean that the last mod loaded that
makes these changes will have set the relative path.
This commit is contained in:
xen-42 2024-08-07 14:23:07 -04:00 committed by GitHub
commit b3de2b2ce6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 5 deletions

View File

@ -694,10 +694,25 @@ namespace NewHorizons
if (SystemDict.ContainsKey(starSystemName)) if (SystemDict.ContainsKey(starSystemName))
{ {
// Both changing the Mod and RelativePath are weird and will likely cause incompat issues if two mods both affected the same system
// It's mostly just to fix up the config compared to the default one NH makes for the base StarSystem
if (SystemDict[starSystemName].Config.GlobalMusic == null && SystemDict[starSystemName].Config.Skybox == null) if (SystemDict[starSystemName].Config.GlobalMusic == null && SystemDict[starSystemName].Config.Skybox == null)
{
SystemDict[starSystemName].Mod = mod; SystemDict[starSystemName].Mod = mod;
if (SystemDict[starSystemName].Config.extras == null) }
// If a mod contains a change to the default system, set the relative path.
// Warning: If multiple systems make changes to the default system, only the relativePath will be set to the last mod loaded.
if (string.IsNullOrEmpty(SystemDict[starSystemName].RelativePath))
{
SystemDict[starSystemName].RelativePath = relativePath; SystemDict[starSystemName].RelativePath = relativePath;
}
else
{
NHLogger.LogWarning($"Two (or more) mods are making system changes to {starSystemName} which may result in errors");
}
SystemDict[starSystemName].Config.Merge(starSystemConfig); SystemDict[starSystemName].Config.Merge(starSystemConfig);
} }
else else

View File

@ -143,9 +143,11 @@ namespace NewHorizons
public object QueryBody(Type outType, string bodyName, string jsonPath) public object QueryBody(Type outType, string bodyName, string jsonPath)
{ {
var planet = Main.BodyDict[Main.Instance.CurrentStarSystem].Find((b) => b.Config.name == bodyName); var planet = Main.BodyDict[Main.Instance.CurrentStarSystem].Find((b) => b.Config.name == bodyName);
return planet == null if (planet == null){
? null NHLogger.LogError($"Could not find planet with body name {bodyName}.");
: QueryJson(outType, Path.Combine(planet.Mod.ModHelper.Manifest.ModFolderPath, planet.RelativePath), jsonPath); return null;
}
return QueryJson(outType, Path.Combine(planet.Mod.ModHelper.Manifest.ModFolderPath, planet.RelativePath), jsonPath);
} }
public T QueryBody<T>(string bodyName, string jsonPath) public T QueryBody<T>(string bodyName, string jsonPath)

View File

@ -49,7 +49,7 @@ 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 data = api.QueryBody<MyCoolExtensionData>("$.extras.myCoolExtensionData", name); var data = api.QueryBody<MyCoolExtensionData>(name, "$.extras.myCoolExtensionData");
// Makes sure the module is not null // Makes sure the module is not null
if (data != null) { if (data != null) {
ModHelper.Console.WriteLine($"myCoolExtensionProperty for {name} is {data.myCoolExtensionProperty}!"); ModHelper.Console.WriteLine($"myCoolExtensionProperty for {name} is {data.myCoolExtensionProperty}!");