1616// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717
1818using System ;
19+ using System . IO ;
1920using System . Collections . Generic ;
2021using System . Diagnostics ;
2122using System . Linq ;
@@ -27,6 +28,52 @@ namespace ORTS.Common
2728 /// </summary>
2829 public abstract class SettingsBase
2930 {
31+ public const string DefaultRegistryKey = "SOFTWARE\\ OpenRails\\ ORTS" ;
32+ public const string DefaultSettingsFileName = "OpenRails.ini" ;
33+
34+ public static string RegistryKey { get ; private set ; } // ie @"SOFTWARE\OpenRails\ORTS"
35+ public static string SettingsFilePath { get ; private set ; } // ie @"C:\Program Files\Open Rails\OpenRails.ini"
36+
37+ static SettingsBase ( )
38+ {
39+ // Only one of these is allowed; if the INI file exists, we use that, otherwise we use the registry.
40+ RegistryKey = DefaultRegistryKey ;
41+ SettingsFilePath = Path . Combine ( ApplicationInfo . ProcessDirectory , DefaultSettingsFileName ) ;
42+ if ( File . Exists ( SettingsFilePath ) )
43+ RegistryKey = null ;
44+ else
45+ SettingsFilePath = null ;
46+ }
47+
48+ /// <summary>
49+ /// Override the location for the settings. This only changes the static names.
50+ /// Only one must be specified (see SettingsBase static constructor).
51+ /// If settings objects already exist, they need to be changed using ChangeSettingsStore().
52+ /// </summary>
53+ /// <param name="filePath">The new ini file path, relative to the OpenRails base directory, or NULL.</param>
54+ /// <param name="registryKey">The new registry key, relative to the HKEY_CURRENT_USER, or NULL.</param>
55+ public static void OverrideSettingsLocations ( string filePath , string registryKey )
56+ {
57+ if ( ! String . IsNullOrEmpty ( filePath ) && ! String . IsNullOrEmpty ( registryKey ) )
58+ {
59+ throw new ArgumentException ( "Only one of filePath and registryKey may be provided." ) ;
60+ }
61+ else if ( ! String . IsNullOrEmpty ( filePath ) )
62+ {
63+ SettingsFilePath = Path . Combine ( ApplicationInfo . ProcessDirectory , filePath ) ;
64+ RegistryKey = null ;
65+ }
66+ else if ( ! String . IsNullOrEmpty ( registryKey ) )
67+ {
68+ RegistryKey = registryKey ;
69+ SettingsFilePath = null ;
70+ }
71+ else
72+ {
73+ throw new ArgumentException ( "One of filePath and registryKey must be provided." ) ;
74+ }
75+ }
76+
3077 /// <summary>
3178 /// Enumeration of the various sources for settings
3279 /// </summary>
@@ -42,6 +89,7 @@ protected enum Source
4289
4390 /// <summary>The store of the settings</summary>
4491 protected SettingsStore SettingStore { get ; private set ; }
92+
4593 /// <summary>Translates name of a setting to its source</summary>
4694 protected readonly Dictionary < string , Source > Sources = new Dictionary < string , Source > ( ) ;
4795
@@ -100,6 +148,33 @@ protected SettingsBase(SettingsStore settings)
100148 /// </summary>
101149 public abstract void Reset ( ) ;
102150
151+ public String GetSettingsStoreName ( )
152+ {
153+ string name = "none" ;
154+ if ( SettingStore != null ) { name = SettingStore . GetStoreName ( ) ; }
155+ return name ;
156+ }
157+
158+ /// <summary>
159+ /// Change the settings store. Creates a new SettingsStore based on the provided parameters.
160+ /// Only one of filePath and registry key should be specified. If both are provided, filePath
161+ /// prevails. Does not change the static locations (as that would affect other objects).
162+ /// See also SettingsStore.GetSettingStore() and OverrideSettingsLocations().
163+ /// </summary>
164+ /// <param name="filePath">The path to the INI file, or NULL if using the registry.</param>
165+ /// <param name="registryKey">The registry key (name), or NULL if using an INI file. </param>
166+ /// <param name="section">Optional, the name of the section / subkey.</param>
167+ public virtual void ChangeSettingsStore ( string filePath , string registryKey , string section )
168+ {
169+ if ( SettingStore != null )
170+ {
171+ SettingStore . Discard ( ) ;
172+ SettingStore = null ;
173+ }
174+ var fullPath = String . IsNullOrEmpty ( filePath ) ? null : Path . Combine ( ApplicationInfo . ProcessDirectory , filePath ) ;
175+ SettingStore = SettingsStore . GetSettingStore ( fullPath , registryKey , section ) ;
176+ }
177+
103178 /// <summary>
104179 /// Load settings from the options
105180 /// </summary>
0 commit comments