Made the INT32 and BOOLEAN resource types more type-sane.

This has required reworking the way load methods work slightly.



git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2973 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
mcmartin
2008-05-15 03:15:27 +00:00
parent f183008374
commit 38b00505d5
10 changed files with 119 additions and 70 deletions
+1
View File
@@ -1,4 +1,5 @@
Changes towards version 0.7: Changes towards version 0.7:
- INT32, BOOLEAN, and STRING resource types - Michael
- UNKNOWNRES is now safe to load, and "loads" as its resvalue - 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 - CODE is now SHIP, and uses an integer descriptor instead of a one-byte
.cod file - Michael .cod file - Michael
+3 -3
View File
@@ -37,8 +37,8 @@ typedef struct
RACE_DESC data _ALIGNED_ANY; RACE_DESC data _ALIGNED_ANY;
} CODERES_STRUCT; } CODERES_STRUCT;
static void * static void
GetCodeResData (const char *ship_id) GetCodeResData (const char *ship_id, RESOURCE_DATA *resdata)
{ {
enum enum
{ {
@@ -305,7 +305,7 @@ enum
cs->data = *RDPtr; // Structure assignment. cs->data = *RDPtr; // Structure assignment.
} }
} }
return (hData); resdata->ptr = (hData);
} }
BOOLEAN BOOLEAN
+6 -6
View File
@@ -18,16 +18,16 @@
#include "gfxintrn.h" #include "gfxintrn.h"
static void * static void
GetCelFileData (const char *pathname) GetCelFileData (const char *pathname, RESOURCE_DATA *resdata)
{ {
return LoadResourceFromPath (pathname, _GetCelData); resdata->ptr = LoadResourceFromPath (pathname, _GetCelData);
} }
static void * static void
GetFontFileData (const char *pathname) GetFontFileData (const char *pathname, RESOURCE_DATA *resdata)
{ {
return LoadResourceFromPath (pathname, _GetFontData); resdata->ptr = LoadResourceFromPath (pathname, _GetFontData);
} }
+8 -4
View File
@@ -30,16 +30,18 @@ typedef RESOURCE_INDEX_DESC *RESOURCE_INDEX;
typedef const char *RESOURCE; typedef const char *RESOURCE;
typedef union {
DWORD num;
void *ptr;
} RESOURCE_DATA;
#define NULL_RESOURCE NULL #define NULL_RESOURCE NULL
extern const char *_cur_resfile_name; 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); typedef BOOLEAN (ResourceFreeFun) (void *handle);
void *UseDescriptorAsRes (const char *descriptor);
BOOLEAN NullFreeRes (void *handle);
typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len); typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len);
void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn); void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn);
@@ -63,6 +65,8 @@ void *res_GetResource (RESOURCE res);
void *res_DetachResource (RESOURCE res); void *res_DetachResource (RESOURCE res);
BOOLEAN FreeResource (RESOURCE res); BOOLEAN FreeResource (RESOURCE res);
COUNT CountResourceTypes (void); COUNT CountResourceTypes (void);
DWORD res_GetIntResource (RESOURCE res);
BOOLEAN res_GetBooleanResource (RESOURCE res);
void LoadResourceIndex (uio_DirHandle *dir, const char *filename); void LoadResourceIndex (uio_DirHandle *dir, const char *filename);
+57 -13
View File
@@ -32,11 +32,10 @@ lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res) {
return (ResourceDesc *) CharHashTable_find (idx->map, res); return (ResourceDesc *) CharHashTable_find (idx->map, res);
} }
void * void
loadResourceDesc (ResourceDesc *desc) loadResourceDesc (ResourceDesc *desc)
{ {
desc->resdata = desc->vtable->loadFun (desc->fname); desc->vtable->loadFun (desc->fname, &desc->resdata);
return desc->resdata;
} }
void * void *
@@ -97,15 +96,46 @@ res_GetResource (RESOURCE res)
return NULL; return NULL;
} }
if (desc->resdata != NULL) if (desc->resdata.ptr != NULL)
return desc->resdata; return desc->resdata.ptr;
loadResourceDesc (desc); loadResourceDesc (desc);
return desc->resdata; return desc->resdata.ptr;
// May still be NULL, if the load failed. // 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! // NB: this function appears to be never called!
void void
res_FreeResource (RESOURCE res) res_FreeResource (RESOURCE res)
@@ -120,17 +150,23 @@ res_FreeResource (RESOURCE res)
"resource."); "resource.");
return; 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 " log_add (log_Debug, "Warning: trying to free not loaded "
"resource."); "resource.");
return; return;
} }
freeFun = desc->vtable->freeFun; (*freeFun) (desc->resdata.ptr);
(*freeFun) (desc->resdata); desc->resdata.ptr = NULL;
desc->resdata = NULL;
} }
// By calling this function the caller will be responsible of unloading // By calling this function the caller will be responsible of unloading
@@ -141,6 +177,7 @@ void *
res_DetachResource (RESOURCE res) res_DetachResource (RESOURCE res)
{ {
ResourceDesc *desc; ResourceDesc *desc;
ResourceFreeFun *freeFun;
void *result; void *result;
desc = lookupResourceDesc (_get_current_index_header(), res); desc = lookupResourceDesc (_get_current_index_header(), res);
@@ -151,15 +188,22 @@ res_DetachResource (RESOURCE res)
return NULL; 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 " log_add (log_Debug, "Warning: trying to detach from a not loaded "
"resource."); "resource.");
return NULL; return NULL;
} }
result = desc->resdata; result = desc->resdata.ptr;
desc->resdata = NULL; desc->resdata.ptr = NULL;
return result; return result;
} }
+3 -3
View File
@@ -23,19 +23,19 @@
#include "reslib.h" #include "reslib.h"
#include "libs/uio/charhashtable.h" #include "libs/uio/charhashtable.h"
typedef struct typedef struct resource_handlers
{ {
const char *resType; const char *resType;
ResourceLoadFun *loadFun; ResourceLoadFun *loadFun;
ResourceFreeFun *freeFun; ResourceFreeFun *freeFun;
} ResourceHandlers; } ResourceHandlers;
typedef struct typedef struct resource_desc
{ {
RESOURCE res_id; RESOURCE res_id;
char *fname; char *fname;
ResourceHandlers *vtable; ResourceHandlers *vtable;
void *resdata; RESOURCE_DATA resdata;
} ResourceDesc; } ResourceDesc;
struct resource_index_desc struct resource_index_desc
+28 -28
View File
@@ -84,7 +84,7 @@ newResourceDesc (const char *res_id, const char *resval)
handlerdesc = lookupResourceDesc(idx, "sys.UNKNOWNRES"); handlerdesc = lookupResourceDesc(idx, "sys.UNKNOWNRES");
} }
vtable = (ResourceHandlers *)handlerdesc->resdata; vtable = (ResourceHandlers *)handlerdesc->resdata.ptr;
if (vtable->loadFun == NULL) if (vtable->loadFun == NULL)
{ {
@@ -101,7 +101,16 @@ newResourceDesc (const char *res_id, const char *resval)
strncpy (result->fname, path, pathlen); strncpy (result->fname, path, pathlen);
result->fname[pathlen] = '\0'; result->fname[pathlen] = '\0';
result->vtable = vtable; 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; return result;
} }
@@ -117,7 +126,7 @@ process_resource_desc (const char *key, const char *value)
ResourceDesc *oldDesc = (ResourceDesc *)CharHashTable_find (map, key); ResourceDesc *oldDesc = (ResourceDesc *)CharHashTable_find (map, key);
if (oldDesc != NULL) if (oldDesc != NULL)
{ {
if (newDesc->resdata != NULL) if (newDesc->resdata.ptr != NULL)
{ {
/* XXX: It might be nice to actually clean it up properly */ /* XXX: It might be nice to actually clean it up properly */
log_add (log_Warning, "LEAK WARNING: Replaced '%s' while it was live", key); 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 * void
UseDescriptorAsRes (const char *descriptor) UseDescriptorAsRes (const char *descriptor, RESOURCE_DATA *resdata)
{ {
return (void *)descriptor; resdata->ptr = (void *)descriptor;
} }
void * void
DescriptorToInt (const char *descriptor) DescriptorToInt (const char *descriptor, RESOURCE_DATA *resdata)
{ {
intptr_t value = atoi(descriptor); resdata->num = atoi (descriptor);
return (void *) value;
} }
void * void
DescriptorToBoolean (const char *descriptor) DescriptorToBoolean (const char *descriptor, RESOURCE_DATA *resdata)
{ {
if (!stricmp(descriptor, "true")) if (!stricmp (descriptor, "true"))
{ {
return (void *)(1); resdata->num = TRUE;
} }
else else
{ {
return (void *)(0); resdata->num = FALSE;
} }
} }
BOOLEAN
NullFreeRes (void *data)
{
(void)data;
return TRUE;
}
RESOURCE_INDEX RESOURCE_INDEX
InitResourceSystem (void) InitResourceSystem (void)
@@ -172,10 +172,10 @@ InitResourceSystem (void)
_set_current_index_header (ndx); _set_current_index_header (ndx);
InstallResTypeVectors ("UNKNOWNRES", UseDescriptorAsRes, NullFreeRes); InstallResTypeVectors ("UNKNOWNRES", UseDescriptorAsRes, NULL);
InstallResTypeVectors ("STRING", UseDescriptorAsRes, NullFreeRes); InstallResTypeVectors ("STRING", UseDescriptorAsRes, NULL);
InstallResTypeVectors ("INT32", DescriptorToInt, NullFreeRes); InstallResTypeVectors ("INT32", DescriptorToInt, NULL);
InstallResTypeVectors ("BOOLEAN", DescriptorToBoolean, NullFreeRes); InstallResTypeVectors ("BOOLEAN", DescriptorToBoolean, NULL);
InstallGraphicResTypes (); InstallGraphicResTypes ();
InstallStringTableResType (); InstallStringTableResType ();
InstallAudioResTypes (); InstallAudioResTypes ();
@@ -227,7 +227,7 @@ InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun,
strncpy (result->fname, resType, typelen); strncpy (result->fname, resType, typelen);
result->fname[typelen] = '\0'; result->fname[typelen] = '\0';
result->vtable = NULL; result->vtable = NULL;
result->resdata = handlers; result->resdata.ptr = handlers;
CharHashTable_HashTable *map = _get_current_index_header ()->map; CharHashTable_HashTable *map = _get_current_index_header ()->map;
return CharHashTable_add (map, key, result) != 0; return CharHashTable_add (map, key, result) != 0;
+1 -1
View File
@@ -24,7 +24,7 @@
#include "index.h" #include "index.h"
ResourceDesc *lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res); ResourceDesc *lookupResourceDesc (RESOURCE_INDEX idx, RESOURCE res);
void *loadResourceDesc (ResourceDesc *desc); void loadResourceDesc (ResourceDesc *desc);
void _set_current_index_header (RESOURCE_INDEX newResourceIndex); void _set_current_index_header (RESOURCE_INDEX newResourceIndex);
RESOURCE_INDEX _get_current_index_header (void); RESOURCE_INDEX _get_current_index_header (void);
+6 -6
View File
@@ -18,16 +18,16 @@
#include "sndintrn.h" #include "sndintrn.h"
static void * static void
GetSoundBankFileData (const char *pathname) GetSoundBankFileData (const char *pathname, RESOURCE_DATA *resdata)
{ {
return LoadResourceFromPath (pathname, _GetSoundBankData); resdata->ptr = LoadResourceFromPath (pathname, _GetSoundBankData);
} }
static void * static void
GetMusicFileData (const char *pathname) GetMusicFileData (const char *pathname, RESOURCE_DATA *resdata)
{ {
return LoadResourceFromPath (pathname, _GetMusicData); resdata->ptr = LoadResourceFromPath (pathname, _GetMusicData);
} }
BOOLEAN BOOLEAN
+6 -6
View File
@@ -18,16 +18,16 @@
#include "strintrn.h" #include "strintrn.h"
static void * static void
GetStringTableFileData (const char *pathname) GetStringTableFileData (const char *pathname, RESOURCE_DATA *resdata)
{ {
return LoadResourceFromPath (pathname, _GetStringData); resdata->ptr = LoadResourceFromPath (pathname, _GetStringData);
} }
static void * static void
GetBinaryTableFileData (const char *pathname) GetBinaryTableFileData (const char *pathname, RESOURCE_DATA *resdata)
{ {
return LoadResourceFromPath (pathname, _GetBinaryTableData); resdata->ptr = LoadResourceFromPath (pathname, _GetBinaryTableData);
} }
BOOLEAN BOOLEAN