-
Notifications
You must be signed in to change notification settings - Fork 6
/
MainWindow.xaml.cs
199 lines (171 loc) · 6.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
using System;
using System.Collections.Generic;
using System.Linq;
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.Shapes;
using System.Net;
using System.Threading;
using System.ComponentModel;
using KailleraNET.Util;
using log4net;
using KailleraNET.Views;
using System.Windows.Interop;
using KailleraNET.Hotkeys;
namespace KailleraNET
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
IPAddress ip;
int port;
bool selectedServerFromList;
private static HotKeyHost hotkeyHost;
ServerListView serverList;
KailleraWindowController k = KailleraWindowController.getMgr();
SettingsManager settings = SettingsManager.getMgr();
public static HotKeyHost getHotkey()
{
return hotkeyHost;
}
public MainWindow()
{
InitializeComponent();
addServersToComboBox();
addUsernamesToComboBox();
Loaded += assignHotkeys;
Closed += new EventHandler(MainWindow_Closed);
}
private void assignHotkeys(object sender, RoutedEventArgs e)
{
hotkeyHost = new HotKeyHost((HwndSource)HwndSource.FromVisual(this));
hotkeyHost.AddHotKey(new ShowUserListHotkey("ShowUserListHotkey", Key.U, ModifierKeys.Control | ModifierKeys.Shift, true));
hotkeyHost.AddHotKey(new ShowServerChatHotkey("ShowServerChatHotkey", Key.S, ModifierKeys.Shift | ModifierKeys.Control, true));
hotkeyHost.AddHotKey(new ShowGameListHotkey("ShowGameListHotkey", Key.G, ModifierKeys.Shift | ModifierKeys.Control, true));
hotkeyHost.AddHotKey(new BeginDisconnectHotkey("BeginDisconnectHotkey", Key.D, ModifierKeys.Control | ModifierKeys.Shift, true));
hotkeyHost.AddHotKey(new MinimizeAllWindowsHotkey("MinimizeAllWindowsHotkey", Key.M, ModifierKeys.Shift | ModifierKeys.Control, true));
}
void MainWindow_Closed(object sender, EventArgs e)
{
Application.Current.Shutdown(0);
}
/// <summary>
/// Populates the username combobox with usernames
/// </summary>
private void addUsernamesToComboBox()
{
foreach (String name in settings.getUsernames())
{
usernameBox.Items.Add(name);
}
if(!usernameBox.Items.IsEmpty)
usernameBox.Text = usernameBox.Items[0].ToString();
}
/// <summary>
/// Populates the server combobox with server names
/// </summary>
private void addServersToComboBox()
{
foreach (KeyValuePair<string, string> entry in settings.getServers())
{
serverBox.Items.Add(entry.Key + "," + entry.Value);
}
serverBox.Text = serverBox.Items[0].ToString();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string username = usernameBox.Text;
if (username.Equals(""))
{
MessageBox.Show("Error - You must enter a username.", "Please enter a username.");
return;
}
try
{
parseIPString(ref ip, ref port, serverBox.Text);
if (ip == null)
{
throw new ArgumentException();
}
}
catch (Exception)
{
MessageBox.Show("Error - cannot read ip address. Format is <name>,<ip>:<port>.", "IP Error");
return;
}
settings.addUsername(username);
/*
ChatWindow wind = new ChatWindow();
wind.Closed += connectionClosed;
wind.Show();
wind.Start(ip, port, username);
*/
this.Visibility = Visibility.Hidden;
k.BeginNewConnection(ip, port, username);
k.connectionFailed += () => Application.Current.Dispatcher.BeginInvoke(new Action(() => Show()));
k.connectionFailed += () => Application.Current.Dispatcher.BeginInvoke(new Action(() => k.closeAllWindows()));
this.Hide();
}
/// <summary>
/// Sets the correct ip and port - iterates first through saved servers and then tries
/// to parse text directly
/// </summary>
/// <param name="ip">ip to set</param>
/// <param name="port">port to set</param>
/// <param name="text">text to parse</param>
private void parseIPString(ref IPAddress ip, ref int port, string text)
{
bool shouldSplit = false;
string searchText = text;
if (text.Contains(','))
{
searchText = text.Split(',')[0];
shouldSplit = true;
}
foreach (var entry in settings.getServers())
{
if (searchText.Equals(entry.Key))
{
string[] ipPort = entry.Value.Split(':');
IPAddress.TryParse(ipPort[0], out ip);
int.TryParse(ipPort[1], out port);
return;
}
}
if (shouldSplit) searchText = text.Split(',')[1];
//Directly entered ip:port
string[] args = searchText.Split(':');
IPAddress.TryParse(args[0], out ip);
int.TryParse(args[1], out port);
if(!selectedServerFromList)
settings.addServer("Server", searchText);
selectedServerFromList = false;
}
private void connectionClosed(object sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
}
private void serverListButton_Click(object sender, RoutedEventArgs e)
{
serverList = new ServerListView();
serverList.chooseServer += addAndSelectServer;
serverList.begin();
}
public void addAndSelectServer(Server curr)
{
selectedServerFromList = true;
settings.addServer(curr.name, curr.ip.ToString() + ":" + curr.port.ToString());
serverBox.Text = curr.name + "," + curr.ip.ToString() + ":" + curr.port.ToString();
serverList = null;
}
}
}