diff --git a/common/bspfile.cc b/common/bspfile.cc index a4c3f746..c5bb7398 100644 --- a/common/bspfile.cc +++ b/common/bspfile.cc @@ -1955,9 +1955,7 @@ void LoadBSPFile(fs::path &filename, bspdata_t *bspdata) while (xlumps-- > 0) { uint32_t ofs = LittleLong(xlump[xlumps].fileofs); uint32_t len = LittleLong(xlump[xlumps].filelen); - uint8_t *lumpdata = new uint8_t[len]; - memcpy(lumpdata, (const uint8_t *)file_data->data() + ofs, len); - bspdata->bspx.transfer(xlump[xlumps].lumpname.data(), lumpdata, len); + bspdata->bspx.transfer(xlump[xlumps].lumpname.data(), std::vector(file_data->begin() + ofs, file_data->begin() + ofs + len - 1)); } } else { if (memcmp(&bspx->id, "BSPX", 4)) @@ -2154,14 +2152,14 @@ public: static constexpr char pad[4]{}; bspx_lump_t &lump = xlumps.emplace_back(); - lump.filelen = x.second.lumpsize; + lump.filelen = x.second.size(); lump.fileofs = stream.tellp(); memcpy(lump.lumpname.data(), x.first.c_str(), std::min(x.first.size(), lump.lumpname.size() - 1)); - stream.write(reinterpret_cast(x.second.lumpdata.get()), x.second.lumpsize); + stream.write(reinterpret_cast(x.second.data()), x.second.size()); - if (x.second.lumpsize % 4) - stream.write(pad, 4 - (x.second.lumpsize % 4)); + if (x.second.size() % 4) + stream.write(pad, 4 - (x.second.size() % 4)); } stream.seekp(bspxheader); @@ -2305,6 +2303,6 @@ void PrintBSPFileSizes(const bspdata_t *bspdata) } for (auto &x : bspdata->bspx.entries) { - logging::print("{:7} {:<12} {:10}\n", "BSPX", x.first, x.second.lumpsize); + logging::print("{:7} {:<12} {:10}\n", "BSPX", x.first, x.second.size()); } } diff --git a/common/bspinfo.cc b/common/bspinfo.cc index 658a4550..0f8b0ca2 100644 --- a/common/bspinfo.cc +++ b/common/bspinfo.cc @@ -42,11 +42,11 @@ static std::string hex_string(const uint8_t *bytes, const size_t count) /** * returns a JSON array of models */ -static json serialize_bspxbrushlist(const bspxentry_t &lump) +static json serialize_bspxbrushlist(const std::vector &lump) { json j = json::array(); - memstream p(lump.lumpdata.get(), lump.lumpsize, std::ios_base::in | std::ios_base::binary); + memstream p(lump.data(), lump.size(), std::ios_base::in | std::ios_base::binary); p >> endianness; @@ -468,7 +468,7 @@ void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path & } else { // unhandled BSPX lump, just write the raw data entry["lumpdata"] = - hex_string(reinterpret_cast(lump.second.lumpdata.get()), lump.second.lumpsize); + hex_string(lump.second.data(), lump.second.size()); } } } diff --git a/include/common/bspfile.hh b/include/common/bspfile.hh index 76b659de..37ba6e1e 100644 --- a/include/common/bspfile.hh +++ b/include/common/bspfile.hh @@ -407,7 +407,7 @@ using texvecf = texvec; #include "bspfile_q2.hh" #include "bspxfile.hh" -using bspxentries_t = std::unordered_map; +using bspxentries_t = std::unordered_map>; struct bspdata_t { @@ -420,21 +420,17 @@ struct bspdata_t struct { bspxentries_t entries; - - // convenience function to transfer a generic pointer into - // the entries list - inline void transfer(const char *xname, uint8_t *&xdata, size_t xsize) + + // transfer ownership of the vector into a BSPX lump + inline void transfer(const char *xname, std::vector &xdata) { - entries.insert_or_assign(xname, bspxentry_t{xdata, xsize}); - xdata = nullptr; + entries.insert_or_assign(xname, std::move(xdata)); } - - // copies the data over to the BSP - void copy(const char *xname, const uint8_t *xdata, size_t xsize) + + // transfer ownership of the vector into a BSPX lump + inline void transfer(const char *xname, std::vector &&xdata) { - uint8_t *copy = new uint8_t[xsize]; - memcpy(copy, xdata, xsize); - transfer(xname, copy, xsize); + entries.insert_or_assign(xname, xdata); } } bspx; }; diff --git a/include/common/bspxfile.hh b/include/common/bspxfile.hh index c572acdc..e2c2cf43 100644 --- a/include/common/bspxfile.hh +++ b/include/common/bspxfile.hh @@ -89,15 +89,3 @@ struct bspxfacenormals_header }; // BSPX data - -struct bspxentry_t -{ - std::unique_ptr lumpdata; - size_t lumpsize; - - // bspxentry_t takes ownership over the pointer and will - // free it automatically. - inline bspxentry_t(void *lumpdata, size_t lumpsize) : lumpdata(reinterpret_cast(lumpdata)), lumpsize(lumpsize) - { - } -}; \ No newline at end of file diff --git a/include/light/light.hh b/include/light/light.hh index a8dbf210..a3e745ae 100644 --- a/include/light/light.hh +++ b/include/light/light.hh @@ -528,8 +528,8 @@ public: extern settings::light_settings options; extern uint8_t *filebase; -extern uint8_t *lit_filebase; -extern uint8_t *lux_filebase; +extern std::vector lit_filebase; +extern std::vector lux_filebase; extern std::vector extended_texinfo_flags; diff --git a/light/light.cc b/light/light.cc index 7b1e2f06..d8a31f0e 100644 --- a/light/light.cc +++ b/light/light.cc @@ -69,14 +69,14 @@ static int file_p; static int file_end; /// start of litfile data -uint8_t *lit_filebase; +std::vector lit_filebase; /// offset of start of free space after litfile data (should be kept a multiple of 12) static int lit_file_p; /// offset of end of space for litfile data static int lit_file_end; /// start of luxfile data -uint8_t *lux_filebase; +std::vector lux_filebase; /// offset of start of free space after luxfile data (should be kept a multiple of 12) static int lux_file_p; /// offset of end of space for luxfile data @@ -219,8 +219,8 @@ void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata, light_mutex.lock(); *lightdata = filebase + file_p; - *colordata = lit_filebase + lit_file_p; - *deluxdata = lux_filebase + lux_file_p; + *colordata = lit_filebase.data() + lit_file_p; + *deluxdata = lux_filebase.data() + lux_file_p; // if size isn't a multiple of 4, round up to the next multiple of 4 if ((size % 4) != 0) { @@ -253,11 +253,11 @@ void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata, *lightdata = filebase + lightofs; if (colordata) { - *colordata = lit_filebase + (lightofs * 3); + *colordata = lit_filebase.data() + (lightofs * 3); } if (deluxdata) { - *deluxdata = lux_filebase + (lightofs * 3); + *deluxdata = lux_filebase.data() + (lightofs * 3); } // NOTE: file_p et. al. are not updated, since we're not dynamically allocating the lightmaps @@ -424,8 +424,8 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) mbsp_t &bsp = std::get(bspdata->bsp); delete[] filebase; - delete[] lit_filebase; - delete[] lux_filebase; + lit_filebase.clear(); + lux_filebase.clear(); /* greyscale data stored in a separate buffer */ filebase = new uint8_t[MAX_MAP_LIGHTING]{}; @@ -435,16 +435,12 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) file_end = MAX_MAP_LIGHTING; /* litfile data stored in a separate buffer */ - lit_filebase = new uint8_t[MAX_MAP_LIGHTING * 3]{}; - if (!lit_filebase) - FError("allocation of {} bytes failed.", MAX_MAP_LIGHTING * 3); + lit_filebase.resize(MAX_MAP_LIGHTING * 3); lit_file_p = 0; lit_file_end = (MAX_MAP_LIGHTING * 3); /* lux data stored in a separate buffer */ - lux_filebase = new uint8_t[MAX_MAP_LIGHTING * 3]{}; - if (!lux_filebase) - FError("allocation of {} bytes failed.", MAX_MAP_LIGHTING * 3); + lux_filebase.resize(MAX_MAP_LIGHTING * 3); lux_file_p = 0; lux_file_end = (MAX_MAP_LIGHTING * 3); @@ -460,7 +456,7 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) if (lmshift_lump != bspdata->bspx.entries.end()) { for (int i = 0; i < bsp.dfaces.size(); i++) - faces_sup[i].lmscale = nth_bit(reinterpret_cast(lmshift_lump->second.lumpdata.get())[i]); + faces_sup[i].lmscale = nth_bit(reinterpret_cast(lmshift_lump->second.data())[i]); } else { for (int i = 0; i < bsp.dfaces.size(); i++) faces_sup[i].lmscale = modelinfo.at(0)->lightmapscale; @@ -505,7 +501,7 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) if (!options.litonly.value()) { if (bsp.loadversion->game->has_rgb_lightmap) { bsp.dlightdata.resize(lit_file_p); - memcpy(bsp.dlightdata.data(), lit_filebase, bsp.dlightdata.size()); + memcpy(bsp.dlightdata.data(), lit_filebase.data(), bsp.dlightdata.size()); } else { bsp.dlightdata.resize(file_p); memcpy(bsp.dlightdata.data(), filebase, bsp.dlightdata.size()); @@ -520,15 +516,17 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) bspdata->bspx.entries.erase("LMOFFSET"); if (faces_sup) { - uint8_t *styles = new uint8_t[4 * bsp.dfaces.size()]; - int32_t *offsets = new int32_t[bsp.dfaces.size()]; + std::vector styles(4 * bsp.dfaces.size()); + std::vector offsets_mem(bsp.dfaces.size() * sizeof(int32_t)); + memstream offsets(offsets_mem.data(), std::ios_base::out | std::ios_base::binary); + offsets << endianness; for (int i = 0; i < bsp.dfaces.size(); i++) { - offsets[i] = faces_sup[i].lightofs; + offsets <= faces_sup[i].lightofs; for (int j = 0; j < MAXLIGHTMAPS; j++) styles[i * 4 + j] = faces_sup[i].styles[j]; } - bspdata->bspx.transfer("LMSTYLE", styles, sizeof(*styles) * 4 * bsp.dfaces.size()); - bspdata->bspx.transfer("LMOFFSET", (uint8_t *&)offsets, sizeof(*offsets) * bsp.dfaces.size()); + bspdata->bspx.transfer("LMSTYLE", styles); + bspdata->bspx.transfer("LMOFFSET", offsets_mem); } } @@ -860,8 +858,8 @@ static inline void WriteNormals(const mbsp_t &bsp, bspdata_t &bspdata) } size_t data_size = sizeof(uint32_t) + (sizeof(qvec3f) * unique_normals.size()) + (sizeof(uint32_t) * num_normals); - uint8_t *data = new uint8_t[data_size]; - memstream stream(data, data_size); + std::vector data(data_size); + memstream stream(data.data(), data_size); stream << endianness; stream <= numeric_cast(unique_normals.size()); @@ -887,33 +885,7 @@ static inline void WriteNormals(const mbsp_t &bsp, bspdata_t &bspdata) logging::print(logging::flag::VERBOSE, "Compressed {} normals down to {}\n", num_normals, unique_normals.size()); - bspdata.bspx.transfer("FACENORMALS", data, data_size); - - ofstream obj("test.obj"); - - size_t index_id = 1; - - for (auto &face : bsp.dfaces) { - auto &cache = FaceCacheForFNum(&face - bsp.dfaces.data()); - - for (size_t i = 0; i < cache.points().size(); i++) { - auto &pt = cache.points()[i]; - auto &n = cache.normals()[i]; - - fmt::print(obj, "v {}\n", pt); - fmt::print(obj, "vn {}\n", n.normal); - } - - for (size_t i = 1; i < cache.points().size() - 1; i++) { - size_t n1 = 0; - size_t n2 = i; - size_t n3 = (i + 1) % cache.points().size(); - - fmt::print(obj, "f {0}//{0} {1}//{1} {2}//{2}\n", index_id + n1, index_id + n2, index_id + n3); - } - - index_id += cache.points().size(); - } + bspdata.bspx.transfer("FACENORMALS", data); } /* @@ -1035,13 +1007,15 @@ int light_main(int argc, const char **argv) WriteLitFile(&bsp, faces_sup, source, LIT_VERSION); } if (options.write_litfile & lightfile::bspx) { - bspdata.bspx.transfer("RGBLIGHTING", lit_filebase, bsp.dlightdata.size() * 3); + lit_filebase.resize(bsp.dlightdata.size() * 3); + bspdata.bspx.transfer("RGBLIGHTING", lit_filebase); } if (options.write_luxfile & lightfile::external) { WriteLuxFile(&bsp, source, LIT_VERSION); } if (options.write_luxfile & lightfile::bspx) { - bspdata.bspx.transfer("LIGHTINGDIR", lux_filebase, bsp.dlightdata.size() * 3); + lux_filebase.resize(bsp.dlightdata.size() * 3); + bspdata.bspx.transfer("LIGHTINGDIR", lux_filebase); } } diff --git a/light/litfile.cc b/light/litfile.cc index 1948649b..de9c2870 100644 --- a/light/litfile.cc +++ b/light/litfile.cc @@ -56,11 +56,11 @@ void WriteLitFile(const mbsp_t *bsp, facesup_t *facesup, const fs::path &filenam j++; litfile <= (uint8_t) j; } - litfile.write((const char *) lit_filebase, bsp->dlightdata.size() * 3); - litfile.write((const char *) lux_filebase, bsp->dlightdata.size() * 3); + litfile.write((const char *) lit_filebase.data(), bsp->dlightdata.size() * 3); + litfile.write((const char *) lux_filebase.data(), bsp->dlightdata.size() * 3); } else - litfile.write((const char *) lit_filebase, bsp->dlightdata.size() * 3); + litfile.write((const char *) lit_filebase.data(), bsp->dlightdata.size() * 3); } #include @@ -76,5 +76,5 @@ void WriteLuxFile(const mbsp_t *bsp, const fs::path &filename, int version) std::ofstream luxfile(luxname, std::ios_base::out | std::ios_base::binary); luxfile <= header.v1; - luxfile.write((const char *) lux_filebase, bsp->dlightdata.size() * 3); + luxfile.write((const char *) lux_filebase.data(), bsp->dlightdata.size() * 3); } diff --git a/light/ltface.cc b/light/ltface.cc index e0a5c099..a7c6706d 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -3226,7 +3226,7 @@ static void WriteLightmaps( // Q2/HL native colored lightmaps if (bsp->loadversion->game->has_rgb_lightmap) { - lightofs = lit - lit_filebase; + lightofs = lit - lit_filebase.data(); } else { lightofs = out - filebase; } diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index cd6be901..add5c7e5 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -390,10 +390,10 @@ static void WriteBSPFile() bspdata.version = &bspver_generic; if (map.needslmshifts) { - bspdata.bspx.copy("LMSHIFT", map.exported_lmshifts.data(), map.exported_lmshifts.size()); + bspdata.bspx.transfer("LMSHIFT", map.exported_lmshifts); } if (!map.exported_bspxbrushes.empty()) { - bspdata.bspx.copy("BRUSHLIST", map.exported_bspxbrushes.data(), map.exported_bspxbrushes.size()); + bspdata.bspx.transfer("BRUSHLIST", map.exported_bspxbrushes); } if (!ConvertBSPFormat(&bspdata, options.target_version)) {