Skip to content

Commit

Permalink
Big big undo/redo upgrade!
Browse files Browse the repository at this point in the history
Big big undo/redo upgrade!  And bugfixes.
  • Loading branch information
UnrealKaraulov committed Nov 30, 2023
1 parent d68ea48 commit c2fbb0b
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 336 deletions.
63 changes: 34 additions & 29 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Bsp::Bsp()
is_blue_shift = false;
is_colored_lightmap = true;

extralumps = NULL;

force_skip_crc = false;

bsp_header = BSPHEADER();
Expand Down Expand Up @@ -1450,19 +1452,15 @@ bool Bsp::does_model_use_shared_structures(int modelIdx)

LumpState Bsp::duplicate_lumps(int targets)
{
LumpState state = LumpState();
LumpState state{};

for (int i = 0; i < HEADER_LUMPS; i++)
{
if ((targets & (1 << i)) == 0)
{
state.lumps[i] = NULL;
state.lumpLen[i] = 0;
continue;
}
state.lumps[i] = new unsigned char[bsp_header.lump[i].nLength];
state.lumpLen[i] = bsp_header.lump[i].nLength;
memcpy(state.lumps[i], lumps[i], bsp_header.lump[i].nLength);
state.lumps[i] = std::vector<unsigned char>(lumps[i], lumps[i] + bsp_header.lump[i].nLength);
}

return state;
Expand Down Expand Up @@ -1515,26 +1513,19 @@ int Bsp::delete_embedded_textures()
return numRemoved;
}

void Bsp::replace_lumps(LumpState& state)
void Bsp::replace_lumps(const LumpState& state)
{
for (unsigned int i = 0; i < HEADER_LUMPS; i++)
{
if (!state.lumps[i])
if (state.lumps[i].size())
{
continue;
}

delete[] lumps[i];
lumps[i] = new unsigned char[state.lumpLen[i]];
memcpy(lumps[i], state.lumps[i], state.lumpLen[i]);
bsp_header.lump[i].nLength = state.lumpLen[i];

if (i == LUMP_ENTITIES)
{
load_ents();
unsigned char * tmplump = new unsigned char[state.lumps[i].size()];
std::copy(state.lumps[i].begin(), state.lumps[i].end(), tmplump);
replace_lump(i, tmplump, state.lumps[i].size());
}
}

load_ents();
update_lump_pointers();
}

Expand Down Expand Up @@ -2715,8 +2706,11 @@ void Bsp::update_ent_lump(bool stripNodes)
std::string str_data = ent_data.str();

unsigned char* newEntData = new unsigned char[str_data.size() + 1];
memcpy(newEntData, str_data.c_str(), str_data.size());
newEntData[str_data.size()] = 0; // null terminator required too(?)

if (str_data.size())
memcpy(newEntData, str_data.c_str(), str_data.size());

newEntData[str_data.size()] = 0;

replace_lump(LUMP_ENTITIES, newEntData, str_data.size() + 1);
}
Expand Down Expand Up @@ -4231,13 +4225,18 @@ bool Bsp::validate()
int texlen = getBspTextureSize(i);
int dataOffset = (textureCount + 1) * sizeof(int);
BSPMIPTEX* tex = (BSPMIPTEX*)(textures + texOffset);
if (tex->szName[0] == '\0' || strlen(tex->szName) >= MAXTEXTURENAME)
if (tex->szName[0] == '\0')
{
logf("Warning: invalid texture name in {} texture.\n", i);
}
else if (strlen(tex->szName) >= MAXTEXTURENAME)
{
logf("Error: found memory leak in texture->name of {} texture.\n", i);
}
if (tex->nOffsets[0] > 0 && dataOffset + texOffset + texlen > bsp_header.lump[LUMP_TEXTURES].nLength)
{
logf("Warning: texture data buffer overrun in {} texture.\n", i);
logf("Warning: texture data buffer overrun in {} texture. {} > {}.\n", i, dataOffset + texOffset + texlen, bsp_header.lump[LUMP_TEXTURES].nLength);
logf("Tex size {}. Tex name \"{}\". Tex offset {}. Data offset {}. \n", i, tex->szName[0] != '\0' ? tex->szName : "UNKNOWN_NAME", texOffset, dataOffset);
}
}
}
Expand Down Expand Up @@ -5362,18 +5361,18 @@ int Bsp::add_texture(WADTEX* tex)
logf("Wad texture with name {} found in map.\n", tex->szName);
if (oldtex->nWidth != tex->nWidth || oldtex->nHeight != tex->nHeight)
{
oldtex->szName[0] = '\0';
logf("Warning! Texture size different {}x{} > {}x{}.\nRenaming old texture and create new one.\n",
oldtex->nWidth, oldtex->nHeight, tex->nWidth, tex->nHeight);
oldtex->nOffsets[0] = oldtex->nOffsets[1] = oldtex->nOffsets[2] =
oldtex->nOffsets[3] = 0;
oldtex->szName[0] = '\0';
}
else
{
logf("Warning! Wad texture with same name found in map.\nNeed replace by new texture.\n");
oldtex->nOffsets[0] = oldtex->nOffsets[1] = oldtex->nOffsets[2] =
oldtex->nOffsets[3] = 0;
oldtex->szName[0] = '\0';
logf("Warning! Wad texture with same name found in map.\nNeed replace by new texture.\n");
}
}
}
Expand All @@ -5395,14 +5394,20 @@ int Bsp::add_texture(WADTEX* tex)

if (oldtex && oldtexid >= 0)
{
int tex_usage = 0;
for (int i = 0; i < texinfoCount; i++)
{
BSPTEXTUREINFO& texinfo = texinfos[i];
if (texinfo.iMiptex == oldtexid)
{
texinfo.iMiptex = textureCount;
tex_usage++;
}
}
if (tex_usage > 0)
{
logf("Replaced {} texture indices in map.\n", tex_usage);
}
}

int w = tex->nWidth;
Expand Down Expand Up @@ -5949,12 +5954,14 @@ int Bsp::create_clipnode_box(const vec3& mins, const vec3& maxs, BSPMODEL* targe

BSPPLANE* newPlanes = new BSPPLANE[planeCount + addPlanes.size()];
memcpy(newPlanes, planes, planeCount * sizeof(BSPPLANE));
memcpy(newPlanes + planeCount, &addPlanes[0], addPlanes.size() * sizeof(BSPPLANE));
if (addPlanes.size())
std::copy(addPlanes.begin(), addPlanes.end(), newPlanes + planeCount);
replace_lump(LUMP_PLANES, newPlanes, (planeCount + addPlanes.size()) * sizeof(BSPPLANE));

BSPCLIPNODE32* newClipnodes = new BSPCLIPNODE32[clipnodeCount + addNodes.size()];
memcpy(newClipnodes, clipnodes, clipnodeCount * sizeof(BSPCLIPNODE32));
memcpy(newClipnodes + clipnodeCount, &addNodes[0], addNodes.size() * sizeof(BSPCLIPNODE32));
if (addNodes.size())
std::copy(addNodes.begin(), addNodes.end(), newClipnodes + clipnodeCount);
replace_lump(LUMP_CLIPNODES, newClipnodes, (clipnodeCount + addNodes.size()) * sizeof(BSPCLIPNODE32));

return solidNodeIdx;
Expand Down Expand Up @@ -7426,8 +7433,6 @@ int Bsp::getBspTextureSize(int textureid)
{
sz += (tex->nWidth >> i) * (tex->nHeight >> i);
}

// sz = (sz + 3) & ~3; // + padding align by 4
}
return sz;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class Bsp
// returns the current lump contents
LumpState duplicate_lumps(int targets);

void replace_lumps(LumpState& state);
void replace_lumps(const LumpState& state);

int delete_embedded_textures();

Expand Down
4 changes: 2 additions & 2 deletions src/bsp/Wad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Wad::~Wad(void)

void W_CleanupName(const char* in, char* out)
{
int i;
int c;
int i;

for (i = 0; i < MAXTEXTURENAME; i++) {
char c;
c = in[i];
if (!c)
break;
Expand Down
33 changes: 16 additions & 17 deletions src/bsp/bsptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@

enum lump_copy_targets
{
ENTITIES = 1,
PLANES = 2,
TEXTURES = 4,
VERTICES = 8,
VISIBILITY = 16,
NODES = 32,
TEXINFO = 64,
FACES = 128,
LIGHTING = 256,
CLIPNODES = 512,
LEAVES = 1024,
MARKSURFACES = 2048,
EDGES = 4096,
SURFEDGES = 8192,
MODELS = 16384
FL_ENTITIES = 1,
FL_PLANES = 2,
FL_TEXTURES = 4,
FL_VERTICES = 8,
FL_VISIBILITY = 16,
FL_NODES = 32,
FL_TEXINFO = 64,
FL_FACES = 128,
FL_LIGHTING = 256,
FL_CLIPNODES = 512,
FL_LEAVES = 1024,
FL_MARKSURFACES = 2048,
FL_EDGES = 4096,
FL_SURFEDGES = 8192,
FL_MODELS = 16384
};


Expand Down Expand Up @@ -175,8 +175,7 @@ struct BSPHEADER_EX

struct LumpState
{
unsigned char* lumps[HEADER_LUMPS];
int lumpLen[HEADER_LUMPS];
std::vector<unsigned char> lumps[HEADER_LUMPS];
};

struct BSPPLANE
Expand Down
13 changes: 8 additions & 5 deletions src/bsp/remap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,31 @@ bool STRUCTCOUNT::allZero()

void print_stat(int indent, int stat, const char* data)
{
if (stat == 0)
{
return;
}
int statabs = abs(stat);

for (int i = 0; i < indent; i++)
logf(" ");
const char* plural = "s";
if (std::string(data) == "vertex")
{
plural = "es";
}
int statabs = abs(stat);

logf("{} {} {}{}\n", stat > 0 ? "Deleted" : "Added", statabs, data, statabs > 1 ? plural : "");
}

void print_stat_mem(int indent, int bytes, const char* data)
{
if (!bytes)
return;
for (int i = 0; i < indent; i++)
logf(" ");
if (bytes == 0)
{
return;
}
for (int i = 0; i < indent; i++)
logf(" ");
logf("{} {:.2f} KB of {}\n", bytes > 0 ? "Deleted" : "Added", (abs(bytes) / 1024.0f), data);
}

Expand Down
Loading

0 comments on commit c2fbb0b

Please sign in to comment.