diff --git a/sc2/ChangeLog b/sc2/ChangeLog index c86b08332..9f431fac9 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.7: +- INT32, BOOLEAN, and STRING resource types - Michael - UNKNOWNRES is now safe to load, and "loads" as its resvalue - Michael - CODE is now SHIP, and uses an integer descriptor instead of a one-byte .cod file - Michael diff --git a/sc2/src/sc2code/dummy.c b/sc2/src/sc2code/dummy.c index f8e84fa49..6a4f2ec57 100644 --- a/sc2/src/sc2code/dummy.c +++ b/sc2/src/sc2code/dummy.c @@ -37,8 +37,8 @@ typedef struct RACE_DESC data _ALIGNED_ANY; } CODERES_STRUCT; -static void * -GetCodeResData (const char *ship_id) +static void +GetCodeResData (const char *ship_id, RESOURCE_DATA *resdata) { enum { @@ -305,7 +305,7 @@ enum cs->data = *RDPtr; // Structure assignment. } } - return (hData); + resdata->ptr = (hData); } BOOLEAN diff --git a/sc2/src/sc2code/libs/graphics/resgfx.c b/sc2/src/sc2code/libs/graphics/resgfx.c index 528f7ae6c..18cb58e22 100644 --- a/sc2/src/sc2code/libs/graphics/resgfx.c +++ b/sc2/src/sc2code/libs/graphics/resgfx.c @@ -18,16 +18,16 @@ #include "gfxintrn.h" -static void * -GetCelFileData (const char *pathname) +static void +GetCelFileData (const char *pathname, RESOURCE_DATA *resdata) { - return LoadResourceFromPath (pathname, _GetCelData); + resdata->ptr = LoadResourceFromPath (pathname, _GetCelData); } -static void * -GetFontFileData (const char *pathname) +static void +GetFontFileData (const char *pathname, RESOURCE_DATA *resdata) { - return LoadResourceFromPath (pathname, _GetFontData); + resdata->ptr = LoadResourceFromPath (pathname, _GetFontData); } diff --git a/sc2/src/sc2code/libs/reslib.h b/sc2/src/sc2code/libs/reslib.h index 8e05fc6b6..ba235c033 100644 --- a/sc2/src/sc2code/libs/reslib.h +++ b/sc2/src/sc2code/libs/reslib.h @@ -30,16 +30,18 @@ typedef RESOURCE_INDEX_DESC *RESOURCE_INDEX; typedef const char *RESOURCE; +typedef union { + DWORD num; + void *ptr; +} RESOURCE_DATA; + #define NULL_RESOURCE NULL extern const char *_cur_resfile_name; -typedef void *(ResourceLoadFun) (const char *pathname); +typedef void (ResourceLoadFun) (const char *pathname, RESOURCE_DATA *resdata); typedef BOOLEAN (ResourceFreeFun) (void *handle); -void *UseDescriptorAsRes (const char *descriptor); -BOOLEAN NullFreeRes (void *handle); - typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len); void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn); @@ -63,6 +65,8 @@ void *res_GetResource (RESOURCE res); void *res_DetachResource (RESOURCE res); BOOLEAN FreeResource (RESOURCE res); COUNT CountResourceTypes (void); +DWORD res_GetIntResource (RESOURCE res); +BOOLEAN res_GetBooleanResource (RESOURCE res); void LoadResourceIndex (uio_DirHandle *dir, const char *filename); diff --git a/sc2/src/sc2code/libs/resource/getres.c b/sc2/src/sc2code/libs/resource/getres.c index 44d77788a..f735ed12f 100644 --- a/sc2/src/sc2code/libs/resource/getres.c +++ b/sc2/src/sc2code/libs/resource/getres.c @@ -32,11 +32,10 @@ lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res) { return (ResourceDesc *) CharHashTable_find (idx->map, res); } -void * +void loadResourceDesc (ResourceDesc *desc) { - desc->resdata = desc->vtable->loadFun (desc->fname); - return desc->resdata; + desc->vtable->loadFun (desc->fname, &desc->resdata); } void * @@ -97,15 +96,46 @@ res_GetResource (RESOURCE res) return NULL; } - if (desc->resdata != NULL) - return desc->resdata; + if (desc->resdata.ptr != NULL) + return desc->resdata.ptr; loadResourceDesc (desc); - return desc->resdata; + return desc->resdata.ptr; // May still be NULL, if the load failed. } +DWORD +res_GetIntResource (RESOURCE res) +{ + RESOURCE_INDEX resourceIndex; + ResourceDesc *desc; + + if (res == NULL_RESOURCE) + { + log_add (log_Warning, "Trying to get null resource"); + return 0; + } + + resourceIndex = _get_current_index_header (); + + desc = lookupResourceDesc (resourceIndex, res); + if (desc == NULL) + { + log_add (log_Warning, "Trying to get undefined resource '%s'", + res); + return 0; + } + + return desc->resdata.num; +} + +BOOLEAN +res_GetBooleanResource (RESOURCE res) +{ + return (res_GetIntResource (res) != 0); +} + // NB: this function appears to be never called! void res_FreeResource (RESOURCE res) @@ -120,17 +150,23 @@ res_FreeResource (RESOURCE res) "resource."); return; } + + freeFun = desc->vtable->freeFun; + if (freeFun == NULL) + { + log_add (log_Debug, "Warning: trying to free a non-heap resource."); + return; + } - if (desc->resdata == NULL) + if (desc->resdata.ptr == NULL) { log_add (log_Debug, "Warning: trying to free not loaded " "resource."); return; } - freeFun = desc->vtable->freeFun; - (*freeFun) (desc->resdata); - desc->resdata = NULL; + (*freeFun) (desc->resdata.ptr); + desc->resdata.ptr = NULL; } // By calling this function the caller will be responsible of unloading @@ -141,6 +177,7 @@ void * res_DetachResource (RESOURCE res) { ResourceDesc *desc; + ResourceFreeFun *freeFun; void *result; desc = lookupResourceDesc (_get_current_index_header(), res); @@ -151,15 +188,22 @@ res_DetachResource (RESOURCE res) return NULL; } - if (desc->resdata == NULL) + freeFun = desc->vtable->freeFun; + if (freeFun == NULL) + { + log_add (log_Debug, "Warning: trying to detatch from a non-heap resource."); + return NULL; + } + + if (desc->resdata.ptr == NULL) { log_add (log_Debug, "Warning: trying to detach from a not loaded " "resource."); return NULL; } - result = desc->resdata; - desc->resdata = NULL; + result = desc->resdata.ptr; + desc->resdata.ptr = NULL; return result; } diff --git a/sc2/src/sc2code/libs/resource/index.h b/sc2/src/sc2code/libs/resource/index.h index e9c80a75d..680d3d551 100644 --- a/sc2/src/sc2code/libs/resource/index.h +++ b/sc2/src/sc2code/libs/resource/index.h @@ -23,19 +23,19 @@ #include "reslib.h" #include "libs/uio/charhashtable.h" -typedef struct +typedef struct resource_handlers { const char *resType; ResourceLoadFun *loadFun; ResourceFreeFun *freeFun; } ResourceHandlers; -typedef struct +typedef struct resource_desc { RESOURCE res_id; char *fname; ResourceHandlers *vtable; - void *resdata; + RESOURCE_DATA resdata; } ResourceDesc; struct resource_index_desc diff --git a/sc2/src/sc2code/libs/resource/resinit.c b/sc2/src/sc2code/libs/resource/resinit.c index 35f5cb250..5888b57e7 100644 --- a/sc2/src/sc2code/libs/resource/resinit.c +++ b/sc2/src/sc2code/libs/resource/resinit.c @@ -84,7 +84,7 @@ newResourceDesc (const char *res_id, const char *resval) handlerdesc = lookupResourceDesc(idx, "sys.UNKNOWNRES"); } - vtable = (ResourceHandlers *)handlerdesc->resdata; + vtable = (ResourceHandlers *)handlerdesc->resdata.ptr; if (vtable->loadFun == NULL) { @@ -101,7 +101,16 @@ newResourceDesc (const char *res_id, const char *resval) strncpy (result->fname, path, pathlen); result->fname[pathlen] = '\0'; result->vtable = vtable; - result->resdata = NULL; + + if (vtable->freeFun == NULL) + { + /* Non-heap resources are raw values. Work those out at load time. */ + vtable->loadFun (result->fname, &result->resdata); + } + else + { + result->resdata.ptr = NULL; + } return result; } @@ -117,7 +126,7 @@ process_resource_desc (const char *key, const char *value) ResourceDesc *oldDesc = (ResourceDesc *)CharHashTable_find (map, key); if (oldDesc != NULL) { - if (newDesc->resdata != NULL) + if (newDesc->resdata.ptr != NULL) { /* XXX: It might be nice to actually clean it up properly */ log_add (log_Warning, "LEAK WARNING: Replaced '%s' while it was live", key); @@ -131,39 +140,30 @@ process_resource_desc (const char *key, const char *value) } } -void * -UseDescriptorAsRes (const char *descriptor) +void +UseDescriptorAsRes (const char *descriptor, RESOURCE_DATA *resdata) { - return (void *)descriptor; + resdata->ptr = (void *)descriptor; } -void * -DescriptorToInt (const char *descriptor) +void +DescriptorToInt (const char *descriptor, RESOURCE_DATA *resdata) { - intptr_t value = atoi(descriptor); - return (void *) value; + resdata->num = atoi (descriptor); } -void * -DescriptorToBoolean (const char *descriptor) +void +DescriptorToBoolean (const char *descriptor, RESOURCE_DATA *resdata) { - if (!stricmp(descriptor, "true")) + if (!stricmp (descriptor, "true")) { - return (void *)(1); + resdata->num = TRUE; } else { - return (void *)(0); + resdata->num = FALSE; } } - - -BOOLEAN -NullFreeRes (void *data) -{ - (void)data; - return TRUE; -} RESOURCE_INDEX InitResourceSystem (void) @@ -172,10 +172,10 @@ InitResourceSystem (void) _set_current_index_header (ndx); - InstallResTypeVectors ("UNKNOWNRES", UseDescriptorAsRes, NullFreeRes); - InstallResTypeVectors ("STRING", UseDescriptorAsRes, NullFreeRes); - InstallResTypeVectors ("INT32", DescriptorToInt, NullFreeRes); - InstallResTypeVectors ("BOOLEAN", DescriptorToBoolean, NullFreeRes); + InstallResTypeVectors ("UNKNOWNRES", UseDescriptorAsRes, NULL); + InstallResTypeVectors ("STRING", UseDescriptorAsRes, NULL); + InstallResTypeVectors ("INT32", DescriptorToInt, NULL); + InstallResTypeVectors ("BOOLEAN", DescriptorToBoolean, NULL); InstallGraphicResTypes (); InstallStringTableResType (); InstallAudioResTypes (); @@ -227,7 +227,7 @@ InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun, strncpy (result->fname, resType, typelen); result->fname[typelen] = '\0'; result->vtable = NULL; - result->resdata = handlers; + result->resdata.ptr = 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 8543944b4..cd14776c2 100644 --- a/sc2/src/sc2code/libs/resource/resintrn.h +++ b/sc2/src/sc2code/libs/resource/resintrn.h @@ -24,7 +24,7 @@ #include "index.h" ResourceDesc *lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res); -void *loadResourceDesc (ResourceDesc *desc); +void loadResourceDesc (ResourceDesc *desc); void _set_current_index_header (RESOURCE_INDEX newResourceIndex); RESOURCE_INDEX _get_current_index_header (void); diff --git a/sc2/src/sc2code/libs/sound/resinst.c b/sc2/src/sc2code/libs/sound/resinst.c index 9f4cb7494..ee799854a 100644 --- a/sc2/src/sc2code/libs/sound/resinst.c +++ b/sc2/src/sc2code/libs/sound/resinst.c @@ -18,16 +18,16 @@ #include "sndintrn.h" -static void * -GetSoundBankFileData (const char *pathname) +static void +GetSoundBankFileData (const char *pathname, RESOURCE_DATA *resdata) { - return LoadResourceFromPath (pathname, _GetSoundBankData); + resdata->ptr = LoadResourceFromPath (pathname, _GetSoundBankData); } -static void * -GetMusicFileData (const char *pathname) +static void +GetMusicFileData (const char *pathname, RESOURCE_DATA *resdata) { - return LoadResourceFromPath (pathname, _GetMusicData); + resdata->ptr = LoadResourceFromPath (pathname, _GetMusicData); } BOOLEAN diff --git a/sc2/src/sc2code/libs/strings/sresins.c b/sc2/src/sc2code/libs/strings/sresins.c index 6b6074ac4..d431768a3 100644 --- a/sc2/src/sc2code/libs/strings/sresins.c +++ b/sc2/src/sc2code/libs/strings/sresins.c @@ -18,16 +18,16 @@ #include "strintrn.h" -static void * -GetStringTableFileData (const char *pathname) +static void +GetStringTableFileData (const char *pathname, RESOURCE_DATA *resdata) { - return LoadResourceFromPath (pathname, _GetStringData); + resdata->ptr = LoadResourceFromPath (pathname, _GetStringData); } -static void * -GetBinaryTableFileData (const char *pathname) +static void +GetBinaryTableFileData (const char *pathname, RESOURCE_DATA *resdata) { - return LoadResourceFromPath (pathname, _GetBinaryTableData); + resdata->ptr = LoadResourceFromPath (pathname, _GetBinaryTableData); } BOOLEAN