-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inicio.xaml.cs
176 lines (158 loc) · 8.47 KB
/
Inicio.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
#pragma warning disable CS0649
using System;
using System.Windows;
using CmlLib.Core;
using CmlLib.Core.Auth;
using CmlLib.Core.Auth.Microsoft.UI.WinForm;
using Downloader;
using System.IO;
using System.Net.Http;
using static OCM_Installer_V2.Util;
namespace OCM_Installer_V2
{
public partial class Inicio
{
readonly HttpClient httpClient = new();
readonly bool packUpdate = false;
readonly string installPath = Globals.CustomLocation;
readonly string packVersionFile = Globals.CustomLocation + @"\Otako Craft Mods\PackVersion.txt";
readonly string usernameFile = Globals.AppDirectory + @"\Username.txt";
public Inicio()
{
InitializeComponent();
try
{
string latestPackVersion = httpClient.GetStringAsync("https://otcr.tk/packversion.txt").Result;
if (!File.Exists(packVersionFile))
{
Directory.CreateDirectory(Globals.CustomLocation + @"\Otako Craft Mods");
var cf = File.Create(packVersionFile);
cf.Close();
}
string localPackVersion = File.ReadAllText(packVersionFile);
if (File.Exists(Globals.AppDirectory + @"\CustomLocation.txt") && string.IsNullOrEmpty(localPackVersion)) File.WriteAllText(packVersionFile, latestPackVersion);
if (!string.IsNullOrEmpty(localPackVersion) && !localPackVersion.Equals(latestPackVersion)) packUpdate = true;
if (Directory.Exists(installPath + @"\Otako Craft Mods\mods") && !packUpdate && Directory.GetFiles(installPath + @"\Otako Craft Mods\mods").Length > 140) PlayButton.IsEnabled = true; else PlayButton.Content = "Instala todo primero";
if (packUpdate) { PlayButton.Content = "Actualización disponible"; InstallAll.Content = "Actualizar y jugar"; }
}
catch (Exception err)
{
new Reporter().ReportError(err.ToString());
return;
}
}
private void Downloader_DownloadProgressChanged(object? sender, Downloader.DownloadProgressChangedEventArgs e)
{
string prog = e.ProgressPercentage.ToString("0.0");
Application.Current.Dispatcher.Invoke(new Action(() =>
{
ProgresoInstalaciones.Value = e.ProgressPercentage;
InstallAll.Content = prog + "%";
}));
}
private void Downloader_DownloadFileCompleted(object? sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Application.Current.Dispatcher.Invoke(new Action(() => { ProgresoInstalaciones.Value = 0; InstallAll.Content = "Cargando..."; }));
}
private void Launcher_FileChanged(CmlLib.Core.Downloader.DownloadFileChangedEventArgs e)
{
Application.Current.Dispatcher.Invoke(new Action(() => { DownloadedFiles.Content = e.ProgressedFileCount + "/" + e.TotalFileCount; }));
}
private void Launcher_ProgressChanged(object? sender, System.ComponentModel.ProgressChangedEventArgs e)
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
DownloadedFilePercentage.Content = e.ProgressPercentage + "%";
ProgresoInstalacionesJugar.Value = e.ProgressPercentage;
}));
}
private void InstallAllButton_Click(object sender, RoutedEventArgs e)
{
PrepareToPlay(true);
}
private void PlayButton_Click(object sender, RoutedEventArgs e)
{
PrepareToPlay(false);
}
public void PrepareToPlay(bool doInstallation)
{
InstallAll.IsEnabled = false;
PlayButton.IsEnabled = false;
PlayButton.Content = doInstallation ? "Instalación en curso" : "Cargando juego...";
var computerMemory = GetMemoryMb();
if (computerMemory == null) { computerMemory = 4096; return; }
var max = computerMemory / 2;
if (max <= 4096) max = 4096; else if (max > 8192) max = 8192;
System.Net.ServicePointManager.DefaultConnectionLimit = 256;
var downloader = new DownloadService(new DownloadConfiguration(){ChunkCount = 8,ParallelDownload = true,ParallelCount = 8});
var path = new MinecraftPath(installPath + @"\Otako Craft Mods");
var launcher = new CMLauncher(installPath + @"\Otako Craft Mods");
downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
downloader.DownloadFileCompleted += Downloader_DownloadFileCompleted;
launcher.FileChanged += Launcher_FileChanged;
launcher.ProgressChanged += Launcher_ProgressChanged;
MicrosoftLoginForm loginWindow = new()
{
LoadingText = "Cargando...\nEspera un momento plis :D",
Text = "Iniciar sesión | Recuerda que tienes que tener Minecraft comprado para que esto sirva"
};
Application.Current.Dispatcher.Invoke(new Action(async () =>
{
try
{
if (Directory.Exists(installPath + @"\Otako Craft Mods\mods") && doInstallation)
{
Directory.Delete(installPath + @"\Otako Craft Mods\mods", true);
PlayButton.IsEnabled = false;
}
string customUsername = File.ReadAllText(usernameFile);
bool isUserPremium = false;
if (string.IsNullOrEmpty(customUsername) || IsLauncherPremiumOnly()) isUserPremium = true;
MSession account = isUserPremium ? await loginWindow.ShowLoginDialog() : MSession.GetOfflineSession(customUsername);
var premiumAccount = new MSession(account.Username, account.AccessToken, account.UUID);
loginWindow.Close();
if (doInstallation)
{
await downloader.DownloadFileTaskAsync("https://otcr.tk/pack.zip", installPath + @"\Otako Craft Mods\pack.zip");
System.IO.Compression.ZipFile.ExtractToDirectory(installPath + @"\Otako Craft Mods\pack.zip", installPath + @"\Otako Craft Mods", true);
DownloadedFiles.Visibility = Visibility.Visible;
DownloadedFilePercentage.Visibility = Visibility.Visible;
InstallAll.FontSize = 21.7;
InstallAll.Content = "Descargando y comprobando archivos";
File.Delete(installPath + @"\Otako Craft Mods\pack.zip");
}
WriteCustomModConfigs();
if (packUpdate)
{
string latestPackVersion = httpClient.GetStringAsync("https://otcr.tk/packversion.txt").Result;
File.WriteAllText(packVersionFile, latestPackVersion);
}
var process = await launcher.CreateProcessAsync("Otako Craft Mods", new MLaunchOption()
{
GameLauncherName = "Otako Craft Launcher",
Session = isUserPremium ? premiumAccount : MSession.GetOfflineSession(customUsername),
Path = path,
MaximumRamMb = (int)max,
VersionType = "Otako Craft",
StartVersion = new CmlLib.Core.Version.MVersion("Otako Craft Mods")
});
process.Start();
Environment.Exit(0);
}
catch (Exception err)
{
if (!err.Message.Contains("The user has denied access") && !err.Message.Contains("User cancelled login") && !err.Message.Contains("mojang_noprofile"))
{
new Reporter().ReportError(err.ToString());
}
if (err.Message.Contains("mojang_noprofile"))
{
ShowMessageBox("No tienes Minecraft comprado", "Has iniciado sesión con una cuenta que no tiene Minecraft comprado.\nVe al apartado Cuenta y ponte un nombre de usuario para poder jugar.\nSi el servidor está en modo de solo cuentas compradas, no tendrás opción de jugar sin Minecraft comprado.");
}
InstallAll.IsEnabled = true;
return;
}
}));
}
}
}