forked from STY1001/Unowhy-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
198 lines (181 loc) · 6.84 KB
/
App.xaml.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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Unowhy_Tools;
using Unowhy_Tools_WPF.Services;
using Unowhy_Tools_WPF.Services.Contracts;
using Unowhy_Tools_WPF.ViewModels;
using Unowhy_Tools_WPF.Views;
using Wpf.Ui.Mvvm.Contracts;
using Wpf.Ui.Mvvm.Services;
namespace Unowhy_Tools_WPF;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
private static readonly IHost _host = Host
.CreateDefaultBuilder()
.ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)); })
.ConfigureServices((context, services) =>
{
services.AddHostedService<ApplicationHostService>();
services.AddSingleton<IThemeService, ThemeService>();
services.AddSingleton<ITaskBarService, TaskBarService>();
services.AddSingleton<ISnackbarService, SnackbarService>();
services.AddSingleton<IDialogService, DialogService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<ITestWindowService, TestWindowService>();
services.AddSingleton<INavigationService, NavigationService>();
services.AddScoped<INavigationWindow, MainWindow>();
services.AddScoped<Views.Pages.Dashboard>();
services.AddScoped<DashboardViewModel>();
services.AddScoped<Views.Pages.About>();
services.AddScoped<Views.Pages.HisqoolManager>();
services.AddScoped<Views.Pages.Repair>();
services.AddScoped<Views.Pages.Delete>();
services.AddScoped<Views.Pages.Customize>();
services.AddScoped<Views.Pages.Drivers>();
services.AddScoped<Views.Pages.WinRE>();
services.AddScoped<Views.Pages.PCname>();
services.AddScoped<Views.Pages.AddUser>();
services.AddScoped<Views.Pages.AdminUser>();
services.AddScoped<Views.Pages.DrvBack>();
services.AddScoped<Views.Pages.DrvRest>();
services.AddScoped<Views.Pages.DrvConv>();
services.AddScoped<Views.Pages.PCinfo>();
services.AddScoped<Views.Pages.DebugPage>();
services.AddScoped<Views.Pages.Settings>();
services.AddScoped<Views.Pages.Updater>();
services.AddScoped<Views.Pages.Wifi>();
services.AddScoped<Views.Pages.FirstConfig>();
services.AddScoped<TrayWindow>();
services.AddScoped<UT>();
}).Build();
/// <summary>
/// Gets registered service.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetService<T>()
where T : class
{
return _host.Services.GetService(typeof(T)) as T;
}
/// <summary>
/// Occurs when the application is loading.
/// </summary>
private async void OnStartup(object sender, StartupEventArgs e)
{
UT.Data UTdata = new UT.Data();
/*
if (e.Args.Length > 0)
{
if (e.Args[0] == "-user")
{
UTdata.UserID = e.Args[1];
}
else
{
string user = await UT.RunReturn("whoami", "");
UTdata.UserID = UT.GetLine(user, 1);
}
if (e.Args[])
}
else
{
string user = await UT.RunReturn("whoami", "");
UTdata.UserID = UT.GetLine(user, 1);
}
*/
bool useTray = false;
string userId = null;
bool isHelp = false;
if(e.Args.Length > 0)
{
string args = string.Join(" ", e.Args);
UT.Write2Log("Unowhy Tools Args: " + args);
}
else
{
UT.Write2Log("Unowhy Tools Args: No arg");
}
if (e.Args.Length > 0)
{
for (int i = 0; i < e.Args.Length; i++)
{
if (e.Args[i] == "-user")
{
if (i < e.Args.Length - 1)
{
userId = e.Args[i + 1];
}
i++;
}
else if (e.Args[i] == "-tray")
{
useTray = true;
}
else if (e.Args[i] == "-help" || e.Args[i] == "-?")
{
isHelp = true;
}
}
}
if (userId == null)
{
string user = await UT.RunReturn("whoami", "");
userId = UT.GetLine(user, 1);
}
UTdata.UserID = userId;
if(useTray)
{
UTdata.RunTray = true;
}
if (isHelp)
{
string UTsver = UT.version.getverfull().ToString().Insert(2, ".") + " (Build " + UT.version.getverbuild().ToString() + ") ";
if (UT.version.isdeb()) UTsver = UTsver + "(Debug)";
else UTsver = UTsver + "(Release)";
AllocConsole();
Console.WriteLine($"Unowhy Tools by STY1001\nVersion {UTsver}\n\n\nArgs for Unowhy Tools:\n");
Console.WriteLine("-user [string] Set a custom UserID");
Console.WriteLine("-tray Launch UT in tray mode, can only open 1 tray simultaneously");
Console.WriteLine("-help Display help");
Console.ReadKey();
Application.Current.Shutdown();
}
else
{
await _host.StartAsync();
}
}
/// <summary>
/// Occurs when the application is closing.
/// </summary>
private async void OnExit(object sender, ExitEventArgs e)
{
await _host.StopAsync();
_host.Dispose();
}
/// <summary>
/// Occurs when an exception is thrown by an application but not handled.
/// </summary>
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
}
}