This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFrmMain.cs
351 lines (310 loc) · 11.8 KB
/
FrmMain.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using CmlLib.Core;
using CmlLib.Core.Auth;
using CmlLib.Core.Downloader;
using Newtonsoft.Json.Linq;
using Salaros.Configuration;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MythicalLauncher
{
public partial class FrmMain : Form
{
private string appConfig = Application.StartupPath + @"\settings.ini";
private string buildfile = Application.StartupPath + @"\buildnumber";
private string versionfile = Application.StartupPath + @"\version";
public FrmMain(MSession session)
{
this.session = session;
InitializeComponent();
}
CMLauncher launcher;
readonly MSession session;
MinecraftPath gamePath;
bool useMJava = true;
string javaPath = "java.exe";
private int uiThreadId = Thread.CurrentThread.ManagedThreadId;
void Alert(string msg, FrmAlert.enmType type)
{
FrmAlert frm = new FrmAlert();
frm.showAlert(msg, type);
}
void RememberMe()
{
var cfg = new ConfigParser(appConfig);
var Xms = cfg.GetValue("RAM", "Xms");
var Xmx = cfg.GetValue("RAM", "Xmx");
if (Xms == "" || Xmx == "")
{
Alert("Please set your Desired Ram Settings!", FrmAlert.enmType.Info);
}
else
{
txtXms.Text = Xms;
TxtXmx.Text = Xmx;
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
string avatarUrl = $"https://minotar.net/avatar/{FrmLogin.l_username}";
using (WebClient client = new WebClient())
{
byte[] imageBytes = client.DownloadData(avatarUrl);
mcpicture.Image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageBytes));
}
if (Program.debugmdoe == "true")
{
lblver.Text = "Version: " + File.ReadAllText(versionfile) + " (DEBUG MODE)";
}
else
{
lblver.Text = "Version: " + File.ReadAllText(versionfile);
}
this.WindowState = FormWindowState.Normal;
label1.Text = "Welcome, " + FrmLogin.l_username;
lblbuilnumber.Text = "Build number: " + File.ReadAllText(buildfile);
RememberMe();
DisplayImage();
LoadSettings();
}
public static JObject GetDataFromUrl(string url)
{
using (var client = new WebClient())
{
string jsonText = client.DownloadString(url);
JObject jsonObject = JObject.Parse(jsonText);
return jsonObject;
}
}
private void DisplayImage()
{
var appcfg = new ConfigParser(appConfig);
var r_key = appcfg.GetValue("RemoteLauncher", "key");
string jsonFilePath = r_key + "/api/mythicallauncher/settings/getconfig.php";
JObject data = GetDataFromUrl(jsonFilePath);
string imageUrl = (string)data["appLogo"];
using (WebClient webClient = new WebClient())
{
byte[] imageBytes = webClient.DownloadData(imageUrl);
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{
pictureBox1.Image = Image.FromStream(memoryStream);
}
}
using (WebClient webClient = new WebClient())
{
byte[] iconBytes = webClient.DownloadData(imageUrl);
using (MemoryStream ms = new MemoryStream(iconBytes))
{
Bitmap bitmap = (Bitmap)Image.FromStream(ms);
this.Icon = Icon.FromHandle(bitmap.GetHicon());
}
}
string bgimageURL = (string)data["appBg"];
using (WebClient backWebClient = new WebClient())
{
byte[] bgimageBytes = backWebClient.DownloadData(bgimageURL);
using (MemoryStream bgmemoryStream = new MemoryStream(bgimageBytes))
{
this.BackgroundImage = Image.FromStream(bgmemoryStream);
}
}
string appcolour = (string)data["appMainColour"];
btnplay.FillColor = ColorTranslator.FromHtml(appcolour);
btnplay.FillColor2 = ColorTranslator.FromHtml(appcolour);
}
void LoadSettings()
{
lbllaunchername.Text = FrmLoading.version + " | Home";
lblver.Text = "Version: " + FrmLoading.version;
}
private async void FrmMain_Shown(object sender, EventArgs e)
{
this.Refresh();
var defaultPath = new MinecraftPath(MinecraftPath.GetOSDefaultPath());
await initializeLauncher(defaultPath);
}
private async Task initializeLauncher(MinecraftPath path)
{
this.gamePath = path;
launcher = new CMLauncher(path);
launcher.FileChanged += Launcher_FileChanged;
launcher.ProgressChanged += Launcher_ProgressChanged;
await refreshVersions(null);
}
private void Launcher_FileChanged(DownloadFileChangedEventArgs e)
{
if (Thread.CurrentThread.ManagedThreadId != uiThreadId)
{
Debug.WriteLine(e);
}
Pb_Progress.Maximum = e.TotalFileCount;
Pb_Progress.Value = e.ProgressedFileCount;
Lv_Status.Text = $"{e.FileKind} : {e.FileName} ({e.ProgressedFileCount}/{e.TotalFileCount})";
}
private void Launcher_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (Thread.CurrentThread.ManagedThreadId != uiThreadId)
{
Debug.WriteLine(e);
}
Pb_Progress.Maximum = 100;
Pb_Progress.Value = e.ProgressPercentage;
}
private async void button1_Click(object sender, EventArgs e)
{
if (session == null)
{
Alert("Oops, we could not verify your session.", FrmAlert.enmType.Warning);
return;
}
if (cbVersion.Text == "")
{
Alert("You need to select a version first.", FrmAlert.enmType.Warning);
return;
}
var cfg = new ConfigParser(appConfig);
var Xms = cfg.GetValue("RAM", "Xms");
var Xmx = cfg.GetValue("RAM", "Xmx");
if (Xms == "" || Xmx == "")
{
Alert("Please set your Desired Ram Settings!", FrmAlert.enmType.Info);
return;
}
try
{
var launchOption = new MLaunchOption()
{
MaximumRamMb = int.Parse(TxtXmx.Text),
Session = this.session,
GameLauncherName = FrmLoading.appname,
};
if (FrmLoading.enable_auto_joiner == "true")
{
launchOption.ServerIp = FrmLoading.auto_joiner_ip;
}
if (!useMJava)
launchOption.JavaPath = javaPath;
if (!string.IsNullOrEmpty(txtXms.Text))
launchOption.MinimumRamMb = int.Parse(txtXms.Text);
if (rbParallelDownload.Checked)
{
System.Net.ServicePointManager.DefaultConnectionLimit = 256;
launcher.FileDownloader = new AsyncParallelDownloader();
}
else
launcher.FileDownloader = new SequenceDownloader();
launcher.GameFileCheckers.AssetFileChecker.CheckHash = cbSkipHashCheck.Checked;
launcher.GameFileCheckers.ClientFileChecker.CheckHash = cbSkipHashCheck.Checked;
launcher.GameFileCheckers.LibraryFileChecker.CheckHash = cbSkipHashCheck.Checked;
if (cbSkipAssets.Checked)
launcher.GameFileCheckers.AssetFileChecker = null;
var process = await launcher.CreateProcessAsync(cbVersion.Text, launchOption);
StartProcess(process);
}
catch (FormatException fex)
{
MessageBox.Show("Failed to create Launch Options\n\n" + fex);
}
catch (MDownloadFileException mex)
{
MessageBox.Show(
$"FileName : {mex.ExceptionFile.Name}\n" +
$"FilePath : {mex.ExceptionFile.Path}\n" +
$"FileUrl : {mex.ExceptionFile.Url}\n" +
$"FileType : {mex.ExceptionFile.Type}\n\n" +
mex.ToString());
}
catch (Win32Exception wex)
{
Alert(wex + "Oops, we found a problem in your Java.", FrmAlert.enmType.Error);
}
catch (Exception ex)
{
Alert(ex + "", FrmAlert.enmType.Error);
}
finally
{
this.WindowState = FormWindowState.Minimized;
}
}
private void StartProcess(Process process)
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
}
private void btnSetLastVersion_Click(object sender, EventArgs e)
{
cbVersion.Text = launcher.Versions.LatestReleaseVersion?.Name;
}
private async Task refreshVersions(string showVersion)
{
cbVersion.Items.Clear();
var versions = await launcher.GetAllVersionsAsync();
bool showVersionExist = false;
foreach (var item in versions)
{
if (showVersion != null && item.Name == showVersion)
showVersionExist = true;
cbVersion.Items.Add(item.Name);
}
if (showVersion == null || !showVersionExist)
btnSetLastVersion_Click(null, null);
else
cbVersion.Text = showVersion;
}
private void label3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void txtXms_TextChanged(object sender, EventArgs e)
{
var cfg = new ConfigParser(appConfig);
cfg.SetValue("RAM", "Xms", txtXms.Text);
cfg.Save();
}
private void TxtXmx_TextChanged(object sender, EventArgs e)
{
var cfg = new ConfigParser(appConfig);
cfg.SetValue("RAM", "Xmx", TxtXmx.Text);
cfg.Save();
}
private void bunifuButton25_Click(object sender, EventArgs e)
{
Misc.SetPage(SettingsPage);
}
private void bunifuButton26_Click(object sender, EventArgs e)
{
Misc.SetPage(HomePage);
}
private async void pictureBox5_Click(object sender, EventArgs e)
{
await refreshVersions(null);
}
private async void label4_Click(object sender, EventArgs e)
{
await refreshVersions(null);
}
private void label10_Click(object sender, EventArgs e)
{
FrmChangeLog x = new FrmChangeLog();
x.Show();
this.Hide();
}
}
}