-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.xaml.cs
238 lines (221 loc) · 8.73 KB
/
Main.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
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
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;
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 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("logs/log_{0}.txt", DateTime.Now.ToString("yyyyMMdd_HHmm")))
.WriteTo.Logger(log => log
.MinimumLevel.ControlledBy(lls)
.WriteTo.Console()
);
Log.Logger = logger.CreateLogger();
try
{
ModLoader.LoadFiles();
}
catch(Exception ex)
{
Log.Error(ex, "Something went wrong");
}
SettingsPage = new Settings();
InitializeComponent();
Viewer.Content = MainPage;
}
public void LogModList()
{
foreach (ModFile modFile in ModPage.Mods.Where(x => x.isEnabled))
{
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);
}
}
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> EnableMods = 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.EnableMods.Count > 0)
{
List<ModFile> listModFile = ModInfos.Instance.Mods;
foreach (string i in Main.Settings.EnableMods)
{
(int indexMod, ModFile? modFile) = listModFile.Enumerate().FirstOrDefault(t => t.Item2.Name == i);
if (modFile != null)
listModFile[indexMod].isEnabled = true;
else
Log.Warning($"Mod {i} not found");
}
}
}
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));
}
}
}