Fold the ResourceHandlers vtable into the ResourceDesc directly.
The vtables themselves are stored in the main resource map under the "sys." branch instead of in their own side array. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2969 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
Changes towards version 0.7:
|
||||
- Removed RES_TYPE enum, folded into ResourceDesc - Michael
|
||||
- Split STRTAB into STRTAB (strings) and BINTAB (color/xlat tables) - Michael
|
||||
- Removed internal references to defunct resource types - Michael
|
||||
- Revamped resource system to only use .rmp files - Michael
|
||||
|
||||
@@ -45,7 +45,6 @@ FreeKernel (void)
|
||||
UninitKernel (TRUE);
|
||||
UninitContexts ();
|
||||
|
||||
UNINIT_INSTANCES ();
|
||||
UninitResourceSystem ();
|
||||
|
||||
UninitPlayerInput ();
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#include "reslib.h"
|
||||
|
||||
extern void *LoadCodeResFile (const char *pStr);
|
||||
extern BOOLEAN InstallCodeResType (COUNT code_type);
|
||||
extern BOOLEAN InstallCodeResType (void);
|
||||
extern void *LoadCodeResInstance (RESOURCE res);
|
||||
extern void *CaptureCodeRes (void *hCode, void *pData, void **ppLocData);
|
||||
extern void *ReleaseCodeRes (void *CodeRef);
|
||||
|
||||
@@ -317,9 +317,9 @@ _ReleaseCodeResData (void *data)
|
||||
}
|
||||
|
||||
BOOLEAN
|
||||
InstallCodeResType (COUNT code_type)
|
||||
InstallCodeResType ()
|
||||
{
|
||||
return (InstallResTypeVectors (code_type,
|
||||
return (InstallResTypeVectors ("CODE",
|
||||
GetCodeResData, _ReleaseCodeResData));
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +201,7 @@ extern BOOLEAN GetFrameRect (FRAME Frame, RECT *pRect);
|
||||
|
||||
extern HOT_SPOT SetFrameHot (FRAME Frame, HOT_SPOT HotSpot);
|
||||
extern HOT_SPOT GetFrameHot (FRAME Frame);
|
||||
extern BOOLEAN InstallGraphicResTypes (COUNT cel_type, COUNT font_type);
|
||||
extern BOOLEAN InstallGraphicResTypes (void);
|
||||
extern DRAWABLE LoadGraphicFile (const char *pStr);
|
||||
extern FONT LoadFontFile (const char *pStr);
|
||||
extern void *LoadGraphicInstance (RESOURCE res);
|
||||
|
||||
@@ -20,10 +20,10 @@
|
||||
|
||||
|
||||
BOOLEAN
|
||||
InstallGraphicResTypes (COUNT cel_type, COUNT font_type)
|
||||
InstallGraphicResTypes (void)
|
||||
{
|
||||
InstallResTypeVectors (cel_type, _GetCelData, _ReleaseCelData);
|
||||
InstallResTypeVectors (font_type, _GetFontData, _ReleaseFontData);
|
||||
InstallResTypeVectors ("GFXRES", _GetCelData, _ReleaseCelData);
|
||||
InstallResTypeVectors ("FONTRES", _GetFontData, _ReleaseFontData);
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,18 +30,6 @@ typedef RESOURCE_INDEX_DESC *RESOURCE_INDEX;
|
||||
|
||||
typedef const char *RESOURCE;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
UNKNOWNRES = 0,
|
||||
GFXRES,
|
||||
FONTRES,
|
||||
STRTAB,
|
||||
BINTAB,
|
||||
SNDRES,
|
||||
MUSICRES,
|
||||
CODE
|
||||
} RES_TYPE;
|
||||
|
||||
#define NULL_RESOURCE NULL
|
||||
|
||||
extern const char *_cur_resfile_name;
|
||||
@@ -65,7 +53,7 @@ extern BOOLEAN DeleteResFile (uio_DirHandle *dir, const char *filename);
|
||||
|
||||
extern RESOURCE_INDEX InitResourceSystem ();
|
||||
extern void UninitResourceSystem (void);
|
||||
extern BOOLEAN InstallResTypeVectors (RES_TYPE res_type,
|
||||
extern BOOLEAN InstallResTypeVectors (const char *res_type,
|
||||
ResourceLoadFun *loadFun, ResourceFreeFun *freeFun);
|
||||
extern void *res_GetResource (RESOURCE res);
|
||||
extern void *res_DetachResource (RESOURCE res);
|
||||
|
||||
@@ -27,16 +27,16 @@ const char *_cur_resfile_name;
|
||||
// When a file is being loaded, _cur_resfile_name is set to its name.
|
||||
// At other times, it is NULL.
|
||||
|
||||
static ResourceDesc *
|
||||
ResourceDesc *
|
||||
lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res) {
|
||||
return (ResourceDesc *) CharHashTable_find (idx->map, res);
|
||||
}
|
||||
|
||||
void *
|
||||
loadResourceDesc (RESOURCE_INDEX idx, ResourceDesc *desc)
|
||||
loadResourceDesc (ResourceDesc *desc)
|
||||
{
|
||||
desc->resdata = loadResource (desc->fname,
|
||||
idx->typeInfo.handlers[desc->restype].loadFun);
|
||||
desc->vtable->loadFun);
|
||||
return desc->resdata;
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ res_GetResource (RESOURCE res)
|
||||
if (desc->resdata != NULL)
|
||||
return desc->resdata;
|
||||
|
||||
loadResourceDesc (resourceIndex, desc);
|
||||
loadResourceDesc (desc);
|
||||
|
||||
return desc->resdata;
|
||||
// May still be NULL, if the load failed.
|
||||
@@ -113,7 +113,6 @@ res_FreeResource (RESOURCE res)
|
||||
{
|
||||
ResourceDesc *desc;
|
||||
ResourceFreeFun *freeFun;
|
||||
RESOURCE_INDEX idx;
|
||||
|
||||
desc = lookupResourceDesc (_get_current_index_header(), res);
|
||||
if (desc == NULL)
|
||||
@@ -130,8 +129,7 @@ res_FreeResource (RESOURCE res)
|
||||
return;
|
||||
}
|
||||
|
||||
idx = _get_current_index_header ();
|
||||
freeFun = idx->typeInfo.handlers[desc->restype].freeFun;
|
||||
freeFun = desc->vtable->freeFun;
|
||||
(*freeFun) (desc->resdata);
|
||||
desc->resdata = NULL;
|
||||
}
|
||||
|
||||
@@ -25,31 +25,22 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
RESOURCE res_id;
|
||||
char *fname;
|
||||
RES_TYPE restype;
|
||||
void *resdata;
|
||||
} ResourceDesc;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *resType;
|
||||
ResourceLoadFun *loadFun;
|
||||
ResourceFreeFun *freeFun;
|
||||
} ResourceHandlers;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
RES_TYPE numTypes;
|
||||
/* Number of types in the handlers array (whether NULL or not).
|
||||
* == the highest stored handler number + 1.
|
||||
*/
|
||||
ResourceHandlers *handlers;
|
||||
} ResourceTypeInfo;
|
||||
RESOURCE res_id;
|
||||
char *fname;
|
||||
ResourceHandlers *vtable;
|
||||
void *resdata;
|
||||
} ResourceDesc;
|
||||
|
||||
struct resource_index_desc
|
||||
{
|
||||
CharHashTable_HashTable *map;
|
||||
ResourceTypeInfo typeInfo;
|
||||
size_t numRes;
|
||||
};
|
||||
|
||||
|
||||
@@ -29,8 +29,6 @@
|
||||
static RESOURCE_INDEX
|
||||
allocResourceIndex (void) {
|
||||
RESOURCE_INDEX ndx = HMalloc (sizeof (RESOURCE_INDEX_DESC));
|
||||
ndx->typeInfo.numTypes = 0;
|
||||
ndx->typeInfo.handlers = NULL;
|
||||
ndx->map = CharHashTable_newHashTable (NULL, NULL, NULL, NULL, 0, 0.85, 0.9);
|
||||
return ndx;
|
||||
}
|
||||
@@ -41,86 +39,57 @@ freeResourceIndex (RESOURCE_INDEX h) {
|
||||
{
|
||||
/* TODO: This leaks the contents of h->map */
|
||||
CharHashTable_deleteHashTable (h->map);
|
||||
if (h->typeInfo.handlers != NULL)
|
||||
HFree (h->typeInfo.handlers);
|
||||
HFree (h);
|
||||
}
|
||||
}
|
||||
|
||||
#define TYPESIZ 32
|
||||
/* These MUST COME in the SAME ORDER as the enums in reslib.h!!!
|
||||
They also must be no more than TYPESIZ-1 characters long, but that's
|
||||
unlikely to be a problem. */
|
||||
static const char *res_type_strings[] = {
|
||||
"UNKNOWNRES",
|
||||
"GFXRES",
|
||||
"FONTRES",
|
||||
"STRTAB",
|
||||
"BINTAB",
|
||||
"SNDRES",
|
||||
"MUSICRES",
|
||||
"CODE",
|
||||
NULL
|
||||
};
|
||||
|
||||
static ResourceDesc *
|
||||
newResourceDesc (const char *res_id, const char *resval)
|
||||
{
|
||||
char *path;
|
||||
const char *path;
|
||||
int pathlen;
|
||||
RES_TYPE resType;
|
||||
ResourceDesc *result;
|
||||
ResourceHandlers *vtable;
|
||||
ResourceDesc *result, *handlerdesc;
|
||||
RESOURCE_INDEX idx = _get_current_index_header ();
|
||||
char typestr[TYPESIZ];
|
||||
|
||||
path = strchr (resval, ':');
|
||||
if (path == NULL)
|
||||
{
|
||||
log_add (log_Warning, "Could not find type information for resource '%s'", res_id);
|
||||
return NULL;
|
||||
strncpy(typestr, "sys.UNKNOWNRES", TYPESIZ);
|
||||
path = resval;
|
||||
}
|
||||
else
|
||||
{
|
||||
char typestr[TYPESIZ];
|
||||
int n = path - resval;
|
||||
|
||||
if (n >= TYPESIZ)
|
||||
if (n >= TYPESIZ - 4)
|
||||
{
|
||||
n = TYPESIZ - 1;
|
||||
n = TYPESIZ - 5;
|
||||
}
|
||||
|
||||
strncpy (typestr, resval, n);
|
||||
typestr[n] = '\0';
|
||||
|
||||
strncpy (typestr, "sys.", TYPESIZ);
|
||||
strncat (typestr+1, resval, n);
|
||||
typestr[n+4] = '\0';
|
||||
path++;
|
||||
}
|
||||
pathlen = strlen (path);
|
||||
|
||||
resType = UNKNOWNRES;
|
||||
while (res_type_strings[resType])
|
||||
{
|
||||
if (!strcmp (typestr, res_type_strings[resType]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
resType++;
|
||||
}
|
||||
if (!res_type_strings[resType])
|
||||
{
|
||||
log_add (log_Warning, "Illegal type '%s' for resource '%s'", typestr, res_id);
|
||||
return NULL;
|
||||
}
|
||||
handlerdesc = lookupResourceDesc(idx, typestr);
|
||||
if (handlerdesc == NULL) {
|
||||
path = resval;
|
||||
log_add (log_Warning, "Illegal type '%s' for resource '%s'; treating as UNKNOWNRES", typestr, res_id);
|
||||
handlerdesc = lookupResourceDesc(idx, "sys.UNKNOWNRES");
|
||||
}
|
||||
|
||||
if (resType >= idx->typeInfo.numTypes)
|
||||
{
|
||||
log_add (log_Warning, "Warning: Unable to load '%s'; no handler "
|
||||
"for type %d defined.", res_id, resType);
|
||||
return NULL;
|
||||
}
|
||||
vtable = (ResourceHandlers *)handlerdesc->resdata;
|
||||
|
||||
if (idx->typeInfo.handlers[resType].loadFun == NULL)
|
||||
if (vtable->loadFun == NULL)
|
||||
{
|
||||
log_add (log_Warning, "Warning: Unable to load '%s'; no handler "
|
||||
"for type %s defined.", res_id, res_type_strings[resType]);
|
||||
"for type %s defined.", res_id, typestr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -131,7 +100,7 @@ newResourceDesc (const char *res_id, const char *resval)
|
||||
result->fname = HMalloc (pathlen + 1);
|
||||
strncpy (result->fname, path, pathlen);
|
||||
result->fname[pathlen] = '\0';
|
||||
result->restype = resType;
|
||||
result->vtable = vtable;
|
||||
result->resdata = NULL;
|
||||
return result;
|
||||
}
|
||||
@@ -169,7 +138,11 @@ InitResourceSystem (void)
|
||||
|
||||
_set_current_index_header (ndx);
|
||||
|
||||
INIT_INSTANCES ();
|
||||
InstallResTypeVectors ("UNKNOWNRES", NULL, NULL);
|
||||
InstallGraphicResTypes ();
|
||||
InstallStringTableResType ();
|
||||
InstallAudioResTypes ();
|
||||
InstallCodeResType ();
|
||||
|
||||
return ndx;
|
||||
}
|
||||
@@ -188,31 +161,37 @@ UninitResourceSystem (void)
|
||||
}
|
||||
|
||||
BOOLEAN
|
||||
InstallResTypeVectors (RES_TYPE resType, ResourceLoadFun *loadFun,
|
||||
InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun,
|
||||
ResourceFreeFun *freeFun)
|
||||
{
|
||||
ResourceHandlers handlers;
|
||||
RESOURCE_INDEX idx = _get_current_index_header ();
|
||||
handlers.loadFun = loadFun;
|
||||
handlers.freeFun = freeFun;
|
||||
ResourceHandlers *handlers;
|
||||
ResourceDesc *result;
|
||||
char key[TYPESIZ];
|
||||
int typelen;
|
||||
|
||||
if (resType >= idx->typeInfo.numTypes) {
|
||||
// Have to enlarge the handler array.
|
||||
ResourceHandlers *newHandlers = HRealloc (idx->typeInfo.handlers,
|
||||
(resType + 1) * sizeof (ResourceHandlers));
|
||||
if (newHandlers == NULL)
|
||||
return FALSE; // idx->typeInfo.handlers is untouched
|
||||
snprintf(key, TYPESIZ, "sys.%s", resType);
|
||||
key[TYPESIZ-1] = '\0';
|
||||
typelen = strlen(resType);
|
||||
|
||||
// Clear the space for new entries. No need to init the last one;
|
||||
// it's going to be used immediately.
|
||||
memset (&newHandlers[idx->typeInfo.numTypes], 0,
|
||||
(resType - idx->typeInfo.numTypes /* + 1 - 1 */)
|
||||
* sizeof (ResourceHandlers));
|
||||
|
||||
idx->typeInfo.handlers = newHandlers;
|
||||
idx->typeInfo.numTypes = resType + 1;
|
||||
handlers = HMalloc (sizeof (ResourceHandlers));
|
||||
if (handlers == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
handlers->loadFun = loadFun;
|
||||
handlers->freeFun = freeFun;
|
||||
handlers->resType = resType;
|
||||
|
||||
idx->typeInfo.handlers[resType] = handlers;
|
||||
return TRUE;
|
||||
result = HMalloc (sizeof (ResourceDesc));
|
||||
if (result == NULL)
|
||||
return FALSE;
|
||||
|
||||
result->fname = HMalloc (strlen(resType) + 1);
|
||||
strncpy (result->fname, resType, typelen);
|
||||
result->fname[typelen] = '\0';
|
||||
result->vtable = NULL;
|
||||
result->resdata = handlers;
|
||||
|
||||
CharHashTable_HashTable *map = _get_current_index_header ()->map;
|
||||
return CharHashTable_add (map, key, result) != 0;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
#include "reslib.h"
|
||||
#include "index.h"
|
||||
|
||||
void *loadResourceDesc (RESOURCE_INDEX idx, ResourceDesc *desc);
|
||||
ResourceDesc *lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res);
|
||||
void *loadResourceDesc (ResourceDesc *desc);
|
||||
void *loadResource(const char *path, ResourceLoadFun *loadFun);
|
||||
|
||||
void _set_current_index_header (RESOURCE_INDEX newResourceIndex);
|
||||
|
||||
@@ -49,8 +49,7 @@ extern BOOLEAN InitSound (int argc, char *argv[]);
|
||||
extern void UninitSound (void);
|
||||
extern SOUND_REF LoadSoundFile (const char *pStr);
|
||||
extern MUSIC_REF LoadMusicFile (const char *pStr);
|
||||
extern BOOLEAN InstallAudioResTypes (COUNT sound_type, COUNT
|
||||
music_type);
|
||||
extern BOOLEAN InstallAudioResTypes (void);
|
||||
extern SOUND_REF LoadSoundInstance (RESOURCE res);
|
||||
extern MUSIC_REF LoadMusicInstance (RESOURCE res);
|
||||
extern BOOLEAN DestroySound (SOUND_REF SoundRef);
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
#include "sndintrn.h"
|
||||
|
||||
BOOLEAN
|
||||
InstallAudioResTypes (COUNT sound_type, COUNT music_type)
|
||||
InstallAudioResTypes (void)
|
||||
{
|
||||
InstallResTypeVectors (sound_type, _GetSoundBankData, _ReleaseSoundBankData);
|
||||
InstallResTypeVectors (music_type, _GetMusicData, _ReleaseMusicData);
|
||||
InstallResTypeVectors ("SNDRES", _GetSoundBankData, _ReleaseSoundBankData);
|
||||
InstallResTypeVectors ("MUSICRES", _GetMusicData, _ReleaseMusicData);
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
#include "strintrn.h"
|
||||
|
||||
BOOLEAN
|
||||
InstallStringTableResType (COUNT string_type, COUNT bin_type)
|
||||
InstallStringTableResType (void)
|
||||
{
|
||||
InstallResTypeVectors (string_type, _GetStringData, FreeResourceData);
|
||||
InstallResTypeVectors (bin_type, _GetBinaryTableData, FreeResourceData);
|
||||
InstallResTypeVectors ("STRTAB", _GetStringData, FreeResourceData);
|
||||
InstallResTypeVectors ("BINTAB", _GetBinaryTableData, FreeResourceData);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ typedef BYTE *STRINGPTR;
|
||||
/* This has to go here because reslib requires the above typedefs. */
|
||||
#include "reslib.h"
|
||||
|
||||
extern BOOLEAN InstallStringTableResType (COUNT string_type, COUNT bin_type);
|
||||
extern BOOLEAN InstallStringTableResType (void);
|
||||
extern STRING_TABLE LoadStringTableInstance (RESOURCE res);
|
||||
extern STRING_TABLE LoadStringTableFile (uio_DirHandle *dir,
|
||||
const char *fileName);
|
||||
|
||||
@@ -29,14 +29,5 @@
|
||||
#define LoadSound LoadSoundInstance
|
||||
#define LoadMusic LoadMusicInstance
|
||||
|
||||
#define INIT_INSTANCES() do \
|
||||
{ \
|
||||
InstallGraphicResTypes (GFXRES, FONTRES); \
|
||||
InstallStringTableResType (STRTAB, BINTAB); \
|
||||
InstallAudioResTypes (SNDRES, MUSICRES); \
|
||||
InstallCodeResType (CODE); \
|
||||
} while (0)
|
||||
#define UNINIT_INSTANCES()
|
||||
|
||||
#endif /* _NAMEREF_H */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user