From 5cf00d903b094bc57941a40083c6d1914782d2cd Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 28 Sep 2024 01:47:18 +0200 Subject: [PATCH 1/5] super smol optimization to S_FindMusicCredit --- src/s_sound.c | 18 ++++++++++-------- src/s_sound.h | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/s_sound.c b/src/s_sound.c index ec5ebf00d..884c430cc 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1392,6 +1392,7 @@ void S_LoadMusicDefs(UINT16 wadnum) def = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL); STRBUFCPY(def->name, value); strlwr(def->name); + def->hash = quickncasehash (def->name, 6); if (prev != NULL) prev->next = def; //CONS_Printf("S_LoadMusicDefs: Added song '%s'\n", def->name); @@ -1649,19 +1650,20 @@ void S_InitMTDefs(void) // musicdef_t *S_FindMusicCredit(const char *musname) { - musicdef_t *def = musicdefstart; + UINT32 hash = quickncasehash (musname, 6); + musicdef_t *def; if (!def) // No definitions return NULL; - while (def) + for (def = musicdefstart; def; def = def->next) { - if (!stricmp(def->name, musname)) - { - return def; - } - else - def = def->next; + if (hash != def->hash) + continue; + if (stricmp(def->name, musname)) + continue; + + return def; } return NULL; diff --git a/src/s_sound.h b/src/s_sound.h index 6f4d03f09..b88d1121e 100644 --- a/src/s_sound.h +++ b/src/s_sound.h @@ -156,6 +156,7 @@ boolean S_SpeedMusic(float speed); typedef struct musicdef_s { char name[7]; + UINT32 hash; char usage[256]; char source[256]; char filename[256+1]; From a15cefc80fcdd28c2592b215700c54aa849a0724 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 28 Sep 2024 03:20:37 +0200 Subject: [PATCH 2/5] oops --- src/s_sound.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/s_sound.c b/src/s_sound.c index 884c430cc..3e594882a 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1653,9 +1653,6 @@ musicdef_t *S_FindMusicCredit(const char *musname) UINT32 hash = quickncasehash (musname, 6); musicdef_t *def; - if (!def) // No definitions - return NULL; - for (def = musicdefstart; def; def = def->next) { if (hash != def->hash) From affa23040b25c04361b68855919a7ff4bb8f6422 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 28 Sep 2024 03:20:57 +0200 Subject: [PATCH 3/5] refactor musicdef stuff well it fixed the hash find lmao --- src/s_sound.c | 266 ++++++++++++++++++++++++++++---------------------- 1 file changed, 148 insertions(+), 118 deletions(-) diff --git a/src/s_sound.c b/src/s_sound.c index 3e594882a..d0ed39b15 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1317,160 +1317,190 @@ static UINT16 W_CheckForMusicDefInPwad(UINT16 wadid) return INT16_MAX; // not found } -void S_LoadMusicDefs(UINT16 wadnum) +static boolean +ReadMusicDefFields (UINT16 wadnum, int line, char *stoken, musicdef_t **defp) { - UINT16 lump; - char *buf; - char *buf2; - char *stoken; + musicdef_t *def; + char *value; - size_t size; - musicdef_t *def, *prev; - UINT16 line = 1; // for better error msgs + char *textline; - lump = W_CheckForMusicDefInPwad(wadnum); - if (lump == INT16_MAX) - return; - - buf = W_CacheLumpNumPwad(wadnum, lump, PU_CACHE); - size = W_LumpLengthPwad(wadnum, lump); - - // for strtok - buf2 = malloc(size+1); - if (!buf2) - I_Error("S_LoadMusicDefs: No more free memory\n"); - M_Memcpy(buf2,buf,size); - buf2[size] = '\0'; - - def = prev = NULL; - - stoken = strtok (buf2, "\r\n "); - // Find music def - while (stoken) + if (!stricmp(stoken, "lump")) { - if (!stricmp(stoken, "lump")) + value = strtok(NULL, " "); + if (!value) { - value = strtok(NULL, "\r\n "); - - if (!value) - { - CONS_Alert(CONS_WARNING, "MUSICDEF: Lump '%s' is missing name. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - stoken = strtok(NULL, "\r\n"); // skip end of line - goto skip_lump; - } - - // No existing musicdefs - if (!musicdefstart) - { - musicdefstart = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL); - STRBUFCPY(musicdefstart->name, value); - strlwr(musicdefstart->name); - def = musicdefstart; - //CONS_Printf("S_LoadMusicDefs: Initialized musicdef w/ song '%s'\n", def->name); - } - else - { - def = musicdefstart; - - // Search if this is a replacement - //CONS_Printf("S_LoadMusicDefs: Searching for song replacement...\n"); - while (def) - { - if (!stricmp(def->name, value)) - { - //CONS_Printf("S_LoadMusicDefs: Found song replacement '%s'\n", def->name); - break; - } - - prev = def; - def = def->next; - } - - // Nothing found, add to the end. - if (!def) - { - def = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL); - STRBUFCPY(def->name, value); - strlwr(def->name); - def->hash = quickncasehash (def->name, 6); - if (prev != NULL) - prev->next = def; - //CONS_Printf("S_LoadMusicDefs: Added song '%s'\n", def->name); - } - } - - strncpy(def->filename, wadfiles[wadnum]->filename, 256); - def->filename[256] = '\0'; - -skip_lump: - stoken = strtok(NULL, "\r\n "); - line++; + CONS_Alert(CONS_WARNING, + "MUSICDEF: Field '%s' is missing name. (file %s, line %d)\n", + stoken, wadfiles[wadnum]->filename, line); + return false; } else { - value = strtok(NULL, "\r\n= "); + def = S_FindMusicCredit(value); - if (!value) + // Nothing found, add to the end. + if (!def) { - CONS_Alert(CONS_WARNING, "MUSICDEF: Field '%s' is missing value. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - stoken = strtok(NULL, "\r\n"); // skip end of line - goto skip_field; + def = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL); + + STRBUFCPY(def->name, value); + strlwr(def->name); + def->hash = quickncasehash (def->name, 6); + + def->next = musicdefstart; + musicdefstart = def; } + (*defp) = def; + } + } + else + { + value = strtok(NULL, ""); + + if (value) + { + // Find the equals sign. + value = strchr(value, '='); + } + + if (!value) + { + CONS_Alert(CONS_WARNING, + "MUSICDEF: Field '%s' is missing value. (file %s, line %d)\n", + stoken, wadfiles[wadnum]->filename, line); + return false; + } + else + { + def = (*defp); + if (!def) { - CONS_Alert(CONS_ERROR, "MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - free(buf2); - return; + CONS_Alert(CONS_ERROR, + "MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n", + stoken, wadfiles[wadnum]->filename, line); + return false; } - + + // Skip the equals sign. + value++; + + // Now skip funny whitespace. + value += strspn(value, "\t "); + + textline = value; + if (!stricmp(stoken, "usage")) { - STRBUFCPY(def->usage, value); - for (value = def->usage; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set usage to '%s'\n", def->usage); + STRBUFCPY(def->usage, textline); + for (textline = def->usage; *textline; textline++) + if (*textline == '_') *textline = ' '; // turn _ into spaces. } else if (!stricmp(stoken, "source")) { - STRBUFCPY(def->source, value); - for (value = def->source; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); + STRBUFCPY(def->source, textline); + for (textline = def->source; *textline; textline++) + if (*textline == '_') *textline = ' '; // turn _ into spaces. } else if (!stricmp(stoken, "title")) { def->use_info = true; - STRBUFCPY(def->title, value); - for (value = def->title; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); + STRBUFCPY(def->title, textline); + for (textline = def->title; *textline; textline++) + if (*textline == '_') *textline = ' '; // turn _ into spaces. } else if (!stricmp(stoken, "alttitle")) { - STRBUFCPY(def->alttitle, value); - for (value = def->alttitle; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); + STRBUFCPY(def->alttitle, textline); + for (textline = def->alttitle; *textline; textline++) + if (*textline == '_') *textline = ' '; // turn _ into spaces. } else if (!stricmp(stoken, "authors")) { - STRBUFCPY(def->authors, value); - for (value = def->authors; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); + STRBUFCPY(def->authors, textline); + for (textline = def->authors; *textline; textline++) + if (*textline == '_') *textline = ' '; // turn _ into spaces. } else CONS_Alert(CONS_WARNING, "MUSICDEF: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - -skip_field: - stoken = strtok(NULL, "\r\n= "); - line++; } } - free(buf2); - return; + return true; +} + +void S_LoadMusicDefs(UINT16 wadnum) +{ + UINT16 lumpnum; + char *lump; + char *musdeftext; + size_t size; + + char *lf; + char *stoken; + + size_t nlf; + size_t ncr; + + musicdef_t *def = NULL; + int line = 1; // for better error msgs + + lumpnum = W_CheckForMusicDefInPwad(wadnum); + if (lumpnum == INT16_MAX) + return; + + lump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); + size = W_LumpLengthPwad(wadnum, lumpnum); + + // Null-terminated MUSICDEF lump. + musdeftext = malloc(size+1); + if (!musdeftext) + I_Error("S_LoadMusicDefs: No more free memory for the parser\n"); + M_Memcpy(musdeftext, lump, size); + musdeftext[size] = '\0'; + + // Find music def + stoken = musdeftext; + for (;;) + { + lf = strpbrk(stoken, "\r\n"); + if (lf) + { + if (*lf == '\n') + nlf = 1; + else + nlf = 0; + *lf++ = '\0';/* now we can delimit to here */ + } + + stoken = strtok(stoken, " "); + if (stoken) + { + if (! ReadMusicDefFields(wadnum, line, stoken, &def)) + break; + } + + if (lf) + { + do + { + line += nlf; + ncr = strspn(lf, "\r");/* skip CR */ + lf += ncr; + nlf = strspn(lf, "\n"); + lf += nlf; + } + while (nlf || ncr) ; + + stoken = lf;/* now the next nonempty line */ + } + else + break;/* EOF */ + } + + free(musdeftext); } // From f1cf37068015512ff3b13d0b3b9f2629b117b8e7 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 28 Sep 2024 15:04:12 +0200 Subject: [PATCH 4/5] make a funky macro --- src/s_sound.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/s_sound.c b/src/s_sound.c index d0ed39b15..4d5ea5bb3 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1392,39 +1392,36 @@ ReadMusicDefFields (UINT16 wadnum, int line, char *stoken, musicdef_t **defp) textline = value; +// turn _ into spaces. +#define ADDDEF(field)\ + STRBUFCPY(def->field, textline);\ + for (textline = def->field; *textline; textline++)\ + if (*textline == '_') *textline = ' '; + if (!stricmp(stoken, "usage")) { - STRBUFCPY(def->usage, textline); - for (textline = def->usage; *textline; textline++) - if (*textline == '_') *textline = ' '; // turn _ into spaces. + ADDDEF(usage); } else if (!stricmp(stoken, "source")) { - STRBUFCPY(def->source, textline); - for (textline = def->source; *textline; textline++) - if (*textline == '_') *textline = ' '; // turn _ into spaces. + ADDDEF(source); } else if (!stricmp(stoken, "title")) { def->use_info = true; - STRBUFCPY(def->title, textline); - for (textline = def->title; *textline; textline++) - if (*textline == '_') *textline = ' '; // turn _ into spaces. + ADDDEF(title); } else if (!stricmp(stoken, "alttitle")) { - STRBUFCPY(def->alttitle, textline); - for (textline = def->alttitle; *textline; textline++) - if (*textline == '_') *textline = ' '; // turn _ into spaces. + ADDDEF(alttitle); } else if (!stricmp(stoken, "authors")) { - STRBUFCPY(def->authors, textline); - for (textline = def->authors; *textline; textline++) - if (*textline == '_') *textline = ' '; // turn _ into spaces. + ADDDEF(authors); } else CONS_Alert(CONS_WARNING, "MUSICDEF: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); +#undef ADDDEF } } From bf67c6b1d05da7525a222fb1c3e428371b681366 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 28 Sep 2024 17:03:09 +0200 Subject: [PATCH 5/5] remove code dupe and let S_LoadMusicDefs also load MUSCINFO def --- src/d_main.c | 1 - src/p_setup.c | 5 - src/s_sound.c | 268 +++++++++----------------------------------------- 3 files changed, 49 insertions(+), 225 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 9ca370c2a..76d55c204 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1861,7 +1861,6 @@ void D_SRB2Main(void) } S_InitMusicDefs(); - S_InitMTDefs(); CONS_Printf("ST_Init(): Init status bar.\n"); ST_Init(); diff --git a/src/p_setup.c b/src/p_setup.c index 17d050dc4..153e2c96b 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -3157,11 +3157,6 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, boolean local) // S_LoadMusicDefs(wadnum); - // - // edit music defs for stuff like musictest - // - S_LoadMTDefs(wadnum); - // // search for maps // diff --git a/src/s_sound.c b/src/s_sound.c index 4d5ea5bb3..cbdd17ed7 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1301,22 +1301,6 @@ static UINT32 queue_fadeinms; musicdef_t *musicdefstart = NULL; // First music definition struct cursongcredit cursongcredit; // Currently displayed song credit info -// -// search for music definition in wad -// -static UINT16 W_CheckForMusicDefInPwad(UINT16 wadid) -{ - UINT16 i; - lumpinfo_t *lump_p; - - lump_p = wadfiles[wadid]->lumpinfo; - for (i = 0; i < wadfiles[wadid]->numlumps; i++, lump_p++) - if (memcmp(lump_p->name, "MUSICDEF", 8) == 0) - return i; - - return INT16_MAX; // not found -} - static boolean ReadMusicDefFields (UINT16 wadnum, int line, char *stoken, musicdef_t **defp) { @@ -1444,60 +1428,64 @@ void S_LoadMusicDefs(UINT16 wadnum) musicdef_t *def = NULL; int line = 1; // for better error msgs - lumpnum = W_CheckForMusicDefInPwad(wadnum); - if (lumpnum == INT16_MAX) - return; - - lump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); - size = W_LumpLengthPwad(wadnum, lumpnum); - - // Null-terminated MUSICDEF lump. - musdeftext = malloc(size+1); - if (!musdeftext) - I_Error("S_LoadMusicDefs: No more free memory for the parser\n"); - M_Memcpy(musdeftext, lump, size); - musdeftext[size] = '\0'; - - // Find music def - stoken = musdeftext; - for (;;) + for (int k = 0; k < 2; k++) { - lf = strpbrk(stoken, "\r\n"); - if (lf) - { - if (*lf == '\n') - nlf = 1; - else - nlf = 0; - *lf++ = '\0';/* now we can delimit to here */ - } + lumpnum = W_CheckNumForNamePwad((k == 1 ? "MUSCINFO" : "MUSICDEF") , wadnum, 0); //check for MUSCINFO lump on 2nd iteration - stoken = strtok(stoken, " "); - if (stoken) - { - if (! ReadMusicDefFields(wadnum, line, stoken, &def)) - break; - } + if (lumpnum == INT16_MAX) + continue; - if (lf) + lump = W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE); + size = W_LumpLengthPwad(wadnum, lumpnum); + + // Null-terminated MUSICDEF lump. + musdeftext = malloc(size+1); + if (!musdeftext) + I_Error("S_LoadMusicDefs: No more free memory for the parser\n"); + M_Memcpy(musdeftext, lump, size); + musdeftext[size] = '\0'; + + // Find music def + stoken = musdeftext; + for (;;) { - do + lf = strpbrk(stoken, "\r\n"); + if (lf) { - line += nlf; - ncr = strspn(lf, "\r");/* skip CR */ - lf += ncr; - nlf = strspn(lf, "\n"); - lf += nlf; + if (*lf == '\n') + nlf = 1; + else + nlf = 0; + *lf++ = '\0';/* now we can delimit to here */ } - while (nlf || ncr) ; - stoken = lf;/* now the next nonempty line */ + stoken = strtok(stoken, " "); + if (stoken) + { + if (! ReadMusicDefFields(wadnum, line, stoken, &def)) + break; + } + + if (lf) + { + do + { + line += nlf; + ncr = strspn(lf, "\r");/* skip CR */ + lf += ncr; + nlf = strspn(lf, "\n"); + lf += nlf; + } + while (nlf || ncr) ; + + stoken = lf;/* now the next nonempty line */ + } + else + break;/* EOF */ } - else - break;/* EOF */ - } - free(musdeftext); + free(musdeftext); + } } // @@ -1512,164 +1500,6 @@ void S_InitMusicDefs(void) S_LoadMusicDefs(i); } -// -// search for music definition in wad -// -static UINT16 W_CheckForMusicInfoInPwad(UINT16 wadid) -{ - UINT16 i; - lumpinfo_t *lump_p; - - lump_p = wadfiles[wadid]->lumpinfo; - for (i = 0; i < wadfiles[wadid]->numlumps; i++, lump_p++) - if (memcmp(lump_p->name, "MUSCINFO", 8) == 0) - return i; - - return INT16_MAX; // not found -} - -void S_LoadMTDefs(UINT16 wadnum) -{ - UINT16 lump; - char *buf; - char *buf2; - char *stoken; - char *value; - size_t size; - musicdef_t *def, *prev; - UINT16 line = 1; // for better error msgs - - lump = W_CheckForMusicInfoInPwad(wadnum); - if (lump == INT16_MAX) - return; - - buf = W_CacheLumpNumPwad(wadnum, lump, PU_CACHE); - size = W_LumpLengthPwad(wadnum, lump); - - // for strtok - buf2 = malloc(size+1); - if (!buf2) - I_Error("S_LoadMTDefs: No more free memory\n"); - M_Memcpy(buf2,buf,size); - buf2[size] = '\0'; - - def = prev = NULL; - - stoken = strtok (buf2, "\r\n "); - // Find music def - while (stoken) - { - if (!stricmp(stoken, "lump")) - { - value = strtok(NULL, "\r\n "); - - if (!value) - { - CONS_Alert(CONS_WARNING, "MUSICDEF: Lump '%s' is missing name. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - stoken = strtok(NULL, "\r\n"); // skip end of line - goto skip_lump; - } - - // No existing musicdefs - if (!musicdefstart) - { - musicdefstart = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL); - STRBUFCPY(musicdefstart->name, value); - strlwr(musicdefstart->name); - def = musicdefstart; - //CONS_Printf("S_LoadMusicDefs: Initialized musicdef w/ song '%s'\n", def->name); - } - else - { - def = musicdefstart; - - // Search if this is a replacement - //CONS_Printf("S_LoadMusicDefs: Searching for song replacement...\n"); - while (def) - { - if (!stricmp(def->name, value)) - { - //CONS_Printf("S_LoadMusicDefs: Found song replacement '%s'\n", def->name); - break; - } - - prev = def; - def = def->next; - } - - // Nothing found, abort. - if (!def) - { - CONS_Alert(CONS_WARNING, "MUSCINFO: Music def for '%s' does not exist. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - free(buf2); - return; - } - } - -skip_lump: - stoken = strtok(NULL, "\r\n "); - line++; - } - else - { - value = strtok(NULL, "\r\n= "); - - if (!value) - { - CONS_Alert(CONS_WARNING, "MUSCINFO: Field '%s' is missing value. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - stoken = strtok(NULL, "\r\n"); // skip end of line - goto skip_field; - } - - if (!def) - { - CONS_Alert(CONS_ERROR, "MUSCINFO: No music definition before field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - free(buf2); - return; - } - - if (!stricmp(stoken, "title")) - { - def->use_info = true; - STRBUFCPY(def->title, value); - for (value = def->title; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); - } - else if (!stricmp(stoken, "alttitle")) - { - STRBUFCPY(def->alttitle, value); - for (value = def->alttitle; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); - } - else if (!stricmp(stoken, "authors")) - { - STRBUFCPY(def->authors, value); - for (value = def->authors; *value; value++) - if (*value == '_') *value = ' '; // turn _ into spaces. - //CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source); - } - else - CONS_Alert(CONS_WARNING, "MUSCINFO: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); - -skip_field: - stoken = strtok(NULL, "\r\n= "); - line++; - } - } - - free(buf2); - return; -} - -void S_InitMTDefs(void) -{ - UINT16 i; - for (i = 0; i < numwadfiles; i++) - S_LoadMTDefs(i); -} - // // S_FindMusicCredit //