Skip to content

Commit

Permalink
add renametex command-line option
Browse files Browse the repository at this point in the history
  • Loading branch information
wootguy committed Sep 26, 2024
1 parent 7fbcbd7 commit 1165b4c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ msvc
/imgui_177
/imgui_178
/merge
/glew_x64
/glfw_30
/imgui_181
/msvc_x86
21 changes: 21 additions & 0 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> Bsp::selectConnectedTexture(int modelId, int faceId) {
set<int> selected;
const float epsilon = 1.0f;
Expand Down
2 changes: 2 additions & 0 deletions src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
31 changes: 31 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 <mapname> -old <oldname> -new <newname>\n"
"Example: bspguy rename c1a0.bsp -old aaatrigger -new aaadigger\n"
);
}
else {
logf("%s\n\n", g_version_string);
logf(
Expand All @@ -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 <command> help' to read about a specific command.\n"
"\nTo launch the 3D editor. Drag and drop a .bsp file onto the executable,\n"
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 1165b4c

Please sign in to comment.