Skip to content

Commit 1165b4c

Browse files
committed
add renametex command-line option
1 parent 7fbcbd7 commit 1165b4c

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ msvc
77
/imgui_177
88
/imgui_178
99
/merge
10+
/glew_x64
11+
/glfw_30
12+
/imgui_181
13+
/msvc_x86

src/bsp/Bsp.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -3287,6 +3287,27 @@ void Bsp::downscale_invalid_textures() {
32873287
logf("Downscaled %d textures\n", count);
32883288
}
32893289

3290+
bool Bsp::rename_texture(const char* oldName, const char* newName) {
3291+
if (strlen(newName) > 16) {
3292+
logf("ERROR: New texture name longer than 15 characters (%d)\n", strlen(newName));
3293+
return false;
3294+
}
3295+
3296+
for (int i = 0; i < textureCount; i++) {
3297+
int32_t texOffset = ((int32_t*)textures)[i + 1];
3298+
BSPMIPTEX& tex = *((BSPMIPTEX*)(textures + texOffset));
3299+
3300+
if (!strncmp(tex.szName, oldName, 16)) {
3301+
strncpy(tex.szName, newName, 16);
3302+
logf("Renamed texture '%s' -> '%s'\n", oldName, newName);
3303+
return true;
3304+
}
3305+
}
3306+
3307+
logf("No texture found with name '%s'\n", oldName);
3308+
return false;
3309+
}
3310+
32903311
set<int> Bsp::selectConnectedTexture(int modelId, int faceId) {
32913312
set<int> selected;
32923313
const float epsilon = 1.0f;

src/bsp/Bsp.h

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ class Bsp
222222

223223
bool downscale_texture(int textureId, int newWidth, int newHeight);
224224

225+
bool rename_texture(const char* oldName, const char* newName);
226+
225227
// updates texture coordinates after a texture has been downscaled
226228
void adjust_downscaled_texture_coordinates(int textureId, int oldWidth, int oldHeight);
227229

src/main.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,25 @@ int unembed(CommandLine& cli) {
519519
return 0;
520520
}
521521

522+
int rename_texture(CommandLine& cli) {
523+
Bsp* map = new Bsp(cli.bspfile);
524+
if (!map->valid)
525+
return 1;
526+
527+
string oldName = cli.getOption("-old");
528+
string newName = cli.getOption("-new");
529+
530+
map->rename_texture(oldName.c_str(), newName.c_str());
531+
532+
if (map->isValid()) map->write(cli.hasOption("-o") ? cli.getOption("-o") : map->path);
533+
logf("\n");
534+
535+
delete map;
536+
537+
return 0;
538+
}
539+
540+
522541
void print_help(string command) {
523542
if (command == "merge") {
524543
logf(
@@ -631,6 +650,14 @@ void print_help(string command) {
631650
"Example: bspguy unembed c1a0.bsp\n"
632651
);
633652
}
653+
else if (command == "renametex") {
654+
logf(
655+
"renametex - Renames a texture. This changes the texture if it's loaded from a WAD.\n\n"
656+
657+
"Usage: bspguy renametex <mapname> -old <oldname> -new <newname>\n"
658+
"Example: bspguy rename c1a0.bsp -old aaatrigger -new aaadigger\n"
659+
);
660+
}
634661
else {
635662
logf("%s\n\n", g_version_string);
636663
logf(
@@ -645,6 +672,7 @@ void print_help(string command) {
645672
" simplify : Simplify BSP models\n"
646673
" transform : Apply 3D transformations to the BSP\n"
647674
" unembed : Deletes embedded texture data\n"
675+
" renametex : Renames/replaces a texture in the BSP\n"
648676

649677
"\nRun 'bspguy <command> help' to read about a specific command.\n"
650678
"\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[])
756784
else if (cli.command == "unembed") {
757785
return unembed(cli);
758786
}
787+
else if (cli.command == "renametex") {
788+
return rename_texture(cli);
789+
}
759790
else {
760791
logf("unrecognized command: %d\n", cli.command.c_str());
761792
}

0 commit comments

Comments
 (0)