-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainLauncherForm.cs
225 lines (197 loc) · 7.11 KB
/
MainLauncherForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using CmlLib.Core.Auth.Microsoft;
using WolfLauncher.core;
using WolfLauncher.gui.launcher;
using WolfLauncher.model;
using MojangAPI.Model;
/**
* Author: HypherionSA
* Main Launcher Interface Window
**/
namespace WolfLauncher
{
public partial class MainLauncherForm : Form
{
// Static References
private Launcher launcher = Launcher.INSTANCE;
public static MainLauncherForm INSTANCE;
public MainLauncherForm()
{
InitializeComponent();
INSTANCE = this;
this.Text = String.Format("{0} [{1}] - {2}", LauncherConstants.LauncherName, LauncherConstants.LauncherBuild, LauncherConstants.Version);
}
private void Form1_LoadAsync(object sender, EventArgs e)
{
SplashScreen splash = new SplashScreen();
splash.ShowDialog(this);
}
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
// Instance was double clicked, so we open the console window
if (listView1.SelectedItems.Count > 0)
{
InstanceWindow instanceWindow = new InstanceWindow((Instance)listView1.SelectedItems[0].Tag);
instanceWindow.ShowDialog(this);
}
}
/**
* Load instaces from drive
**/
public void loadInstances()
{
// Load from disk
List<Instance> instances = launcher.loadInstances();
listView1.Items.Clear();
foreach (var i in instances)
{
// Set up listview item
ListViewItem itm = new ListViewItem(i.name);
itm.Tag = i;
// TODO Custom Icons
itm.ImageKey = "mclogo.png";
listView1.Items.Add(itm);
}
}
private void instanceMenu_Opening(object sender, CancelEventArgs e)
{
// If no instance was selected, we don't allow the menu to open
if (listView1.SelectedItems.Count == 0)
{
e.Cancel = true;
return;
}
// Check if play and kill menu items can be enabled for the instance
killToolStripMenuItem.Enabled = !(launcher.getRunning() == null || launcher.getRunning() != (Instance)listView1.SelectedItems[0].Tag);
playToolStripMenuItem.Enabled = launcher.getRunning() == null;
}
/**
* Delete Instance
**/
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
// No instance was selected, so we return
if (listView1.SelectedItems.Count == 0)
return;
// Show warning to user
var res = MessageBox.Show("Are you sure? This action cannot be reversed", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// User accepted, so we nuke it
if (res == DialogResult.Yes)
Launcher.INSTANCE.deleteInstance((Instance)listView1.SelectedItems[0].Tag);
}
/**
* Open Launcher Settings
**/
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
// No instance was selected, so we return
if (listView1.SelectedItems.Count == 0)
return;
// Display settings dialog.
// TODO: Move this into console window
InstanceSettings settings = new InstanceSettings((Instance)listView1.SelectedItems[0].Tag);
settings.ShowDialog(this);
}
private void MainLauncherForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Kill running instance if any
Launcher.INSTANCE.killInstance();
}
/**
* Kill running instance
**/
private void killToolStripMenuItem_Click(object sender, EventArgs e)
{
// No instance was selected, so we return
if (listView1.SelectedItems.Count == 0)
return;
// Micheal Meyers it! It must die
launcher.killInstance();
}
/**
* Launch Instance
**/
public void launchInstance(Instance ins = null)
{
// Launched from Console Window
if (ins != null)
{
launcher.launch(this, ins);
return;
}
// Launched from context menu
if (listView1.SelectedItems.Count > 0)
{
launcher.launch(this, (model.Instance)listView1.SelectedItems[0].Tag);
}
}
/**
* Start Instance
**/
private void playToolStripMenuItem_Click(object sender, EventArgs e)
{
launchInstance();
}
/**
* Add new instance
**/
private void addInstanceButton_Click(object sender, EventArgs e)
{
// Show instance create dialog
CreateVanillaInstance vanillaInstance = new CreateVanillaInstance();
vanillaInstance.ShowDialog(this);
}
/**
* Logout from signed in account
**/
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
// Signout and refresh profile dropdown
launcher.accountHandler().Signout();
loadProfile();
}
/**
* Load user profile information from the Mojang API
**/
public async void loadProfile()
{
// Check if any accounts are stored
if (launcher.accountHandler().AccountManager.GetAccounts().Count > 0)
{
// Get first stored account
var acc = launcher.accountHandler().AccountManager.GetAccounts().First();
// Check authentication
var s = launcher.accountHandler().Authenticate(acc);
// Retrieve MC profile
PlayerProfile profile = await launcher.getMojangApi().GetProfileUsingAccessToken(s.Result.AccessToken);
// Set Dropdown text to profile name
userDropdown.Text = profile.Name;
// Set Profile Image
userDropdown.Image = launcher.loadProfileImage(profile);
}
else
{
// No accounts are stored
userDropdown.Text = "No Account";
}
}
private void userDropdown_DropDownOpening(object sender, EventArgs e)
{
// No account is loaded or stored, so start login process
if (userDropdown.Text == "No Account")
{
// Cancel dropdown
userDropdown.AllowDrop = false;
// Authenticate
var loginHandler = JELoginHandlerBuilder.BuildDefault();
loginHandler.Authenticate().Wait();
// Refresh Dropdown
loadProfile();
}
}
}
}