Skip to content

Commit

Permalink
Added ability to choose name style
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
dedmen committed Nov 20, 2019
1 parent ffde8f1 commit 67946e7
Showing 1 changed file with 101 additions and 21 deletions.
122 changes: 101 additions & 21 deletions MipMapTool.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <iostream>
#include <regex>
#include "TextureFile.hpp"
#include <optional>

void printStart() {
std::cout << "MipMap Tool v6 OwO\n";
std::cout << "MipMap Tool v8(wrrrm wrrm) OwO\n";
}
#include <windows.h>
void printHelp() {
Expand Down Expand Up @@ -31,16 +32,24 @@ void printHelp() {
<< "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 \"P:/file1_co.paa\" \"P:\\file2_co.paa\"\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 \"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 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"
<< " 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"
<< " (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"
Expand All @@ -50,6 +59,74 @@ void printHelp() {
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) {
Expand All @@ -69,6 +146,18 @@ int main(int argc, char* argv[]) {
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");
});

char namestyleChar = (*namestyleArg)[10];
namestyle = static_cast<NameStyle>(namestyleChar - '0');

allArguments.erase(namestyleArg);


if (firstArg == "unpack") {
std::cout << "Unpacking files:\n";
allArguments.erase(allArguments.begin());
Expand All @@ -81,16 +170,8 @@ int main(int argc, char* argv[]) {
for (auto& mipmap : texFile->mipmaps) {
auto newFile = texFile->copyNoMipmap();
newFile->mipmaps.push_back(mipmap);
std::filesystem::path outputPath = (inputFile.parent_path() / (inputFile.filename().stem().string() + "_mip" + std::to_string(mipmap->getRealSize()) + inputFile.extension().string()));

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()));

}
std::filesystem::path outputPath = makeOutputPath(inputFile, mipmap, namestyle);

newFile->writeToFile(outputPath);
}
Expand Down Expand Up @@ -126,20 +207,19 @@ int main(int argc, char* argv[]) {
std::filesystem::path inputFile(file);
auto filename = inputFile.filename().string();

std::regex reg("(.*)_mip(\\d*)([^\\d]*)\\.paa");
std::cmatch cm;
std::regex_match(filename.c_str(), cm, reg);
if (cm.size() != 4) {
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() / (std::string(cm[1]) + std::string(cm[3]) + ".paa");
output = inputFile.parent_path() / *outputFilename;
hasOutput = true;
} else {
if ((std::string(cm[1]) + std::string(cm[3]) + ".paa") != output.filename()) {
if (*outputFilename != output.filename()) {
std::cerr << "ERROR filename " << filename << " doesn't match previously determined filename " << output.filename();
std::cin.get();
return 1;
Expand Down

0 comments on commit 67946e7

Please sign in to comment.