-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMain.xaml.cs
More file actions
298 lines (276 loc) · 11.3 KB
/
Main.xaml.cs
File metadata and controls
298 lines (276 loc) · 11.3 KB
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
using ModShardLauncher.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using UndertaleModLib;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using System.Runtime.InteropServices;
using ModShardLauncher.Mods;
using System.Diagnostics;
using UndertaleModLib.Models;
namespace ModShardLauncher
{
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
public partial class Main : Window
{
public static Main Instance;
public MainPage MainPage;
public ModInfos ModPage;
public ModSourceInfos ModSourcePage;
public Settings SettingsPage;
public static UserSettings Settings = new();
public static LoggingLevelSwitch lls = new();
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
public static IntPtr handle;
public string mslVersion;
public string utmtlibVersion;
public readonly string logPath = "logs";
private const double DefaultWidth = 960; // Исходная ширина
private const double DefaultHeight = 800; // Исходная высота
private const double AspectRatio = DefaultWidth / DefaultHeight; // Соотношение сторон
private const double ScreenSizePercentage = 0.85; // Процент от размера экрана
public Main()
{
handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
Instance = this;
MainPage = new MainPage();
ModPage = new ModInfos();
UserSettings.LoadSettings();
ModSourcePage = new ModSourceInfos();
if (!Directory.Exists(ModLoader.ModPath))
Directory.CreateDirectory(ModLoader.ModPath);
if (!Directory.Exists(ModLoader.ModSourcesPath))
Directory.CreateDirectory(ModLoader.ModSourcesPath);
// create File and Console (controlledby a switch) sinks
LoggerConfiguration logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(string.Format("{0}/log_{1}.txt", logPath, DateTime.Now.ToString("yyyyMMdd_HHmm")))
.WriteTo.Logger(log => log
.MinimumLevel.ControlledBy(lls)
.WriteTo.Console()
);
Log.Logger = logger.CreateLogger();
// work around to find the FileVersion of ModShardLauncher.dll for single file publishing
// see: https://github.com/dotnet/runtime/issues/13051
try
{
ProcessModule mainProcess = Msl.ThrowIfNull(Process.GetCurrentProcess().MainModule);
string mainProcessName = Msl.ThrowIfNull(mainProcess.FileName);
mslVersion = "v" + FileVersionInfo.GetVersionInfo(mainProcessName).FileVersion;
utmtlibVersion = "v" + FileVersionInfo.GetVersionInfo(typeof(UndertaleCode).Assembly.Location).FileVersion;
}
catch (FileNotFoundException ex)
{
Log.Error(ex, "Cannot find the dll of ModShardLauncher");
throw;
}
Log.Information("Launching msl {{{0}}} using UTMT {{{1}}}", mslVersion, utmtlibVersion);
try
{
ModLoader.LoadFiles();
}
catch (Exception ex)
{
Log.Error(ex, "Something went wrong");
}
SettingsPage = new Settings();
InitializeComponent();
// Начальный размер окна
SetInitialSize();
Viewer.Content = MainPage;
}
private void SetInitialSize()
{
var screenWidth = SystemParameters.PrimaryScreenWidth;
var screenHeight = SystemParameters.PrimaryScreenHeight;
if (screenWidth < DefaultWidth || screenHeight < DefaultHeight)
{
if (screenWidth < screenHeight)
{
Width = screenWidth * ScreenSizePercentage;
Height = Width / AspectRatio; // Поддержка соотношения сторон
}
else
{
Height = screenHeight * ScreenSizePercentage;
Width = Height * AspectRatio; // Поддержка соотношения сторон
}
}
else
{
Width = DefaultWidth;
Height = DefaultHeight;
}
}
public void LogModListStatus()
{
foreach (ModFile modFile in ModPage.Mods.Where(x => x.Enabled))
{
LogModStatus(modFile);
}
}
public static void LogModStatus(ModFile modFile)
{
string statusMessage = "";
switch (modFile.PatchStatus)
{
case PatchStatus.Patching:
statusMessage = "Patching failed";
break;
case PatchStatus.Success:
statusMessage = "Patching succeeded";
break;
case PatchStatus.None:
statusMessage = "Waiting to be patched";
break;
}
Log.Warning("Patching {{{2}}} for {{{0}}} {{{1}}}", modFile.Name, modFile.Version, statusMessage);
}
public ModFile GetFailingMod()
{
return ModPage.Mods.Where(x => x.Enabled).First(x => x.PatchStatus == PatchStatus.Patching); ;
}
private void MyToggleButton_Checked(object sender, EventArgs e)
{
foreach (var i in stackPanel.Children)
{
if (i != sender && i is MyToggleButton button)
{
button.MyButton.IsChecked = false;
}
}
}
public void Refresh()
{
ModPage = new ModInfos();
ModSourcePage = new ModSourceInfos();
Settings settingCache = new();
settingCache.Viewer.Content = SettingsPage.Viewer.Content;
SettingsPage = settingCache;
try
{
ModLoader.LoadFiles();
}
catch (Exception ex)
{
Log.Error(ex, "Something went wrong");
}
if (Viewer.Content is ModInfos) Viewer.Content = ModPage;
else if (Viewer.Content is ModSourceInfos) Viewer.Content = ModSourcePage;
else if (Viewer.Content is Settings) Viewer.Content = SettingsPage;
else Viewer.Content = MainPage;
}
private void MyToggleButton_Click(object sender, EventArgs e)
{
_ = Log.CloseAndFlushAsync();
Close();
}
private void MyToggleButton_Click_1(object sender, EventArgs e)
{
if (sender is MyToggleButton button && Msl.ThrowIfNull(button.MyButton.IsChecked)) Viewer.Content = ModPage;
else Viewer.Content = MainPage;
}
private void MyToggleButton_Click_2(object sender, EventArgs e)
{
if (sender is MyToggleButton button && Msl.ThrowIfNull(button.MyButton.IsChecked)) Viewer.Content = ModSourcePage;
else Viewer.Content = MainPage;
}
private void MyToggleButton_Click_4(object sender, EventArgs e)
{
if (sender is MyToggleButton button && Msl.ThrowIfNull(button.MyButton.IsChecked)) Viewer.Content = SettingsPage;
else Viewer.Content = MainPage;
}
private void MyToggleButton_Click_3(object sender, EventArgs e)
{
WindowState = WindowState.Minimized;
if (sender is MyToggleButton button) button.MyButton.IsChecked = false;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Settings.SaveSettings();
}
}
public class UserSettings
{
public string Language = "English";
public bool EnableLogger = true;
public List<string> EnabledMods = new();
public static void LoadSettings()
{
// if no settings file, early stop
if (!File.Exists("Settings.json")) return;
// read file
string settings = File.ReadAllText("Settings.json");
// convert if as UserSettings
Main.Settings = Msl.ThrowIfNull(JsonConvert.DeserializeObject<UserSettings>(settings));
CheckLog(Main.Settings.EnableLogger);
// auto check active mods
if (Main.Settings.EnabledMods.Count > 0)
{
List<ModFile> listModFile = ModInfos.Instance.Mods;
foreach (string i in Main.Settings.EnabledMods)
{
(int indexMod, ModFile? modFile) = listModFile.Enumerate().FirstOrDefault(t => t.Item2.Name == i);
if (modFile != null) listModFile[indexMod].Enabled = true;
else Log.Warning("Mod {0} not found", i);
}
}
}
public static void CheckLog(bool isLogConsole)
{
if (isLogConsole)
{
Main.ShowWindow(Main.handle, Main.SW_SHOW);
Main.lls.MinimumLevel = LogEventLevel.Information;
}
else
{
Main.ShowWindow(Main.handle, Main.SW_HIDE);
Main.lls.MinimumLevel = (LogEventLevel)1 + (int)LogEventLevel.Fatal;
}
}
public static void ChangeLanguage(int index)
{
ResourceDictionary resDict;
switch (index)
{
case 0:
resDict = Application.Current.Resources.MergedDictionaries.First(t => t.Source.OriginalString == @"Language/zh-cn.xaml");
Application.Current.Resources.MergedDictionaries.Remove(resDict);
Application.Current.Resources.MergedDictionaries.Add(resDict);
Main.Settings.Language = "Chinese";
break;
case 1:
resDict = Application.Current.Resources.MergedDictionaries.First(t => t.Source.OriginalString == @"Language/en-us.xaml");
Application.Current.Resources.MergedDictionaries.Remove(resDict);
Application.Current.Resources.MergedDictionaries.Add(resDict);
Main.Settings.Language = "English";
break;
case 2:
resDict = Application.Current.Resources.MergedDictionaries.First(t => t.Source.OriginalString == @"Language/ru-ru.xaml");
Application.Current.Resources.MergedDictionaries.Remove(resDict);
Application.Current.Resources.MergedDictionaries.Add(resDict);
Main.Settings.Language = "Русский";
break;
}
}
public void SaveSettings()
{
File.WriteAllText("Settings.json", JsonConvert.SerializeObject(this));
}
}
}