@@ -17,60 +17,84 @@ public static class Configuration
1717 public static string UserName { get ; set ; } = "" ;
1818 public static string Password { get ; set ; } = "" ;
1919
20+ public static readonly IReadOnlyList < ConfigSetter > Configs = new List < ConfigSetter > ( )
21+ {
22+ new ( "token" , nameof ( Token ) , null , "" ) ,
23+ new ( "server" , nameof ( Server ) , null , "" ) ,
24+ new ( "port" , nameof ( Port ) , ParseInt , 1027 ) ,
25+
26+ new ( "updateFrequency" , nameof ( UpdateFrequency ) , ParseInt , 100 ) ,
27+ new ( "updateFlippedPlayers" , nameof ( UpdateFlippedPlayersFrequency ) , ParseInt , - 1 ) ,
28+ new ( "onlyRequestWhenNecessary" , nameof ( OnlyRequestWhenNecessary ) , ParseBool , true ) ,
29+
30+ new ( "serverName" , nameof ( ServerName ) , null , "Odyssey Server" ) ,
31+ new ( "accountName" , nameof ( UserName ) , null , "admin" ) ,
32+ new ( "accountPassword" , nameof ( Password ) , null , "admin" ) ,
33+ } ;
34+
2035 public static void LoadConfig ( ConfigurationManager manager )
2136 {
22- var settingsPath = File . Exists ( "/.dockerenv" ) ? "/data/settings.json" : null ;
23-
24- if ( ! string . IsNullOrWhiteSpace ( settingsPath ) )
37+ foreach ( var config in Configs )
38+ {
39+ var value = manager [ config . ConfigName ] ;
40+ if ( value == null ) continue ;
41+ config . SetConfig ( value ) ;
42+ }
43+
44+ var settingsPath = GetSettingsPath ( ) ;
45+ if ( settingsPath != null )
2546 {
2647 if ( ! File . Exists ( settingsPath ) )
2748 {
2849 GenerateSettingsFile ( settingsPath ) ;
29- Console . WriteLine ( $ "Settings was generated in { settingsPath } .\n Please configure a proper server ip/domain and restart the application.") ;
30- while ( true )
31- {
32- Thread . Sleep ( 10000 ) ;
33- }
34- return ;
3550 }
3651 var json = File . ReadAllText ( settingsPath ) ;
3752 var jObject = JObject . Parse ( json ) ;
3853
39- Token = jObject [ "token" ] ? . ToString ( ) ?? "" ;
40- Server = jObject [ "server" ] ? . ToString ( ) ?? "" ;
41-
42- Port = ParseInt ( jObject [ "port" ] ? . ToString ( ) , 1027 ) ;
43- UpdateFrequency = ParseInt ( jObject [ "updateFrequency" ] ? . ToString ( ) , 100 ) ;
44- UpdateFlippedPlayersFrequency = ParseInt ( jObject [ "updateFlippedPlayers" ] ? . ToString ( ) , - 1 ) ;
45- OnlyRequestWhenNecessary = ParseBool ( jObject [ "onlyRequestWhenNecessary" ] ? . ToString ( ) , true ) ;
46-
47- ServerName = jObject [ "serverName" ] ? . ToString ( ) ?? "" ;
48- UserName = manager [ "accountName" ] ?? "admin" ;
49- Password = manager [ "accountPassword" ] ?? "admin" ;
50- return ;
54+ foreach ( var config in Configs )
55+ {
56+ var value = jObject [ config . ConfigName ] ? . ToString ( ) ;
57+ if ( value == null ) continue ;
58+ config . SetConfig ( value ) ;
59+ }
5160 }
5261
53- Token = manager [ "Token" ] ?? "" ;
54- Server = manager [ "Server" ] ?? "" ;
55- Port = ParseInt ( manager [ "Port" ] , 1027 ) ;
56- UpdateFrequency = ParseInt ( manager [ "UpdateFrequency" ] , 100 ) ;
57- UpdateFlippedPlayersFrequency = ParseInt ( manager [ "UpdateFlippedPlayersFrequency" ] , - 1 ) ;
58- OnlyRequestWhenNecessary = ParseBool ( manager [ "OnlyRequestWhenNecessary" ] , true ) ;
59- ServerName = manager [ "ServerName" ] ?? "Odyssey Online" ;
60- UserName = manager [ "AccountName" ] ?? "admin" ;
61- Password = manager [ "AccountPassword" ] ?? "admin" ;
62+ foreach ( var config in Configs )
63+ {
64+ var value = Environment . GetEnvironmentVariable ( config . ConfigName . ToUpper ( ) ) ;
65+ if ( value == null ) continue ;
66+ config . SetConfig ( value ) ;
67+ }
68+
69+ if ( ! string . IsNullOrWhiteSpace ( Server ) && Port >= 0 ) return ;
70+
71+ Console . WriteLine ( $ "You need to configure a proper Host/IP and Port. Please check your configuration and restart the application. Current Configuration:{ Server } :{ Port } ") ;
72+ while ( true )
73+ {
74+ Thread . Sleep ( 1000 ) ;
75+ }
6276 }
6377
64- private static int ParseInt ( string ? input , int defaultValue )
78+ private static object ParseInt ( string input , object defaultValue )
6579 {
6680 return int . TryParse ( input , out var result ) ? result : defaultValue ;
6781 }
68-
69- private static bool ParseBool ( string ? input , bool defaultValue )
82+
83+ private static object ParseBool ( string input , object defaultValue )
7084 {
7185 return bool . TryParse ( input , out var result ) ? result : defaultValue ;
7286 }
7387
88+ private static string ? GetSettingsPath ( )
89+ {
90+ if ( File . Exists ( "/.dockerenv" ) )
91+ {
92+ return "/data/settings.json" ;
93+ }
94+
95+ return File . Exists ( "settings.json" ) ? "settings.json" : null ;
96+ }
97+
7498 private static void GenerateSettingsFile ( string file )
7599 {
76100 var jObject = new JObject ( )
@@ -88,4 +112,15 @@ private static void GenerateSettingsFile(string file)
88112 File . Create ( file ) . Close ( ) ;
89113 File . WriteAllText ( file , jObject . ToString ( ) ) ;
90114 }
115+
116+ public class ConfigSetter ( string configName , string propertyName , Func < string , object , object > ? parser , object defaultValue )
117+ {
118+ public string ConfigName => configName ;
119+
120+ public void SetConfig ( string value )
121+ {
122+ var finalValue = parser ? . Invoke ( value , defaultValue ) ?? value ;
123+ typeof ( Configuration ) . GetProperty ( propertyName ) ? . SetValue ( this , finalValue ) ;
124+ }
125+ }
91126}
0 commit comments