From 67946e7dc21dc45a192216b2f3301618de7258a5 Mon Sep 17 00:00:00 2001 From: Dedmen Miller Date: Wed, 20 Nov 2019 11:01:07 +0100 Subject: [PATCH] Added ability to choose name style Fixes #2 --- MipMapTool.cpp | 122 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 101 insertions(+), 21 deletions(-) diff --git a/MipMapTool.cpp b/MipMapTool.cpp index 7008815..2dfd5b4 100644 --- a/MipMapTool.cpp +++ b/MipMapTool.cpp @@ -1,9 +1,10 @@ #include #include #include "TextureFile.hpp" +#include void printStart() { - std::cout << "MipMap Tool v6 OwO\n"; + std::cout << "MipMap Tool v8(wrrrm wrrm) OwO\n"; } #include void printHelp() { @@ -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" @@ -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, 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 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) { @@ -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(namestyleChar - '0'); + + allArguments.erase(namestyleArg); + + if (firstArg == "unpack") { std::cout << "Unpacking files:\n"; allArguments.erase(allArguments.begin()); @@ -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); } @@ -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;