diff --git a/Settings_Engine/Compute/LoadSettings.cs b/Settings_Engine/Compute/LoadSettings.cs index 953c10860..050b8a8dd 100644 --- a/Settings_Engine/Compute/LoadSettings.cs +++ b/Settings_Engine/Compute/LoadSettings.cs @@ -82,6 +82,53 @@ public static void LoadSettings(string settingsFolder = null, string fileFilter Global.BHoMSettingsLoaded.Add(fullSettingsLoadedKey); } + + /***************************************************/ + + [Description("Load a single settings file into memory for use. Settings file must contain one object in BHoM serialised JSON which is an object implementing the ISettings interface. Any settings object within the file will overwrite any previously loaded settings in memory.")] + [Input("filePath", "The full file path of the settings JSON file to be loaded.")] + public static void LoadSettings(string filePath) + { + if (string.IsNullOrEmpty(filePath)) + { + BH.Engine.Base.Compute.RecordError("File Path is null or empty."); + return; + } + + if(!File.Exists(filePath)) + { + BH.Engine.Base.Compute.RecordError($"File at {filePath} does not exist to read from."); + return; + } + + string contents = ""; + + try + { + contents = File.ReadAllText(filePath); + } + catch(Exception ex) + { + BH.Engine.Base.Compute.RecordError(ex, $"Error occurred in reading in file from {filePath}."); + } + + if(string.IsNullOrEmpty(contents)) + { + BH.Engine.Base.Compute.RecordError($"Failed to read data from {filePath}."); + return; + } + + try + { + ISettings settings = BH.Engine.Serialiser.Convert.FromJson(contents) as ISettings; + Type type = settings.GetType(); + Global.BHoMSettings[type] = settings; + Global.BHoMSettingsFilePaths[type] = filePath; + } + catch (Exception ex) + { + BH.Engine.Base.Compute.RecordError(ex, $"Cannot deserialise the contents of {filePath} to an ISettings object."); + } + } } } - diff --git a/Settings_Engine/Compute/SaveSettings.cs b/Settings_Engine/Compute/SaveSettings.cs index 6c1a4addc..c0f5dfe1e 100644 --- a/Settings_Engine/Compute/SaveSettings.cs +++ b/Settings_Engine/Compute/SaveSettings.cs @@ -68,6 +68,8 @@ public static bool SaveSettings(ISettings settings, bool run = false, string fil return true; } + /***************************************************/ + [Description("Saves all settings in memory to their respective files. Settings are saved back to the same file they were loaded from, overwriting them if they've changed. If settings were added during runtime and do not have an associated file, then a new file will be created with the name {settingsType}.json. If no folder is specified, the default of %ProgramData%/BHoM/Settings/{settingsType}.json will be used.")] [Input("outputDirectory", "Optional input to specify where to save settings file to. If the provided output directory differs from the saved load directory, settings will be saved in the provided output directory and not where they were originally loaded from.")] [Input("run", "Boolean toggle to determine whether to run the method. Useful for Visual Programming to prevent settings being saved/overwritten before providing a file path if desired.")]