1- using GameFrameX . Foundation . Json ;
2-
3- namespace GameFrameX . Utility . Setting ;
4-
5- /// <summary>
6- /// 全局设置
7- /// </summary>
8- public static class GlobalSettings
9- {
10- /// <summary>
11- /// 存储应用设置的列表
12- /// </summary>
13- private static readonly List < AppSetting > Settings = new ( 16 ) ;
14-
15- private static int _saveIntervalInMilliSeconds ;
16-
17- /// <summary>
18- /// 是否运行中
19- /// </summary>
20- public static bool IsAppRunning { get ; set ; }
21-
22- /// <summary>
23- /// 启动时间
24- /// </summary>
25- public static DateTime LaunchTime { get ; set ; }
26-
27- /// <summary>
28- /// 是否是调试模式
29- /// </summary>
30- public static bool IsDebug { get ; set ; }
31-
32- /// <summary>
33- /// 服务器ID
34- /// </summary>
35- public static int ServerId { get ; set ; }
36-
37- /// <summary>
38- /// 数据存储间隔 单位 毫秒,默认5分钟,最小1秒
39- /// </summary>
40- public static int SaveIntervalInMilliSeconds
41- {
42- get { return _saveIntervalInMilliSeconds ; }
43- set
44- {
45- if ( value < 1000 )
46- {
47- _saveIntervalInMilliSeconds = GlobalConst . SaveIntervalInMilliSeconds ;
48- return ;
49- }
50-
51- _saveIntervalInMilliSeconds = value ;
52- }
53- }
54-
55- /// <summary>
56- /// 加载启动配置
57- /// </summary>
58- /// <param name="path">配置文件路径</param>
59- /// <exception cref="InvalidOperationException">当配置文件解析失败时抛出</exception>
60- /// <exception cref="Exception">当服务器ID不在合法范围内时抛出</exception>
61- public static void Load ( string path )
62- {
63- Settings . Clear ( ) ;
64-
65- if ( ! File . Exists ( path ) )
66- {
67- Console . BackgroundColor = ConsoleColor . Yellow ;
68- Console . Write ( "配置文件不存在。可能会导致启动失败==>>>" + path ) ;
69- Console . ResetColor ( ) ;
70- Console . WriteLine ( ) ;
71- return ;
72- }
73-
74- var configJson = File . ReadAllText ( path ) ;
75- var settings = JsonHelper . Deserialize < List < AppSetting > > ( configJson ) ?? throw new InvalidOperationException ( ) ;
76-
77- foreach ( var setting in settings )
78- {
79- if ( setting . ServerId < GlobalConst . MinServerId || setting . ServerId > GlobalConst . MaxServerId )
80- {
81- throw new Exception ( $ "ServerId不合法{ setting . ServerId } ,需要在[{ GlobalConst . MinServerId } ,{ GlobalConst . MaxServerId } ]范围之内") ;
82- }
83-
84- Settings . Add ( setting ) ;
85- }
86- }
87-
88- /// <summary>
89- /// 获取所有设置
90- /// </summary>
91- /// <returns>返回所有设置的列表</returns>
92- public static List < AppSetting > GetSettings ( )
93- {
94- return Settings . ToList ( ) ;
95- }
96-
97- /// <summary>
98- /// 根据服务器类型获取设置
99- /// </summary>
100- /// <param name="serverType">服务器类型</param>
101- /// <returns>返回匹配的设置列表</returns>
102- public static List < AppSetting > GetSettings ( ServerType serverType )
103- {
104- var result = new List < AppSetting > ( ) ;
105- foreach ( var setting in Settings )
106- {
107- if ( ( setting . ServerType & serverType ) != 0 )
108- {
109- result . Add ( setting ) ;
110- }
111- }
112-
113- return result ;
114- }
115-
116- /// <summary>
117- /// 根据服务器类型获取特定类型的设置
118- /// </summary>
119- /// <param name="serverType">服务器类型</param>
120- /// <typeparam name="T">设置类型</typeparam>
121- /// <returns>返回匹配的设置,如果没有找到则返回null</returns>
122- public static AppSetting GetSetting < T > ( ServerType serverType )
123- {
124- foreach ( var setting in Settings )
125- {
126- if ( ( setting . ServerType & serverType ) != 0 )
127- {
128- return setting ;
129- }
130- }
131-
132- return null ;
133- }
1+ using GameFrameX . Foundation . Json ;
2+
3+ namespace GameFrameX . Utility . Setting ;
4+
5+ /// <summary>
6+ /// 全局设置
7+ /// </summary>
8+ public static class GlobalSettings
9+ {
10+ /// <summary>
11+ /// 存储应用设置的列表
12+ /// </summary>
13+ private static readonly List < AppSetting > Settings = new ( 16 ) ;
14+
15+ private static int _saveIntervalInMilliSeconds ;
16+
17+ /// <summary>
18+ /// 是否运行中
19+ /// </summary>
20+ public static bool IsAppRunning { get ; set ; }
21+
22+ /// <summary>
23+ /// 启动时间
24+ /// </summary>
25+ public static DateTime LaunchTime { get ; set ; }
26+
27+ /// <summary>
28+ /// 是否是调试模式
29+ /// </summary>
30+ public static bool IsDebug { get ; set ; }
31+
32+ /// <summary>
33+ /// 服务器ID
34+ /// </summary>
35+ public static int ServerId { get ; set ; }
36+
37+ /// <summary>
38+ /// 数据存储间隔 单位 毫秒,默认5分钟,最小1秒
39+ /// </summary>
40+ public static int SaveIntervalInMilliSeconds
41+ {
42+ get { return _saveIntervalInMilliSeconds ; }
43+ set
44+ {
45+ if ( value < 1000 )
46+ {
47+ _saveIntervalInMilliSeconds = GlobalConst . SaveIntervalInMilliSeconds ;
48+ return ;
49+ }
50+
51+ _saveIntervalInMilliSeconds = value ;
52+ }
53+ }
54+
55+ /// <summary>
56+ /// 加载启动配置
57+ /// </summary>
58+ /// <param name="path">配置文件路径</param>
59+ /// <exception cref="InvalidOperationException">当配置文件解析失败时抛出</exception>
60+ /// <exception cref="Exception">当服务器ID不在合法范围内时抛出</exception>
61+ public static void Load ( string path )
62+ {
63+ Settings . Clear ( ) ;
64+
65+ if ( ! File . Exists ( path ) )
66+ {
67+ Console . BackgroundColor = ConsoleColor . Yellow ;
68+ Console . Write ( "配置文件不存在。可能会导致启动失败==>>>" + path ) ;
69+ Console . ResetColor ( ) ;
70+ Console . WriteLine ( ) ;
71+ return ;
72+ }
73+
74+ var configJson = File . ReadAllText ( path ) ;
75+ var settings = JsonHelper . Deserialize < List < AppSetting > > ( configJson ) ?? throw new InvalidOperationException ( ) ;
76+
77+ foreach ( var setting in settings )
78+ {
79+ if ( setting . ServerId < GlobalConst . MinServerId || setting . ServerId > GlobalConst . MaxServerId )
80+ {
81+ throw new Exception ( $ "ServerId不合法{ setting . ServerId } ,需要在[{ GlobalConst . MinServerId } ,{ GlobalConst . MaxServerId } ]范围之内") ;
82+ }
83+
84+ Settings . Add ( setting ) ;
85+ }
86+ }
87+
88+ /// <summary>
89+ /// 获取所有设置
90+ /// </summary>
91+ /// <returns>返回所有设置的列表</returns>
92+ public static List < AppSetting > GetSettings ( )
93+ {
94+ return Settings . ToList ( ) ;
95+ }
96+
97+ /// <summary>
98+ /// 根据服务器类型获取设置
99+ /// </summary>
100+ /// <param name="serverType">服务器类型</param>
101+ /// <returns>返回匹配的设置列表</returns>
102+ public static List < AppSetting > GetSettings ( ServerType serverType )
103+ {
104+ var result = new List < AppSetting > ( ) ;
105+ foreach ( var setting in Settings )
106+ {
107+ if ( ( setting . ServerType & serverType ) != 0 )
108+ {
109+ result . Add ( setting ) ;
110+ }
111+ }
112+
113+ return result ;
114+ }
115+ /// <summary>
116+ /// 根据服务器名称获取特定类型的设置
117+ /// </summary>
118+ /// <param name="serverName">服务器名称,用于匹配AppSetting中的ServerName属性</param>
119+ /// <typeparam name="T">设置类型,用于类型安全检查,确保返回正确的设置类型</typeparam>
120+ /// <returns>返回匹配的设置,如果没有找到则返回null。返回的设置可以被转换为类型T</returns>
121+ /// <exception cref="ArgumentNullException">当serverName为null时抛出此异常</exception>
122+ public static AppSetting GetSettingByServerName < T > ( string serverName )
123+ {
124+ ArgumentNullException . ThrowIfNull ( serverName , nameof ( serverName ) ) ;
125+ foreach ( var setting in Settings )
126+ {
127+ if ( setting . ServerName == serverName )
128+ {
129+ return setting ;
130+ }
131+ }
132+
133+ return null ;
134+ }
135+
136+ /// <summary>
137+ /// 根据服务器Id获取特定类型的设置
138+ /// </summary>
139+ /// <param name="tagName">服务器Id,用于匹配AppSetting中的ServerId属性</param>
140+ /// <typeparam name="T">设置类型,用于类型安全检查,确保返回正确的设置类型</typeparam>
141+ /// <returns>返回匹配的设置,如果没有找到则返回null。返回的设置可以被转换为类型T</returns>
142+ /// <remarks>此方法不会对传入的serverId进行有效性验证,请确保传入的值在有效范围内</remarks>
143+ public static AppSetting GetSettingByServerId < T > ( int tagName )
144+ {
145+ foreach ( var setting in Settings )
146+ {
147+ if ( setting . ServerId == tagName )
148+ {
149+ return setting ;
150+ }
151+ }
152+
153+ return null ;
154+ }
155+
156+ /// <summary>
157+ /// 根据服务器标签名称获取特定类型的设置
158+ /// </summary>
159+ /// <param name="tagName">服务器标签名称,用于匹配AppSetting中的TagName属性</param>
160+ /// <typeparam name="T">设置类型,用于类型安全检查,确保返回正确的设置类型</typeparam>
161+ /// <returns>返回匹配的设置,如果没有找到则返回null。返回的设置可以被转换为类型T</returns>
162+ /// <exception cref="ArgumentNullException">当tagName为null时抛出此异常</exception>
163+ public static AppSetting GetSettingByTagName < T > ( string tagName )
164+ {
165+ ArgumentNullException . ThrowIfNull ( tagName , nameof ( tagName ) ) ;
166+ foreach ( var setting in Settings )
167+ {
168+ if ( setting . TagName == tagName )
169+ {
170+ return setting ;
171+ }
172+ }
173+
174+ return null ;
175+ }
176+
177+ /// <summary>
178+ /// 根据服务器类型获取特定类型的设置
179+ /// </summary>
180+ /// <param name="serverType">服务器类型</param>
181+ /// <typeparam name="T">设置类型</typeparam>
182+ /// <returns>返回匹配的设置,如果没有找到则返回null</returns>
183+ public static AppSetting GetSetting < T > ( ServerType serverType )
184+ {
185+ foreach ( var setting in Settings )
186+ {
187+ if ( ( setting . ServerType & serverType ) != 0 )
188+ {
189+ return setting ;
190+ }
191+ }
192+
193+ return null ;
194+ }
134195}
0 commit comments