forked from psenough/NPlusPlusAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
315 lines (276 loc) · 12.8 KB
/
Form1.cs
File metadata and controls
315 lines (276 loc) · 12.8 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Globalization;
namespace N__Assistant
{
public partial class Form1 : Form
{
//TODO: more robust autodetect (in case people are using non default installation directories)
string steamPath = @"C:\Program Files (x86)\Steam\steamapps\common\N++";
string profilePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + @"\Documents\Metanet\N++";
string screenshotsPath = @"C:\Program Files (x86)\Steam\userdata\64929984\760\remote\230270\screenshots";
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\N++Assistant";
BackgroundWorker bgwBackupNow = new BackgroundWorker();
public Form1()
{
InitializeComponent();
// rename linked labels
steamInstallDir.Text = steamPath;
profileDir.Text = profilePath;
screenshotsDir.Text = screenshotsPath;
backupsDir.Text = savePath;
// create backup directory if it doesnt exist
if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
if (!Directory.Exists(savePath + @"\Profiles")) Directory.CreateDirectory(savePath + @"\Profiles");
if (!Directory.Exists(savePath + @"\Sounds")) Directory.CreateDirectory(savePath + @"\Sounds");
if (!Directory.Exists(savePath + @"\Replays")) Directory.CreateDirectory(savePath + @"\Replays");
if (!Directory.Exists(savePath + @"\Levels")) Directory.CreateDirectory(savePath + @"\Levels");
if (!Directory.Exists(savePath + @"\EditorLevels")) Directory.CreateDirectory(savePath + @"\EditorLevels");
if (!Directory.Exists(savePath + @"\Palettes")) Directory.CreateDirectory(savePath + @"\Palettes");
if (!Directory.Exists(savePath + @"\GameLevels")) Directory.CreateDirectory(savePath + @"\GameLevels");
// create palettes directory in game if it doesnt exist (it's needed to place custom palettes)
if (!Directory.Exists(steamPath + @"\NPP\Palettes")) Directory.CreateDirectory(steamPath + @"\NPP\Palettes");
// mark backup now checklist items as checked by default
//TODO: save user settings and reload the same on launch
// default should probably be just profile and editor levels, not everything
/*for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}*/
// set checkbox on profile backup default on
checkedListBox1.SetItemChecked(0, true);
// set checkbox on level editor backup default on
checkedListBox1.SetItemChecked(2, true);
// initialize background worker for backup now button
bgwBackupNow.DoWork += new DoWorkEventHandler(bgwBackupNow_DoWork);
bgwBackupNow.ProgressChanged += new ProgressChangedEventHandler(bgwBackupNow_ProgressChanged);
bgwBackupNow.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwBackupNow_RunWorkerCompleted);
bgwBackupNow.WorkerReportsProgress = true;
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
/*Process[] processCollection = Process.GetProcesses();
foreach (Process p in processCollection)
{
Console.WriteLine(p.ProcessName); // N++
}*/
loadProfile.Enabled = false;
deleteProfile.Enabled = false;
}
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
TabPage current = (sender as TabControl).SelectedTab;
// switch to profile tab
if (current == tabPage2)
{
profileList.Items.Clear();
PopulateListBox(profileList, savePath + @"\Profiles", "*.zip");
loadProfile.Enabled = false;
deleteProfile.Enabled = false;
}
// switch to soundpacks tab
if (current == tabPage3)
{
// spreadsheet of new sound packs
// https://docs.google.com/spreadsheets/d/18PshamVuDNyH396a7U3YDFQmCw18s4gIVZ_WrFODRd4/edit#gid=0
// latest google sheets api example code
// https://github.com/popcron/sheets
}
// switch to palettes tab
if (current == tabPage4)
{
// all official palettes
// https://cdn.discordapp.com/attachments/197793786389200896/592821804746276864/Palettes.zip
// spreadsheet of new palettes
// https://docs.google.com/spreadsheets/d/1I2f87Qhfs6rxzZq5dQRDbLKYyaGLqTdCkLqfNfrw1Mk/edit#gid=0
// allpalettes.zip (supposedly contains all community palettes, updated whenever)
// https://drive.google.com/file/d/1Ly3g_4VKcMsTLwXJpY3jPk-IlAGZ5RJG/view?usp=sharing
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (Directory.Exists(steamPath))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = steamPath,
FileName = "explorer.exe"
};
Process.Start(startInfo);
}
else
{
MessageBox.Show(string.Format("{0} Directory does not exist!", steamPath));
}
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (Directory.Exists(profilePath))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = profilePath,
FileName = "explorer.exe"
};
Process.Start(startInfo);
}
else
{
MessageBox.Show(string.Format("{0} Directory does not exist!", profilePath));
}
}
private void button2_Click(object sender, EventArgs e)
{
if (DetectNPPRunning() == true)
{
MessageBox.Show("Please close N++ before doing backup");
return;
}
backupNow.Enabled = false;
progressBar1.Show();
bgwBackupNow.RunWorkerAsync();
}
void bgwBackupNow_DoWork(object sender, DoWorkEventArgs e)
{
bgwBackupNow.ReportProgress(0, "Zipping nprofile");
if (checkedListBox1.GetItemCheckState(0) == CheckState.Checked)
{
// backup profile
using (FileStream fs = new FileStream(savePath + @"\Profiles\nprofile" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip", FileMode.Create))
using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create))
{
arch.CreateEntryFromFile(profilePath + @"\nprofile", "nprofile");
}
}
bgwBackupNow.ReportProgress(15,"Zipping Soundpack");
if (checkedListBox1.GetItemCheckState(1) == CheckState.Checked)
{
// backup soundpack
ZipFile.CreateFromDirectory(steamPath + @"\NPP\Sounds", savePath + @"\Sounds\Sounds" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
}
bgwBackupNow.ReportProgress(30, "Zipping Editor Levels");
if (checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{
// backup editor levels
ZipFile.CreateFromDirectory(profilePath + @"\levels", savePath + @"\EditorLevels\EditorLevels" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
}
bgwBackupNow.ReportProgress(40, "Zipping Replays (attract files)");
if (checkedListBox1.GetItemCheckState(3) == CheckState.Checked)
{
// backup replays (attract files)
ZipFile.CreateFromDirectory(profilePath + @"\attract", savePath + @"\Replays\Replays" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
}
bgwBackupNow.ReportProgress(60, "Zipping Palettes");
if (checkedListBox1.GetItemCheckState(4) == CheckState.Checked)
{
// backup palettes
ZipFile.CreateFromDirectory(steamPath + @"\NPP\Palettes", savePath + @"\Palettes\Palettes" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
}
bgwBackupNow.ReportProgress(80, "Zipping Game Levels");
if (checkedListBox1.GetItemCheckState(5) == CheckState.Checked)
{
// backup gamelevels
ZipFile.CreateFromDirectory(steamPath + @"\NPP\Levels", savePath + @"\GameLevels\GameLevels" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
}
bgwBackupNow.ReportProgress(100, "Done with backup!");
}
void bgwBackupNow_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressLabel.Text = String.Format("{0}", e.UserState);
//label2.Text = String.Format("Total items transfered: {0}", e.UserState);
}
void bgwBackupNow_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressLabel.Text = "Done with backup!";
progressBar1.Hide();
backupNow.Enabled = true;
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (Directory.Exists(savePath))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = savePath,
FileName = "explorer.exe"
};
Process.Start(startInfo);
}
else
{
MessageBox.Show(string.Format("{0} Directory does not exist!", savePath));
}
}
private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (Directory.Exists(screenshotsPath))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = screenshotsPath,
FileName = "explorer.exe"
};
Process.Start(startInfo);
}
else
{
MessageBox.Show(string.Format("{0} Directory does not exist!", screenshotsPath));
}
}
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name + " (" + file.Length/1024 + "Kb)");
}
}
private bool DetectNPPRunning()
{
Process[] pname = Process.GetProcessesByName("N++");
if (pname.Length == 0)
return false;
else
return true;
}
private void backupProfile_Click(object sender, EventArgs e)
{
profileBackupLabel.Text = "Profile backup started!";
// backup profile
using (FileStream fs = new FileStream(savePath + @"\Profiles\nprofile" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip", FileMode.Create))
using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create))
{
arch.CreateEntryFromFile(profilePath + @"\nprofile", "nprofile");
}
profileBackupLabel.Text = "Profile backup completed!";
profileList.Items.Clear();
PopulateListBox(profileList, savePath + @"\Profiles", "*.zip");
}
private void profileList_SelectedIndexChanged(object sender, EventArgs e)
{
loadProfile.Enabled = true;
deleteProfile.Enabled = true;
}
private void deleteProfile_Click(object sender, EventArgs e)
{
try {
File.Delete(savePath + @"\Profiles\" + profileList.SelectedItem.ToString().Split(' ')[0]);
profileList.Items.Remove(profileList.SelectedItem);
loadProfile.Enabled = false;
deleteProfile.Enabled = false;
} catch (Exception exc) {
MessageBox.Show(exc.Message);
}
}
}
}