Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings_Engine: Add LoadSettings method for loading a single settings file #3300

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion Settings_Engine/Compute/LoadSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
}

2 changes: 2 additions & 0 deletions Settings_Engine/Compute/SaveSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand Down