-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
290 lines (277 loc) · 10.9 KB
/
FormMain.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
using DpLib.Helpers;
using DpLib.Models;
using DpLib.Winform;
using DpLib.Winform.Controls;
using EzPayloadSender.Helpers;
using EzPayloadSender.Models;
using System.Diagnostics;
namespace EzPayloadSender
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
#region EVENTS
#region BUTTONS
void ButtonBrowse_Click(object sender, EventArgs e)
{
BrowsePayload();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
networkTools.Cancel();
SetSendCancelButtonsVisibility(true);
}
async void ButtonSend_Click(object sender, EventArgs e)
{
await Task.Run(() => Send());
}
#endregion
#region FORM
void FormMain_Load(object sender, EventArgs e)
{
Init();
}
void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
OnClose();
}
#endregion
private void PictureBoxGitHub_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo psi = new()
{
FileName = "https://github.com/DjPopol/EzPayloadSender",
UseShellExecute = true
};
Process.Start(psi);
}
catch (Exception ex)
{
DpMessageBox.ShowDialog("Error opening page: " + ex.Message, "Error");
}
}
private void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowUpdate();
}
#region TEXTBOX
#region IP ADDRESS
void TextBoxIpAddress_KeyPress(object sender, KeyPressEventArgs e)
{
// Only accept digits, the period, and the backspace key
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
// Limit the total length to 15 characters (maximum for an IPv4 address)
if (((TextBox)sender).Text.Length >= 15 && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
}
void TextBoxIpAddress_TextChanged(object sender, EventArgs e)
{
if (sender is not TextBox) return;
RefreshForm();
}
#endregion
#region PORT
void TextBoxPort_KeyPress(object sender, KeyPressEventArgs e)
{
// Only accept digits and the backspace key
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
// Limit the total length to 5 characters (maximum for a port)
if (((TextBox)sender).Text.Length >= 5 && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
}
void TextBoxPort_TextChanged(object sender, EventArgs e)
{
if (sender is not TextBox) return;
RefreshForm();
}
#endregion
#endregion
#endregion
#region FUNCTIONS
private void AdjustLayout()
{
int menuStripHeight = menuStrip1.Height;
int clientHeight = 0;
if (menuStrip1.Visible)
{
groupBoxConnection.Location = new Point(groupBoxConnection.Location.X, menuStrip1.Location.Y + menuStripHeight + 5);
clientHeight += menuStripHeight + groupBoxConnection.Height + 10;
}
else
{
groupBoxConnection.Location = new Point(groupBoxConnection.Location.X, groupBoxConnection.Location.Y - menuStripHeight);
clientHeight += groupBoxConnection.Height + 5;
}
groupBoxBrowse.Location = new Point(groupBoxBrowse.Location.X, groupBoxConnection.Location.Y + groupBoxConnection.Height + 5);
groupBoxSend.Location = new Point(groupBoxSend.Location.X, groupBoxBrowse.Location.Y + groupBoxBrowse.Height + 5);
pictureBoxGitHub.Location = new Point(pictureBoxGitHub.Location.X, groupBoxSend.Location.Y + groupBoxSend.Height + 5);
labelDjPopol.Location = new Point(labelDjPopol.Location.X, groupBoxSend.Location.Y + groupBoxSend.Height + 5);
clientHeight += groupBoxBrowse.Height + groupBoxSend.Height + pictureBoxGitHub.Height + 15;
ClientSize = new Size(ClientSize.Width, clientHeight);
Refresh();
}
void BrowsePayload()
{
OpenFileDialog opendialog = new()
{
CheckFileExists = true,
Multiselect = false,
Title = $"Select Payload file",
Filter = $"Payload BIN File (*.bin)|*.bin",
InitialDirectory = Tools.MyConfig.PayloadPath,
};
if (opendialog.ShowDialog() == DialogResult.OK)
{
labelPayload.Text = opendialog.SafeFileName;
Tools.MyConfig.PayloadPathFilename = opendialog.FileName;
}
RefreshForm();
}
async void Init()
{
UpdateManager updateManager = new("https://api.github.com/repos/DjPopol/EzPayloadSender/releases", Tools.GetToken());
Tools.MyConfig.Load();
Text = Tools.GetTitle();
textBoxIpAddress.Text = Tools.MyConfig.IpAdress;
textBoxPort.Text = $"{Tools.MyConfig.Port}";
labelPayload.Text = Tools.MyConfig.PayloadFilename;
SetSendCancelButtonsVisibility(true);
RefreshForm();
Version currentVersion = Tools.GetVersion();
if (await Tools.IsConnectedToInternetAsync())
{
latestInfos = await updateManager.GetLastReleaseInfosAsync();
}
else
{
latestInfos = new();
}
bool IsUptoDate = !await Tools.IsConnectedToInternetAsync() || latestInfos.Version == new Version() || latestInfos.Version <= currentVersion;
menuStrip1.Visible = !IsUptoDate;
AdjustLayout();
Console.WriteLine($"DownloadUrl : {latestInfos.DownloadURL}\nVersion : {latestInfos.Version}");
if (Tools.MyConfig.CheckUpdateOnStartUp && !IsUptoDate)
{
ShowUpdate();
}
}
void OnClose()
{
if (NetworkTools.IsValidIpAddress(textBoxIpAddress.Text))
{
Tools.MyConfig.IpAdress = textBoxIpAddress.Text;
}
Tools.MyConfig.Port = int.Parse(textBoxPort.Text);
Tools.MyConfig.Save();
}
void RefreshForm()
{
bool isIpAdressOk = NetworkTools.IsValidIpAddress(textBoxIpAddress.Text);
bool isPortOk = NetworkTools.IsValidPort(textBoxPort.Text);
bool isPayloadOk = Tools.MyConfig.PayloadPathFilename != string.Empty && File.Exists(Tools.MyConfig.PayloadPathFilename);
buttonBrowse.Enabled = isIpAdressOk && isPortOk;
buttonSend.Enabled = isIpAdressOk && isPortOk && isPayloadOk;
}
void Send()
{
Invoke(new Action(async () =>
{
buttonCancel.Enabled = true;
SetSendCancelButtonsVisibility(false);
try
{
string result = await networkTools.Connect2PS4Async(textBoxIpAddress.Text, textBoxPort.Text);
if (result != string.Empty)
{
buttonCancel.Enabled = false;
DpMessageBox.ShowDialog("Error while connecting.\n" + result, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
catch (Exception ex)
{
buttonCancel.Enabled = false;
DpMessageBox.ShowDialog("Error while connecting.\n" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
await networkTools.SendPayloadAsync(Tools.MyConfig.PayloadPathFilename);
}
catch (Exception ex)
{
buttonCancel.Enabled = false;
DpMessageBox.ShowDialog($"Error while sending {labelPayload.Text}!\n" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
buttonCancel.Enabled = false;
networkTools.DisconnectPayload();
DpMessageBox.ShowDialog($"{Tools.MyConfig.PayloadFilename} sent successful!", "Sent Payload", MessageBoxButtons.OK);
}
catch (Exception ex)
{
buttonCancel.Enabled = false;
DpMessageBox.ShowDialog("Error while disconnecting!\n" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
SetSendCancelButtonsVisibility(true);
RefreshForm();
}));
}
public void SetSendCancelButtonsVisibility(bool sendVisible)
{
Invoke(new Action(() =>
{
buttonCancel.Visible = !sendVisible;
buttonSend.Visible = sendVisible;
}));
}
async void ShowUpdate()
{
if (await Tools.IsConnectedToInternetAsync())
{
DpMessageBoxResult result = DpMessageBox.ShowDialog($"{latestInfos.Name} is avaible.\nWould you like to update ?", "New Update avaible", "Show at startup", Tools.MyConfig.CheckUpdateOnStartUp, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
Tools.MyConfig.CheckUpdateOnStartUp = result.IsChecked;
Tools.MyConfig.Save();
if (DialogResult == DialogResult.Yes)
{
// Update
DpFormUpdate formUpdate = new(latestInfos, Tools.MyConfig.ShowConsole);
formUpdate.FormClosing += new FormClosingEventHandler((object? sender, FormClosingEventArgs e) =>
{
Enabled = true;
Close();
});
Enabled = false;
await Task.Delay(100);
formUpdate.Show();
Hide();
}
}
else
{
DpMessageBox.ShowDialog("Internet connexion required !\nYou must connect to internet and restart application", "Update", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
#region PROPERTIES
readonly NetworkTools networkTools = new();
ReleaseInfos latestInfos = new();
#endregion
}
}