-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
145 lines (129 loc) · 5.15 KB
/
Program.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
using SteamDatabase.ValvePak;
//Clear the console
Console.Clear();
Console.ResetColor();
//Maximize console window
//Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
//Write a welcome message
Console.WriteLine("Counter-Strike 2 VPK map unpacker v1.0");
Console.WriteLine("Created by Francesc Pàez (C) 2023");
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Download the latest version on GitHub: https://github.com/fpaezf/cs2-vpk-map-unpacker");
Console.WriteLine("");
if (args.Length == 0)
{ //User has launched the application without arguments, throw a warning message and exit application
PrintMessage("FAIL:", "No arguments detected!", ConsoleColor.DarkRed);
Console.WriteLine("");
Console.WriteLine("Launch this application again but using 2 command line arguments with quotes:");
Console.WriteLine("MapUnPacker.exe \"c:\\targetfolder\\mymap.vpk\" \"c:\\sourcefolder\\mymap\"");
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
else if (args.Length == 1)
{ //User has launched the application with only one argument, throw a warning message and exit application
PrintMessage("FAIL:", "Only 1 argument detected!", ConsoleColor.DarkRed);
Console.WriteLine("");
Console.WriteLine("Launch this application again but using 2 command line arguments with quotes:");
Console.WriteLine("MapUnPacker.exe \"c:\\targetfolder\\mymap.vpk\" \"c:\\sourcefolder\\mymap\"");
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
else if (args.Length == 2)
{ //User has launched the application with 2 arguments, show pass message and continue
PrintMessage("Source file:", args[0], ConsoleColor.DarkYellow);
PrintMessage("Target directory:", args[1], ConsoleColor.DarkYellow);
Console.WriteLine("");
if (File.Exists(args[0]))
{ //Check if provided file exists and if so, show a pass message and continue
PrintMessage("PASS:", "Source file is accesible.", ConsoleColor.DarkGreen);
}
else if (!File.Exists(args[0]))
{ //If source folder not exists, show a warning message and exit
PrintMessage("FAIL:", "Source file is not accesible.", ConsoleColor.DarkRed);
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
if (Directory.Exists(args[1]))
{
PrintMessage("PASS:", "Target directory is accesible.", ConsoleColor.DarkGreen);
} else if (!Directory.Exists(args[1]))
{
PrintMessage("FAIL:", "Target directory not exists.", ConsoleColor.DarkRed);
try
{
Directory.CreateDirectory(args[1]);
PrintMessage("WORK:", "Target directory created succesfully.", ConsoleColor.Blue);
}
catch (Exception e)
{
PrintMessage("FAIL:", "Can't create target directory.", ConsoleColor.DarkRed);
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
}
string VPKFile = args[0];
PrintMessage("WORK:", "Unpacking files and folders...", ConsoleColor.Blue);
try
{
var mapPackage = new Package();
mapPackage.Read(VPKFile);
ExtractPackage(mapPackage);//Call the unpack folder routine
mapPackage.Dispose();
PrintMessage("PASS:", "All files were extracted!", ConsoleColor.DarkGreen);
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
catch (Exception ex)
{
PrintMessage("FAIL:", "Error extracting files!", ConsoleColor.DarkRed);
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
}
void ExtractPackage(Package package)
{
var inPackageSet = new HashSet<string>();
try
{
foreach (var entries in package.Entries.Values)
{
foreach (var entry in entries)
{
var filePath = entry.GetFullPath();
inPackageSet.Add(filePath);
var extractFilePath = Path.Combine(args[1], filePath);
Directory.CreateDirectory(Path.GetDirectoryName(extractFilePath)!);
package.ReadEntry(entry, out var data);
File.WriteAllBytes(extractFilePath, data);
}
}
}
catch (Exception a)
{
PrintMessage("FAIL:", "Error extracting files!", ConsoleColor.DarkRed);
Console.WriteLine("");
Console.WriteLine("Hit a key to exit.");
Console.ReadKey(true);
return;
}
}
void PrintMessage(string Head, string Message, ConsoleColor Color) {
Console.BackgroundColor = Color;
Console.ForegroundColor = ConsoleColor.White;
Console.Write(Head);
Console.ResetColor();
Console.Write(" " + Message);
Console.WriteLine("");
}