1
- using System . Collections ;
2
- using System . Reflection ;
3
- using CommandLine ;
4
- using GameFrameX . Launcher . Common . Options ;
5
- using Newtonsoft . Json ;
6
- using Newtonsoft . Json . Converters ;
7
- using GameFrameX . Monitor ;
8
- using GameFrameX . NetWork . Message ;
1
+ using GameFrameX . NetWork . Message ;
9
2
10
3
namespace GameFrameX . Launcher
11
4
{
12
5
internal static class Program
13
6
{
14
- private static readonly Dictionary < Type , StartUpTagAttribute > StartUpTypes = new ( ) ;
15
- private static readonly List < Task > AppStartUpTasks = new List < Task > ( ) ;
16
- private static readonly List < IAppStartUp > AppStartUps = new List < IAppStartUp > ( ) ;
17
-
18
7
static async Task Main ( string [ ] args )
19
8
{
20
- List < string > environmentVariablesList = new List < string > ( ) ;
21
- environmentVariablesList . AddRange ( args ) ;
22
- Console . WriteLine ( "启动参数:" + string . Join ( " " , args ) ) ;
23
- Console . WriteLine ( "当前环境变量START---------------------" ) ;
24
- var environmentVariables = Environment . GetEnvironmentVariables ( ) ;
25
- foreach ( DictionaryEntry environmentVariable in environmentVariables )
26
- {
27
- if ( environmentVariable . Value == null || environmentVariable . Key . ToString ( ) . IsNullOrWhiteSpace ( ) )
28
- {
29
- continue ;
30
- }
31
-
32
- var key = environmentVariable . Key . ToString ( ) . StartsWith ( "--" ) ? environmentVariable . Key . ToString ( ) : "--" + environmentVariable . Key ;
33
- if ( environmentVariablesList . Contains ( key ) )
34
- {
35
- continue ;
36
- }
37
-
38
- environmentVariablesList . Add ( key ) ;
39
- environmentVariablesList . Add ( environmentVariable . Value . ToString ( ) ) ;
40
- }
41
-
42
- Console . WriteLine ( "当前环境变量END---------------------" ) ;
43
- Console . WriteLine ( ) ;
44
- Console . WriteLine ( ) ;
45
- var commandLineParser = new Parser ( configuration => { configuration . IgnoreUnknownArguments = true ; } ) ;
46
-
47
- var launcherOptions = commandLineParser . ParseArguments < LauncherOptions > ( environmentVariablesList ) . WithParsed ( ( LauncherOptionsValidate ) ) ? . Value ;
48
- var serverType = launcherOptions ? . ServerType ;
49
- if ( ! serverType . IsNullOrEmpty ( ) )
50
- {
51
- Console . WriteLine ( "启动的服务器类型 ServerType: " + serverType ) ;
52
- }
53
-
54
- LoggerHandler . Start ( serverType ) ;
55
- JsonSetting ( ) ;
56
- GlobalSettings . Load < AppSetting > ( $ "Configs/app_config.json") ;
57
- CacheStateTypeManager . Init ( ) ;
58
- MessageProtoHelper . Init ( typeof ( MessageProtoHandler ) . Assembly ) ;
59
-
60
- var types = Assembly . GetEntryAssembly ( ) ? . GetTypes ( ) ;
61
- if ( types != null )
62
- {
63
- foreach ( var type in types )
64
- {
65
- if ( type . IsClass && type . IsImplWithInterface ( typeof ( IAppStartUp ) ) && type . GetCustomAttribute < StartUpTagAttribute > ( ) != null )
66
- {
67
- var startUpTag = type . GetCustomAttribute < StartUpTagAttribute > ( ) ;
68
- StartUpTypes . Add ( type , startUpTag ) ;
69
- }
70
- }
71
- }
72
-
73
- var sortedStartUpTypes = StartUpTypes . OrderBy ( m => m . Value . Priority ) ;
74
-
75
- LogHelper . Info ( $ "----------------------------开始启动服务器啦------------------------------") ;
76
- var appSettings = GlobalSettings . GetSettings < AppSetting > ( ) ;
77
- if ( serverType != null && Enum . TryParse ( serverType , out ServerType serverTypeValue ) )
78
- {
79
- var startKv = sortedStartUpTypes . FirstOrDefault ( m => m . Value . ServerType == serverTypeValue ) ;
80
- if ( startKv . Value != null )
81
- {
82
- var appSetting = appSettings . FirstOrDefault ( m => m . ServerType == serverTypeValue ) ;
83
- if ( appSetting != null )
84
- {
85
- LogHelper . Error ( "从配置文件中找到对应的服务器类型的启动配置,将以配置启动=>" + startKv . Value . ServerType ) ;
86
- }
87
- else
88
- {
89
- LogHelper . Error ( "没有找到对应的服务器类型的启动配置,将以默认配置启动=>" + startKv . Value . ServerType ) ;
90
- appSetting = new AppSetting
91
- {
92
- ServerId = launcherOptions . ServerId ,
93
- ServerType = serverTypeValue ,
94
- APMPort = launcherOptions . APMPort ,
95
- IsDebug = launcherOptions . IsDebug ,
96
- IsDebugSend = launcherOptions . IsDebugSend ,
97
- IsDebugReceive = launcherOptions . IsDebugReceive ,
98
- Language = launcherOptions . Language ,
99
- DataCenter = launcherOptions . DataCenter ,
100
- DiscoveryCenterIp = launcherOptions . DiscoveryCenterIp ,
101
- DiscoveryCenterPort = launcherOptions . DiscoveryCenterPort ,
102
- DBIp = launcherOptions . DBIp ,
103
- DBPort = launcherOptions . DBPort ,
104
- SaveDataInterval = launcherOptions . SaveDataInterval ,
105
- HttpCode = launcherOptions . HttpCode ,
106
- HttpPort = launcherOptions . HttpPort ,
107
- HttpsPort = launcherOptions . HttpsPort ,
108
- HttpUrl = launcherOptions . HttpUrl ,
109
- InnerIp = launcherOptions . InnerIp ,
110
- InnerPort = launcherOptions . InnerPort ,
111
- OuterIp = launcherOptions . OuterIp ,
112
- OuterPort = launcherOptions . OuterPort ,
113
- WsPort = launcherOptions . WsPort ,
114
- WssPort = launcherOptions . WssPort ,
115
- WssCertFilePath = launcherOptions . WssCertFilePath ,
116
- DataBaseUrl = launcherOptions . DataBaseUrl ,
117
- DataBaseName = launcherOptions . DataBaseName ,
118
- MinModuleId = launcherOptions . MinModuleId ,
119
- MaxModuleId = launcherOptions . MaxModuleId
120
- } ;
121
- }
122
-
123
- Launcher ( args , startKv , appSetting ) ;
124
- }
125
- }
126
- else
127
- {
128
- foreach ( var keyValuePair in sortedStartUpTypes )
129
- {
130
- bool isFind = false ;
131
-
132
- foreach ( var appSetting in appSettings )
133
- {
134
- if ( keyValuePair . Value . ServerType == appSetting . ServerType )
135
- {
136
- Launcher ( args , keyValuePair , appSetting ) ;
137
- isFind = true ;
138
- break ;
139
- }
140
- }
141
-
142
- if ( isFind == false )
143
- {
144
- LogHelper . Error ( "没有找到对应的服务器类型的启动配置,将以默认配置启动=>" + keyValuePair . Value . ServerType ) ;
145
- Launcher ( args , keyValuePair ) ;
146
- }
147
- }
148
- }
149
-
150
- LogHelper . Info ( $ "----------------------------启动服务器结束啦------------------------------") ;
151
- ApplicationPerformanceMonitorStart ( serverType ) ;
152
- ConsoleLogo ( ) ;
153
-
154
- await Task . WhenAll ( AppStartUpTasks ) ;
155
- }
156
-
157
- private static void LauncherOptionsValidate ( LauncherOptions options )
158
- {
159
- if ( ! options . ServerType . IsNullOrEmpty ( ) && Enum . TryParse ( options . ServerType , out ServerType serverTypeValue ) )
9
+ await GameApp . Entry ( args , ( ) =>
160
10
{
161
- // options.CheckAPMPort();
162
-
163
- options . CheckServerId ( ) ;
164
-
165
- options . CheckInnerPort ( ) ;
166
-
167
- switch ( serverTypeValue )
168
- {
169
- case ServerType . Log :
170
- break ;
171
- case ServerType . DataBase :
172
- {
173
- options . CheckDataBaseUrl ( ) ;
174
-
175
- options . CheckDataBaseName ( ) ;
176
-
177
- options . CheckOuterIp ( ) ;
178
-
179
- options . CheckOuterPort ( ) ;
180
- }
181
- break ;
182
- case ServerType . Cache :
183
- break ;
184
- case ServerType . Gateway :
185
- {
186
- options . CheckOuterIp ( ) ;
187
-
188
- options . CheckOuterPort ( ) ;
189
- }
190
- break ;
191
- case ServerType . Account :
192
- break ;
193
- case ServerType . Router :
194
- {
195
- options . CheckOuterIp ( ) ;
196
- options . CheckOuterPort ( ) ;
197
- options . CheckWsPort ( ) ;
198
- options . CheckDiscoveryCenterIp ( ) ;
199
- options . CheckDiscoveryCenterPort ( ) ;
200
- }
201
- break ;
202
- case ServerType . DiscoveryCenter :
203
- {
204
- options . CheckOuterIp ( ) ;
205
- options . CheckOuterPort ( ) ;
206
- }
207
- break ;
208
- case ServerType . Backup :
209
- break ;
210
- case ServerType . Login :
211
- break ;
212
- case ServerType . Game :
213
- {
214
- options . CheckMinModuleId ( ) ;
215
- options . CheckMaxModuleId ( ) ;
216
- options . CheckOuterIp ( ) ;
217
- options . CheckOuterPort ( ) ;
218
- options . CheckDiscoveryCenterIp ( ) ;
219
- options . CheckDiscoveryCenterPort ( ) ;
220
- }
221
- break ;
222
- case ServerType . Recharge :
223
- break ;
224
- case ServerType . Logic :
225
- break ;
226
- case ServerType . Chat :
227
- break ;
228
- case ServerType . Mail :
229
- break ;
230
- case ServerType . Guild :
231
- break ;
232
- case ServerType . Room :
233
- break ;
234
- case ServerType . All :
235
- break ;
236
- }
237
- }
238
- }
239
-
240
- private static void Launcher ( string [ ] args , KeyValuePair < Type , StartUpTagAttribute > keyValuePair , AppSetting appSetting = null )
241
- {
242
- var task = Start ( args , keyValuePair . Key , keyValuePair . Value . ServerType , appSetting , out var startUp ) ;
243
- AppStartUps . Add ( startUp ) ;
244
- AppStartUpTasks . Add ( task ) ;
245
- }
246
-
247
- private static void ApplicationPerformanceMonitorStart ( string serverType )
248
- {
249
- if ( serverType != null && Enum . TryParse ( serverType , out ServerType serverTypeValue ) )
250
- {
251
- foreach ( var appStartUp in AppStartUps )
252
- {
253
- if ( appStartUp . ServerType == serverTypeValue )
254
- {
255
- if ( appStartUp . Setting . APMPort is > 0 and < ushort . MaxValue )
256
- {
257
- MetricsHelper . Start ( appStartUp . Setting . APMPort ) ;
258
- }
259
- else
260
- {
261
- LogHelper . Error ( "APM端口没有配置和无效,将不会启动APM监控" ) ;
262
- }
263
-
264
- break ;
265
- }
266
- }
267
- }
268
- }
269
-
270
- /// <summary>
271
- /// Json 配置
272
- /// </summary>
273
- private static void JsonSetting ( )
274
- {
275
- JsonConvert . DefaultSettings = ( ) => new JsonSerializerSettings
276
- {
277
- ReferenceLoopHandling = ReferenceLoopHandling . Ignore ,
278
-
279
- NullValueHandling = NullValueHandling . Ignore , // 忽略 null 值
280
- // Formatting = Formatting.Indented, // 生成格式化的 JSON
281
- MissingMemberHandling = MissingMemberHandling . Ignore , // 忽略缺失的成员
282
- Converters = new List < JsonConverter >
283
- {
284
- new StringEnumConverter ( ) // 将枚举转换为字符串
285
- }
286
- } ;
287
- }
288
-
289
- private static Task Start ( string [ ] args , Type appStartUpType , ServerType serverType , BaseSetting setting , out IAppStartUp startUp )
290
- {
291
- startUp = ( IAppStartUp ) Activator . CreateInstance ( appStartUpType ) ;
292
- if ( startUp != null )
293
- {
294
- bool isSuccess = startUp . Init ( serverType , setting , args ) ;
295
- if ( isSuccess )
296
- {
297
- LogHelper . Info ( $ "----------------------------START-----{ serverType } ------------------------------") ;
298
- LogHelper . Info ( $ "启动服务器类型:{ serverType } , 配置信息:{ startUp . Setting . ToFormatString ( ) } ") ;
299
- LogHelper . Info ( $ "--------------------------------------------------------------------------------") ;
300
- var task = AppEnter . Entry ( startUp ) ;
301
- LogHelper . Info ( $ "-----------------------------END------{ serverType } ------------------------------") ;
302
- return task ;
303
- }
304
- }
305
-
306
- return Task . CompletedTask ;
11
+ CacheStateTypeManager . Init ( ) ;
12
+ MessageProtoHelper . Init ( typeof ( MessageProtoHandler ) . Assembly ) ;
13
+ } , LogAction ) ;
307
14
}
308
15
309
- private static void ConsoleLogo ( )
16
+ private static void LogAction ( LogOptions options )
310
17
{
311
- Console . WriteLine ( @" _____ ______ __ __" ) ;
312
- Console . WriteLine ( @"| __ \ | ___| \ \ / /" ) ;
313
- Console . WriteLine ( @"| | \/ __ _ _ __ ___ ___ | |_ _ __ __ _ _ __ ___ ___ \ V / " ) ;
314
- Console . WriteLine ( @"| | __ / _` || '_ ` _ \ / _ \| _| | '__| / _` || '_ ` _ \ / _ \ / \ " ) ;
315
- Console . WriteLine ( @"| |_\ \| (_| || | | | | || __/| | | | | (_| || | | | | || __// /^\ \" ) ;
316
- Console . WriteLine ( @" \____/ \__,_||_| |_| |_| \___|\_| |_| \__,_||_| |_| |_| \___|\/ \/" ) ;
317
- Console . WriteLine ( @" " ) ;
18
+ // options.IsConsole = false;
318
19
}
319
20
}
320
21
}
0 commit comments