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("music.wad")
MUSICTEST("music.pk3")
#undef MUSICTEST

View file

@ -1598,172 +1598,219 @@ static tic_t pause_starttic;
/// Music Definitions
/// ------------------------
enum
{
MUSICDEF_20,
};
musicdef_t *musicdefstart = NULL;
struct cursongcredit cursongcredit; // Currently displayed song credit info
int musicdef_volume;
//
// search for music definition in wad
//
static UINT16 W_CheckForMusicDefInPwad(UINT16 wadid)
{
UINT16 i;
lumpinfo_t *lump_p;
static boolean
MusicDefError
(
alerttype_t level,
const char * description,
const char * field,
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;
for (i = 0; i < wadfiles[wadid]->numlumps; i++, lump_p++)
if (memcmp(lump_p->name, "MUSICDEF", 8) == 0)
return i;
CONS_Alert(level,
va("%%s|%%s: %s (line %%d)\n", description),
wad->filename,
lump->fullname,
field,
line
);
return INT16_MAX; // not found
return false;
}
void S_LoadMusicDefs(UINT16 wadnum)
{
UINT16 lump;
char *buf;
char *buf2;
char *stoken;
static boolean
ReadMusicDefFields
(
lumpnum_t lumpnum,
int line,
char * stoken,
musicdef_t ** defp
){
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 ((stoken[0] == '/' && stoken[1] == '/')
|| (stoken[0] == '#')) // skip comments
value = strtok(NULL, " ");
if (!value)
{
stoken = strtok(NULL, "\r\n"); // skip end of line
if (def)
stoken = strtok(NULL, "\r\n= ");
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++;
return MusicDefError(CONS_WARNING,
"Field '%'s is missing name.",
stoken, lumpnum, line);
}
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);
stoken = strtok(NULL, "\r\n"); // skip end of line
goto skip_field;
if (!stricmp((*tail)->name, value))
{
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)
{
CONS_Alert(CONS_ERROR, "MUSICDEF: No music definition before field '%s'. (file %s, line %d)\n", stoken, wadfiles[wadnum]->filename, line);
free(buf2);
return;
return MusicDefError(CONS_ERROR,
"No music definition before field '%s'.",
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 0 // Ignore for now
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);
#endif
} 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);
} else if (!stricmp(stoken, "volume")) {
def->volume = atoi(value);
def->volume = atoi(textline);
} 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;
return true;
}
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 (!(
strcasecmp(var, "TITLE") &&
strcasecmp(var, "ALTTITLE") &&
strcasecmp(var, "AUTHORS")
strcasecmp(var, "USAGE") &&
strcasecmp(var, "SOURCE")
)){
if (( val = strtok(0, "") ))
{