1
+ using System ;
2
+ using System . Globalization ;
3
+ using System . IO ;
4
+ using System . Reflection ;
5
+ using System . Text . RegularExpressions ;
6
+
7
+ namespace Domain {
8
+ public class Settings {
9
+ public const string DiscordAppId = "551089446460850176" ;
10
+ public const string ProgramName = "Exiled Presence" ;
11
+ public const string GameWindowTitle = "Path of Exile" ;
12
+ public const string Version = "v1.0.3" ;
13
+
14
+ public static readonly string StartupFolderPath = Environment . GetFolderPath ( Environment . SpecialFolder . Startup ) ;
15
+ public static readonly string StartupShortcutPath = Path . Combine ( StartupFolderPath , $ "{ ProgramName } .url") ;
16
+ public static readonly string AppPath = Assembly . GetEntryAssembly ( ) . Location ;
17
+ public static readonly Regex SessIdRegex = new Regex ( "^[0-9a-fA-F]{32}$" ) ;
18
+ public static readonly Regex VersionRegex = new Regex ( @"^v\d+(\.\d+)*$" ) ;
19
+
20
+ public static readonly TimeSpan PresencePollInterval = TimeSpan . FromMilliseconds ( 500 ) ;
21
+ public static readonly TimeSpan UpdateCheckInterval = TimeSpan . FromHours ( 24 ) ;
22
+ public static readonly TimeSpan CharacterUpdateInterval = TimeSpan . FromSeconds ( 60 ) ;
23
+ private const string ConfTimeFormat = "yyyy-MM-dd HH:mm" ;
24
+
25
+
26
+ public string AccountName { get ; set ; }
27
+ public string PoeSessionId { get ; set ; }
28
+
29
+ private string _configVersion ;
30
+ private string _lastUpdateCheck ;
31
+ private string _showElapsedTime ;
32
+ private string _showCharName ;
33
+ private string _showCharXp ;
34
+ private string _showCharLevel ;
35
+
36
+ public bool ShowElapsedTime => _showElapsedTime . Equals ( "1" ) ;
37
+ public bool ShowCharName => _showCharName . Equals ( "1" ) ;
38
+ public bool ShowCharXp => _showCharXp . Equals ( "1" ) ;
39
+ public bool ShowCharLevel => _showCharLevel . Equals ( "1" ) ;
40
+
41
+ public bool CheckUpdates => string . IsNullOrEmpty ( _lastUpdateCheck ) ||
42
+ DateTime . ParseExact ( _lastUpdateCheck , ConfTimeFormat , CultureInfo . InvariantCulture ,
43
+ DateTimeStyles . AssumeUniversal ) < DateTime . UtcNow . Subtract ( UpdateCheckInterval ) ;
44
+
45
+ /// <summary>
46
+ /// Loads in settings from another instance
47
+ /// </summary>
48
+ public void Update ( Settings settings ) {
49
+ AccountName = settings . AccountName ;
50
+ PoeSessionId = settings . PoeSessionId ;
51
+ _lastUpdateCheck = settings . _lastUpdateCheck ;
52
+ _configVersion = settings . _configVersion ;
53
+
54
+ _showElapsedTime = settings . _showElapsedTime ;
55
+ _showCharName = settings . _showCharName ;
56
+ _showCharXp = settings . _showCharXp ;
57
+ _showCharLevel = settings . _showCharXp ;
58
+ }
59
+
60
+ /// <summary>
61
+ /// Validate all config values
62
+ /// </summary>
63
+ public void Validate ( ) {
64
+ if ( AccountName == null || PoeSessionId == null || _lastUpdateCheck == null || _configVersion == null ||
65
+ _showCharXp == null | _showCharName == null || _showCharLevel == null || _showElapsedTime == null ) {
66
+ throw new ArgumentNullException ( ) ;
67
+ }
68
+
69
+ if ( ! string . IsNullOrEmpty ( PoeSessionId ) && ! SessIdRegex . IsMatch ( PoeSessionId ) ) {
70
+ throw new ArgumentException ( "Invalid session ID" ) ;
71
+ }
72
+
73
+ if ( ! string . IsNullOrEmpty ( AccountName ) && AccountName . Length < 3 ) {
74
+ throw new ArgumentException ( "Invalid account name" ) ;
75
+ }
76
+
77
+ if ( ! string . IsNullOrEmpty ( _lastUpdateCheck ) && ! DateTime . TryParseExact ( _lastUpdateCheck ,
78
+ ConfTimeFormat , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal , out _ ) ) {
79
+ throw new ArgumentException ( $ "Invalid last update time (expected format { ConfTimeFormat } )") ;
80
+ }
81
+
82
+ if ( string . IsNullOrEmpty ( _configVersion ) || ! VersionRegex . IsMatch ( _configVersion ) ) {
83
+ throw new ArgumentException ( "Invalid config version" ) ;
84
+ }
85
+
86
+ if ( string . IsNullOrEmpty ( _showElapsedTime ) ||
87
+ ! _showElapsedTime . Equals ( "0" ) &&
88
+ ! _showElapsedTime . Equals ( "1" ) ) {
89
+ throw new ArgumentException ( "Invalid value for show elapsed time (expected 1 or 0)" ) ;
90
+ }
91
+
92
+ if ( string . IsNullOrEmpty ( _showCharName ) ||
93
+ ! _showCharName . Equals ( "0" ) &&
94
+ ! _showCharName . Equals ( "1" ) ) {
95
+ throw new ArgumentException ( "Invalid value for show character name (expected 1 or 0)" ) ;
96
+ }
97
+
98
+ if ( string . IsNullOrEmpty ( _showCharXp ) ||
99
+ ! _showCharXp . Equals ( "0" ) &&
100
+ ! _showCharXp . Equals ( "1" ) ) {
101
+ throw new ArgumentException ( "Invalid value for show character xp (expected 1 or 0)" ) ;
102
+ }
103
+
104
+ if ( string . IsNullOrEmpty ( _showCharLevel ) ||
105
+ ! _showCharLevel . Equals ( "0" ) &&
106
+ ! _showCharLevel . Equals ( "1" ) ) {
107
+ throw new ArgumentException ( "Invalid value for show character level (expected 1 or 0)" ) ;
108
+ }
109
+ }
110
+
111
+ /// <summary>
112
+ /// Loads the default values
113
+ /// </summary>
114
+ public void Reset ( ) {
115
+ Update ( new Settings ( ) ) ;
116
+ }
117
+
118
+ /// <summary>
119
+ /// Attempt to store value with key
120
+ /// </summary>
121
+ public void ParseValue ( string key , string val ) {
122
+ if ( string . IsNullOrEmpty ( val ) ) {
123
+ val = null ;
124
+ } else if ( val . StartsWith ( "#" ) ) {
125
+ val = "" ;
126
+ }
127
+
128
+ switch ( key ) {
129
+ case "account name" :
130
+ AccountName = val ;
131
+ break ;
132
+
133
+ case "session id" :
134
+ PoeSessionId = val ;
135
+ break ;
136
+
137
+ case "last update check" :
138
+ _lastUpdateCheck = val ;
139
+ break ;
140
+
141
+ case "show elapsed time" :
142
+ _showElapsedTime = val ;
143
+ break ;
144
+
145
+ case "show character name" :
146
+ _showCharName = val ;
147
+ break ;
148
+
149
+ case "show character xp" :
150
+ _showCharXp = val ;
151
+ break ;
152
+
153
+ case "show character level" :
154
+ _showCharLevel = val ;
155
+ break ;
156
+
157
+ case "config version" :
158
+ _configVersion = val ;
159
+ break ;
160
+
161
+ default :
162
+ return ;
163
+ }
164
+ }
165
+
166
+ /// <summary>
167
+ /// Attempt to get value from key
168
+ /// </summary>
169
+ public string GetValue ( string key ) {
170
+ switch ( key ) {
171
+ case "account name" :
172
+ return AccountName ;
173
+
174
+ case "session id" :
175
+ return PoeSessionId ;
176
+
177
+ case "last update check" :
178
+ return _lastUpdateCheck ;
179
+
180
+ case "config version" :
181
+ return _configVersion ?? Version ;
182
+
183
+ case "show elapsed time" :
184
+ return _showElapsedTime ?? "1" ;
185
+
186
+ case "show character name" :
187
+ return _showCharName ?? "1" ;
188
+
189
+ case "show character xp" :
190
+ return _showCharXp ?? "1" ;
191
+
192
+ case "show character level" :
193
+ return _showCharLevel ?? "1" ;
194
+
195
+ default :
196
+ return null ;
197
+ }
198
+ }
199
+
200
+ /// <summary>
201
+ /// Sets the last update check time to now
202
+ /// </summary>
203
+ public void UpdateLastUpdateTime ( ) {
204
+ _lastUpdateCheck = DateTime . UtcNow . ToString ( ConfTimeFormat ) ;
205
+ }
206
+ }
207
+ }
0 commit comments