-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm1.cs
2244 lines (1974 loc) · 97.6 KB
/
Form1.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Win32;
using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
using Newtonsoft.Json;
using Popcron.Sheets;
using System.Globalization;
using System.Threading;
using System.Collections;
using System.Text;
namespace N__Assistant
{
public partial class Form1 : Form
{
// more robust autodetect added by YupdanielThatsMe#6492
private string steamGamePath;
//private string profilePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + @"\Documents\Metanet\N++";
private string profilePath = (string)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal", "null") + @"\Metanet\N++";
private string screenshotsPath = @"\userdata\64929984\760\remote\230270\screenshots";
//private string savePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\N++Assistant";
private string savePath = (string)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Local AppData", "null") + @"\N++Assistant";
static Thread MyLoadingThread = null;
static Thread MyReadingTextThread = null;
private BackgroundWorker bgwBackupNow = new BackgroundWorker();
// there is probably a better way to map this but fuck me if i know C# properly
private List<sheetMap> sheetMapList = new List<sheetMap>();
private string COMMUNITY_PALETTES = "1I2f87Qhfs6rxzZq5dQRDbLKYyaGLqTdCkLqfNfrw1Mk";
private string COMMUNITY_SOUNDPACKS = "18PshamVuDNyH396a7U3YDFQmCw18s4gIVZ_WrFODRd4";
//private string COMMUNITY_MAPPACKS = "1M9W3_jk3nULledALJNzRDRRpNhIofeTD2SF8ES6vCy8";
private string COMMUNITY_MAPPACKS = "18PshamVuDNyH396a7U3YDFQmCw18s4gIVZ_WrFODRd4";
public Form1()
{
try
{
InitializeComponent();
// get rid of "The request was aborted: Could not create SSL/TLS secure channel" error that happens on some versions of windows
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// get steam path
string steampath = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Valve\\Steam", "InstallPath", "null");
if (steampath == "null")
{
steampath = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Valve\\Steam", "InstallPath", "null");
}
if (steampath == "null")
{
throw new FileNotFoundException("Steam Not Found, WTF Bro is your pc good?");
}
if (!Directory.Exists(steampath + "\\steamapps\\"))
{
throw new FileNotFoundException("steamapps Not Found");
}
screenshotsPath = steampath + screenshotsPath;
string[] configLines = File.ReadAllLines(steampath + "\\steamapps\\libraryfolders.vdf");
List<string> possiblepaths = new List<string>() { steampath };
foreach (string configline in configLines)
{
if (configline.Contains("\t\t\"path\"\t\t\""))
{
possiblepaths.Add(configline.Replace("\t\t\"path\"\t\t\"", "").Replace("\\\\", "\\").Replace("\"", ""));
}
}
foreach (string possiblepath in possiblepaths)
{
if (Directory.Exists(possiblepath + "\\steamapps\\common\\N++"))
{
steamGamePath = possiblepath + "\\steamapps\\common\\N++";
break;
}
}
if (steamGamePath == "")
{
throw new FileNotFoundException("N++ not installed in steam");
}
// create backup directories if they dont 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 + @"\Maps")) Directory.CreateDirectory(savePath + @"\Maps");
if (!Directory.Exists(savePath + @"\Palettes")) Directory.CreateDirectory(savePath + @"\Palettes");
if (!Directory.Exists(savePath + @"\MapPacks")) Directory.CreateDirectory(savePath + @"\MapPacks");
if (!Directory.Exists(savePath + @"\NPPDLL")) Directory.CreateDirectory(savePath + @"\NPPDLL");
MyLoadingThread = new Thread(new ThreadStart(DownloadStuff));
MyLoadingThread.IsBackground = true;
MyLoadingThread.Start();
// create palettes directory in steam game dir if it doesnt exist (it's needed to install the custom palettes)
if (!Directory.Exists(steamGamePath + @"\NPP\Palettes")) Directory.CreateDirectory(steamGamePath + @"\NPP\Palettes");
// copy npp.dll into \NPPDLL if it doesn't exist
if (!File.Exists(savePath + @"\NPPDLL\npp.dll")) File.Copy(steamGamePath + @"\npp.dll", savePath + @"\NPPDLL\npp.dll");
MyReadingTextThread = new Thread(new ThreadStart(ReadNPPTextLogs));
MyReadingTextThread.IsBackground = true;
MyReadingTextThread.Start();
//TODO: save user settings and reload the same on launch
// 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);
updateHighDPIFixButton();
tabControlDebugTabs.Selecting += new TabControlCancelEventHandler(tabControlLogDebug_Selecting);
}
catch (Exception exc) {
MessageBox.Show("Coulnd't initialize because: " + exc.Message);
}
}
private void DownloadStuff()
{
try {
// download default Metanet Palettes.zip pack to backup dir
if (!Directory.Exists(savePath + @"\Palettes\Palettes"))
{
try
{
string myStringWebResource = "https://cdn.discordapp.com/attachments/197793786389200896/592821804746276864/Palettes.zip";
WebClient myWebClient = new WebClient();
string filename = savePath + @"\Palettes\Palettes.zip";
myWebClient.DownloadFile(myStringWebResource, filename);
myWebClient.Dispose();
ZipFile.ExtractToDirectory(filename, savePath + @"\Palettes");
File.Delete(savePath + @"\Palettes\Palettes.zip");
statusLabel.Text = "Done downloading Metanet allpalettes.zip";
}
catch (Exception exc)
{
MessageBox.Show("Couldn't download Metanet Palettes pack because: " + exc.Message);
}
}
// download NPP_AllLevels.zip to backup dir
if (!Directory.Exists(savePath + @"\Maps\NPP_AllLevels"))
{
try
{
string myStringWebResource = "https://cdn.discordapp.com/attachments/592913929630384138/890428300911050752/NPP_AllLevels.zip";
WebClient myWebClient = new WebClient();
string filename = savePath + @"\Maps\NPP_AllLevels.zip";
myWebClient.DownloadFile(myStringWebResource, filename);
myWebClient.Dispose();
Directory.CreateDirectory(savePath + @"\Maps\NPP_AllLevels");
ZipFile.ExtractToDirectory(filename, savePath + @"\Maps\NPP_AllLevels");
File.Delete(savePath + @"\Maps\NPP_AllLevels.zip");
statusLabel.Text = "Done downloading NPP_AllLevels.zip";
}
catch (Exception exc)
{
MessageBox.Show("Couldn't download NPP_AllLevels.zip because: " + exc.Message);
}
}
// list spreadsheet of new sound packs
// https://docs.google.com/spreadsheets/d/18PshamVuDNyH396a7U3YDFQmCw18s4gIVZ_WrFODRd4/edit#gid=0
if (spreadsheetSoundpacks.Items.Count == 0)
{
//spreadsheetSoundpacks.Items.Clear();
PopulateListBoxWithSpreadsheetData(spreadsheetSoundpacks, 0, COMMUNITY_SOUNDPACKS, new APIKey().key, "Sound Packs");
installSpreadsheetSoundpack.Enabled = false;
statusLabel.Text = "Getting community sound packs spreadsheet data ...";
}
// populate community palettes from spreadsheet link
// https://docs.google.com/spreadsheets/d/1I2f87Qhfs6rxzZq5dQRDbLKYyaGLqTdCkLqfNfrw1Mk/edit#gid=0
if (communityPalettesList.Items.Count == 0)
{
//communityPalettesList.Items.Clear();
PopulateListBoxWithSpreadsheetData(communityPalettesList, 0, COMMUNITY_PALETTES, new APIKey().key, "Palettes");
installCommunityPalette.Enabled = false;
statusLabel.Text = "Getting community palettes spreadsheet data ...";
}
// populate community map packs from spreadsheet link
// https://docs.google.com/spreadsheets/d/1M9W3_jk3nULledALJNzRDRRpNhIofeTD2SF8ES6vCy8/edit#gid=0
// https://docs.google.com/spreadsheets/d/18PshamVuDNyH396a7U3YDFQmCw18s4gIVZ_WrFODRd4/edit#gid=1470738075
if (communityMapPacksList.Items.Count == 0)
{
//communityMapPacksList.Items.Clear();
PopulateListBoxWithSpreadsheetData(communityMapPacksList, 2, COMMUNITY_MAPPACKS, new APIKey().key, "Map Packs");
installCommunityMapPack.Enabled = false;
patchLeaderboardsForMapPack.Enabled = false;
statusLabel.Text = "Getting community map packs spreadsheet data ...";
}
}
catch (Exception exc)
{
MessageBox.Show("Coulnd't download because: " + exc.Message);
}
}
private string Readnppconf()
{
string nppconfText_Text = "";
try
{
nppconfText_Text = File.ReadAllText(profilePath + @"\npp.conf");
}
catch (Exception)
{
try
{
nppconfText_Text = File.ReadAllText(steamGamePath + @"\NPP\npp.conf");
}
catch (Exception exc2)
{
MessageBox.Show("Couldn't read any npp.conf! \n" + exc2.Message);
}
}
return nppconfText_Text;
}
private string Readkeysvars()
{
string text = "";
try
{
text = File.ReadAllText(profilePath + @"\keys.vars");
}
catch (Exception)
{
try
{
text = File.ReadAllText(steamGamePath + @"\NPP\keys.vars");
}
catch (Exception exc2)
{
MessageBox.Show("Couldn't read any keys.vars! \n" + exc2.Message);
}
}
return text;
}
private string Readnpplog()
{
string npplogText_Text = "";
try
{
npplogText_Text = File.ReadAllText(profilePath + @"\NPPLog.txt");
}
catch (Exception exc)
{
MessageBox.Show("Couldn't read NPPLog.txt! \n" + exc.Message);
}
return npplogText_Text;
}
private void ReadNPPTextLogs()
{
string nppconfText_Text = Readnppconf();
string npplogText_Text = Readnpplog();
string keysvarsText_Text = Readkeysvars();
if (InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate {
nppconfText.Text = nppconfText_Text;
npplogText.Text = npplogText_Text;
keysvarsText.Text = keysvarsText_Text;
statusLabel.Text = "Done loading NPP config and log files";
}));
return;
}
}
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
TabPage current = (sender as TabControl).SelectedTab;
// switch to status / home tab
if (current == tabStatus)
{
}
// switch to profile tab
if (current == tabProfile)
{
profileList.Items.Clear();
PopulateListBoxWithFileType(profileList, savePath + @"\Profiles", "*.zip");
loadProfile.Enabled = false;
deleteProfile.Enabled = false;
}
// switch to soundpacks tab
if (current == tabSoundpacks)
{
// list backups
soundpackBackups.Items.Clear();
PopulateListBoxWithFileType(soundpackBackups, savePath + @"\Sounds", "*.zip");
installSoundpackButton.Enabled = false;
deleteSoundpackBackupButton.Enabled = false;
// list preview
previewSoundsList.Items.Clear();
PopulateListBoxWithFileType(previewSoundsList, steamGamePath + @"\NPP\Sounds", "*.wav");
}
// switch to palettes tab
if (current == tabPalettes)
{
// populate metanet palettes (useful only for auto-unlock and references when creating palettes)
// https://cdn.discordapp.com/attachments/197793786389200896/592821804746276864/Palettes.zip
metanetPalettesList.Items.Clear();
PopulateListBoxWithSubDirectories(metanetPalettesList, savePath + @"\Palettes\Palettes");
installMetanetPalette.Enabled = false;
// list all palettes in local backup dir
localBackupPalettesList.Items.Clear();
PopulateListBoxWithFileType(localBackupPalettesList, savePath + @"\Palettes", "*.zip");
installBackupPalette.Enabled = false;
deleteBackupPalette.Enabled = false;
// list installed palettes
palettesInstalledList.Items.Clear();
PopulateListBoxWithSubDirectories(palettesInstalledList, steamGamePath + @"\NPP\Palettes");
uninstallPalette.Enabled = false;
backupPalette.Enabled = false;
updateCustomPalleteInstalledCounter();
}
// switch to practice maps tab
if (current == tabMaps)
{
// list metanetMapsList
// https://cdn.discordapp.com/attachments/197765375503368192/580483404533989396/NPP_AllLevels.zip
string dir = savePath + @"\Maps\NPP_AllLevels";
metanetMapsList.Nodes.Clear();
LoadMapFiles(dir + @"\SI", "SI-", 5, 5, 0, metanetMapsList.Nodes.Add("Solo Intro"));
LoadMapFiles(dir + @"\S", "S-", 20, 6, 0, metanetMapsList.Nodes.Add("Solo N++"));
LoadMapFiles(dir + @"\S2", "SU-", 20, 6, 0, metanetMapsList.Nodes.Add("Solo Ultimate"));
LoadMapFiles(dir + @"\SL", "SL-", 20, 6, 0, metanetMapsList.Nodes.Add("Solo Legacy"));
LoadMapFiles(dir + @"\SL2", "SD-", 20, 6, 0, metanetMapsList.Nodes.Add("Solo Legacy Discarded"));
LoadMapFiles(dir + @"\SS", "?-", 4, 6, 0, metanetMapsList.Nodes.Add("Solo ?"));
LoadMapFiles(dir + @"\SS2", "!-", 4, 6, 0, metanetMapsList.Nodes.Add("Solo !"));
LoadMapFiles(dir + @"\CI", "CI-", 2, 5, 0, metanetMapsList.Nodes.Add("Co-op Intro"));
LoadMapFiles(dir + @"\C", "C-", 10, 6, 0, metanetMapsList.Nodes.Add("Co-op N++"));
LoadMapFiles(dir + @"\C2", "CU-", 10, 6, 10, metanetMapsList.Nodes.Add("Co-op Ultimate"));
LoadMapFiles(dir + @"\CL", "CL-", 5, 5, 0, metanetMapsList.Nodes.Add("Co-op Legacy"));
LoadMapFiles(dir + @"\CL2", "CL-", 6, 6, 5, metanetMapsList.Nodes.Add("Co-op Legacy Ultimate"));
LoadMapFiles(dir + @"\RI", "RI-", 1, 5, 0, metanetMapsList.Nodes.Add("Race Intro"));
LoadMapFiles(dir + @"\R", "R-", 10, 5, 0, metanetMapsList.Nodes.Add("Race N++"));
LoadMapFiles(dir + @"\R2", "RU-", 10, 5, 10, metanetMapsList.Nodes.Add("Race Ultimate"));
LoadMapFiles(dir + @"\RL", "RL-", 10, 5, 0, metanetMapsList.Nodes.Add("Race Legacy"));
LoadMapFiles(dir + @"\RL2", "RL-", 10, 5, 10, metanetMapsList.Nodes.Add("Race Legacy Ultimate"));
installMetanetMap.Enabled = false;
// list local backups
localMapsBackupsList.Items.Clear();
PopulateListBoxWithFileType(localMapsBackupsList, savePath + @"\Maps", "*.zip");
installBackupMap.Enabled = false;
// list editor maps
RefreshListEditorMaps();
}
if (current == tabMapPacks)
{
// list local map packs backups
localBackupsMapPacksList.Items.Clear();
PopulateListBoxWithFileType(localBackupsMapPacksList, savePath + @"\MapPacks", "*.zip");
renameLocalBackupMapPack.Enabled = false;
installLocalBackupMapPack.Enabled = false;
deleteLocalBackupMapPack.Enabled = false;
installLocalBackupMapPackWithProfile.Enabled = false;
// list local profile backups
profileMapBackupList.Items.Clear();
PopulateListBoxWithFileType(profileMapBackupList, savePath + @"\Profiles", "*.zip");
renameProfileBackup.Enabled = false;
installBackupMapPackProfile.Enabled = false;
deleteBackupMapPackProfile.Enabled = false;
}
}
private void steamGamePath_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
launchExplorer(steamGamePath);
}
private void profileLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
launchExplorer(profilePath);
}
private void backupNow_Click(object sender, EventArgs e)
{
if (DetectNPPRunning() == true)
{
MessageBox.Show("Please close N++ before doing backup");
return;
}
backupNow.Enabled = false;
//progressBar1.Show();
bgwBackupNow.RunWorkerAsync();
}
private 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("yyMMddHHmm") + ".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(steamGamePath + @"\NPP\Sounds", savePath + @"\Sounds\Sounds" + DateTime.Now.ToString("yyMMddHHmm") + ".zip");
}
bgwBackupNow.ReportProgress(30, "Zipping Editor Levels");
if (checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{
// backup editor levels
ZipFile.CreateFromDirectory(profilePath + @"\levels", savePath + @"\Maps\Maps" + DateTime.Now.ToString("yyMMddHHmm") + ".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("yyMMddHHmm") + ".zip");
}
bgwBackupNow.ReportProgress(60, "Zipping Palettes");
if (checkedListBox1.GetItemCheckState(4) == CheckState.Checked)
{
// backup palettes
//ZipFile.CreateFromDirectory(steamGamePath + @"\NPP\Palettes", savePath + @"\Palettes\Palettes" + DateTime.Now.ToString("yyMMddHHmm") + ".zip");
try
{
string[] dirs = Directory.GetDirectories(steamGamePath + @"\NPP\Palettes");
foreach (string dir in dirs)
{
string[] splits = dir.Split('\\');
string palName = splits[splits.Length - 1];
ZipFile.CreateFromDirectory(dir, savePath + @"\Palettes\" + palName + DateTime.Now.ToString("yyMMddHHmm") + ".zip");
}
}
catch (Exception exc)
{
MessageBox.Show("Failed to zip palettes: {0}", exc.ToString());
}
}
bgwBackupNow.ReportProgress(80, "Zipping Game Levels");
if (checkedListBox1.GetItemCheckState(5) == CheckState.Checked)
{
// backup map packs
ZipFile.CreateFromDirectory(steamGamePath + @"\NPP\Levels", savePath + @"\MapPacks\MapPacks" + DateTime.Now.ToString("yyMMddHHmm") + ".zip");
}
bgwBackupNow.ReportProgress(100, "Done with backup!");
}
private void bgwBackupNow_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
statusLabel.Text = String.Format("{0}", e.UserState);
}
private void bgwBackupNow_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
statusLabel.Text = "Done with backup!";
//progressBar.Hide();
backupNow.Enabled = true;
}
private void savePathLabel_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 screenshotsPathLabel_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 PopulateListBoxWithFileType(ListBox lsb, string Folder, string FileType)
{
try
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name + " (" + file.Length / 1024 + "Kb)");
}
}
catch (System.IO.DirectoryNotFoundException exc) {
MessageBox.Show(exc.Message);
}
}
private void PopulateListBoxWithSubDirectories(ListBox lsb, string Folder)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
DirectoryInfo[] dirs = dinfo.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
// yyyy/MM/dd format is coherent with what's coming from google sheets
lsb.Items.Add(dir.Name + " (" + dir.LastWriteTime.ToString("yyyy/MM/dd") + ")");
}
}
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)
{
statusLabel.Text = "Profile backup started!";
using (FileStream fs = new FileStream(savePath + @"\Profiles\nprofile" + DateTime.Now.ToString("yyMMddHHmm") + ".zip", FileMode.Create))
using (ZipArchive arch = new ZipArchive(fs, ZipArchiveMode.Create))
{
arch.CreateEntryFromFile(profilePath + @"\nprofile", "nprofile");
}
statusLabel.Text = "Profile backup completed!";
profileList.Items.Clear();
PopulateListBoxWithFileType(profileList, savePath + @"\Profiles", "*.zip");
}
private void profileList_SelectedIndexChanged(object sender, EventArgs e)
{
loadProfile.Enabled = true;
deleteProfile.Enabled = true;
}
private void loadProfileBackup_Click(object sender, EventArgs e)
{
if (DetectNPPRunning() == true)
{
MessageBox.Show("Please close N++ before installing a profile backup");
statusLabel.Text = "Aborted loading profile because N++ was running.";
return;
}
DialogResult dialogResult = MessageBox.Show("Are you sure you wish to replace your game profile with the selected backup? This process is irreversible if you haven't done a recent backup.", "Replace Existing nprofile?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
File.Delete(profilePath + @"\nprofile");
string zipPath = savePath + @"\Profiles\" + profileList.SelectedItem.ToString().Substring(0, profileList.SelectedItem.ToString().LastIndexOf(' ')).TrimEnd();
string extractPath = profilePath + @"\nprofile";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.Name.Equals("nprofile"))
{
entry.ExtractToFile(extractPath, true);
statusLabel.Text = "Replaced current N++ game profile with " + profileList.SelectedItem.ToString().Substring(0, profileList.SelectedItem.ToString().LastIndexOf(' ')).TrimEnd();
}
}
}
}
else if (dialogResult == DialogResult.No)
{
// do nothing
}
}
private void deleteProfileBackup_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you wish to delete the selected backup? This process is irreversible.", "Delete Selected File?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
try
{
string filename = profileList.SelectedItem.ToString().Split(' ')[0];
File.Delete(savePath + @"\Profiles\" + filename);
profileList.Items.Remove(profileList.SelectedItem);
loadProfile.Enabled = false;
deleteProfile.Enabled = false;
statusLabel.Text = "Deleted profile backup " + filename;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
statusLabel.Text = "Exception trying to delete profile backup " + profileList.SelectedItem.ToString().Split(' ')[0];
}
}
}
private void uninstallPalette_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you wish to uninstall the selected palette? This process is irreversible if you haven't done a recent backup.", "Uninstall Palette?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
try
{
string palName = palettesInstalledList.SelectedItem.ToString().Split(' ')[0];
Directory.Delete(steamGamePath + @"\NPP\Palettes\" + palName, true);
palettesInstalledList.Items.Remove(palettesInstalledList.SelectedItem);
uninstallPalette.Enabled = false;
backupPalette.Enabled = false;
updateCustomPalleteInstalledCounter();
statusLabel.Text = "Done uninstalling " + palName;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
private void palettesInstalled_SelectedIndexChanged(object sender, EventArgs e)
{
uninstallPalette.Enabled = true;
backupPalette.Enabled = true;
}
private void backupPalette_Click(object sender, EventArgs e)
{
// backup the selected palette only
string palName = palettesInstalledList.SelectedItem.ToString().Substring(0, palettesInstalledList.SelectedItem.ToString().Length - 13);
ZipFile.CreateFromDirectory(steamGamePath + @"\NPP\Palettes\" + palName, savePath + @"\Palettes\" + palName + DateTime.Now.ToString("yyMMddHHmm") + ".zip");
// update local backup palettes list
localBackupPalettesList.Items.Clear();
PopulateListBoxWithFileType(localBackupPalettesList, savePath + @"\Palettes", "*.zip");
installBackupPalette.Enabled = false;
deleteBackupPalette.Enabled = false;
// notify backup was done
backupPalette.Enabled = false;
statusLabel.Text = "Done with backup of palette " + palName;
}
private void palettesInstalledLinkedLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
launchExplorer(steamGamePath + @"\NPP\Palettes\");
}
public class JSONSerializer : SheetsSerializer
{
public override T DeserializeObject<T>(string data)
{
return JsonConvert.DeserializeObject<T>(data);
}
public override string SerializeObject(object data)
{
return JsonConvert.SerializeObject(data);
}
}
private async void PopulateListBoxWithSpreadsheetData(ListBox lsb, int sheetsId, string spreadsheetId, string key, string sht)
{
// test url https://sheets.googleapis.com/v4/spreadsheets/spreadsheetid?key=key
try
{
SheetsSerializer.Serializer = new JSONSerializer();
Popcron.Sheets.Authorization authorization = await Popcron.Sheets.Authorization.Authorize(key);
Spreadsheet spreadsheet = await Spreadsheet.Get(spreadsheetId, authorization);
Sheet sheet = spreadsheet.Sheets[sheetsId];
Cell[,] data = sheet.Data;
for (int y = 1; y < sheet.Rows; y++)
{
string parsedDate = data[2, y].Value;
if ((data[2, y].Value != null) && (data[2, y].Value.Split('/').Length > 3))
{
parsedDate = data[2, y].Value.Split('/')[2] + "/" + data[2, y].Value.Split('/')[1] + "/" + data[2, y].Value.Split('/')[0];
}
if (data[0, y].Value != null && data[0, y].Value != "")
lsb.Items.Add(data[0, y].Value + " by " + data[1, y].Value + " (" + parsedDate + ")");
}
bool exists = false;
foreach (var mapSheet in sheetMapList)
{
if (mapSheet.sheetId.Equals(spreadsheetId) == true)
{
mapSheet.sheetData = sheet;
exists = true;
}
}
if (exists == false)
{
sheetMapList.Add(new sheetMap(spreadsheetId, sheet));
}
statusLabel.Text = "Done retrieving spreadsheet data " + sht;
}
catch (System.Net.WebException webexc)
{
MessageBox.Show(webexc.Message);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void metanetPalettesList_SelectedIndexChanged(object sender, EventArgs e)
{
installMetanetPalette.Enabled = true;
}
private void installMetanetPalette_Click(object sender, EventArgs e)
{
string palName = metanetPalettesList.SelectedItem.ToString().Substring(0, metanetPalettesList.SelectedItem.ToString().Length - 13);
// check if it already exists
if (Directory.Exists(steamGamePath + @"\NPP\Palettes\" + palName))
{
DialogResult dialogResult = MessageBox.Show("A palette already exists with this name, would you like to replace it?", "Replace Existing?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Directory.Delete(steamGamePath + @"\NPP\Palettes\" + palName, true);
DirectoryCopy(savePath + @"\Palettes\Palettes\" + palName, steamGamePath + @"\NPP\Palettes\" + palName, false);
palettesInstalledList.Items.Clear();
PopulateListBoxWithSubDirectories(palettesInstalledList, steamGamePath + @"\NPP\Palettes");
installMetanetPalette.Enabled = false;
statusLabel.Text = "Done installing palette " + palName;
}
}
else
{
DirectoryCopy(savePath + @"\Palettes\Palettes\" + palName, steamGamePath + @"\NPP\Palettes\" + palName, false);
palettesInstalledList.Items.Clear();
PopulateListBoxWithSubDirectories(palettesInstalledList, steamGamePath + @"\NPP\Palettes");
installMetanetPalette.Enabled = false;
statusLabel.Text = "Done installing palette " + palName;
}
}
private void communityPalettesList_SelectedIndexChanged(object sender, EventArgs e)
{
installCommunityPalette.Enabled = true;
}
private void installCommunityPalette_Click(object sender, EventArgs e)
{
if (DetectNPPRunning() == true)
{
MessageBox.Show("Please close N++ before installing palettes");
return;
}
try
{
string myStringWebResource = null;
WebClient myWebClient = new WebClient();
// get the url
foreach (var mapSheet in sheetMapList)
{
if (mapSheet.sheetId.Equals(COMMUNITY_PALETTES) == true)
{
Cell[,] data = mapSheet.sheetData.Data;
myStringWebResource = data[3, communityPalettesList.SelectedIndex + 1].raw.hyperlink;
}
}
// download the file and save it into the current filesystem folder.
string filename = steamGamePath + @"\NPP\Palettes\" + "nppassisttemppal.zip";
myWebClient.DownloadFile(myStringWebResource, filename);
// check if palette has subdir in zip or not
var zip = ZipFile.OpenRead(filename);
var names = zip.Entries[0].FullName;
zip.Dispose();
if (names.ToString().StartsWith("background.tga"))
{
// assume the palette should have same name as archive and create + extract into that directory
string dirname = myStringWebResource.Substring(myStringWebResource.LastIndexOf('/') + 1, myStringWebResource.LastIndexOf('.') - myStringWebResource.LastIndexOf('/') - 1).TrimEnd();
// check if it already exists
if (Directory.Exists(steamGamePath + @"\NPP\Palettes\" + dirname))
{
DialogResult dialogResult = MessageBox.Show("A palette already exists with this name, would you like to replace it?", "Replace Existing?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Directory.Delete(steamGamePath + @"\NPP\Palettes\" + dirname, true);
Directory.CreateDirectory(steamGamePath + @"\NPP\Palettes\" + dirname);
ZipFile.ExtractToDirectory(filename, steamGamePath + @"\NPP\Palettes\" + dirname);
File.Delete(filename);
statusLabel.Text = "Done replacing palette " + dirname;
}
else
{
// do nothing
}
}
else
{
Directory.CreateDirectory(steamGamePath + @"\NPP\Palettes\" + dirname);
ZipFile.ExtractToDirectory(filename, steamGamePath + @"\NPP\Palettes\" + dirname);
File.Delete(filename);
}
} else {
// assume it has a subdir with same name, extract in root of palettes
if (Directory.Exists(steamGamePath + @"\NPP\Palettes\" + names))
{
DialogResult dialogResult = MessageBox.Show("A palette already exists with this name, would you like to replace it?", "Replace Existing?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Directory.Delete(steamGamePath + @"\NPP\Palettes\" + names, true);
ZipFile.ExtractToDirectory(filename, steamGamePath + @"\NPP\Palettes");
File.Delete(filename);
statusLabel.Text = "Done replacing palette " + names;
}
else
{
// do nothing
}
}
else
{
ZipFile.ExtractToDirectory(filename, steamGamePath + @"\NPP\Palettes");
File.Delete(filename);
statusLabel.Text = "Done installing palette " + filename;
}
}
// refresh installed palettes directory
palettesInstalledList.Items.Clear();
PopulateListBoxWithSubDirectories(palettesInstalledList, steamGamePath + @"\NPP\Palettes");
installCommunityPalette.Enabled = false;
updateCustomPalleteInstalledCounter();
}
catch (Exception exc)
{
MessageBox.Show("Couldn't install community palette because: " + exc.Message);
}
}
private void installBackupPalette_Click(object sender, EventArgs e)
{
string palName = localBackupPalettesList.SelectedItem.ToString().Substring(0, localBackupPalettesList.SelectedItem.ToString().LastIndexOf('.') - 14).TrimEnd();
string filename = localBackupPalettesList.SelectedItem.ToString().Substring(0, localBackupPalettesList.SelectedItem.ToString().LastIndexOf(' ')).TrimEnd();
// check if destination dir exists
if (Directory.Exists(steamGamePath + @"\NPP\Palettes\" + palName))
{
// prompt asking if replacing existing dir or not
DialogResult dialogResult = MessageBox.Show("A palette with this name is already installed, do you want to replace it?", "Replace Existing?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
// delete installed palette
Directory.Delete(steamGamePath + @"\NPP\Palettes\" + palName, true);
// install new palette from backup
Directory.CreateDirectory(steamGamePath + @"\NPP\Palettes\" + palName);
ZipFile.ExtractToDirectory(savePath + @"\Palettes\" + filename, steamGamePath + @"\NPP\Palettes\" + palName);
// refresh installed palettes list
palettesInstalledList.Items.Clear();
PopulateListBoxWithSubDirectories(palettesInstalledList, steamGamePath + @"\NPP\Palettes");
uninstallPalette.Enabled = false;
backupPalette.Enabled = false;
// notify it's done
installBackupPalette.Enabled = false;
statusLabel.Text = "Done replacing palette " + palName;
}
else if (dialogResult == DialogResult.No)
{
// do nothing
}
}
else
{
// install palette
Directory.CreateDirectory(steamGamePath + @"\NPP\Palettes\" + palName);
ZipFile.ExtractToDirectory(savePath + @"\Palettes\" + filename, steamGamePath + @"\NPP\Palettes\" + palName);
// refresh installed palettes list
palettesInstalledList.Items.Clear();
PopulateListBoxWithSubDirectories(palettesInstalledList, steamGamePath + @"\NPP\Palettes");
uninstallPalette.Enabled = false;
backupPalette.Enabled = false;
// notify it's done
installBackupPalette.Enabled = false;
updateCustomPalleteInstalledCounter();
statusLabel.Text = "Done installing palette " + palName;
}
}
private void deleteBackupPalette_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you wish to delete this backup? This process is irreversible.", "Delete backup?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
string palName = localBackupPalettesList.SelectedItem.ToString().Substring(0, localBackupPalettesList.SelectedItem.ToString().LastIndexOf(' ')).TrimEnd();
File.Delete(savePath + @"\Palettes\" + palName);
// remove item instead of refreshing whole list, to keep position in view
localBackupPalettesList.Items.Remove(localBackupPalettesList.SelectedItem);
installBackupPalette.Enabled = false;
deleteBackupPalette.Enabled = false;
statusLabel.Text = "Done deleting backup " + palName;
}
}
private void updateCustomPalleteInstalledCounter()
{
int count = 0;
foreach (string item in palettesInstalledList.Items)
{
string f1 = item.Substring(0, item.LastIndexOf(' ')).TrimEnd();
bool isMetanet = false;
foreach (string item2 in metanetPalettesList.Items)
{
string f2 = item2.Substring(0, item2.LastIndexOf(' ')).TrimEnd();
if (f1.Equals(f2)) isMetanet = true;
}
if (!isMetanet) count++;
}
countCustomPalettesInstalled.Text = "Custom: " + count.ToString();
if (count > 132)
{
countCustomPalettesInstalled.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("You've reached maximum number of custom palettes the game can handle, here be dragons if you don't revert");
}
else
{
countCustomPalettesInstalled.ForeColor = System.Drawing.Color.Black;
}
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.