Merge branch 'music-changes' into 'master'

Load multiple musicdef, use music.pk3

See merge request KartKrew/Kart!324
This commit is contained in:
James R 2020-10-28 01:16:49 -04:00
commit faefe23d88
3 changed files with 183 additions and 137 deletions

View file

@ -1116,7 +1116,7 @@ static void IdentifyVersion(void)
} }
MUSICTEST("sounds.wad") MUSICTEST("sounds.wad")
MUSICTEST("music.wad") MUSICTEST("music.pk3")
#undef MUSICTEST #undef MUSICTEST

View file

@ -1598,172 +1598,219 @@ static tic_t pause_starttic;
/// Music Definitions /// Music Definitions
/// ------------------------ /// ------------------------
enum
{
MUSICDEF_20,
};
musicdef_t *musicdefstart = NULL; musicdef_t *musicdefstart = NULL;
struct cursongcredit cursongcredit; // Currently displayed song credit info struct cursongcredit cursongcredit; // Currently displayed song credit info
int musicdef_volume; int musicdef_volume;
// static boolean
// search for music definition in wad MusicDefError
// (
static UINT16 W_CheckForMusicDefInPwad(UINT16 wadid) alerttype_t level,
{ const char * description,
UINT16 i; const char * field,
lumpinfo_t *lump_p; lumpnum_t lumpnum,
int line
){
const wadfile_t * wad = wadfiles[WADFILENUM (lumpnum)];
const lumpinfo_t * lump = &wad->lumpinfo[LUMPNUM (lumpnum)];
lump_p = wadfiles[wadid]->lumpinfo; CONS_Alert(level,
for (i = 0; i < wadfiles[wadid]->numlumps; i++, lump_p++) va("%%s|%%s: %s (line %%d)\n", description),
if (memcmp(lump_p->name, "MUSICDEF", 8) == 0) wad->filename,
return i; lump->fullname,
field,
line
);
return INT16_MAX; // not found return false;
} }
void S_LoadMusicDefs(UINT16 wadnum) static boolean
{ ReadMusicDefFields
UINT16 lump; (
char *buf; lumpnum_t lumpnum,
char *buf2; int line,
char *stoken; char * stoken,
musicdef_t ** defp
){
musicdef_t *def;
char *value; char *value;
size_t size; char *textline;
musicdef_t *def, *prev;
UINT16 line = 1; // for better error msgs
lump = W_CheckForMusicDefInPwad(wadnum); if (!stricmp(stoken, "lump"))
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 ((stoken[0] == '/' && stoken[1] == '/') value = strtok(NULL, " ");
|| (stoken[0] == '#')) // skip comments if (!value)
{ {
stoken = strtok(NULL, "\r\n"); // skip end of line return MusicDefError(CONS_WARNING,
if (def) "Field '%'s is missing name.",
stoken = strtok(NULL, "\r\n= "); stoken, lumpnum, line);
else
stoken = strtok(NULL, "\r\n ");
line++;
}
else*/ 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, add to the end.
if (!def)
{
def = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL);
STRBUFCPY(def->name, value);
strlwr(def->name);
if (prev != NULL)
prev->next = def;
//CONS_Printf("S_LoadMusicDefs: Added song '%s'\n", def->name);
}
}
def->volume = DEFAULT_MUSICDEF_VOLUME;
skip_lump:
stoken = strtok(NULL, "\r\n ");
line++;
} }
else else
{ {
value = strtok(NULL, "\r\n= "); musicdef_t **tail = &musicdefstart;
if (!value) // Search if this is a replacement
while (*tail)
{ {
CONS_Alert(CONS_WARNING, "MUSICDEF: Field '%s' is missing value. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); if (!stricmp((*tail)->name, value))
stoken = strtok(NULL, "\r\n"); // skip end of line {
goto skip_field; break;
}
tail = &(*tail)->next;
} }
// Nothing found, add to the end.
if (!(*tail))
{
def = Z_Calloc(sizeof (musicdef_t), PU_STATIC, NULL);
STRBUFCPY(def->name, value);
strlwr(def->name);
def->volume = DEFAULT_MUSICDEF_VOLUME;
(*tail) = def;
}
(*defp) = (*tail);
}
}
else
{
value = strtok(NULL, "");
if (value)
{
// Find the equals sign.
value = strchr(value, '=');
}
if (!value)
{
return MusicDefError(CONS_WARNING,
"Field '%s' is missing value.",
stoken, lumpnum, line);
}
else
{
def = (*defp);
if (!def) if (!def)
{ {
CONS_Alert(CONS_ERROR, "MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); return MusicDefError(CONS_ERROR,
free(buf2); "No music definition before field '%s'.",
return; stoken, lumpnum, line);
} }
// Skip the equals sign.
value++;
// Now skip funny whitespace.
value += strspn(value, "\t ");
textline = value;
/* based ignored lumps */
if (!stricmp(stoken, "usage")) { if (!stricmp(stoken, "usage")) {
#if 0 // Ignore for now #if 0 // Ignore for now
STRBUFCPY(def->usage, value); STRBUFCPY(def->usage, textline);
for (value = def->usage; *value; value++)
if (*value == '_') *value = ' '; // turn _ into spaces.
//CONS_Printf("S_LoadMusicDefs: Set usage to '%s'\n", def->usage);
#endif #endif
} else if (!stricmp(stoken, "source")) { } else if (!stricmp(stoken, "source")) {
STRBUFCPY(def->source, value); STRBUFCPY(def->source, textline);
for (value = def->source; *value; value++)
if (*value == '_') *value = ' '; // turn _ into spaces.
//CONS_Printf("S_LoadMusicDefs: Set source to '%s'\n", def->source);
} else if (!stricmp(stoken, "volume")) { } else if (!stricmp(stoken, "volume")) {
def->volume = atoi(value); def->volume = atoi(textline);
} else { } else {
CONS_Alert(CONS_WARNING, "MUSICDEF: Invalid field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line); MusicDefError(CONS_WARNING,
"Unknown field '%s'.",
stoken, lumpnum, line);
} }
skip_field:
stoken = strtok(NULL, "\r\n= ");
line++;
} }
} }
free(buf2); return true;
return; }
static void S_LoadMusicDefLump(lumpnum_t 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
lump = W_CacheLumpNum(lumpnum, PU_CACHE);
size = W_LumpLength(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(lumpnum, 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);
}
void S_LoadMusicDefs(UINT16 wad)
{
const lumpnum_t wadnum = wad << 16;
UINT16 lump = 0;
while (( lump = W_CheckNumForNamePwad("MUSICDEF", wad, lump) ) != INT16_MAX)
{
S_LoadMusicDefLump(wadnum | lump);
lump++;
}
} }
// //

View file

@ -54,9 +54,8 @@ main (int ac, char **av)
if (( var = strtok(buf, " =") )) if (( var = strtok(buf, " =") ))
{ {
if (!( if (!(
strcasecmp(var, "TITLE") && strcasecmp(var, "USAGE") &&
strcasecmp(var, "ALTTITLE") && strcasecmp(var, "SOURCE")
strcasecmp(var, "AUTHORS")
)){ )){
if (( val = strtok(0, "") )) if (( val = strtok(0, "") ))
{ {