forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
327 lines (278 loc) · 9.72 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ReSharper disable once CheckNamespace
namespace BD.WTTS;
sealed partial class Program : Startup
{
static Program? instance;
public Program(string[]? args = null) : base(args)
{
#if DEBUG
Console.WriteLine("args: ");
if (args != null)
foreach (var item in args)
{
Console.WriteLine(item);
}
#endif
}
protected override void ConfigureRequiredServices(IServiceCollection services)
{
services.AddSingleton(_ => UI.App.Instance);
services.AddSingleton<Startup>(this);
#if WINDOWS
services.AddScheduledTaskService();
#endif
// 平台服务 此项放在其他通用业务实现服务之前
services.AddPlatformService(this);
#if WINDOWS
//#if !MAUI TODO
// services.AddMSAppCenterApplicationSettings();
//#endif
services.AddJumpListService();
#endif
// 添加日志实现
services.AddGeneralLogging();
#if ANDROID || IOS
// 添加运行时权限
services.AddPlatformPermissions();
#endif
// 添加 app 配置项
services.TryAddSingleton(Options.Create(AppSettings.Instance));
#if MAUI || ANDROID || IOS
// 键值对存储 - 由 Essentials 提供
services.TryAddEssentialsSecureStorage();
#else
// 键值对存储 - 由 Repository 提供
services.TryAddRepositorySecureStorage();
// 首选项(Preferences) - 由 Repository 提供
services.AddRepositoryPreferences();
#endif
// 添加主线程助手(MainThreadDesktop)
services.AddMainThreadPlatformService();
// Essentials
#if LINUX
services.TryAddSingleton<IApplicationVersionService, Essentials_AppVerS>();
services.AddSingleton<IDeviceInfoPlatformService, LinuxDeviceInfoPlatformServiceImpl>();
#else
#if WINDOWS
#if AVALONIA
services.AddSingleton<IDeviceInfoPlatformService, AvaWinDeviceInfoPlatformServiceImpl>();
#endif
#endif
services.TryAddEssentials<Essentials_AppVerS>();
#endif
// 添加安全服务
services.AddSecurityService<EmbeddedAesDataProtectionProvider, LocalDataProtectionProvider>();
if (IsMainProcess)
{
services.AddSingleton<IPCMainProcessService, IPCMainProcessServiceImpl>();
}
else if (HasIPCRoot)
{
services.AddSingleton(_ => IPCSubProcessService.Instance);
}
}
protected override void ConfigureDemandServices(IServiceCollection services)
{
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Start();
#endif
if (HasUI)
{
services.AddPinyin();
#if MAUI || ANDROID || IOS
services.TryAddFontManager();
#elif AVALONIA
//services.TryAddAvaloniaTheme();
services.TryAddAvaloniaFontManager(useGdiPlusFirst: true);
#else
#endif
// 添加 Toast 提示服务
services.AddStartupToastIntercept();
services.TryAddToast();
services.AddSingleton<IApplication>(s => s.GetRequiredService<App>());
services.TryAddAvaloniaFilePickerPlatformService();
#if LINUX
services.AddSingleton<IClipboardPlatformService, AvaloniaClipboardPlatformService>();
#endif
#region WindowManager
services.TryAddWindowManager();
services.TryAddNavigationService();
services.TryToastService();
#endregion
// 添加视图模型管理服务
services.AddViewModelManager();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.UI");
#endif
}
if (HasHttpClientFactory || HasServerApiClient)
{
// 添加 Http 平台助手桌面端或移动端实现
services.TryAddClientHttpPlatformHelperService();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.HttpPlatformHelper");
#endif
}
if (HasHttpClientFactory)
{
#if ANDROID || IOS
// 添加 HttpClientFactory 平台原生实现
services.AddNativeHttpClient();
#endif
// 通用 Http 服务
Fusillade.NetCache.RequestCache = this;
services.AddFusilladeHttpClient();
services.TryImageHttpClientService();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.HttpClientFactory");
#endif
}
if (HasServerApiClient)
{
// 添加模型验证框架
services.TryAddModelValidator();
// 添加服务端 API 调用(BD.WTTS.MicroServices.ClientSDK)
services.TryAddMicroServiceClient();
services.AddAutoMapper(cfg =>
{
if (TryGetPlugins(out var plugins))
{
foreach (var plugin in plugins)
{
try
{
plugin.OnAddAutoMapper(cfg);
}
catch (Exception ex)
{
GlobalExceptionHandler.Handler(ex,
$"{plugin.UniqueEnglishName}{nameof(plugin.OnAddAutoMapper)}");
}
}
}
});
// 业务平台用户管理
services.TryAddUserManager();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.ServerApiClient");
#endif
}
if (HasRepositories)
{
// 添加仓储服务
services.AddRepositories();
}
// 添加通知服务
AddNotificationService();
void AddNotificationService()
{
#if (WINDOWS || MACCATALYST || MACOS || LINUX) && !(IOS || ANDROID)
if (!IsMainProcess) return;
#endif
services.TryAddNotificationService();
}
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.Notification");
#endif
if (HasHosts || HasIPCRoot)
{
// hosts 文件助手服务
services.AddHostsFileService();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.Hosts");
#endif
}
if (HasSteam)
{
#if (WINDOWS || MACCATALYST || MACOS || LINUX) && !(IOS || ANDROID)
// Steam 相关助手、工具类服务
services.AddSteamService2();
// Steamworks LocalApi Service
services.TryAddSteamworksLocalApiService();
#endif
// SteamDb WebApi Service
services.AddSteamDbWebApiService();
// Steamworks WebApi Service
services.AddSteamworksWebApiService();
// SteamGridDB WebApi Service
services.AddSteamGridDBWebApiService();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.Steam");
#endif
}
if (IsMainProcess)
{
// 应用程序更新服务
services.AddApplicationUpdateService();
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Record("ConfigureDemandServices.AppUpdate");
#endif
}
#if STARTUP_WATCH_TRACE || DEBUG
WatchTrace.Stop();
#endif
}
sealed class Essentials_AppVerS : IApplicationVersionService
{
string IApplicationVersionService.ApplicationVersion => AssemblyInfo.FileVersion;
string IApplicationVersionService.AssemblyTrademark => AssemblyInfo.Trademark;
}
#if LINUX
sealed class LinuxDeviceInfoPlatformServiceImpl : IDeviceInfoPlatformService
{
public string Model => "";
public string Manufacturer => "";
public string Name => "";
public string VersionString => Environment.OSVersion.VersionString;
public DeviceType DeviceType => DeviceType.Physical;
public bool IsChromeOS => false;
public bool IsWinUI => false;
public bool IsUWP => false;
public DeviceIdiom Idiom => DeviceIdiom.Desktop;
}
#endif
#if AVALONIA && WINDOWS
sealed class AvaWinDeviceInfoPlatformServiceImpl :
Common.Services.Implementation.Essentials.DeviceInfoPlatformServiceImpl
{
public sealed override bool IsWinUI => default;
public sealed override bool IsUWP => default;
}
#endif
public override void InitSettingSubscribe()
{
base.InitSettingSubscribe();
UI.App.Instance.InitSettingSubscribe();
}
protected override ActiveUserRecordDTO GetActiveUserRecord()
{
#if !__MOBILE__ && !MAUI
var app = UI.App.Instance;
var window = app.GetFirstOrDefaultWindow();
var screens = window?.Screens;
#else
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var mainDisplayInfoH = mainDisplayInfo.Height.ToInt32(NumberToInt32Format.Ceiling);
var mainDisplayInfoW = mainDisplayInfo.Width.ToInt32(NumberToInt32Format.Ceiling);
#endif
var result = new ActiveUserRecordDTO
{
#if __MOBILE__ || MAUI
ScreenCount = 1,
PrimaryScreenPixelDensity = mainDisplayInfo.Density,
PrimaryScreenWidth = mainDisplayInfoW,
PrimaryScreenHeight = mainDisplayInfoH,
SumScreenWidth = mainDisplayInfoW,
SumScreenHeight = mainDisplayInfoH,
#else
ScreenCount = screens?.ScreenCount ?? default,
PrimaryScreenPixelDensity = screens?.Primary?.Scaling ?? default,
PrimaryScreenWidth = screens?.Primary?.Bounds.Width ?? default,
PrimaryScreenHeight = screens?.Primary?.Bounds.Height ?? default,
SumScreenWidth = screens?.All.Sum(x => x.Bounds.Width) ?? default,
SumScreenHeight = screens?.All.Sum(x => x.Bounds.Height) ?? default,
#endif
};
return result;
}
}