-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMipMapTool.cpp
275 lines (226 loc) · 9.33 KB
/
MipMapTool.cpp
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
#include <iostream>
#include <regex>
#include "TextureFile.hpp"
#include <optional>
void printStart() {
std::cout << "MipMap Tool v8(wrrrm wrrm) OwO\n";
}
#include <windows.h>
void printHelp() {
HANDLE output_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (output_handle != INVALID_HANDLE_VALUE) {
SMALL_RECT rect = {};
rect.Bottom = static_cast<SHORT>(100);
rect.Right = static_cast<SHORT>(80);
SetConsoleWindowInfo(output_handle, TRUE, &rect);
COORD coord = {};
coord.X = 100;
coord.Y = 80;
SetConsoleScreenBufferSize(output_handle, coord);
}
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r); //stores the console's current dimensions
MoveWindow(console, r.left, r.top, 1000, 800, TRUE); // 800 width, 100 height
std::cout
<< "Hewo! I'm the MipMap tool and I'll Map Mips for you.\n\n"
<< "I can unpack your textures like so:\n"
<< " mipmaptool unpack -namestyle0 \"P:/file1_co.paa\" \"P:\\file2_co.paa\"\n"
<< "I can print info about your texture like so:\n"
<< " mipmaptool info \"P:/file1_co.paa\"\n"
<< "I can combine the best mipmaps like so:\n"
<< " mipmaptool -namestyle0 \"P:/tex_mip4096_co.paa\" \"P:\\tex_mip2048_co.paa\" \"P:/tex_mip4_co.paa\"\n"
<< " These filenames have to be in a specific format, see -namestyle below\n"
<< "Or I also can combine the best mipmaps like so:\n"
<< " mipmaptool output \"P:/output_co.paa\" \"P:/file1_co.paa\" \"P:\\file2_co.paa\"\n"
<< " (which writes into specified output file)\n\n"
<< "-namestyle parameter sets he filename format for input/outputs\n"
<< "-namestyle0 (default):\n"
<< " xxx_mip1234_yy.paa, output file will be xxx_yy.paa\n"
<< " (for you nerds out there the regex is (.*)_mip(\\d*)(([^\\d]*)*)\\.paa )\n"
<< "-namestyle1 (default):\n"
<< " xxx.mip1234.paa, output file will be xxx.paa\n"
<< " regex: (.*)\\.mip(\\d*)\\.paa\n\n\n"
<< "File paths have to always be encased in quotes like shown.\n"
<< "Windows does that automatically if you just drag&drop files onto the binary.\n"
<< "As you can see / and \\ are allowed, anything that works in windows should work.\n\n"
<< "I hav been made by dedmen who has a patreon UwU: https://patreon.com/dedmen\n(Just so you know)";
std::cin.get();
}
enum class NameStyle {
xx_mip_yy_paa,
xx_mip_paa,
};
std::filesystem::path makeOutputPath(std::filesystem::path inputFile, std::shared_ptr<MipMap> mipmap, NameStyle style = NameStyle::xx_mip_yy_paa) {
std::filesystem::path outputPath = (inputFile.parent_path() / (inputFile.filename().stem().string() + "_mip" + std::to_string(mipmap->getRealSize()) + inputFile.extension().string()));
switch (style) {
case NameStyle::xx_mip_yy_paa: {
std::regex reg("(.*)_(.*?)\\.paa");
std::cmatch cm;
std::string inputString = inputFile.filename().string();
std::regex_match(inputString.c_str(), cm, reg);
if (cm.size() == 3) {
outputPath = (inputFile.parent_path() / (std::string(cm[1]) + "_mip" + std::to_string(mipmap->getRealSize()) + "_" + std::string(cm[2]) + inputFile.extension().string()));
}
return outputPath;
}
case NameStyle::xx_mip_paa: {
std::regex reg("(.*)\\.paa");
std::cmatch cm;
std::string inputString = inputFile.filename().string();
std::regex_match(inputString.c_str(), cm, reg);
if (cm.size() == 2) {
outputPath = (inputFile.parent_path() / (std::string(cm[1]) + ".mip" + std::to_string(mipmap->getRealSize()) + inputFile.extension().string()));
}
return outputPath;
}
default: {
std::cerr << "ERROR invalid -namestyle parameter provided";
std::cin.get();
exit(1);
}
}
}
std::optional<std::string> parseOutputFilename(std::filesystem::path inputFile, NameStyle style = NameStyle::xx_mip_yy_paa) {
auto filename = inputFile.filename().string();
switch (style) {
case NameStyle::xx_mip_yy_paa: {
std::regex reg("(.*)_mip(\\d*)([^\\d]*)\\.paa");
std::cmatch cm;
std::regex_match(filename.c_str(), cm, reg);
if (cm.size() != 4) {
return {};
}
return (std::string(cm[1]) + std::string(cm[3]) + ".paa");
}
case NameStyle::xx_mip_paa: {
std::regex reg("(.*)\\.mip(\\d*)\\.paa");
std::cmatch cm;
std::regex_match(filename.c_str(), cm, reg);
if (cm.size() != 3) {
return {};
}
return std::string(cm[1]) + ".paa";
}
default: {
std::cerr << "ERROR invalid -namestyle parameter provided";
std::cin.get();
exit(1);
}
}
}
int main(int argc, char* argv[]) {
printStart();
if (argc <= 1) {
printHelp();
return 0;
}
std::string_view firstArg = argv[1];
if (firstArg == "help") {
printHelp();
return 0;
}
std::vector<std::string> allArguments;
for (int i = 1; i < argc; ++i) {
allArguments.push_back(argv[i]);
}
NameStyle namestyle = NameStyle::xx_mip_yy_paa;
auto namestyleArg = std::find_if(allArguments.begin(), allArguments.end(), [](const std::string& arg) {
return arg.starts_with("-namestyle");
});
if (namestyleArg != allArguments.end()) {
char namestyleChar = (*namestyleArg)[10];
namestyle = static_cast<NameStyle>(namestyleChar - '0');
allArguments.erase(namestyleArg);
}
auto highestMIPArg = std::find_if(allArguments.begin(), allArguments.end(), [](const std::string& arg) {
return arg.starts_with("-onlyHighestMip");
});
if (firstArg == "unpack") {
std::cout << "Unpacking files:\n";
allArguments.erase(allArguments.begin());
for (auto& file : allArguments) {
auto texFile = std::make_shared<TextureFile>();
std::filesystem::path inputFile(file);//#TODO file exists check
texFile->readFromFile(inputFile);
//texFile->writeToFile("P:/outtest.paa");
for (auto& mipmap : texFile->mipmaps) {
auto newFile = texFile->copyNoMipmap();
newFile->mipmaps.push_back(mipmap);
std::filesystem::path outputPath = makeOutputPath(inputFile, mipmap, namestyle);
newFile->writeToFile(outputPath);
if (highestMIPArg != allArguments.end()) return 0;
}
}
return 0;
}
if (firstArg == "info") {
std::cout << "file info:\n";
allArguments.erase(allArguments.begin());
for (auto& file : allArguments) {
auto texFile = std::make_shared<TextureFile>();
std::filesystem::path inputFile(file);//#TODO file exists check
texFile->verboseLog = true;
texFile->readFromFile(inputFile);
}
return 0;
}
std::filesystem::path output;
bool hasOutput = false;
if (firstArg == "output") {
allArguments.erase(allArguments.begin());
output = allArguments.front();
allArguments.erase(allArguments.begin());
hasOutput = true;
}
if (!hasOutput) {
for (auto& file : allArguments) {
std::filesystem::path inputFile(file);
auto filename = inputFile.filename().string();
auto outputFilename = parseOutputFilename(inputFile, namestyle);
if (!outputFilename) {
std::cerr << "ERROR filename " << filename << " doesn't match correct format";
std::cin.get();
return 1;
}
if (!hasOutput) {
output = inputFile.parent_path() / *outputFilename;
hasOutput = true;
} else {
if (*outputFilename != output.filename()) {
std::cerr << "ERROR filename " << filename << " doesn't match previously determined filename " << output.filename();
std::cin.get();
return 1;
}
}
}
}
std::vector<std::shared_ptr<TextureFile>> textures;
for (auto& file : allArguments) {
auto texFile = std::make_shared<TextureFile>();
std::filesystem::path inputFile(file);
texFile->readFromFile(inputFile);
textures.push_back(texFile);
}
//Sort by their max mipmap size
std::sort(textures.begin(), textures.end(), [](auto& left, auto& right) {
return left->mipmaps.front()->width > right->mipmaps.front()->width;
});
std::map<uint16_t, std::shared_ptr<MipMap>> mipmaps;
for (auto& texture : textures) {
for (auto& mipmap : texture->mipmaps) {
mipmaps.insert_or_assign(mipmap->height, mipmap);
}
}
auto result = textures.back()->copyNoMipmap();
std::cout << "packing mipmaps...\n";
for (auto& [key, val] : mipmaps) {
std::cout << "\t" << val->height << "-> \t" << val->inputPath << "\n";
result->mipmaps.push_back(val);
}
if (hasOutput) {
result->writeToFile(output);
}
std::cout << "packed new texture file with " << result->mipmaps.size() << " mipmaps in " << output << "\n";
return 0;
}