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