1- // COPYRIGHT 2012, 2013 by the Open Rails project.
1+ // COPYRIGHT 2009 - 2024 by the Open Rails project.
22//
33// This file is part of Open Rails.
44//
1919using System . Collections . Generic ;
2020using System . IO ;
2121using System . Linq ;
22- using System . Reflection ;
2322using System . Security . Cryptography ;
2423using System . Text ;
2524using ORTS . Common ;
2625
2726namespace ORTS . Settings
2827{
29- [ AttributeUsage ( AttributeTargets . Property ) ]
30- public sealed class DefaultAttribute : Attribute
31- {
32- public readonly object Value ;
33- public DefaultAttribute ( object value )
34- {
35- Value = value ;
36- }
37- }
38-
39- [ AttributeUsage ( AttributeTargets . Property ) ]
40- public sealed class DoNotSaveAttribute : Attribute
41- {
42- }
43-
44- public class UserSettings : SettingsBase
28+ public class UserSettings : PropertySettingsBase
4529 {
4630 public static readonly string RegistryKey ; // ie @"SOFTWARE\OpenRails\ORTS"
4731 public static readonly string SettingsFilePath ; // ie @"C:\Program Files\Open Rails\OpenRails.ini"
@@ -488,7 +472,7 @@ public string DirectXFeatureLevel
488472 public FolderSettings Folders { get ; private set ; }
489473 public InputSettings Input { get ; private set ; }
490474 public RailDriverSettings RailDriver { get ; private set ; }
491- public ContentSettings Content { get ; private set ; }
475+ public ContentSettings Content { get ; private set ; }
492476
493477 public UserSettings ( IEnumerable < string > options )
494478 : base ( SettingsStore . GetSettingStore ( SettingsFilePath , RegistryKey , null ) )
@@ -503,29 +487,12 @@ public UserSettings(IEnumerable<string> options)
503487 Content = new ContentSettings ( options ) ;
504488 }
505489
506- /// <summary>
507- /// Get a saving property from this instance by name.
508- /// </summary>
509- public SavingProperty < T > GetSavingProperty < T > ( string name )
510- {
511- var property = GetProperty ( name ) ;
512- if ( property == null )
513- return null ;
514- else
515- return new SavingProperty < T > ( this , property , AllowUserSettings ) ;
516- }
517-
518490 public override object GetDefaultValue ( string name )
519491 {
520- var property = GetType ( ) . GetProperty ( name ) ;
521-
522- if ( CustomDefaultValues . ContainsKey ( property . Name ) )
523- return CustomDefaultValues [ property . Name ] ;
492+ if ( CustomDefaultValues . ContainsKey ( name ) )
493+ return CustomDefaultValues [ name ] ;
524494
525- if ( property . GetCustomAttributes ( typeof ( DefaultAttribute ) , false ) . Length > 0 )
526- return ( property . GetCustomAttributes ( typeof ( DefaultAttribute ) , false ) [ 0 ] as DefaultAttribute ) . Value ;
527-
528- throw new InvalidDataException ( String . Format ( "UserSetting {0} has no default value." , property . Name ) ) ;
495+ return base . GetDefaultValue ( name ) ;
529496 }
530497
531498 public string GetCacheFilePath ( string type , string key )
@@ -542,122 +509,13 @@ public string GetCacheFilePath(string type, string key)
542509 return Path . Combine ( directory , hash + ".cache-or" ) ;
543510 }
544511
545- public PropertyInfo GetProperty ( string name )
546- {
547- return GetType ( ) . GetProperty ( name , BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy ) ;
548- }
549-
550- PropertyInfo [ ] GetProperties ( )
551- {
552- return GetType ( ) . GetProperties ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy ) .
553- // leave out the properties based on own, non System classes (f.i. RailDriver property)
554- Where ( pi => pi . PropertyType . FullName . Split ( '.' ) [ 0 ] == "System" ) .
555- ToArray ( ) ;
556- }
557-
558- protected override object GetValue ( string name )
559- {
560- return GetProperty ( name ) . GetValue ( this , null ) ;
561- }
562-
563- protected override void SetValue ( string name , object value )
564- {
565- GetProperty ( name ) . SetValue ( this , value , null ) ;
566- }
567-
568- protected override void Load ( Dictionary < string , string > optionsDictionary )
569- {
570- foreach ( var property in GetProperties ( ) )
571- Load ( optionsDictionary , property . Name , property . PropertyType ) ;
572- }
573-
574512 public override void Save ( )
575513 {
576- foreach ( var property in GetProperties ( ) )
577- if ( property . GetCustomAttributes ( typeof ( DoNotSaveAttribute ) , false ) . Length == 0 )
578- {
579- Console . WriteLine ( property . Name , property . PropertyType ) ;
580- Save ( property . Name , property . PropertyType ) ;
581- }
582-
514+ base . Save ( ) ;
583515 Folders . Save ( ) ;
584516 Input . Save ( ) ;
585517 RailDriver . Save ( ) ;
586518 Content . Save ( ) ;
587519 }
588-
589- public override void Save ( string name )
590- {
591- var property = GetProperty ( name ) ;
592- if ( property . GetCustomAttributes ( typeof ( DoNotSaveAttribute ) , false ) . Length == 0 )
593- Save ( property . Name , property . PropertyType ) ;
594- }
595-
596- public override void Reset ( )
597- {
598- foreach ( var property in GetProperties ( ) )
599- Reset ( property . Name ) ;
600- }
601-
602- public void Log ( )
603- {
604- foreach ( var property in GetProperties ( ) . OrderBy ( p => p . Name ) )
605- {
606- var value = property . GetValue ( this , null ) ;
607- var source = Sources [ property . Name ] == Source . CommandLine ? "(command-line)" : Sources [ property . Name ] == Source . User ? "(user set)" : "" ;
608- if ( property . PropertyType == typeof ( string [ ] ) )
609- Console . WriteLine ( "{0,-30} = {2,-14} {1}" , property . Name , String . Join ( ", " , ( ( string [ ] ) value ) . Select ( v => v . ToString ( ) ) . ToArray ( ) ) , source ) ;
610- else if ( property . PropertyType == typeof ( int [ ] ) )
611- Console . WriteLine ( "{0,-30} = {2,-14} {1}" , property . Name , String . Join ( ", " , ( ( int [ ] ) value ) . Select ( v => v . ToString ( ) ) . ToArray ( ) ) , source ) ;
612- else
613- Console . WriteLine ( "{0,-30} = {2,-14} {1}" , property . Name , value , source ) ;
614- }
615- }
616- }
617-
618- /// <summary>
619- /// A wrapper for a UserSettings property that saves any new values immediately.
620- /// </summary>
621- /// <typeparam name="T">Cast values to this type.</typeparam>
622- public class SavingProperty < T >
623- {
624- private readonly UserSettings Settings ;
625- private readonly PropertyInfo Property ;
626- private readonly bool DoSave ;
627-
628- internal SavingProperty ( UserSettings settings , PropertyInfo property , bool allowSave = true )
629- {
630- Settings = settings ;
631- Property = property ;
632- DoSave = allowSave ;
633- }
634-
635- /// <summary>
636- /// Get or set the current value of this property.
637- /// </summary>
638- public T Value
639- {
640- get => GetValue ( ) ;
641- set => SetValue ( value ) ;
642- }
643-
644- /// <summary>
645- /// Get the current value of this property.
646- /// </summary>
647- public T GetValue ( )
648- => Property . GetValue ( Settings ) is T cast ? cast : default ;
649-
650- /// <summary>
651- /// Set the current value of this property.
652- /// </summary>
653- public void SetValue ( T value )
654- {
655- if ( ! GetValue ( ) . Equals ( value ) )
656- {
657- Property . SetValue ( Settings , value ) ;
658- if ( DoSave )
659- Settings . Save ( Property . Name ) ;
660- }
661- }
662520 }
663521}
0 commit comments