From 0f187f6aca1fcbd9bbe284c8b060d4ed9988554e Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 21 Jul 2023 11:03:26 -0400 Subject: [PATCH] Add api method to make ship logs --- .../Builder/ShipLog/RumorModeBuilder.cs | 15 ++++++---- NewHorizons/INewHorizons.cs | 10 +++++++ NewHorizons/NewHorizonsApi.cs | 28 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs b/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs index 727f4c07..d224fffa 100644 --- a/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs +++ b/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs @@ -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(); - 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 newBodies = new List(manager._shipLogXmlAssets) { newAsset }; + var newAsset = new TextAsset(xml.ToString()); + var newBodies = new List(manager._shipLogXmlAssets) { newAsset }; manager._shipLogXmlAssets = newBodies.ToArray(); ShipLogHandler.AddConfig(astroBodyId.Value, entryIDs, body); } diff --git a/NewHorizons/INewHorizons.cs b/NewHorizons/INewHorizons.cs index 14093b0d..a284270e 100644 --- a/NewHorizons/INewHorizons.cs +++ b/NewHorizons/INewHorizons.cs @@ -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 config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")] void Create(Dictionary config, IModBehaviour mod); + /// + /// Directly add ship logs from XML. Call this method right before ShipLogManager awake. + /// + /// + /// + /// + /// + void AddShipLogXML(IModBehaviour mod, ShipLogManager manager, XElement xml, string planetName); + /// /// 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. diff --git a/NewHorizons/NewHorizonsApi.cs b/NewHorizons/NewHorizonsApi.cs index ee3ea62f..fd2c8b44 100644 --- a/NewHorizons/NewHorizonsApi.cs +++ b/NewHorizons/NewHorizonsApi.cs @@ -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()); + 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);