diff options
| author | Shauren <shauren.trinity@gmail.com> | 2024-03-01 16:05:34 +0100 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2024-03-01 16:05:34 +0100 |
| commit | 67244a1f70273e20801b2ae668603af81d72d602 (patch) | |
| tree | 5aa9435c15d66e2bef10c1216c97570bc3200507 /src/tools | |
| parent | 0e01b72df87d9bff4675df710b8bbd57254beb7e (diff) | |
Core/Misc: Replace sprintf with safer alternatives (Trinity::StringFormat or snprintf)
Diffstat (limited to 'src/tools')
| -rw-r--r-- | src/tools/mmaps_generator/IntermediateValues.cpp | 81 | ||||
| -rw-r--r-- | src/tools/mmaps_generator/MapBuilder.cpp | 23 | ||||
| -rw-r--r-- | src/tools/mmaps_generator/TerrainBuilder.cpp | 12 | ||||
| -rw-r--r-- | src/tools/vmap4_extractor/model.cpp | 36 | ||||
| -rw-r--r-- | src/tools/vmap4_extractor/vmapexport.cpp | 11 | ||||
| -rw-r--r-- | src/tools/vmap4_extractor/wmo.cpp | 7 |
6 files changed, 68 insertions, 102 deletions
diff --git a/src/tools/mmaps_generator/IntermediateValues.cpp b/src/tools/mmaps_generator/IntermediateValues.cpp index 10e2bff30f0..068ae951c46 100644 --- a/src/tools/mmaps_generator/IntermediateValues.cpp +++ b/src/tools/mmaps_generator/IntermediateValues.cpp @@ -16,6 +16,7 @@ */ #include "IntermediateValues.h" +#include "StringFormat.h" namespace MMAP { @@ -30,42 +31,32 @@ namespace MMAP void IntermediateValues::writeIV(uint32 mapID, uint32 tileX, uint32 tileY) { - char fileName[255]; - char tileString[25]; - sprintf(tileString, "[%02u,%02u]: ", tileX, tileY); - - printf("%sWriting debug output... \r", tileString); - - std::string name("meshes/%04u%02i%02i."); - -#define DEBUG_WRITE(fileExtension,data) \ - do { \ - sprintf(fileName, (name + fileExtension).c_str(), mapID, tileY, tileX); \ - FILE* file = fopen(fileName, "wb"); \ - if (!file) \ - { \ - char message[1024]; \ - sprintf(message, "%sFailed to open %s for writing!\n", tileString, fileName); \ - perror(message); \ - } \ - else \ - debugWrite(file, data); \ - if (file) fclose(file); \ - printf("%sWriting debug output... \r", tileString); \ - } while (false) + std::string tileString = Trinity::StringFormat("[{:02},{:02}]: ", tileX, tileY); + + printf("%sWriting debug output... \r", tileString.c_str()); + + auto debugWrite = [&](char const* extension, auto const* data) + { + std::string fileName = Trinity::StringFormat("meshes/{:04}{:02}{:02}.{}", mapID, tileY, tileX, extension); + if (FILE* file = fopen(fileName.c_str(), "wb")) + { + this->debugWrite(file, data); + fclose(file); + } + else + perror(Trinity::StringFormat("{}Failed to open {} for writing!\n", tileString, fileName).c_str()); + }; if (heightfield) - DEBUG_WRITE("hf", heightfield); + debugWrite("hf", heightfield); if (compactHeightfield) - DEBUG_WRITE("chf", compactHeightfield); + debugWrite("chf", compactHeightfield); if (contours) - DEBUG_WRITE("cs", contours); + debugWrite("cs", contours); if (polyMesh) - DEBUG_WRITE("pmesh", polyMesh); + debugWrite("pmesh", polyMesh); if (polyMeshDetail) - DEBUG_WRITE("dmesh", polyMeshDetail); - -#undef DEBUG_WRITE + debugWrite("dmesh", polyMeshDetail); } void IntermediateValues::debugWrite(FILE* file, rcHeightfield const* mesh) @@ -200,15 +191,13 @@ namespace MMAP void IntermediateValues::generateObjFile(uint32 mapID, uint32 tileX, uint32 tileY, MeshData &meshData) { - char objFileName[255]; - sprintf(objFileName, "meshes/map%04u%02u%02u.obj", mapID, tileY, tileX); + std::string objFileName; + objFileName = Trinity::StringFormat("meshes/map{:04}{:02}{:02}.obj", mapID, tileY, tileX); - FILE* objFile = fopen(objFileName, "wb"); + FILE* objFile = fopen(objFileName.c_str(), "wb"); if (!objFile) { - char message[1024]; - sprintf(message, "Failed to open %s for writing!\n", objFileName); - perror(message); + perror(Trinity::StringFormat("Failed to open {} for writing!\n", objFileName).c_str()); return; } @@ -233,18 +222,14 @@ namespace MMAP fclose(objFile); - char tileString[25]; - sprintf(tileString, "[%02u,%02u]: ", tileY, tileX); - printf("%sWriting debug output... \r", tileString); + printf("[%02u,%02u]: Writing debug output... \r", tileY, tileX); - sprintf(objFileName, "meshes/%04u.map", mapID); + objFileName = Trinity::StringFormat("meshes/map{:04}.map", mapID); - objFile = fopen(objFileName, "wb"); + objFile = fopen(objFileName.c_str(), "wb"); if (!objFile) { - char message[1024]; - sprintf(message, "Failed to open %s for writing!\n", objFileName); - perror(message); + perror(Trinity::StringFormat("Failed to open {} for writing!\n", objFileName).c_str()); return; } @@ -252,13 +237,11 @@ namespace MMAP fwrite(&b, sizeof(char), 1, objFile); fclose(objFile); - sprintf(objFileName, "meshes/%04u%02u%02u.mesh", mapID, tileY, tileX); - objFile = fopen(objFileName, "wb"); + objFileName = Trinity::StringFormat("meshes/map{:04}{:02}{:02}.mesh", mapID, tileY, tileX); + objFile = fopen(objFileName.c_str(), "wb"); if (!objFile) { - char message[1024]; - sprintf(message, "Failed to open %s for writing!\n", objFileName); - perror(message); + perror(Trinity::StringFormat("Failed to open {} for writing!\n", objFileName).c_str()); return; } diff --git a/src/tools/mmaps_generator/MapBuilder.cpp b/src/tools/mmaps_generator/MapBuilder.cpp index 778377e2af1..7af217acc9b 100644 --- a/src/tools/mmaps_generator/MapBuilder.cpp +++ b/src/tools/mmaps_generator/MapBuilder.cpp @@ -615,17 +615,14 @@ namespace MMAP return; } - char fileName[25]; - sprintf(fileName, "mmaps/%04u.mmap", mapID); + std::string fileName = Trinity::StringFormat("mmaps/{:04}.mmap", mapID); - FILE* file = fopen(fileName, "wb"); + FILE* file = fopen(fileName.c_str(), "wb"); if (!file) { dtFreeNavMesh(navMesh); navMesh = nullptr; - char message[1024]; - sprintf(message, "[Map %04u] Failed to open %s for writing!\n", mapID, fileName); - perror(message); + perror(Trinity::StringFormat("[Map {:04}] Failed to open {} for writing!\n", mapID, fileName).c_str()); return; } @@ -929,14 +926,11 @@ namespace MMAP } // file output - char fileName[255]; - sprintf(fileName, "mmaps/%04u%02i%02i.mmtile", mapID, tileY, tileX); - FILE* file = fopen(fileName, "wb"); + std::string fileName = Trinity::StringFormat("mmaps/{:04}{:02}{:02}.mmtile", mapID, tileY, tileX); + FILE* file = fopen(fileName.c_str(), "wb"); if (!file) { - char message[1024]; - sprintf(message, "[Map %04u] Failed to open %s for writing!\n", mapID, fileName); - perror(message); + perror(Trinity::StringFormat("[Map {:04}] Failed to open {} for writing!\n", mapID, fileName).c_str()); navMesh->removeTile(tileRef, nullptr, nullptr); break; } @@ -1073,9 +1067,8 @@ namespace MMAP /**************************************************************************/ bool TileBuilder::shouldSkipTile(uint32 mapID, uint32 tileX, uint32 tileY) const { - char fileName[255]; - sprintf(fileName, "mmaps/%04u%02i%02i.mmtile", mapID, tileY, tileX); - FILE* file = fopen(fileName, "rb"); + std::string fileName = Trinity::StringFormat("mmaps/{:04}{:02}{:02}.mmtile", mapID, tileY, tileX); + FILE* file = fopen(fileName.c_str(), "rb"); if (!file) return false; diff --git a/src/tools/mmaps_generator/TerrainBuilder.cpp b/src/tools/mmaps_generator/TerrainBuilder.cpp index fd2a6d862fa..e501af2099c 100644 --- a/src/tools/mmaps_generator/TerrainBuilder.cpp +++ b/src/tools/mmaps_generator/TerrainBuilder.cpp @@ -21,6 +21,7 @@ #include "MapTree.h" #include "MMapDefines.h" #include "ModelInstance.h" +#include "StringFormat.h" #include "Util.h" #include "VMapManager2.h" #include <map> @@ -78,17 +79,16 @@ namespace MMAP /**************************************************************************/ bool TerrainBuilder::loadMap(uint32 mapID, uint32 tileX, uint32 tileY, MeshData &meshData, Spot portion) { - char mapFileName[255]; - sprintf(mapFileName, "maps/%04u_%02u_%02u.map", mapID, tileY, tileX); + std::string mapFileName = Trinity::StringFormat("maps/{:04}_{:02}_{:02}.map", mapID, tileY, tileX); - FILE* mapFile = fopen(mapFileName, "rb"); + FILE* mapFile = fopen(mapFileName.c_str(), "rb"); if (!mapFile) { int32 parentMapId = sMapStore[mapID].ParentMapID; while (!mapFile && parentMapId != -1) { - sprintf(mapFileName, "maps/%04d_%02u_%02u.map", parentMapId, tileY, tileX); - mapFile = fopen(mapFileName, "rb"); + mapFileName = Trinity::StringFormat("maps/{:04}_{:02}_{:02}.map", parentMapId, tileY, tileX); + mapFile = fopen(mapFileName.c_str(), "rb"); parentMapId = sMapStore[parentMapId].ParentMapID; } } @@ -101,7 +101,7 @@ namespace MMAP fheader.versionMagic != MapVersionMagic) { fclose(mapFile); - printf("%s is the wrong version, please extract new .map files\n", mapFileName); + printf("%s is the wrong version, please extract new .map files\n", mapFileName.c_str()); return false; } diff --git a/src/tools/vmap4_extractor/model.cpp b/src/tools/vmap4_extractor/model.cpp index a2608c6c6e7..9c8d3f5ac6b 100644 --- a/src/tools/vmap4_extractor/model.cpp +++ b/src/tools/vmap4_extractor/model.cpp @@ -18,6 +18,7 @@ #include "vmapexport.h" #include "Errors.h" #include "model.h" +#include "StringFormat.h" #include "wmo.h" #include "adtfile.h" #include "cascfile.h" @@ -155,9 +156,8 @@ Vec3D fixCoordSystem(Vec3D const& v) void Doodad::Extract(ADT::MDDF const& doodadDef, char const* ModelInstName, uint32 mapID, uint32 originalMapId, FILE* pDirfile, std::vector<ADTOutputCache>* dirfileCache) { - char tempname[1036]; - sprintf(tempname, "%s/%s", szWorkDirWmo, ModelInstName); - FILE* input = fopen(tempname, "r+b"); + std::string tempname = Trinity::StringFormat("{}/{}", szWorkDirWmo, ModelInstName); + FILE* input = fopen(tempname.c_str(), "r+b"); if (!input) return; @@ -243,29 +243,21 @@ void Doodad::ExtractSet(WMODoodadData const& doodadData, ADT::MODF const& wmo, b WMO::MODD const& doodad = doodadData.Spawns[doodadIndex]; - char ModelInstName[1024]; + std::string ModelInstName; if (doodadData.Paths) - sprintf(ModelInstName, "%s", GetPlainName(&doodadData.Paths[doodad.NameIndex])); + ModelInstName = GetPlainName(&doodadData.Paths[doodad.NameIndex]); else if (doodadData.FileDataIds) - sprintf(ModelInstName, "FILE%08X.xxx", doodadData.FileDataIds[doodad.NameIndex]); + ModelInstName = Trinity::StringFormat("FILE{:08X}.xxx", doodadData.FileDataIds[doodad.NameIndex]); else ASSERT(false); - uint32 nlen = strlen(ModelInstName); - NormalizeFileName(ModelInstName, nlen); - if (nlen > 3) - { - char const* extension = &ModelInstName[nlen - 4]; - if (!strcmp(extension, ".mdx") || !strcmp(extension, ".mdl")) - { - ModelInstName[nlen - 2] = '2'; - ModelInstName[nlen - 1] = '\0'; - } - } + uint32 nlen = ModelInstName.length(); + NormalizeFileName(ModelInstName.data(), nlen); + if (ModelInstName.ends_with(".mdx") || ModelInstName.ends_with(".mdl")) + ModelInstName.replace(ModelInstName.length() - 2, 2, "2"); - char tempname[1036]; - sprintf(tempname, "%s/%s", szWorkDirWmo, ModelInstName); - FILE* input = fopen(tempname, "r+b"); + std::string tempname = Trinity::StringFormat("{}/{}", szWorkDirWmo, ModelInstName); + FILE* input = fopen(tempname.c_str(), "r+b"); if (!input) continue; @@ -306,7 +298,7 @@ void Doodad::ExtractSet(WMODoodadData const& doodadData, ADT::MODF const& wmo, b fwrite(&rotation, sizeof(Vec3D), 1, pDirfile); fwrite(&doodad.Scale, sizeof(float), 1, pDirfile); fwrite(&nlen, sizeof(uint32), 1, pDirfile); - fwrite(ModelInstName, sizeof(char), nlen, pDirfile); + fwrite(ModelInstName.c_str(), sizeof(char), nlen, pDirfile); if (dirfileCache) { @@ -329,7 +321,7 @@ void Doodad::ExtractSet(WMODoodadData const& doodadData, ADT::MODF const& wmo, b CACHE_WRITE(&rotation, sizeof(Vec3D), 1, cacheData); CACHE_WRITE(&doodad.Scale, sizeof(float), 1, cacheData); CACHE_WRITE(&nlen, sizeof(uint32), 1, cacheData); - CACHE_WRITE(ModelInstName, sizeof(char), nlen, cacheData); + CACHE_WRITE(ModelInstName.c_str(), sizeof(char), nlen, cacheData); } } }; diff --git a/src/tools/vmap4_extractor/vmapexport.cpp b/src/tools/vmap4_extractor/vmapexport.cpp index 503170ec7f1..759750bcfd0 100644 --- a/src/tools/vmap4_extractor/vmapexport.cpp +++ b/src/tools/vmap4_extractor/vmapexport.cpp @@ -187,12 +187,11 @@ bool ExtractSingleWmo(std::string& fname) // Copy files from archive std::string originalName = fname; - char szLocalFile[1024]; char* plain_name = GetPlainName(&fname[0]); NormalizeFileName(plain_name, strlen(plain_name)); - sprintf(szLocalFile, "%s/%s", szWorkDirWmo, plain_name); + std::string szLocalFile = Trinity::StringFormat("{}/{}", szWorkDirWmo, plain_name); - if (FileExists(szLocalFile)) + if (FileExists(szLocalFile.c_str())) return true; int p = 0; @@ -213,10 +212,10 @@ bool ExtractSingleWmo(std::string& fname) printf("Couldn't open RootWmo!!!\n"); return true; } - FILE *output = fopen(szLocalFile,"wb"); + FILE *output = fopen(szLocalFile.c_str(),"wb"); if(!output) { - printf("couldn't open %s for writing!\n", szLocalFile); + printf("couldn't open %s for writing!\n", szLocalFile.c_str()); return false; } froot.ConvertToVMAPRootWmo(output); @@ -262,7 +261,7 @@ bool ExtractSingleWmo(std::string& fname) // Delete the extracted file in the case of an error if (!file_ok) - remove(szLocalFile); + remove(szLocalFile.c_str()); return true; } diff --git a/src/tools/vmap4_extractor/wmo.cpp b/src/tools/vmap4_extractor/wmo.cpp index c8621d08da7..555559cfb95 100644 --- a/src/tools/vmap4_extractor/wmo.cpp +++ b/src/tools/vmap4_extractor/wmo.cpp @@ -586,13 +586,12 @@ void MapObject::Extract(ADT::MODF const& mapObjDef, char const* WmoInstName, boo //-----------add_in _dir_file---------------- - char tempname[512]; - sprintf(tempname, "%s/%s", szWorkDirWmo, WmoInstName); - FILE* input = fopen(tempname, "r+b"); + std::string tempname = Trinity::StringFormat("{}/{}", szWorkDirWmo, WmoInstName); + FILE* input = fopen(tempname.c_str(), "r+b"); if (!input) { - printf("WMOInstance::WMOInstance: couldn't open %s\n", tempname); + printf("WMOInstance::WMOInstance: couldn't open %s\n", tempname.c_str()); return; } |
