From d05d661c681a36e1cdfbbe669f048245e8fd997c Mon Sep 17 00:00:00 2001 From: seedee Date: Wed, 10 Jul 2024 03:45:04 +0100 Subject: [PATCH] Update LoadWadconfig and LoadWadcfgfile --- src/sdhlt/sdHLCSG/wadcfg.cpp | 96 +++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 39 deletions(-) diff --git a/src/sdhlt/sdHLCSG/wadcfg.cpp b/src/sdhlt/sdHLCSG/wadcfg.cpp index 2ace0a02..60e209e7 100644 --- a/src/sdhlt/sdHLCSG/wadcfg.cpp +++ b/src/sdhlt/sdHLCSG/wadcfg.cpp @@ -3,32 +3,47 @@ #include "csg.h" void LoadWadconfig (const char *filename, const char *configname) { - Log ("Loading wad configuration '%s' from '%s' :\n", configname, filename); - int found = 0; - int count = 0; - int size; - char *buffer; - size = LoadFile (filename, &buffer); - ParseFromMemory (buffer, size); - while (GetToken (true)) + char filenameOnly[_MAX_PATH]; + size_t filenameLength = strlen(filename); + strncpy(filenameOnly, filename, filenameLength); + filenameOnly[filenameLength] = '\0'; //Null terminate + auto pos = std::string(filenameOnly).find_last_of("/\\"); + + if (pos != std::string::npos) // Copy everything after the last slash to the beginning of filenameOnly + { + std::string temp(filenameOnly + pos + 1); + strncpy(filenameOnly, temp.c_str(), _MAX_PATH); + filenameOnly[temp.size()] = '\0'; + } + Log("Loading wadconfig %s from '%s'\n", configname, filenameOnly); + Log("--------------------------------------\n"); + int wadconfigsFound = 0; + int wadPathsFound = 0; + int filesize; + char *file; + filesize = LoadFile(filename, &file); //Load file and store it's size + ParseFromMemory (file, filesize); //Parse contents from memory + + while (GetToken (true)) //Loop through file { - bool skip = true; - if (!strcasecmp (g_token, configname)) + bool skip = true; //Skip until the -wadconfig configname is found + + if (!strcasecmp (g_token, configname)) //If we find configname line { skip = false; - found++; + wadconfigsFound++; } - if (!GetToken (true) || strcasecmp (g_token, "{")) + if (!GetToken (true) || strcasecmp(g_token, "{")) //If next line is not an opening bracket { - Error ("parsing '%s': missing '{'.", filename); + Error ("Parsing %s (missing '{' opening bracket in '%s' config)\n", filenameOnly, configname); } - while (1) + while (1) //Loop through content of braces { if (!GetToken (true)) { - Error ("parsing '%s': unexpected end of file.", filename); + Error("Parsing '%s': unexpected EOF in '%s'\n", filenameOnly, configname); } - if (!strcasecmp (g_token, "}")) + if (!strcasecmp (g_token, "}")) //If we find closing bracket { break; } @@ -36,50 +51,53 @@ void LoadWadconfig (const char *filename, const char *configname) { continue; } - Log (" "); bool include = false; if (!strcasecmp (g_token, "include")) { - Log ("include "); + Log("[include] "); include = true; + if (!GetToken (true)) { - Error ("parsing '%s': unexpected end of file.", filename); + Error ("Parsing '%s': unexpected EOF in '%s'\n", filenameOnly, configname); } } - Log ("\"%s\"\n", g_token); + Log ("%s\n", g_token); if (g_iNumWadPaths >= MAX_WADPATHS) { - Error ("parsing '%s': too many wad files.", filename); + Error ("Parsing '%s': too many wad files (%i/%i) in '%s'\n", filenameOnly, configname, g_iNumWadPaths, MAX_WADPATHS); } - count++; + wadPathsFound++; PushWadPath (g_token, !include); } } - if (found == 0) + Log("- %d wadpaths found in %s\n", wadPathsFound, configname); + Log("--------------------------------------\n\n"); + + if (wadconfigsFound < 1) { - Error ("Couldn't find wad configuration '%s' in file '%s'.\n", configname, filename); + Error ("Couldn't find wad config %s in '%s'\n", configname, filenameOnly); } - if (found >= 2) + if (wadconfigsFound > 1) { - Error ("Found more than one wad configuration for '%s' in file '%s'.\n", configname, filename); + Error("Found more than one wad config %s in '%s'\n", configname, filenameOnly); } - free (buffer); // should not be freed because it is still being used as script buffer - //Log ("Using custom wadfile configuration: '%s' (with %i wad%s)\n", configname, count, count > 1 ? "s" : ""); + free (file); // should not be freed because it is still being used as script buffer + //Log ("Using custom wadfile configuration: '%s' (with %i wad%s)\n", configname, wadPathsFound, wadPathsFound > 1 ? "s" : ""); } void LoadWadcfgfile (const char *filename) { - Log ("Loading wad configuration file '%s' :\n", filename); - int count = 0; - int size; - char *buffer; - size = LoadFile (filename, &buffer); - ParseFromMemory (buffer, size); - while (GetToken (true)) + Log ("Loading %s\n", filename); + Log ("------------\n"); + int wadPathsCount = 0; + int wadFileSize; + char *wadFile; + wadFileSize = LoadFile (filename, &wadFile); + ParseFromMemory (wadFile, wadFileSize); + while (GetToken (true)) //Loop through file { - Log (" "); bool include = false; - if (!strcasecmp (g_token, "include")) + if (!strcasecmp (g_token, "include")) //If line starts with include (or contains?) { Log ("include "); include = true; @@ -93,9 +111,9 @@ void LoadWadcfgfile (const char *filename) { Error ("parsing '%s': too many wad files.", filename); } - count++; + wadPathsCount++; PushWadPath (g_token, !include); } - free (buffer); // should not be freed because it is still being used as script buffer + free (wadFile); // should not be freed because it is still being used as script buffer //Log ("Using custom wadfile configuration: '%s' (with %i wad%s)\n", filename, count, count > 1 ? "s" : ""); }