From 6722ff7ad40fce6a84804e326514f9786fdfea09 Mon Sep 17 00:00:00 2001 From: Ben C Date: Mon, 29 Aug 2022 17:25:42 -0400 Subject: [PATCH] Added Support for Systems --- NewHorizons/INewHorizons.cs | 9 +- NewHorizons/Main.cs | 8 +- NewHorizons/NewHorizonsApi.cs | 41 +++-- NewHorizons/Utility/NewHorizonsSystem.cs | 4 +- SchemaExporter/SchemaExporter.cs | 211 ++++++++++++----------- 5 files changed, 152 insertions(+), 121 deletions(-) diff --git a/NewHorizons/INewHorizons.cs b/NewHorizons/INewHorizons.cs index 7b9936ba..aa014ea6 100644 --- a/NewHorizons/INewHorizons.cs +++ b/NewHorizons/INewHorizons.cs @@ -50,9 +50,14 @@ namespace NewHorizons UnityEvent GetBodyLoadedEvent(); /// - /// Gets an object in the `extras` object of a config, returns null if the key doesn't exist + /// Gets an object in the `extras` object of a body config, returns null if the key doesn't exist /// - object GetExtraModule(Type moduleType, string extrasModuleName, string planetName); + object GetExtraModuleForBody(Type moduleType, string extrasModuleName, string planetName); + + /// + /// Gets an object in the `extras` object of a system config, returns null if the key doesn't exist + /// + object GetExtraModuleForSystem(Type moduleType, string extrasModuleName, string systemName); /// /// Allows you to overwrite the default system. This is where the player is respawned after dying. diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 000eb46b..97ba6124 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -127,7 +127,7 @@ namespace NewHorizons BodyDict["SolarSystem"] = new List(); BodyDict["EyeOfTheUniverse"] = new List(); // Keep this empty tho fr - SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(), Instance) + SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(), "", Instance) { Config = { @@ -143,7 +143,7 @@ namespace NewHorizons } } }; - SystemDict["EyeOfTheUniverse"] = new NewHorizonsSystem("EyeOfTheUniverse", new StarSystemConfig(), Instance) + SystemDict["EyeOfTheUniverse"] = new NewHorizonsSystem("EyeOfTheUniverse", new StarSystemConfig(), "", Instance) { Config = { @@ -517,7 +517,7 @@ namespace NewHorizons } else { - SystemDict[name] = new NewHorizonsSystem(name, starSystemConfig, mod); + SystemDict[name] = new NewHorizonsSystem(name, starSystemConfig, relativePath, mod); } } } @@ -618,7 +618,7 @@ namespace NewHorizons starSystemConfig.Migrate(); starSystemConfig.FixCoordinates(); - var system = new NewHorizonsSystem(config.starSystem, starSystemConfig, mod); + var system = new NewHorizonsSystem(config.starSystem, starSystemConfig, "", mod); SystemDict.Add(config.starSystem, system); diff --git a/NewHorizons/NewHorizonsApi.cs b/NewHorizons/NewHorizonsApi.cs index 13a0fae1..e8e36a24 100644 --- a/NewHorizons/NewHorizonsApi.cs +++ b/NewHorizons/NewHorizonsApi.cs @@ -101,23 +101,42 @@ namespace NewHorizons } } - public object GetExtraModule(Type moduleType, string extraModuleKey, string planetName) + private object GetExtraModule(Type moduleType, string key, string path) { - var planet = Main.BodyDict[Main.Instance.CurrentStarSystem].Find((b) => b.Config.name == planetName); - if (planet == null) + if (path == "") return null; + try { - // Uh idk if we need this but ye it do be here etc. - Logger.LogVerbose($"Attempting To Get Extras On Planet That Doesn't Exist! ({planetName})"); + var jsonText = File.ReadAllText(path); + var jsonData = JObject.Parse(jsonText); + var possibleExtras = jsonData.Property("extras")?.Value; + if (possibleExtras is JObject extras) + { + return extras.Property(key)?.Value.ToObject(moduleType); + } return null; } - var jsonText = File.ReadAllText(planet.Mod.ModHelper.Manifest.ModFolderPath + planet.RelativePath); - var jsonData = JObject.Parse(jsonText); - var possibleExtras = jsonData.Property("extras")?.Value; - if (possibleExtras is JObject extras) + catch (FileNotFoundException) { - return extras.Property(extraModuleKey)?.Value.ToObject(moduleType); + return null; } - return null; + } + + public object GetExtraModuleForBody(Type moduleType, string extraModuleKey, string planetName) + { + var planet = Main.BodyDict[Main.Instance.CurrentStarSystem].Find((b) => b.Config.name == planetName); + return planet == null + ? null + : GetExtraModule(moduleType, extraModuleKey, + planet.Mod.ModHelper.Manifest.ModFolderPath + planet.RelativePath); + } + + public object GetExtraModuleForSystem(Type moduleType, string extraModuleKey, string systemName) + { + var system = Main.SystemDict[Main.Instance.CurrentStarSystem]; + return system == null + ? null + : GetExtraModule(moduleType, extraModuleKey, + system.Mod.ModHelper.Manifest.ModFolderPath + system.RelativePath); } public GameObject SpawnObject(GameObject planet, Sector sector, string propToCopyPath, Vector3 position, Vector3 eulerAngles, diff --git a/NewHorizons/Utility/NewHorizonsSystem.cs b/NewHorizons/Utility/NewHorizonsSystem.cs index ae6bafa3..be9a539f 100644 --- a/NewHorizons/Utility/NewHorizonsSystem.cs +++ b/NewHorizons/Utility/NewHorizonsSystem.cs @@ -11,15 +11,17 @@ namespace NewHorizons.Utility public class NewHorizonsSystem { public string UniqueID; + public string RelativePath; public SpawnModule Spawn = null; public SpawnPoint SpawnPoint = null; public StarSystemConfig Config; public IModBehaviour Mod; - public NewHorizonsSystem(string uniqueID, StarSystemConfig config, IModBehaviour mod) + public NewHorizonsSystem(string uniqueID, StarSystemConfig config, string relativePath, IModBehaviour mod) { UniqueID = uniqueID; Config = config; + RelativePath = relativePath; Mod = mod; } } diff --git a/SchemaExporter/SchemaExporter.cs b/SchemaExporter/SchemaExporter.cs index 2dba6386..33c12666 100644 --- a/SchemaExporter/SchemaExporter.cs +++ b/SchemaExporter/SchemaExporter.cs @@ -1,104 +1,109 @@ -using System; -using System.Collections.Generic; -using System.IO; -using NewHorizons.External.Configs; -using NJsonSchema; -using NJsonSchema.Generation; - -namespace SchemaExporter; - -public static class SchemaExporter -{ - public static void Main(string[] args) - { - const string folderName = "NewHorizons/Schemas"; - - Directory.CreateDirectory(folderName); - Console.WriteLine("Schema Generator: We're winning!"); - var settings = new JsonSchemaGeneratorSettings - { - IgnoreObsoleteProperties = true, - DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull, - FlattenInheritanceHierarchy = true, - AllowReferencesWithProperties = true - }; - var bodySchema = new Schema("Celestial Body Schema", "Schema for a celestial body in New Horizons", $"{folderName}/body_schema", settings); - bodySchema.Output(); - var systemSchema = - new Schema("Star System Schema", "Schema for a star system in New Horizons", $"{folderName}/star_system_schema", settings); - systemSchema.Output(); - var addonSchema = new Schema("Addon Manifest Schema", - "Schema for an addon manifest in New Horizons", $"{folderName}/addon_manifest_schema", settings); - addonSchema.Output(); - var translationSchema = - new Schema("Translation Schema", "Schema for a translation file in New Horizons", $"{folderName}/translation_schema", settings); - translationSchema.Output(); - Console.WriteLine("Done!"); - } - - private readonly struct Schema - { - private readonly JsonSchemaGeneratorSettings _generatorSettings; - private readonly string _title, _description; - private readonly string _outFileName; - - public Schema(string schemaTitle, string schemaDescription, string fileName, JsonSchemaGeneratorSettings settings) - { - _title = schemaTitle; - _description = schemaDescription; - _outFileName = fileName; - _generatorSettings = settings; - } - - public void Output() - { - Console.WriteLine($"Outputting {_title}"); - File.WriteAllText($"{_outFileName}.json", ToString()); - } - - public override string ToString() - { - return GetJsonSchema().ToJson(); - } - - private JsonSchema GetJsonSchema() - { - var schema = JsonSchema.FromType(_generatorSettings); - schema.Title = _title; - var schemaLinkProp = new JsonSchemaProperty - { - Type = JsonObjectType.String, - Description = "The schema to validate with" - }; - schema.Properties.Add("$schema", schemaLinkProp); - schema.ExtensionData ??= new Dictionary(); - schema.ExtensionData.Add("$docs", new Dictionary - { - {"title", _title}, - {"description", _description} - }); - - if (_title == "Celestial Body Schema") - { - schema.Definitions["OrbitModule"].Properties["semiMajorAxis"].Default = 5000f; - schema.Properties.Add("extras", new JsonSchemaProperty { - Type = JsonObjectType.Object, - Description = "Extra data that may be used by extension mods" - }); - } - - if (_title == "Star System Schema") - { - schema.Definitions["NomaiCoordinates"].Properties["x"].UniqueItems = true; - schema.Definitions["NomaiCoordinates"].Properties["y"].UniqueItems = true; - schema.Definitions["NomaiCoordinates"].Properties["z"].UniqueItems = true; - schema.Properties.Add("extras", new JsonSchemaProperty { - Type = JsonObjectType.Object, - Description = "Extra data that may be used by extension mods" - }); - } - - return schema; - } - } +using System; +using System.Collections.Generic; +using System.IO; +using NewHorizons.External.Configs; +using NJsonSchema; +using NJsonSchema.Generation; + +namespace SchemaExporter; + +public static class SchemaExporter +{ + public static void Main(string[] args) + { + const string folderName = "NewHorizons/Schemas"; + + Directory.CreateDirectory(folderName); + Console.WriteLine("Schema Generator: We're winning!"); + var settings = new JsonSchemaGeneratorSettings + { + IgnoreObsoleteProperties = true, + DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull, + FlattenInheritanceHierarchy = true, + AllowReferencesWithProperties = true + }; + var bodySchema = new Schema("Celestial Body Schema", "Schema for a celestial body in New Horizons", $"{folderName}/body_schema", settings); + bodySchema.Output(); + var systemSchema = + new Schema("Star System Schema", "Schema for a star system in New Horizons", $"{folderName}/star_system_schema", settings); + systemSchema.Output(); + var addonSchema = new Schema("Addon Manifest Schema", + "Schema for an addon manifest in New Horizons", $"{folderName}/addon_manifest_schema", settings); + addonSchema.Output(); + var translationSchema = + new Schema("Translation Schema", "Schema for a translation file in New Horizons", $"{folderName}/translation_schema", settings); + translationSchema.Output(); + Console.WriteLine("Done!"); + } + + private readonly struct Schema + { + private readonly JsonSchemaGeneratorSettings _generatorSettings; + private readonly string _title, _description; + private readonly string _outFileName; + + public Schema(string schemaTitle, string schemaDescription, string fileName, JsonSchemaGeneratorSettings settings) + { + _title = schemaTitle; + _description = schemaDescription; + _outFileName = fileName; + _generatorSettings = settings; + } + + public void Output() + { + Console.WriteLine($"Outputting {_title}"); + File.WriteAllText($"{_outFileName}.json", ToString()); + } + + public override string ToString() + { + return GetJsonSchema().ToJson(); + } + + private JsonSchema GetJsonSchema() + { + var schema = JsonSchema.FromType(_generatorSettings); + schema.Title = _title; + var schemaLinkProp = new JsonSchemaProperty + { + Type = JsonObjectType.String, + Description = "The schema to validate with" + }; + schema.Properties.Add("$schema", schemaLinkProp); + schema.ExtensionData ??= new Dictionary(); + schema.ExtensionData.Add("$docs", new Dictionary + { + {"title", _title}, + {"description", _description} + }); + + switch (_title) + { + case "Celestial Body Schema": + schema.Definitions["OrbitModule"].Properties["semiMajorAxis"].Default = 5000f; + break; + case "Star System Schema": + schema.Definitions["NomaiCoordinates"].Properties["x"].UniqueItems = true; + schema.Definitions["NomaiCoordinates"].Properties["y"].UniqueItems = true; + schema.Definitions["NomaiCoordinates"].Properties["z"].UniqueItems = true; + break; + } + + if (_title is "Star System Schema" or "Celestial Body Schema") + { + schema.Properties.Add("extras", new JsonSchemaProperty { + Type = JsonObjectType.Object, + Description = "Extra data that may be used by extension mods", + AllowAdditionalProperties = true, + AdditionalPropertiesSchema = new JsonSchema + { + Type = JsonObjectType.Object + } + }); + } + + return schema; + } + } } \ No newline at end of file