From 1165b4c77e3ccc494b07158782da09f558b5be0c Mon Sep 17 00:00:00 2001 From: wootguy Date: Thu, 26 Sep 2024 13:58:18 -0700 Subject: [PATCH] add renametex command-line option --- .gitignore | 4 ++++ src/bsp/Bsp.cpp | 21 +++++++++++++++++++++ src/bsp/Bsp.h | 2 ++ src/main.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/.gitignore b/.gitignore index 44297ae8..0f5a5248 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,7 @@ msvc /imgui_177 /imgui_178 /merge +/glew_x64 +/glfw_30 +/imgui_181 +/msvc_x86 diff --git a/src/bsp/Bsp.cpp b/src/bsp/Bsp.cpp index 84f52d2f..c6a63f3d 100644 --- a/src/bsp/Bsp.cpp +++ b/src/bsp/Bsp.cpp @@ -3287,6 +3287,27 @@ void Bsp::downscale_invalid_textures() { logf("Downscaled %d textures\n", count); } +bool Bsp::rename_texture(const char* oldName, const char* newName) { + if (strlen(newName) > 16) { + logf("ERROR: New texture name longer than 15 characters (%d)\n", strlen(newName)); + return false; + } + + for (int i = 0; i < textureCount; i++) { + int32_t texOffset = ((int32_t*)textures)[i + 1]; + BSPMIPTEX& tex = *((BSPMIPTEX*)(textures + texOffset)); + + if (!strncmp(tex.szName, oldName, 16)) { + strncpy(tex.szName, newName, 16); + logf("Renamed texture '%s' -> '%s'\n", oldName, newName); + return true; + } + } + + logf("No texture found with name '%s'\n", oldName); + return false; +} + set Bsp::selectConnectedTexture(int modelId, int faceId) { set selected; const float epsilon = 1.0f; diff --git a/src/bsp/Bsp.h b/src/bsp/Bsp.h index baeda720..28769095 100644 --- a/src/bsp/Bsp.h +++ b/src/bsp/Bsp.h @@ -222,6 +222,8 @@ class Bsp bool downscale_texture(int textureId, int newWidth, int newHeight); + bool rename_texture(const char* oldName, const char* newName); + // updates texture coordinates after a texture has been downscaled void adjust_downscaled_texture_coordinates(int textureId, int oldWidth, int oldHeight); diff --git a/src/main.cpp b/src/main.cpp index ebfc19fc..4c76d18c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -519,6 +519,25 @@ int unembed(CommandLine& cli) { return 0; } +int rename_texture(CommandLine& cli) { + Bsp* map = new Bsp(cli.bspfile); + if (!map->valid) + return 1; + + string oldName = cli.getOption("-old"); + string newName = cli.getOption("-new"); + + map->rename_texture(oldName.c_str(), newName.c_str()); + + if (map->isValid()) map->write(cli.hasOption("-o") ? cli.getOption("-o") : map->path); + logf("\n"); + + delete map; + + return 0; +} + + void print_help(string command) { if (command == "merge") { logf( @@ -631,6 +650,14 @@ void print_help(string command) { "Example: bspguy unembed c1a0.bsp\n" ); } + else if (command == "renametex") { + logf( + "renametex - Renames a texture. This changes the texture if it's loaded from a WAD.\n\n" + + "Usage: bspguy renametex -old -new \n" + "Example: bspguy rename c1a0.bsp -old aaatrigger -new aaadigger\n" + ); + } else { logf("%s\n\n", g_version_string); logf( @@ -645,6 +672,7 @@ void print_help(string command) { " simplify : Simplify BSP models\n" " transform : Apply 3D transformations to the BSP\n" " unembed : Deletes embedded texture data\n" + " renametex : Renames/replaces a texture in the BSP\n" "\nRun 'bspguy help' to read about a specific command.\n" "\nTo launch the 3D editor. Drag and drop a .bsp file onto the executable,\n" @@ -756,6 +784,9 @@ int main(int argc, char* argv[]) else if (cli.command == "unembed") { return unembed(cli); } + else if (cli.command == "renametex") { + return rename_texture(cli); + } else { logf("unrecognized command: %d\n", cli.command.c_str()); }