From ffd50ee6fdd9dfa93c58f415808a35165d25815a Mon Sep 17 00:00:00 2001 From: mcmartin Date: Sun, 11 May 2008 03:29:14 +0000 Subject: [PATCH] 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 --- sc2/ChangeLog | 1 + sc2/src/sc2code/cleanup.c | 1 - sc2/src/sc2code/coderes.h | 2 +- sc2/src/sc2code/dummy.c | 4 +- sc2/src/sc2code/libs/gfxlib.h | 2 +- sc2/src/sc2code/libs/graphics/resgfx.c | 6 +- sc2/src/sc2code/libs/reslib.h | 14 +-- sc2/src/sc2code/libs/resource/getres.c | 12 +- sc2/src/sc2code/libs/resource/index.h | 21 +--- sc2/src/sc2code/libs/resource/resinit.c | 135 ++++++++++------------- sc2/src/sc2code/libs/resource/resintrn.h | 3 +- sc2/src/sc2code/libs/sndlib.h | 3 +- sc2/src/sc2code/libs/sound/resinst.c | 6 +- sc2/src/sc2code/libs/strings/sresins.c | 6 +- sc2/src/sc2code/libs/strlib.h | 2 +- sc2/src/sc2code/nameref.h | 9 -- 16 files changed, 87 insertions(+), 140 deletions(-) diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 85f74aee3..36ff4b5c3 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -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 diff --git a/sc2/src/sc2code/cleanup.c b/sc2/src/sc2code/cleanup.c index ffd2a691c..360a29eab 100644 --- a/sc2/src/sc2code/cleanup.c +++ b/sc2/src/sc2code/cleanup.c @@ -45,7 +45,6 @@ FreeKernel (void) UninitKernel (TRUE); UninitContexts (); - UNINIT_INSTANCES (); UninitResourceSystem (); UninitPlayerInput (); diff --git a/sc2/src/sc2code/coderes.h b/sc2/src/sc2code/coderes.h index f7562980b..cf1364022 100644 --- a/sc2/src/sc2code/coderes.h +++ b/sc2/src/sc2code/coderes.h @@ -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); diff --git a/sc2/src/sc2code/dummy.c b/sc2/src/sc2code/dummy.c index 83e9f36e8..01318ac67 100644 --- a/sc2/src/sc2code/dummy.c +++ b/sc2/src/sc2code/dummy.c @@ -317,9 +317,9 @@ _ReleaseCodeResData (void *data) } BOOLEAN -InstallCodeResType (COUNT code_type) +InstallCodeResType () { - return (InstallResTypeVectors (code_type, + return (InstallResTypeVectors ("CODE", GetCodeResData, _ReleaseCodeResData)); } diff --git a/sc2/src/sc2code/libs/gfxlib.h b/sc2/src/sc2code/libs/gfxlib.h index d261960e3..26740005b 100644 --- a/sc2/src/sc2code/libs/gfxlib.h +++ b/sc2/src/sc2code/libs/gfxlib.h @@ -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); diff --git a/sc2/src/sc2code/libs/graphics/resgfx.c b/sc2/src/sc2code/libs/graphics/resgfx.c index bc704baaf..945169bc1 100644 --- a/sc2/src/sc2code/libs/graphics/resgfx.c +++ b/sc2/src/sc2code/libs/graphics/resgfx.c @@ -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); } diff --git a/sc2/src/sc2code/libs/reslib.h b/sc2/src/sc2code/libs/reslib.h index 34f79e0af..5ac447ccd 100644 --- a/sc2/src/sc2code/libs/reslib.h +++ b/sc2/src/sc2code/libs/reslib.h @@ -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); diff --git a/sc2/src/sc2code/libs/resource/getres.c b/sc2/src/sc2code/libs/resource/getres.c index 04225e209..9e742e319 100644 --- a/sc2/src/sc2code/libs/resource/getres.c +++ b/sc2/src/sc2code/libs/resource/getres.c @@ -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; } diff --git a/sc2/src/sc2code/libs/resource/index.h b/sc2/src/sc2code/libs/resource/index.h index 262956b9e..e9c80a75d 100644 --- a/sc2/src/sc2code/libs/resource/index.h +++ b/sc2/src/sc2code/libs/resource/index.h @@ -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; }; diff --git a/sc2/src/sc2code/libs/resource/resinit.c b/sc2/src/sc2code/libs/resource/resinit.c index 9ecf11982..997fd18f3 100644 --- a/sc2/src/sc2code/libs/resource/resinit.c +++ b/sc2/src/sc2code/libs/resource/resinit.c @@ -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); + } + 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) + vtable = (ResourceHandlers *)handlerdesc->resdata; + + if (vtable->loadFun == NULL) { 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) - { - 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; - - 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 - - // 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; + ResourceHandlers *handlers; + ResourceDesc *result; + char key[TYPESIZ]; + int typelen; + + snprintf(key, TYPESIZ, "sys.%s", resType); + key[TYPESIZ-1] = '\0'; + typelen = strlen(resType); + + handlers = HMalloc (sizeof (ResourceHandlers)); + if (handlers == NULL) + { + return FALSE; } + handlers->loadFun = loadFun; + handlers->freeFun = freeFun; + handlers->resType = resType; + + result = HMalloc (sizeof (ResourceDesc)); + if (result == NULL) + return FALSE; - idx->typeInfo.handlers[resType] = handlers; - return TRUE; + 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; } diff --git a/sc2/src/sc2code/libs/resource/resintrn.h b/sc2/src/sc2code/libs/resource/resintrn.h index 2b8fb4fd6..5bc12d57c 100644 --- a/sc2/src/sc2code/libs/resource/resintrn.h +++ b/sc2/src/sc2code/libs/resource/resintrn.h @@ -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); diff --git a/sc2/src/sc2code/libs/sndlib.h b/sc2/src/sc2code/libs/sndlib.h index 21686bb40..a60d2439d 100644 --- a/sc2/src/sc2code/libs/sndlib.h +++ b/sc2/src/sc2code/libs/sndlib.h @@ -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); diff --git a/sc2/src/sc2code/libs/sound/resinst.c b/sc2/src/sc2code/libs/sound/resinst.c index 06da2664e..94eea955d 100644 --- a/sc2/src/sc2code/libs/sound/resinst.c +++ b/sc2/src/sc2code/libs/sound/resinst.c @@ -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); } diff --git a/sc2/src/sc2code/libs/strings/sresins.c b/sc2/src/sc2code/libs/strings/sresins.c index 87b7212ba..123b5f5cb 100644 --- a/sc2/src/sc2code/libs/strings/sresins.c +++ b/sc2/src/sc2code/libs/strings/sresins.c @@ -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; } diff --git a/sc2/src/sc2code/libs/strlib.h b/sc2/src/sc2code/libs/strlib.h index e666f9f91..81c1c83b3 100644 --- a/sc2/src/sc2code/libs/strlib.h +++ b/sc2/src/sc2code/libs/strlib.h @@ -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); diff --git a/sc2/src/sc2code/nameref.h b/sc2/src/sc2code/nameref.h index fefe90cb7..bdb5ea0e0 100644 --- a/sc2/src/sc2code/nameref.h +++ b/sc2/src/sc2code/nameref.h @@ -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 */