Skip to content

Commit 042a53c

Browse files
Small update for fix loading
1 parent 01e35f6 commit 042a53c

25 files changed

+366
-509
lines changed

src/bsp/Bsp.cpp

Lines changed: 254 additions & 304 deletions
Large diffs are not rendered by default.

src/bsp/Bsp.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ class BspRenderer;
2323
#define OOB_CLIP_Z 16
2424
#define OOB_CLIP_Z_NEG 32
2525

26-
struct membuf : std::streambuf
27-
{
28-
membuf(char* begin, int len)
29-
{
30-
this->setg(begin, begin, begin + len);
31-
}
32-
};
33-
3426
struct LeafDebug
3527
{
3628
int leafIdx;
@@ -50,8 +42,8 @@ class Bsp
5042
BSPHEADER bsp_header;
5143
BSPHEADER_EX bsp_header_ex;
5244

53-
unsigned char** lumps;
54-
unsigned char** extralumps;
45+
std::vector<std::vector<unsigned char>> lumps;
46+
std::vector<std::vector<unsigned char>> extralumps;
5547

5648
std::vector<LIGHTMAP> undo_lightmaps;
5749

@@ -111,9 +103,6 @@ class Bsp
111103

112104
std::string bsp_path;
113105
std::string bsp_name;
114-
115-
bool replacedLump[32];
116-
117106
vec3 save_cam_pos, save_cam_angles;
118107

119108
bool bsp_valid;
@@ -413,7 +402,7 @@ class Bsp
413402
bool does_model_use_shared_structures(int modelIdx);
414403

415404
// returns the current lump contents
416-
LumpState duplicate_lumps(int targets);
405+
LumpState duplicate_lumps(unsigned int targets);
417406

418407
void replace_lumps(const LumpState& state);
419408

src/bsp/BspMerger.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ void BspMerger::update_map_series_entity_logic(Bsp* mergedMap, std::vector<MAPBL
990990
if (!noscript)
991991
{
992992
std::ofstream entFile(output_name + ".ent", std::ios::trunc);
993-
entFile.write((const char*)mergedMap->lumps[LUMP_ENTITIES], mergedMap->bsp_header.lump[LUMP_ENTITIES].nLength - 1);
993+
entFile.write((const char*)mergedMap->lumps[LUMP_ENTITIES].data(), mergedMap->bsp_header.lump[LUMP_ENTITIES].nLength - 1);
994994
}
995995
}
996996

@@ -1082,19 +1082,20 @@ bool BspMerger::merge(Bsp& mapA, Bsp& mapB, bool modelMerge)
10821082
continue; // always merge
10831083
}
10841084

1085-
if (!mapA.lumps[i] && !mapB.lumps[i])
1085+
if (!mapA.lumps[i].size() && !mapB.lumps[i].size())
10861086
{
10871087

10881088
}
1089-
else if (!mapA.lumps[i] && mapB.lumps[i])
1089+
else if (!mapA.lumps[i].size() && mapB.lumps[i].size())
10901090
{
10911091
if (!modelMerge)
10921092
{
10931093
print_log(get_localized_string(LANG_0237), g_lump_names[i]);
1094+
10941095
mapA.bsp_header.lump[i].nLength = mapB.bsp_header.lump[i].nLength;
1095-
mapA.lumps[i] = new unsigned char[mapB.bsp_header.lump[i].nLength];
1096-
memcpy(mapA.lumps[i], mapB.lumps[i], mapB.bsp_header.lump[i].nLength);
1096+
mapA.lumps[i] = mapB.lumps[i];
10971097

1098+
mapA.update_lump_pointers();
10981099
// process the lump here (TODO: faster to just copy wtv needs copying)
10991100
switch (i)
11001101
{
@@ -1103,7 +1104,7 @@ bool BspMerger::merge(Bsp& mapA, Bsp& mapB, bool modelMerge)
11031104
}
11041105
}
11051106
}
1106-
else if (!mapB.lumps[i])
1107+
else if (!mapB.lumps[i].size())
11071108
{
11081109
print_log(get_localized_string(LANG_0238), g_lump_names[i]);
11091110
}
@@ -1324,6 +1325,7 @@ void BspMerger::merge_planes(Bsp& mapA, Bsp& mapB)
13241325
memcpy(newPlanes, &mergedPlanes[0], newLen);
13251326

13261327
mapA.replace_lump(LUMP_PLANES, newPlanes, newLen);
1328+
delete[] newPlanes;
13271329
}
13281330

13291331
void BspMerger::merge_textures(Bsp& mapA, Bsp& mapB)
@@ -1413,16 +1415,14 @@ void BspMerger::merge_textures(Bsp& mapA, Bsp& mapB)
14131415

14141416
unsigned int texHeaderSize = (unsigned int)((newTexCount + 1) * sizeof(int));
14151417
unsigned int newLen = (unsigned int)((mipTexWritePtr - newMipTexData) + texHeaderSize);
1416-
unsigned char* newTextureData = new unsigned char[newLen];
1418+
unsigned char* newTextureData = new unsigned char[newLen + sizeof(int)];
14171419

14181420
// write texture lump header
14191421
unsigned int* texHeader = (unsigned int*)(newTextureData);
14201422
texHeader[0] = newTexCount;
14211423
for (unsigned int i = 0; i < newTexCount; i++)
14221424
{
1423-
if (i + 1 < newTexCount + 1) {
1424-
texHeader[i + 1] = (mipTexOffsets[i] == -1) ? -1 : mipTexOffsets[i] + texHeaderSize;
1425-
}
1425+
texHeader[i + 1] = (mipTexOffsets[i] == -1) ? -1 : mipTexOffsets[i] + texHeaderSize;
14261426
}
14271427

14281428
memcpy(newTextureData + texHeaderSize, newMipTexData, mipTexWritePtr - newMipTexData);
@@ -1433,6 +1433,7 @@ void BspMerger::merge_textures(Bsp& mapA, Bsp& mapB)
14331433
print_log("\n");
14341434

14351435
mapA.replace_lump(LUMP_TEXTURES, newTextureData, newLen);
1436+
delete[] newTextureData;
14361437
delete[] newMipTexData;
14371438
}
14381439

@@ -1451,6 +1452,7 @@ void BspMerger::merge_vertices(Bsp& mapA, Bsp& mapB)
14511452
g_progress.tick();
14521453

14531454
mapA.replace_lump(LUMP_VERTICES, newVerts, totalVertCount * sizeof(vec3));
1455+
delete[] newVerts;
14541456
}
14551457

14561458
void BspMerger::merge_texinfo(Bsp& mapA, Bsp& mapB)
@@ -1505,6 +1507,7 @@ void BspMerger::merge_texinfo(Bsp& mapA, Bsp& mapB)
15051507
print_log("\n");
15061508

15071509
mapA.replace_lump(LUMP_TEXINFO, newTexinfoData, newLen);
1510+
delete[] newTexinfoData;
15081511
}
15091512

15101513
void BspMerger::merge_faces(Bsp& mapA, Bsp& mapB)
@@ -1568,14 +1571,15 @@ void BspMerger::merge_faces(Bsp& mapA, Bsp& mapB)
15681571
}
15691572

15701573
mapA.replace_lump(LUMP_FACES, newFaces, totalFaceCount * sizeof(BSPFACE32));
1574+
delete[] newFaces;
15711575
}
15721576

15731577
void BspMerger::merge_leaves(Bsp& mapA, Bsp& mapB)
15741578
{
15751579
thisLeafCount = mapA.bsp_header.lump[LUMP_LEAVES].nLength / sizeof(BSPLEAF32);
15761580
otherLeafCount = mapB.bsp_header.lump[LUMP_LEAVES].nLength / sizeof(BSPLEAF32);
15771581

1578-
int tthisWorldLeafCount = ((BSPMODEL*)mapA.lumps[LUMP_MODELS])->nVisLeafs + 1; // include solid leaf
1582+
int tthisWorldLeafCount = ((BSPMODEL*)mapA.lumps[LUMP_MODELS].data())->nVisLeafs + 1; // include solid leaf
15791583

15801584
g_progress.update("Merging leaves", thisLeafCount + otherLeafCount);
15811585

@@ -1628,6 +1632,7 @@ void BspMerger::merge_leaves(Bsp& mapA, Bsp& mapB)
16281632
memcpy(newLeavesData, &mergedLeaves[0], newLen);
16291633

16301634
mapA.replace_lump(LUMP_LEAVES, newLeavesData, newLen);
1635+
delete[] newLeavesData;
16311636
}
16321637

16331638
void BspMerger::merge_marksurfs(Bsp& mapA, Bsp& mapB)
@@ -1665,6 +1670,7 @@ void BspMerger::merge_marksurfs(Bsp& mapA, Bsp& mapB)
16651670
}
16661671

16671672
mapA.replace_lump(LUMP_MARKSURFACES, newSurfs, totalSurfCount * sizeof(int));
1673+
delete[] newSurfs;
16681674
}
16691675

16701676
void BspMerger::merge_edges(Bsp& mapA, Bsp& mapB)
@@ -1688,6 +1694,7 @@ void BspMerger::merge_edges(Bsp& mapA, Bsp& mapB)
16881694
}
16891695

16901696
mapA.replace_lump(LUMP_EDGES, newEdges, totalEdgeCount * sizeof(BSPEDGE32));
1697+
delete[] newEdges;
16911698
}
16921699

16931700
void BspMerger::merge_surfedges(Bsp& mapA, Bsp& mapB)
@@ -1710,6 +1717,7 @@ void BspMerger::merge_surfedges(Bsp& mapA, Bsp& mapB)
17101717
}
17111718

17121719
mapA.replace_lump(LUMP_SURFEDGES, newSurfs, totalSurfCount * sizeof(int));
1720+
delete[] newSurfs;
17131721
}
17141722

17151723
void BspMerger::merge_nodes(Bsp& mapA, Bsp& mapB)
@@ -1779,6 +1787,7 @@ void BspMerger::merge_nodes(Bsp& mapA, Bsp& mapB)
17791787
memcpy(newNodeData, &mergedNodes[0], newLen);
17801788

17811789
mapA.replace_lump(LUMP_NODES, newNodeData, newLen);
1790+
delete[] newNodeData;
17821791
}
17831792

17841793
void BspMerger::merge_clipnodes(Bsp& mapA, Bsp& mapB)
@@ -1829,6 +1838,7 @@ void BspMerger::merge_clipnodes(Bsp& mapA, Bsp& mapB)
18291838
memcpy(newClipnodeData, &mergedNodes[0], newLen);
18301839

18311840
mapA.replace_lump(LUMP_CLIPNODES, newClipnodeData, newLen);
1841+
delete[] newClipnodeData;
18321842
}
18331843

18341844
void BspMerger::merge_models(Bsp& mapA, Bsp& mapB)
@@ -1897,6 +1907,7 @@ void BspMerger::merge_models(Bsp& mapA, Bsp& mapB)
18971907
memcpy(newModelData, &mergedModels[0], newLen);
18981908

18991909
mapA.replace_lump(LUMP_MODELS, newModelData, newLen);
1910+
delete[] newModelData;
19001911
}
19011912

19021913
void BspMerger::merge_vis(Bsp& mapA, Bsp& mapB)
@@ -1966,6 +1977,7 @@ void BspMerger::merge_vis(Bsp& mapA, Bsp& mapB)
19661977
print_log(get_localized_string(LANG_0244), oldLen, newVisLen);
19671978
print_log("\n");
19681979

1980+
delete[] compressedVisResize;
19691981
delete[] decompressedVis;
19701982
delete[] compressedVis;
19711983
}
@@ -1989,9 +2001,9 @@ void BspMerger::merge_lighting(Bsp& mapA, Bsp& mapB)
19892001
thisColorCount = g_limits.maxSurfaceExtent * g_limits.maxSurfaceExtent;
19902002
totalColorCount += thisColorCount;
19912003
int sz = thisColorCount * sizeof(COLOR3);
1992-
mapA.lumps[LUMP_LIGHTING] = new unsigned char[sz];
2004+
mapA.lumps[LUMP_LIGHTING].resize(sz);
19932005
mapA.bsp_header.lump[LUMP_LIGHTING].nLength = sz;
1994-
thisRad = (COLOR3*)mapA.lumps[LUMP_LIGHTING];
2006+
thisRad = (COLOR3*)mapA.lumps[LUMP_LIGHTING].data();
19952007

19962008
memset(thisRad, 255, sz);
19972009

@@ -2030,7 +2042,7 @@ void BspMerger::merge_lighting(Bsp& mapA, Bsp& mapB)
20302042

20312043
g_progress.tick();
20322044
mapA.replace_lump(LUMP_LIGHTING, newRad, totalColorCount * sizeof(COLOR3));
2033-
2045+
delete[] newRad;
20342046
for (int i = thisWorldFaceCount; i < thisWorldFaceCount + otherFaceCount; i++)
20352047
{
20362048
if (mapA.faces[i].nLightmapOffset >= 0)
@@ -2067,7 +2079,7 @@ void BspMerger::create_merge_headnodes(Bsp& mapA, Bsp& mapB, BSPPLANE separation
20672079
memcpy(newThisPlanes, mapA.planes, mapA.planeCount * sizeof(BSPPLANE));
20682080
newThisPlanes[mapA.planeCount] = separationPlane;
20692081
mapA.replace_lump(LUMP_PLANES, newThisPlanes, (mapA.planeCount + 1) * sizeof(BSPPLANE));
2070-
2082+
delete[] newThisPlanes;
20712083
int separationPlaneIdx = mapA.planeCount - 1;
20722084

20732085

@@ -2094,6 +2106,7 @@ void BspMerger::create_merge_headnodes(Bsp& mapA, Bsp& mapB, BSPPLANE separation
20942106
newThisNodes[0] = headNode;
20952107

20962108
mapA.replace_lump(LUMP_NODES, newThisNodes, (mapA.nodeCount + 1) * sizeof(BSPNODE32));
2109+
delete[] newThisNodes;
20972110
}
20982111

20992112

@@ -2130,7 +2143,6 @@ void BspMerger::create_merge_headnodes(Bsp& mapA, Bsp& mapB, BSPPLANE separation
21302143

21312144
std::vector<BSPCLIPNODE32> newThisClipNodes(newHeadNodes.begin(), newHeadNodes.end());
21322145
newThisClipNodes.insert(newThisClipNodes.end(), mapA.clipnodes, mapA.clipnodes + mapA.clipnodeCount);
2133-
21342146
mapA.replace_lump(LUMP_CLIPNODES, newThisClipNodes.data(), newThisClipNodes.size() * sizeof(BSPCLIPNODE32));
21352147
}
21362148
}

src/bsp/remap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ STRUCTCOUNT::STRUCTCOUNT(Bsp* map)
2929
clipnodes = map->bsp_header.lump[LUMP_CLIPNODES].nLength / sizeof(BSPCLIPNODE32);
3030
verts = map->bsp_header.lump[LUMP_VERTICES].nLength / sizeof(vec3);
3131
faces = map->bsp_header.lump[LUMP_FACES].nLength / sizeof(BSPFACE32);
32-
textures = *((int*)(map->lumps[LUMP_TEXTURES]));
32+
textures = *((int*)(map->lumps[LUMP_TEXTURES].data()));
3333
markSurfs = map->bsp_header.lump[LUMP_MARKSURFACES].nLength / sizeof(int);
3434
surfEdges = map->bsp_header.lump[LUMP_SURFEDGES].nLength / sizeof(int);
3535
edges = map->bsp_header.lump[LUMP_EDGES].nLength / sizeof(BSPEDGE32);

0 commit comments

Comments
 (0)