Skip to content

Commit

Permalink
* Moved several validation and fallback methods to Document
Browse files Browse the repository at this point in the history
* Moved LoadSideDefs to Document
  • Loading branch information
ioan-chera committed Nov 11, 2023
1 parent 782e01f commit 7b1779b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 59 deletions.
10 changes: 10 additions & 0 deletions src/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

class crc32_c;
class Instance;
struct BadCount;

//
// The document associated with a file. All stuff will go here
Expand Down Expand Up @@ -144,6 +145,15 @@ struct Document
void LoadThings_Hexen(int loading_level, const Wad_file *load_wad);
void LoadVertices(int loading_level, const Wad_file *load_wad);
void LoadSectors(int loading_level, const Wad_file *load_wad);
void LoadSideDefs(int loading_level, const Wad_file *load_wad, const ConfigData &config, BadCount &bad);

void CreateFallbackVertices();
void CreateFallbackSideDef(const ConfigData &config);
void CreateFallbackSector(const ConfigData &config);
void ValidateVertexRefs(LineDef &ld, int num, BadCount &bad);
void ValidateSidedefRefs(LineDef &ld, int num, const ConfigData &config, BadCount &bad);
void ValidateSectorRef(SideDef &sd, int num, const ConfigData &config, BadCount &bad);
void ValidateLevel_UDMF(const ConfigData &config, BadCount &bad);

void clear();
private:
Expand Down
9 changes: 0 additions & 9 deletions src/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ class Instance
void LoadLevelNum(const Wad_file *wad, int lev_num);
bool MissingIWAD_Dialog();
bool M_SaveMap();
void ValidateVertexRefs(LineDef *ld, int num, BadCount &bad);
void ValidateSectorRef(SideDef *sd, int num, BadCount &bad);
void ValidateSidedefRefs(LineDef *ld, int num, BadCount &bad);

// M_NODES
void BuildNodesAfterSave(int lev_idx);
Expand Down Expand Up @@ -383,16 +380,13 @@ class Instance
void DoExecuteCommand(const editor_command_t *cmd);

// M_LOADSAVE
void CreateFallbackSector();
void CreateFallbackSideDef();
void EmptyLump(const char *name) const;
void FreshLevel();
void LoadBehavior(int loading_level, const Wad_file *load_wad);
void LoadHeader(int loading_level, const Wad_file &load_wad);
void LoadLineDefs(int loading_level, const Wad_file *load_wad, BadCount &bad);
void LoadLineDefs_Hexen(int loading_level, const Wad_file *load_wad, BadCount &bad);
void LoadScripts(int loading_level, const Wad_file *load_wad);
void LoadSideDefs(int loading_level, const Wad_file *load_wad, BadCount &bad);

bool M_ExportMap();
void Navigate2D();
Expand All @@ -414,9 +408,6 @@ class Instance
// M_NODES
build_result_e BuildAllNodes(nodebuildinfo_t *info);

// M_UDMF
void ValidateLevel_UDMF(BadCount &bad);

// R_GRID
bool Grid_ParseUser(const std::vector<SString> &tokens);
void Grid_WriteUser(std::ostream &os) const;
Expand Down
84 changes: 42 additions & 42 deletions src/m_loadsave.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,33 +454,33 @@ void Document::LoadSectors(int loading_level, const Wad_file *load_wad)
}


void Instance::CreateFallbackSector()
void Document::CreateFallbackSector(const ConfigData &config)
{
gLog.printf("Creating a fallback sector.\n");

auto sec = std::make_unique<Sector>();

sec->SetDefaults(conf);
sec->SetDefaults(config);

level.sectors.push_back(std::move(sec));
sectors.push_back(std::move(sec));
}

void Instance::CreateFallbackSideDef()
void Document::CreateFallbackSideDef(const ConfigData &config)
{
// we need a valid sector too!
if (level.numSectors() == 0)
CreateFallbackSector();
if (numSectors() == 0)
CreateFallbackSector(config);

gLog.printf("Creating a fallback sidedef.\n");

auto sd = std::make_unique<SideDef>();

sd->SetDefaults(conf, false);
sd->SetDefaults(config, false);

level.sidedefs.push_back(std::move(sd));
sidedefs.push_back(std::move(sd));
}

static void CreateFallbackVertices(Document &doc)
void Document::CreateFallbackVertices()
{
gLog.printf("Creating two fallback vertices.\n");

Expand All @@ -493,8 +493,8 @@ static void CreateFallbackVertices(Document &doc)
v2->raw_x = FFixedPoint(555);
v2->raw_y = FFixedPoint(555);

doc.vertices.push_back(std::move(v1));
doc.vertices.push_back(std::move(v2));
vertices.push_back(std::move(v1));
vertices.push_back(std::move(v2));
}

struct BadCount
Expand All @@ -504,60 +504,60 @@ struct BadCount
int sidedef_refs;
};

void Instance::ValidateSidedefRefs(LineDef * ld, int num, BadCount &bad)
void Document::ValidateSidedefRefs(LineDef & ld, int num, const ConfigData &config, BadCount &bad)
{
if (ld->right >= level.numSidedefs() || ld->left >= level.numSidedefs())
if (ld.right >= numSidedefs() || ld.left >= numSidedefs())
{
gLog.printf("WARNING: linedef #%d has invalid sidedefs (%d / %d)\n",
num, ld->right, ld->left);
num, ld.right, ld.left);

bad.sidedef_refs++;

// ensure we have a usable sidedef
if (level.numSidedefs() == 0)
CreateFallbackSideDef();
if (numSidedefs() == 0)
CreateFallbackSideDef(config);

if (ld->right >= level.numSidedefs())
ld->right = 0;
if (ld.right >= numSidedefs())
ld.right = 0;

if (ld->left >= level.numSidedefs())
ld->left = 0;
if (ld.left >= numSidedefs())
ld.left = 0;
}
}

void Instance::ValidateVertexRefs(LineDef *ld, int num, BadCount &bad)
void Document::ValidateVertexRefs(LineDef &ld, int num, BadCount &bad)
{
if (ld->start >= level.numVertices() || ld->end >= level.numVertices() ||
ld->start == ld->end)
if (ld.start >= numVertices() || ld.end >= numVertices() ||
ld.start == ld.end)
{
gLog.printf("WARNING: linedef #%d has invalid vertices (%d -> %d)\n",
num, ld->start, ld->end);
num, ld.start, ld.end);

bad.linedef_count++;

// ensure we have a valid vertex
if (level.numVertices() < 2)
CreateFallbackVertices(level);
if (numVertices() < 2)
CreateFallbackVertices();

ld->start = 0;
ld->end = level.numVertices() - 1;
ld.start = 0;
ld.end = numVertices() - 1;
}
}

void Instance::ValidateSectorRef(SideDef *sd, int num, BadCount &bad)
void Document::ValidateSectorRef(SideDef &sd, int num, const ConfigData &config, BadCount &bad)
{
if (sd->sector >= level.numSectors())
if (sd.sector >= numSectors())
{
gLog.printf("WARNING: sidedef #%d has invalid sector (%d)\n",
num, sd->sector);
num, sd.sector);

bad.sector_refs++;

// ensure we have a valid sector
if (level.numSectors() == 0)
CreateFallbackSector();
if (numSectors() == 0)
CreateFallbackSector(config);

sd->sector = 0;
sd.sector = 0;
}
}

Expand Down Expand Up @@ -671,7 +671,7 @@ void Document::LoadThings_Hexen(int loading_level, const Wad_file *load_wad)
}


void Instance::LoadSideDefs(int loading_level, const Wad_file *load_wad, BadCount &bad)
void Document::LoadSideDefs(int loading_level, const Wad_file *load_wad, const ConfigData &config, BadCount &bad)
{
const Lump_c *lump = Load_LookupAndSeek(loading_level, load_wad, "SIDEDEFS");
if(!lump)
Expand Down Expand Up @@ -707,9 +707,9 @@ void Instance::LoadSideDefs(int loading_level, const Wad_file *load_wad, BadCoun

sd->sector = LE_U16(raw.sector);

ValidateSectorRef(sd.get(), i, bad);
ValidateSectorRef(*sd, i, config, bad);

level.sidedefs.push_back(std::move(sd));
sidedefs.push_back(std::move(sd));
}
}

Expand Down Expand Up @@ -753,8 +753,8 @@ void Instance::LoadLineDefs(int loading_level, const Wad_file *load_wad, BadCoun
if (ld->right == 0xFFFF) ld->right = -1;
if (ld-> left == 0xFFFF) ld-> left = -1;

ValidateVertexRefs(ld.get(), i, bad);
ValidateSidedefRefs(ld.get(), i, bad);
level.ValidateVertexRefs(*ld, i, bad);
level.ValidateSidedefRefs(*ld, i, conf, bad);

level.linedefs.push_back(std::move(ld));
}
Expand Down Expand Up @@ -805,8 +805,8 @@ void Instance::LoadLineDefs_Hexen(int loading_level, const Wad_file *load_wad, B
if (ld->right == 0xFFFF) ld->right = -1;
if (ld-> left == 0xFFFF) ld-> left = -1;

ValidateVertexRefs(ld.get(), i, bad);
ValidateSidedefRefs(ld.get(), i, bad);
level.ValidateVertexRefs(*ld, i, bad);
level.ValidateSidedefRefs(*ld, i, conf, bad);

level.linedefs.push_back(std::move(ld));
}
Expand Down Expand Up @@ -939,7 +939,7 @@ void Instance::LoadLevelNum(const Wad_file *wad, int lev_num)

level.LoadVertices(loading_level, wad);
level.LoadSectors(loading_level, wad);
LoadSideDefs(loading_level, wad, bad);
level.LoadSideDefs(loading_level, wad, conf, bad);

if (loaded.levelFormat == MapFormat::hexen)
{
Expand Down
16 changes: 8 additions & 8 deletions src/m_udmf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -632,19 +632,19 @@ static void UDMF_ParseObject(Document &doc, Udmf_Parser& parser, const Udmf_Toke
}


void Instance::ValidateLevel_UDMF(BadCount &bad)
void Document::ValidateLevel_UDMF(const ConfigData &config, BadCount &bad)
{
for (int n = 0 ; n < level.numSidedefs() ; n++)
for (int n = 0 ; n < numSidedefs() ; n++)
{
ValidateSectorRef(level.sidedefs[n].get(), n, bad);
ValidateSectorRef(*sidedefs[n], n, config, bad);
}

for (int n = 0 ; n < level.numLinedefs(); n++)
for (int n = 0 ; n < numLinedefs(); n++)
{
auto &L = level.linedefs[n];
auto &L = linedefs[n];

ValidateVertexRefs(L.get(), n, bad);
ValidateSidedefRefs(L.get(), n, bad);
ValidateVertexRefs(*L, n, bad);
ValidateSidedefRefs(*L, n, config, bad);
}
}

Expand Down Expand Up @@ -693,7 +693,7 @@ void Instance::UDMF_LoadLevel(int loading_level, const Wad_file *load_wad, BadCo
parser.SkipToEOLN();
}

ValidateLevel_UDMF(bad);
level.ValidateLevel_UDMF(conf, bad);
}


Expand Down

0 comments on commit 7b1779b

Please sign in to comment.