2006-04-13 20:47:06 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
2010-05-25 10:56:00 +00:00
|
|
|
Copyright (C) 2010 EDuke32 developers and contributors
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-05-25 10:56:00 +00:00
|
|
|
This file is part of EDuke32.
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2007-02-08 04:19:39 +00:00
|
|
|
EDuke32 is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "duke3d.h"
|
|
|
|
|
|
|
|
//=============
|
|
|
|
// STATICS
|
|
|
|
//=============
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
int32_t rts_numlumps;
|
|
|
|
static void **rts_lumpcache;
|
|
|
|
static lumpinfo_t *rts_lumpinfo; // location of each lump on disk
|
2009-04-26 05:57:42 +00:00
|
|
|
static int32_t RTS_Started = FALSE;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
char rts_lumplockbyte[11];
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-07-19 15:14:00 +00:00
|
|
|
int32_t IntelLong(int32_t l)
|
|
|
|
{
|
|
|
|
#if B_BIG_ENDIAN != 0
|
|
|
|
int32_t t = ((l & 0x00ff00ffl) << 8) | ((l & 0xff00ff00l) >> 8);
|
|
|
|
return ((t & 0x0000ffffl) << 16) | ((t & 0xffff0000l) >> 16);
|
|
|
|
#else
|
|
|
|
return l;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
/*
|
|
|
|
============================================================================
|
|
|
|
|
|
|
|
LUMP BASED ROUTINES
|
|
|
|
|
|
|
|
============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_AddFile
|
|
|
|
=
|
|
|
|
= All files are optional, but at least one file must be found
|
|
|
|
= Files with a .rts extension are wadlink files with multiple lumps
|
|
|
|
= Other files are single lumps with the base filename for the lump name
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
int32_t RTS_AddFile(const char *filename)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
wadinfo_t header;
|
|
|
|
lumpinfo_t *lump_p;
|
2009-04-26 05:57:42 +00:00
|
|
|
uint32_t i;
|
|
|
|
int32_t handle, length;
|
|
|
|
int32_t startlump;
|
2006-04-13 20:47:06 +00:00
|
|
|
filelump_t *fileinfo, *fileinfoo;
|
|
|
|
|
|
|
|
//
|
|
|
|
// read the entire file in
|
|
|
|
// FIXME: shared opens
|
|
|
|
|
2008-08-20 08:39:07 +00:00
|
|
|
handle = kopen4loadfrommod((char *)filename, 0);
|
2006-11-15 01:16:55 +00:00
|
|
|
if (handle < 0)
|
|
|
|
{
|
2012-06-27 17:45:52 +00:00
|
|
|
initprintf("RTS file \"%s\" was not found\n",filename);
|
2006-04-13 20:47:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
startlump = rts_numlumps;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
// WAD file
|
2012-06-27 17:45:52 +00:00
|
|
|
// initprintf(" Adding \"%s\".\n",filename);
|
2006-11-14 21:35:50 +00:00
|
|
|
kread(handle, &header, sizeof(header));
|
2006-11-15 01:16:55 +00:00
|
|
|
if (strncmp(header.identification,"IWAD",4))
|
|
|
|
{
|
2012-06-27 17:45:52 +00:00
|
|
|
initprintf("RTS file \"%s\" doesn't have IWAD id\n",filename);
|
2006-04-13 20:47:06 +00:00
|
|
|
kclose(handle);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
header.numlumps = IntelLong(header.numlumps);
|
|
|
|
header.infotableofs = IntelLong(header.infotableofs);
|
|
|
|
length = header.numlumps*sizeof(filelump_t);
|
2010-08-02 08:13:51 +00:00
|
|
|
fileinfo = fileinfoo = (filelump_t *)Bmalloc(length);
|
2006-11-15 01:16:55 +00:00
|
|
|
if (!fileinfo)
|
|
|
|
{
|
2006-04-13 20:47:06 +00:00
|
|
|
initprintf("RTS file could not allocate header info\n");
|
|
|
|
kclose(handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-11-14 21:35:50 +00:00
|
|
|
klseek(handle, header.infotableofs, SEEK_SET);
|
2006-04-13 20:47:06 +00:00
|
|
|
kread(handle, fileinfo, length);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fill in lumpinfo
|
|
|
|
//
|
2010-08-02 08:13:51 +00:00
|
|
|
lump_p = Brealloc(rts_lumpinfo, (rts_numlumps + header.numlumps)*sizeof(lumpinfo_t));
|
2006-11-15 01:16:55 +00:00
|
|
|
if (!lump_p)
|
|
|
|
{
|
2006-04-13 20:47:06 +00:00
|
|
|
kclose(handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_lumpinfo = lump_p;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps += header.numlumps;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
lump_p = &rts_lumpinfo[startlump];
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
for (i=startlump ; i<(uint32_t)rts_numlumps ; i++,lump_p++, fileinfo++)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
lump_p->handle = handle;
|
|
|
|
lump_p->position = IntelLong(fileinfo->filepos);
|
|
|
|
lump_p->size = IntelLong(fileinfo->size);
|
2009-02-02 01:49:14 +00:00
|
|
|
Bstrncpy(lump_p->name, fileinfo->name, 8);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(fileinfoo);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_Init
|
|
|
|
=
|
|
|
|
= Files with a .rts extension are idlink files with multiple lumps
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
|
2006-12-23 02:38:47 +00:00
|
|
|
void RTS_Init(const char *filename)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2009-04-26 05:57:42 +00:00
|
|
|
int32_t length;
|
2006-04-13 20:47:06 +00:00
|
|
|
//
|
|
|
|
// open all the files, load headers, and count lumps
|
|
|
|
//
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
|
|
|
rts_lumpinfo = NULL; // will be realloced as lumps are added
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2008-07-09 23:25:38 +00:00
|
|
|
// initprintf("RTS Manager Started.\n");
|
2006-11-14 21:35:50 +00:00
|
|
|
if (RTS_AddFile(filename)) return;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
if (!rts_numlumps) return;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// set up caching
|
|
|
|
//
|
2010-08-02 08:13:51 +00:00
|
|
|
length = (rts_numlumps) * sizeof(*rts_lumpcache);
|
|
|
|
rts_lumpcache = Bmalloc(length);
|
|
|
|
memset(rts_lumpcache,0,length);
|
2009-04-26 05:57:42 +00:00
|
|
|
RTS_Started = TRUE;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_NumSounds
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
int32_t RTS_NumSounds(void)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2010-08-02 08:13:51 +00:00
|
|
|
return rts_numlumps-1;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_SoundLength
|
|
|
|
=
|
|
|
|
= Returns the buffer size needed to load the given lump
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
int32_t RTS_SoundLength(int32_t lump)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
lump++;
|
2010-08-02 08:13:51 +00:00
|
|
|
if (lump >= rts_numlumps)
|
2010-07-19 15:14:00 +00:00
|
|
|
{
|
|
|
|
initprintf("RTS_SoundLength: %i >= numlumps",lump);
|
|
|
|
RTS_Started = FALSE;
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
2010-07-19 15:14:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-08-02 08:13:51 +00:00
|
|
|
return rts_lumpinfo[lump].size;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_GetSoundName
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
const char *RTS_GetSoundName(int32_t i)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
i++;
|
2010-08-02 08:13:51 +00:00
|
|
|
if (i>=rts_numlumps)
|
2010-07-19 15:14:00 +00:00
|
|
|
{
|
|
|
|
initprintf("RTS_GetSoundName: %i >= numlumps",i);
|
|
|
|
RTS_Started = FALSE;
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
2010-07-19 15:14:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
return &(rts_lumpinfo[i].name[0]);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_ReadLump
|
|
|
|
=
|
|
|
|
= Loads the lump into the given buffer, which must be >= RTS_SoundLength()
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
2009-04-26 05:57:42 +00:00
|
|
|
void RTS_ReadLump(int32_t lump, void *dest)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
lumpinfo_t *l;
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
if (lump >= rts_numlumps)
|
2010-07-19 15:14:00 +00:00
|
|
|
{
|
|
|
|
initprintf("RTS_ReadLump: %i >= numlumps",lump);
|
|
|
|
RTS_Started = FALSE;
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
2010-07-19 15:14:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
if (lump < 0)
|
2010-07-19 15:14:00 +00:00
|
|
|
{
|
|
|
|
initprintf("RTS_ReadLump: %i < 0",lump);
|
|
|
|
RTS_Started = FALSE;
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
2010-07-19 15:14:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
l = rts_lumpinfo+lump;
|
2006-11-14 21:35:50 +00:00
|
|
|
klseek(l->handle, l->position, SEEK_SET);
|
2006-04-13 20:47:06 +00:00
|
|
|
kread(l->handle,dest,l->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
=
|
|
|
|
= RTS_GetSound
|
|
|
|
=
|
|
|
|
====================
|
|
|
|
*/
|
2009-04-26 05:57:42 +00:00
|
|
|
void *RTS_GetSound(int32_t lump)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
lump++;
|
2010-08-02 08:13:51 +00:00
|
|
|
if ((uint32_t)lump >= (uint32_t)rts_numlumps)
|
2010-07-19 15:14:00 +00:00
|
|
|
{
|
2010-08-02 08:13:51 +00:00
|
|
|
initprintf("RTS_GetSound: %i >= %i\n",lump,rts_numlumps);
|
2010-07-19 15:14:00 +00:00
|
|
|
RTS_Started = FALSE;
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
2010-07-19 15:14:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
if (rts_lumpcache[lump] == NULL)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_lumplockbyte[lump] = 200;
|
|
|
|
allocache((intptr_t *)&rts_lumpcache[lump],(int32_t)RTS_SoundLength(lump-1),&rts_lumplockbyte[lump]); // JBF 20030910: char * => int32_t *
|
|
|
|
RTS_ReadLump(lump, rts_lumpcache[lump]);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-02 08:13:51 +00:00
|
|
|
if (rts_lumplockbyte[lump] < 200)
|
|
|
|
rts_lumplockbyte[lump] = 200;
|
2006-04-13 20:47:06 +00:00
|
|
|
else
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_lumplockbyte[lump]++;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
2010-08-02 08:13:51 +00:00
|
|
|
return rts_lumpcache[lump];
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|