diff --git a/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs b/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs index 0c90222c..c24cdf40 100644 --- a/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs +++ b/NewHorizons/Builder/Body/AsteroidBeltBuilder.cs @@ -25,7 +25,7 @@ namespace NewHorizons.Builder.Body { var size = Random.Range(minSize, maxSize); - var config = new PlanetConfig(null); + var config = new PlanetConfig(); config.Name = $"{bodyName} Asteroid {i}"; config.StarSystem = parentConfig.StarSystem; diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index fb239008..7026dc24 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -50,7 +50,7 @@ namespace NewHorizons.Builder.Body if (starModule.Curve != null) { var controller = sunAtmosphere.AddComponent(); - controller.scaleCurve = starModule.ToAnimationCurve(); + controller.scaleCurve = starModule.GetAnimationCurve(); controller.initialSize = starModule.Size; } } diff --git a/NewHorizons/Builder/Orbital/FocalPointBuilder.cs b/NewHorizons/Builder/Orbital/FocalPointBuilder.cs index c6b851bf..520d15d9 100644 --- a/NewHorizons/Builder/Orbital/FocalPointBuilder.cs +++ b/NewHorizons/Builder/Orbital/FocalPointBuilder.cs @@ -48,7 +48,7 @@ namespace NewHorizons.Builder.Orbital var gravitationalMass = GetGravitationalMass(primary.Config) + GetGravitationalMass(secondary.Config); // Copying it because I don't want to modify the actual config - var fakeMassConfig = new PlanetConfig(null); + var fakeMassConfig = new PlanetConfig(); // Now need to fake the 3 values to make it return this mass fakeMassConfig.Base.SurfaceSize = 1; diff --git a/NewHorizons/Components/MapSatelliteOrbitFix.cs b/NewHorizons/Components/MapSatelliteOrbitFix.cs index 46e1007e..f4bc540d 100644 --- a/NewHorizons/Components/MapSatelliteOrbitFix.cs +++ b/NewHorizons/Components/MapSatelliteOrbitFix.cs @@ -7,8 +7,7 @@ namespace NewHorizons.Components { public void Awake() { - // TODO: eventually all bodies should have configs - var config = new PlanetConfig(null); + var config = new PlanetConfig(); config.Base.SurfaceSize = 10f; var detector = base.transform.GetComponentInChildren(); diff --git a/NewHorizons/External/Configs/Config.cs b/NewHorizons/External/Configs/Config.cs deleted file mode 100644 index e2b971a7..00000000 --- a/NewHorizons/External/Configs/Config.cs +++ /dev/null @@ -1,40 +0,0 @@ -using NewHorizons.External.Modules; -using NewHorizons.Utility; -using System; -using System.Collections.Generic; -namespace NewHorizons.External.Configs -{ - public class Config - { - public Config(Dictionary dict) - { - if (dict == null) return; - - foreach (var item in dict) - { - var property = GetType().GetProperty(item.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - - if (property == null) property = GetType().GetProperty(item.Key.ToCamelCase(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - if (property == null) property = GetType().GetProperty(item.Key.ToTitleCase(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - - if (property != null) - { - if (property.PropertyType.BaseType == typeof(Module)) - { - if (property.GetValue(this) == null) - { - var module = Activator.CreateInstance(property.PropertyType); - property.SetValue(this, module); - } - ((Module)property.GetValue(this)).Build(item.Value as Dictionary); - } - else - { - property.SetValue(this, Convert.ChangeType(item.Value, property.PropertyType)); - } - } - else Logger.LogError($"{item.Key} {item.Value} is not valid. Is your config formatted correctly?"); - } - } - } -} diff --git a/NewHorizons/External/Configs/PlanetConfig.cs b/NewHorizons/External/Configs/PlanetConfig.cs index d0e2831f..6ec7d24c 100644 --- a/NewHorizons/External/Configs/PlanetConfig.cs +++ b/NewHorizons/External/Configs/PlanetConfig.cs @@ -1,10 +1,9 @@ using NewHorizons.External.Modules; using NewHorizons.External.Modules.VariableSize; -using System.Collections.Generic; namespace NewHorizons.External.Configs { - public class PlanetConfig : Config + public class PlanetConfig { public string Name { get; set; } public string Version { get; set; } @@ -33,7 +32,7 @@ namespace NewHorizons.External.Configs public SandModule Sand { get; set; } public FunnelModule Funnel { get; set; } - public PlanetConfig(Dictionary dict) : base(dict) + public PlanetConfig() { // Always have to have a base module if (Base == null) Base = new BaseModule(); diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index 669728a3..f64f99c5 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -1,7 +1,6 @@ -using System.Collections.Generic; -namespace NewHorizons.External.Configs +namespace NewHorizons.External.Configs { - public class StarSystemConfig : Config + public class StarSystemConfig { public bool canEnterViaWarpDrive = true; public bool startHere = false; @@ -24,8 +23,5 @@ namespace NewHorizons.External.Configs public string path = null; public bool destroyStarField = false; } - - public StarSystemConfig(Dictionary dict) : base(dict) { } - } } diff --git a/NewHorizons/External/Modules/AsteroidBeltModule.cs b/NewHorizons/External/Modules/AsteroidBeltModule.cs index 404adb6e..a128a3b2 100644 --- a/NewHorizons/External/Modules/AsteroidBeltModule.cs +++ b/NewHorizons/External/Modules/AsteroidBeltModule.cs @@ -1,6 +1,6 @@ namespace NewHorizons.External.Modules { - public class AsteroidBeltModule : Module + public class AsteroidBeltModule { public float InnerRadius { get; set; } public float OuterRadius { get; set; } diff --git a/NewHorizons/External/Modules/AtmosphereModule.cs b/NewHorizons/External/Modules/AtmosphereModule.cs index 77e2bd93..2105a0f1 100644 --- a/NewHorizons/External/Modules/AtmosphereModule.cs +++ b/NewHorizons/External/Modules/AtmosphereModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class AtmosphereModule : Module + public class AtmosphereModule { public float Size { get; set; } public MColor CloudTint { get; set; } diff --git a/NewHorizons/External/Modules/BaseModule.cs b/NewHorizons/External/Modules/BaseModule.cs index 6a60b0b7..39e14eaf 100644 --- a/NewHorizons/External/Modules/BaseModule.cs +++ b/NewHorizons/External/Modules/BaseModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class BaseModule : Module + public class BaseModule { public bool HasMapMarker { get; set; } public bool HasAmbientLight { get; set; } diff --git a/NewHorizons/External/Modules/HeightMapModule.cs b/NewHorizons/External/Modules/HeightMapModule.cs index b552ae38..de29f105 100644 --- a/NewHorizons/External/Modules/HeightMapModule.cs +++ b/NewHorizons/External/Modules/HeightMapModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class HeightMapModule : Module + public class HeightMapModule { public string HeightMap { get; set; } public string TextureMap { get; set; } diff --git a/NewHorizons/External/Modules/Module.cs b/NewHorizons/External/Modules/Module.cs deleted file mode 100644 index b02c63a0..00000000 --- a/NewHorizons/External/Modules/Module.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -namespace NewHorizons.External.Modules -{ - public abstract class Module - { - public void Build(Dictionary dict) - { - if (dict == null) - { - return; - } - foreach (var item in dict) - { - var property = GetType().GetProperty(item.Key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); - property.SetValue(this, Convert.ChangeType(item.Value, property.PropertyType)); - } - } - } -} diff --git a/NewHorizons/External/Modules/OrbitModule.cs b/NewHorizons/External/Modules/OrbitModule.cs index 224202cb..93e0577f 100644 --- a/NewHorizons/External/Modules/OrbitModule.cs +++ b/NewHorizons/External/Modules/OrbitModule.cs @@ -2,7 +2,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class OrbitModule : Module, IOrbitalParameters + public class OrbitModule : IOrbitalParameters { public float SemiMajorAxis { get; set; } public float Inclination { get; set; } diff --git a/NewHorizons/External/Modules/ProcGenModule.cs b/NewHorizons/External/Modules/ProcGenModule.cs index 8d0e9bd8..be14d406 100644 --- a/NewHorizons/External/Modules/ProcGenModule.cs +++ b/NewHorizons/External/Modules/ProcGenModule.cs @@ -1,7 +1,8 @@ using NewHorizons.Utility; + namespace NewHorizons.External.Modules { - public class ProcGenModule : Module + public class ProcGenModule { public float Scale { get; set; } public MColor Color { get; set; } diff --git a/NewHorizons/External/Modules/PropModule.cs b/NewHorizons/External/Modules/PropModule.cs index af470ff2..265d319c 100644 --- a/NewHorizons/External/Modules/PropModule.cs +++ b/NewHorizons/External/Modules/PropModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class PropModule : Module + public class PropModule { public ScatterInfo[] Scatter; public DetailInfo[] Details; diff --git a/NewHorizons/External/Modules/ShipLogModule.cs b/NewHorizons/External/Modules/ShipLogModule.cs index 7e1bb676..0e297db4 100644 --- a/NewHorizons/External/Modules/ShipLogModule.cs +++ b/NewHorizons/External/Modules/ShipLogModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class ShipLogModule : Module + public class ShipLogModule { public string xmlFile; public string spriteFolder; diff --git a/NewHorizons/External/Modules/SignalModule.cs b/NewHorizons/External/Modules/SignalModule.cs index 61687912..87490fae 100644 --- a/NewHorizons/External/Modules/SignalModule.cs +++ b/NewHorizons/External/Modules/SignalModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class SignalModule : Module + public class SignalModule { public SignalInfo[] Signals; diff --git a/NewHorizons/External/Modules/SpawnModule.cs b/NewHorizons/External/Modules/SpawnModule.cs index 8d5f113a..1adb5996 100644 --- a/NewHorizons/External/Modules/SpawnModule.cs +++ b/NewHorizons/External/Modules/SpawnModule.cs @@ -1,7 +1,7 @@ using NewHorizons.Utility; namespace NewHorizons.External.Modules { - public class SpawnModule : Module + public class SpawnModule { public MVector3 PlayerSpawnPoint { get; set; } public MVector3 ShipSpawnPoint { get; set; } diff --git a/NewHorizons/External/Modules/VariableSize/VariableSizeModule.cs b/NewHorizons/External/Modules/VariableSize/VariableSizeModule.cs index 45b81690..92ff263e 100644 --- a/NewHorizons/External/Modules/VariableSize/VariableSizeModule.cs +++ b/NewHorizons/External/Modules/VariableSize/VariableSizeModule.cs @@ -1,7 +1,7 @@ using UnityEngine; namespace NewHorizons.External.Modules.VariableSize { - public class VariableSizeModule : Module + public class VariableSizeModule { public TimeValuePair[] Curve { get; set; } @@ -11,7 +11,7 @@ namespace NewHorizons.External.Modules.VariableSize public float Value { get; set; } } - public AnimationCurve ToAnimationCurve(float size = 1f) + public AnimationCurve GetAnimationCurve(float size = 1f) { var curve = new AnimationCurve(); foreach (var pair in this.Curve) diff --git a/NewHorizons/External/Modules/NewHorizonsData.cs b/NewHorizons/External/NewHorizonsData.cs similarity index 99% rename from NewHorizons/External/Modules/NewHorizonsData.cs rename to NewHorizons/External/NewHorizonsData.cs index 5cc5e25f..3e8fc289 100644 --- a/NewHorizons/External/Modules/NewHorizonsData.cs +++ b/NewHorizons/External/NewHorizonsData.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using Logger = NewHorizons.Utility.Logger; -namespace NewHorizons.External.Modules + +namespace NewHorizons.External { public static class NewHorizonsData { diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 9907dbad..becd1584 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -12,7 +12,6 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; using Logger = NewHorizons.Utility.Logger; -using NewHorizons.External.Modules.VariableSize; namespace NewHorizons.Handlers { diff --git a/NewHorizons/Handlers/PlanetGraphHandler.cs b/NewHorizons/Handlers/PlanetGraphHandler.cs index de8471d4..92a2ed6a 100644 --- a/NewHorizons/Handlers/PlanetGraphHandler.cs +++ b/NewHorizons/Handlers/PlanetGraphHandler.cs @@ -36,11 +36,9 @@ namespace NewHorizons.Handlers { if (centers.Length == 0 && Main.Instance.CurrentStarSystem == "SolarSystem") { - var SunConfig = new Dictionary - { - {"name", "Sun"} - }; - _rootNode = ConstructGraph(new NewHorizonsBody(new PlanetConfig(SunConfig), Main.Instance), bodies); + var SunConfig = new PlanetConfig(); + SunConfig.Name = "Sun"; + _rootNode = ConstructGraph(new NewHorizonsBody(SunConfig, Main.Instance), bodies); } else { diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 15543cd1..89d3d868 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -2,7 +2,7 @@ using NewHorizons.Builder.Props; using NewHorizons.Components; using NewHorizons.External.Configs; -using NewHorizons.External.Modules; +using NewHorizons.External; using NewHorizons.Handlers; using NewHorizons.Utility; using OWML.Common; @@ -95,7 +95,7 @@ namespace NewHorizons BodyDict["SolarSystem"] = new List(); BodyDict["EyeOfTheUniverse"] = new List(); // Keep this empty tho fr - SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), Instance) + SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(), Instance) { Config = { @@ -387,7 +387,7 @@ namespace NewHorizons { // Since we didn't load it earlier there shouldn't be a star system config var starSystemConfig = mod.ModHelper.Storage.Load($"systems/{config.StarSystem}.json"); - if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(null); + if (starSystemConfig == null) starSystemConfig = new StarSystemConfig(); else Logger.LogWarning($"Loaded system config for {config.StarSystem}. Why wasn't this loaded earlier?"); var system = new NewHorizonsSystem(config.StarSystem, starSystemConfig, mod); diff --git a/NewHorizons/NewHorizonsApi.cs b/NewHorizons/NewHorizonsApi.cs index 76fcd5bb..a93ad02b 100644 --- a/NewHorizons/NewHorizonsApi.cs +++ b/NewHorizons/NewHorizonsApi.cs @@ -1,9 +1,10 @@ using NewHorizons.Builder.Props; -using NewHorizons.External.Configs; using NewHorizons.Utility; using OWML.Common; +using OWML.Utils; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using UnityEngine; using UnityEngine.Events; @@ -12,21 +13,43 @@ namespace NewHorizons { public class NewHorizonsApi { - [Obsolete("Create(Dictionary config) is deprecated, please use Create(Dictionary config, IModBehaviour mod) instead")] + [Obsolete("Create(Dictionary config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")] public void Create(Dictionary config) { Create(config, null); } + [Obsolete("Create(Dictionary config) is deprecated, please use LoadConfigs(IModBehaviour mod) instead")] public void Create(Dictionary config, IModBehaviour mod) { - Logger.Log("Recieved API request to create planet " + (string)config["Name"], Logger.LogType.Log); - var planetConfig = new PlanetConfig(config); + try + { + var name = (string)config["Name"]; - var body = new NewHorizonsBody(planetConfig, mod ?? Main.Instance); + Logger.LogWarning($"Recieved API request to create planet [{name}]"); - if (!Main.BodyDict.ContainsKey(body.Config.StarSystem)) Main.BodyDict.Add(body.Config.StarSystem, new List()); - Main.BodyDict[body.Config.StarSystem].Add(body); + if (name == null) return; + + var relativePath = $"temp/{name}.json"; + var fullPath = Main.Instance.ModHelper.Manifest.ModFolderPath + relativePath; + if (!Directory.Exists(Main.Instance.ModHelper.Manifest.ModFolderPath + "temp")) + { + Directory.CreateDirectory(Main.Instance.ModHelper.Manifest.ModFolderPath + "temp"); + } + JsonHelper.SaveJsonObject(fullPath, config); + var body = Main.Instance.LoadConfig(Main.Instance, relativePath); + File.Delete(fullPath); + + // Update it to point to their mod for textures and stuff + body.Mod = mod ?? Main.Instance; + + if (!Main.BodyDict.ContainsKey(body.Config.StarSystem)) Main.BodyDict.Add(body.Config.StarSystem, new List()); + Main.BodyDict[body.Config.StarSystem].Add(body); + } + catch(Exception ex) + { + Logger.LogError($"Error in Create API: {ex.Message} {ex.StackTrace}"); + } } public void LoadConfigs(IModBehaviour mod) diff --git a/NewHorizons/Patches/AudioSignalPatches.cs b/NewHorizons/Patches/AudioSignalPatches.cs index 814a5b77..4dcb2fe2 100644 --- a/NewHorizons/Patches/AudioSignalPatches.cs +++ b/NewHorizons/Patches/AudioSignalPatches.cs @@ -1,7 +1,7 @@ using HarmonyLib; using NewHorizons.Builder.Props; using NewHorizons.Components; -using NewHorizons.External.Modules; +using NewHorizons.External; using NewHorizons.Handlers; using System; using UnityEngine; diff --git a/NewHorizons/Patches/PlayerDataPatches.cs b/NewHorizons/Patches/PlayerDataPatches.cs index b9b968ad..1bcf18fd 100644 --- a/NewHorizons/Patches/PlayerDataPatches.cs +++ b/NewHorizons/Patches/PlayerDataPatches.cs @@ -1,6 +1,6 @@ using HarmonyLib; using NewHorizons.Builder.Props; -using NewHorizons.External.Modules; +using NewHorizons.External; using NewHorizons.Handlers; using System.Collections.Generic; using System.Linq;