Add api method to make ship logs

This commit is contained in:
Nick 2023-07-21 11:03:26 -04:00
parent 212e16b5f0
commit 0f187f6aca
3 changed files with 48 additions and 5 deletions

View File

@ -55,15 +55,20 @@ namespace NewHorizons.Builder.ShipLog
{
string systemName = body.Config.starSystem;
XElement astroBodyFile = XElement.Load(Path.Combine(body.Mod.ModHelper.Manifest.ModFolderPath, body.Config.ShipLog.xmlFile));
XElement astroBodyId = astroBodyFile.Element("ID");
AddShipLogXML(manager, astroBodyFile, body);
}
public static void AddShipLogXML(ShipLogManager manager, XElement xml, NewHorizonsBody body)
{
XElement astroBodyId = xml.Element("ID");
if (astroBodyId == null)
{
NHLogger.LogError("Failed to load ship logs for " + systemName + "!");
NHLogger.LogError("Failed to load ship logs for " + body.Config.name + "!");
}
else
{
var entryIDs = new List<string>();
foreach (XElement entryElement in astroBodyFile.DescendantsAndSelf("Entry"))
foreach (XElement entryElement in xml.DescendantsAndSelf("Entry"))
{
XElement curiosityName = entryElement.Element("Curiosity");
XElement id = entryElement.Element("ID");
@ -98,8 +103,8 @@ namespace NewHorizons.Builder.ShipLog
}
AddTranslation(entryElement);
}
TextAsset newAsset = new TextAsset(astroBodyFile.ToString());
List<TextAsset> newBodies = new List<TextAsset>(manager._shipLogXmlAssets) { newAsset };
var newAsset = new TextAsset(xml.ToString());
var newBodies = new List<TextAsset>(manager._shipLogXmlAssets) { newAsset };
manager._shipLogXmlAssets = newBodies.ToArray();
ShipLogHandler.AddConfig(astroBodyId.Value, entryIDs, body);
}

View File

@ -1,6 +1,7 @@
using OWML.Common;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Events;
@ -14,6 +15,15 @@ namespace NewHorizons
[Obsolete("Create(Dictionary<string, object> config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")]
void Create(Dictionary<string, object> config, IModBehaviour mod);
/// <summary>
/// Directly add ship logs from XML. Call this method right before ShipLogManager awake.
/// </summary>
/// <param name="mod"></param>
/// <param name="manager"></param>
/// <param name="xml"></param>
/// <param name="planetName"></param>
void AddShipLogXML(IModBehaviour mod, ShipLogManager manager, XElement xml, string planetName);
/// <summary>
/// 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.

View File

@ -1,6 +1,8 @@
using NewHorizons.Builder.Props;
using NewHorizons.Builder.Props.Audio;
using NewHorizons.Builder.ShipLog;
using NewHorizons.External;
using NewHorizons.External.Configs;
using NewHorizons.External.Modules;
using NewHorizons.External.Modules.Props;
using NewHorizons.External.Modules.Props.Audio;
@ -15,6 +17,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Events;
@ -62,6 +65,31 @@ namespace NewHorizons
}
}
public void AddShipLogXML(IModBehaviour mod, ShipLogManager manager, XElement xml, string planetName)
{
var body = new NewHorizonsBody(new PlanetConfig() { name = planetName, starSystem = Main.Instance.CurrentStarSystem }, mod);
if (!Main.BodyDict.ContainsKey(Main.Instance.CurrentStarSystem))
{
Main.BodyDict.Add(Main.Instance.CurrentStarSystem, new List<NewHorizonsBody>());
Main.BodyDict[Main.Instance.CurrentStarSystem].Add(body);
}
else
{
var existingBody = Main.BodyDict[Main.Instance.CurrentStarSystem].FirstOrDefault(x => x.Config.name == planetName);
if (existingBody != null)
{
body = existingBody;
}
else
{
Main.BodyDict[Main.Instance.CurrentStarSystem].Add(body);
}
}
RumorModeBuilder.AddShipLogXML(manager, xml, body);
}
public void LoadConfigs(IModBehaviour mod)
{
Main.Instance.LoadConfigs(mod);