optimize addon autoload handling

This commit is contained in:
Alug committed 2025-11-01 19:20:04 +01:00
1 parent b0fe13836b
commit 092e061092
1 file changed
+48 -25
+48 -25
View File
@@ -103,6 +103,8 @@ static char *startuppwads[MAX_WADFILES];
// autoloading
static char *autoloadwadfiles[MAX_WADFILES];
static char *autoloadwadfilespost[MAX_WADFILES];
static size_t autoloadcount = 0;
static size_t postloadcount = 0;
static boolean autoloaded = false;
boolean postautoloaded = false;
//
@@ -1062,6 +1064,7 @@ static void D_AddFile(const char *file, char **filearray)
static void D_CleanFile(char **filearray)
{
size_t pnumwadfiles;
for (pnumwadfiles = 0; filearray[pnumwadfiles]; pnumwadfiles++)
{
free(filearray[pnumwadfiles]);
@@ -1069,6 +1072,17 @@ static void D_CleanFile(char **filearray)
}
}
static void D_CleanAutoloadFile(char **filearray, size_t count)
{
size_t i;
for (i = 0; i < count; i++)
{
free(filearray[i]);
filearray[i] = NULL;
}
}
// Taken from TSoURDt3rd
// https://github.com/StarManiaKG/The-Story-of-Uncapped-Revengence-Discord-the-3rd/blob/main/src/STAR/star_functions.c
static INT32 D_DetectFileType(const char* filename)
@@ -1097,9 +1111,8 @@ static INT32 D_DetectFileType(const char* filename)
}
// autoload that shit
static void D_AutoloadFile(const char *file, char **filearray)
static void D_AutoloadFile(const char *file, char **filearray, size_t count)
{
size_t pnumwadfiles;
char *newfile;
INT32 fileType = D_DetectFileType(file);
@@ -1109,9 +1122,6 @@ static void D_AutoloadFile(const char *file, char **filearray)
return;
}
for (pnumwadfiles = 0; filearray[pnumwadfiles]; pnumwadfiles++)
;
if (fileType <= 6)
{
newfile = malloc(strlen(file) + 1);
@@ -1119,7 +1129,7 @@ static void D_AutoloadFile(const char *file, char **filearray)
I_Error("No more free memory to AutoloadFile %s",file);
strcpy(newfile, file);
filearray[pnumwadfiles] = newfile;
filearray[count] = newfile;
}
else
{
@@ -1149,13 +1159,14 @@ static void D_FindAddonsToAutoload(void)
{
FILE *autoloadconfigfile;
const char *autoloadpath;
char *newline;
boolean postload;
INT32 i;
char wadsToAutoload[256] = "";
// does it exist tho
autoloadpath = va("%s"PATHSEP"%s",srb2home,AUTOLOADCONFIGFILENAME);
autoloadpath = va("%s"PATHSEP"%s", srb2home, AUTOLOADCONFIGFILENAME);
autoloadconfigfile = fopen(autoloadpath, "r");
// If the file is found, run our shit
@@ -1165,26 +1176,27 @@ static void D_FindAddonsToAutoload(void)
return;
}
while (fgets(wadsToAutoload, sizeof wadsToAutoload, autoloadconfigfile) != NULL)
while (fgets(wadsToAutoload, sizeof(wadsToAutoload), autoloadconfigfile) != NULL)
{
postload = false;
// skip if commented or empty
if ((wadsToAutoload[1] == '\0' || wadsToAutoload[1] == '\n')
|| (wadsToAutoload[0] == '#'))
if (wadsToAutoload[0] == '\n' ||
wadsToAutoload[0] == '#' ||
wadsToAutoload[0] == '\0')
continue;
// this marks it so that it loads after loading server addons
else if (fastncmp(wadsToAutoload, "postload ", 9))
if (fastncmp(wadsToAutoload, "postload ", 9))
{
strremove(wadsToAutoload, "postload ");
postload = true;
}
// Remove Any Empty or Skipped Lines
for (i = 0; wadsToAutoload[i] != '\0'; i++)
{
if (wadsToAutoload[i] == '\n')
wadsToAutoload[i] = '\0';
}
newline = strchr(wadsToAutoload, '\n');
if (newline)
*newline = '\0';
if (!postload && W_CheckPostLoadList(wadsToAutoload))
{
@@ -1194,15 +1206,20 @@ static void D_FindAddonsToAutoload(void)
// LOAD IT
if (!postload)
D_AutoloadFile(wadsToAutoload, autoloadwadfiles);
{
D_AutoloadFile(wadsToAutoload, autoloadwadfiles, autoloadcount);
autoloadcount++;
}
else
D_AutoloadFile(wadsToAutoload, autoloadwadfilespost);
// end it here
for (i = 0; wadsToAutoload[i] != '\0'; i++)
wadsToAutoload[i] = '\0';
{
D_AutoloadFile(wadsToAutoload, autoloadwadfilespost, postloadcount);
postloadcount++;
}
}
autoloadwadfiles[autoloadcount] = NULL;
autoloadwadfilespost[postloadcount] = NULL;
// we dont want memory leaks around here do we?
fclose(autoloadconfigfile);
}
@@ -1213,10 +1230,13 @@ static void D_AddAutoloadFiles(void)
return;
CONS_Printf("D_AutoloadFile(): Loading autoloaded addons...\n");
if (W_AddAutoloadedLocalFiles(autoloadwadfiles) == 0)
CONS_Printf("D_AutoloadFile(): Are you sure you put in valid files or what?\n");
D_CleanFile(autoloadwadfiles);
D_CleanAutoloadFile(autoloadwadfiles, autoloadcount);
autoloadcount = 0;
autoloaded = true;
}
@@ -1226,10 +1246,13 @@ void D_AddPostloadFiles(void)
return;
CONS_Printf("D_AddPostloadFiles(): Loading postloaded addons...\n");
if (W_AddAutoloadedLocalFiles(autoloadwadfilespost) == 0)
CONS_Printf("D_AddPostloadFiles(): Are you sure you put in valid files or what?\n");
D_CleanFile(autoloadwadfilespost);
D_CleanAutoloadFile(autoloadwadfilespost, postloadcount);
postloadcount = 0;
postautoloaded = true;
}