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:
- 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
+3 -3
View File
@@ -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
+6 -6
View File
@@ -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);
}
+8 -4
View File
@@ -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);
+57 -13
View File
@@ -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)
@@ -121,16 +151,22 @@ res_FreeResource (RESOURCE res)
return;
}
if (desc->resdata == NULL)
freeFun = desc->vtable->freeFun;
if (freeFun == NULL)
{
log_add (log_Debug, "Warning: trying to free a non-heap resource.");
return;
}
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;
}
+3 -3
View File
@@ -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
+28 -28
View File
@@ -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,40 +140,31 @@ 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;
+1 -1
View File
@@ -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);
+6 -6
View File
@@ -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
+6 -6
View File
@@ -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