Skip to content

Commit

Permalink
Switch to MM-patched default-settings cfg instead of static settings …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
KSP-TaxiService committed Nov 22, 2016
1 parent f441f35 commit 5e80a48
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/RemoteTech/RTSettings.cs
Expand Up @@ -71,7 +71,7 @@ public class Settings
[Persistent(collectionIndex = "PRESETS")] public List<string> PreSets;

public const string SaveFileName = "RemoteTech_Settings.cfg";
public const string DefaultSettingFileName = "Default_Settings.cfg";
public const string DefaultSettingCfgURL = "RemoteTech/Default_Settings/RemoteTechSettings";

/// <summary>Trigger to force a reloading of the settings if a selected save is running.</summary>
public bool SettingsLoaded;
Expand All @@ -97,11 +97,6 @@ private static string SaveSettingFile
}
}

/// <summary>
/// Returns the full path of the Default_Settings of the RemoteTech mod
/// </summary>
private static string DefaultSettingFile => KSPUtil.ApplicationRootPath + "/GameData/RemoteTech/" + DefaultSettingFileName;

/// <summary>
/// Saves the current RTSettings object to the RemoteTech_Settings.cfg
/// </summary>
Expand All @@ -127,20 +122,38 @@ public void Save()
}
}

/// <summary>
/// Utilise KSP's GameDatabase to get a list of cfgs, included our Default_Settings.cfg, contained the 'RemoteTechSettings'
/// node and process each cfg accordingly
///
/// NOTE: Please do not use the static 'Default_Settings.cfg' file directly because we want third-party modders to apply
/// ModuleManager patches of their tweaks, like no signal delay, to our default-settings cfg that will be used when a
/// player starts a new game. (refer to our online manual for more details)
/// </summary>
public static Settings Load()
{
// Create a new settings object from the stored default settings
// Create a blank object of settings
var settings = new Settings();
var defaultLoad = ConfigNode.Load(DefaultSettingFile);
if(defaultLoad == null) // disable itself and write explanation to KSP's log
var defaultSuccess = false;

// Exploit KSP's GameDatabase to find our MM-patched cfg of default settings (from GameData/RemoteTech/Default_Settings.cfg)
var cfgs = GameDatabase.Instance.GetConfigs("RemoteTechSettings");
for (var i = 0; i < cfgs.Length; i++)
{
if(cfgs[i].url.Equals(DefaultSettingCfgURL))
{
defaultSuccess = ConfigNode.LoadObjectFromConfig(settings, cfgs[i].config);
RTLog.Notify("Load default settings into object with {0}: LOADED {1}", cfgs[i].config, defaultSuccess ? "OK" : "FAIL");
break;
}
}

if (!defaultSuccess) // disable itself and write explanation to KSP's log
{
RTLog.Notify("RemoteTech is disabled because the default file '{0}' is not found", DefaultSettingFile);
RTLog.Notify("RemoteTech is disabled because the default cfg '{0}' is not found", DefaultSettingCfgURL);
return null;
// the main impact of returning null is the endless loop of invoking Load() in the KSP's loading screen
}
defaultLoad = defaultLoad.GetNode("RemoteTechSettings"); // defaultLoad has root{...} so need to traverse downwards
var success = ConfigNode.LoadObjectFromConfig(settings, defaultLoad);
RTLog.Notify("Load default settings into object with {0}: LOADED {1}", defaultLoad, success ? "OK" : "FAIL");

settings.SettingsLoaded = true;

Expand All @@ -157,7 +170,7 @@ public static Settings Load()
return settings;
}

// try to load from the save-settings.cfg
// try to load from the save-settings.cfg (MM-patches will not touch because it is outside GameData)
var load = ConfigNode.Load(SaveSettingFile);
if (load == null)
{
Expand All @@ -172,7 +185,7 @@ public static Settings Load()
load = load.GetNode("RemoteTechSettings");

// replace the default settings with save-setting file
success = ConfigNode.LoadObjectFromConfig(settings, load);
var success = ConfigNode.LoadObjectFromConfig(settings, load);
RTLog.Notify("Found and load save settings into object with {0}: LOADED {1}", load, success ? "OK" : "FAIL");
}

Expand Down

1 comment on commit 5e80a48

@KSP-TaxiService
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix issue #700 's point 1

Please sign in to comment.