-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
219 lines (189 loc) · 7.55 KB
/
MainWindow.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
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using Lyra.Launcher.Functions;
using Lyra.Launcher.Pages;
using Microsoft.VisualBasic;
using Microsoft.Win32;
using MessageBox = System.Windows.Forms.MessageBox;
using Path = System.IO.Path;
namespace Lyra.Launcher;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly string LauncherPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lyra", "Launcher");
public static readonly RoutedCommand ShowCommand = new();
public static string? CurrentVersion;
private static StackPanel _nsp;
public static bool DllDownloaded;
public static bool DllDownloadedFailed;
public static bool VersionSupported;
private static Panel _dsp;
private static Grid _mg;
private static Grid _dg;
private static Border _db;
private static TextBlock _dTitle;
private static TextBlock _dBody;
private static SolidColorBrush _dbgSolidColorBrushb;
public MainWindow()
{
if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lyra", "Launcher")))
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lyra", "Launcher"));
DownloadDll();
try
{
Minecraft.Init();
CurrentVersion = Minecraft.GetVersion().ToString();
}
catch (Exception e)
{
CreateNotification(Utils.GetTranslation("Unable to find minecraft version, please try running the launcher as admin"));
CurrentVersion = Utils.GetTranslation("Unknown Version");
}
InitializeComponent();
_nsp = NotificationStackPanel;
_mg = MainGrid;
_db = DialogBorder;
_dsp = DialogStackPanel;
_dTitle = DialogTitle;
_dBody = DialogBody;
_dbgSolidColorBrushb = SolidColorBrush;
_dg = DialogGrid;
Config.loadConfig();
IsVersionSupported();
if (Config.RPCEnabled)
Dispatcher.InvokeAsync((Action)(() => Functions.DiscordRPC.Initialize()));
}
public static async Task<int> CreateDialog(string title, string body, string[] buttons)
{
const double blurAmount = 10;
var result = buttons.Length - 1;
var _event = new SemaphoreSlim(0);
_dg.Visibility = Visibility.Visible;
_dsp.Children.Clear();
Animations.ServerPopUpOpen(_db);
Animations.BlurElement(_mg, 0, blurAmount, TimeSpan.FromMilliseconds(250));
Animations.AnimateBrushOpacity(_dbgSolidColorBrushb, 0, 0.3, TimeSpan.FromMilliseconds(250));
_dTitle.Text = title;
_dBody.Text = body;
foreach (var text in buttons)
{
var btn = new Button
{
Content = text,
Margin = new Thickness(2.5,5,2.5,0),
Template = (ControlTemplate)App.Current.Resources["SmallButtonStyle"],
Cursor = Cursors.Hand,
HorizontalAlignment = HorizontalAlignment.Center
};
btn.Click += BtnOnClick;
_dsp.Children.Add(btn);
continue;
async void BtnOnClick(object sender, RoutedEventArgs e)
{
result = _dsp.Children.IndexOf(sender as UIElement);
_event.Release();
Animations.ServerPopUpClose(_db);
Animations.BlurElement(_mg, blurAmount, 0, TimeSpan.FromMilliseconds(250));
Animations.AnimateBrushOpacity(_dbgSolidColorBrushb, 0.3, 0, TimeSpan.FromMilliseconds(250));
await Task.Delay(250);
_dg.Visibility = Visibility.Hidden;
}
}
await _event.WaitAsync();
return result;
}
public static void CreateNotification(string message)
=> _nsp.Children.Insert(0, new Styles.Notification { Message = message });
public static async void DownloadDll()
{
try
{
using var client = new HttpClient();
await using var s = await client.GetStreamAsync("https://raw.githubusercontent.com/megahendick/Lyra.Launcher.CDN/main/Lyra.dll");
await using var fs = new FileStream(LauncherPath + "\\Lyra.dll", FileMode.OpenOrCreate);
await s.CopyToAsync(fs);
DllDownloaded = true;
}
catch (Exception)
{
DllDownloadedFailed = true;
CreateNotification(Utils.GetTranslation("Unable to download DLL"));
}
}
private static void IsVersionSupported()
{
try
{
var fileContent = new WebClient().DownloadString("https://raw.githubusercontent.com/megahendick/Lyra.Launcher.CDN/main/versions.txt")
.Split('\n', StringSplitOptions.RemoveEmptyEntries);
if (fileContent.Any(version => version.Trim() == CurrentVersion))
{
VersionSupported = true;
return;
}
CreateNotification(Utils.GetTranslation("{0} is unsupported, please use one of the following versions: {1}", [CurrentVersion, string.Join(' ', fileContent)]));
}
catch (Exception)
{
CreateNotification(Utils.GetTranslation("Unable to get supported versions"));
}
VersionSupported = false;
}
private void ShowCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
Show();
WindowState = WindowState.Normal;
}
private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) => DragMove();
private void Navigate_Home(object sender, RoutedEventArgs e)
=> Animations.PageScrollAnimation(PageStackPanel, 0);
private void Navigate_News(object sender, RoutedEventArgs e)
=> Animations.PageScrollAnimation(PageStackPanel, -430);
private void Navigate_Settings(object sender, RoutedEventArgs e)
=> Animations.PageScrollAnimation(PageStackPanel, -860);
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
=> RadioButton1.IsChecked = true;
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (Config.CloseToTray == false)
Close();
TaskbarIcon.Visibility = Visibility.Visible;
Hide();
}
private void MenuItem_OnClick(object sender, RoutedEventArgs e) => Close();
private void MenuItem_Inject(object sender, RoutedEventArgs e)
{
if (Config.UseCustomDLL)
{
Injector.Inject(Config.CustomDLLPath);
}
else
{
if (DllDownloadedFailed)
{
CreateNotification(Utils.GetTranslation("The DLL download failed, please check your internet connection or disable your antivirus software"));
return;
}
if (!DllDownloaded)
{
CreateNotification(Utils.GetTranslation("Please wait a few seconds for the DLL to finish downloading"));
return;
}
Injector.Inject(LauncherPath + "\\Lyra.dll");
}
}
}