Added routines to be able to save or reload fragments of a resource map.

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3107 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
mcmartin
2009-02-08 09:27:07 +00:00
parent 67dc3becff
commit f2e4a8e223
9 changed files with 91 additions and 22 deletions
+1 -1
View File
@@ -481,7 +481,7 @@ loadIndices (uio_DirHandle *dir)
for (i = 0; i < indices->numNames; i++) for (i = 0; i < indices->numNames; i++)
{ {
log_add (log_Debug, "Loading resouce index '%s'", indices->names[i]); log_add (log_Debug, "Loading resouce index '%s'", indices->names[i]);
LoadResourceIndex (dir, indices->names[i]); LoadResourceIndex (dir, indices->names[i], NULL);
numLoaded++; numLoaded++;
} }
} }
+1 -1
View File
@@ -319,7 +319,7 @@ BOOLEAN
InstallCodeResType () InstallCodeResType ()
{ {
return (InstallResTypeVectors ("SHIP", return (InstallResTypeVectors ("SHIP",
GetCodeResData, _ReleaseCodeResData)); GetCodeResData, _ReleaseCodeResData, NULL));
} }
+2 -2
View File
@@ -34,8 +34,8 @@ GetFontFileData (const char *pathname, RESOURCE_DATA *resdata)
BOOLEAN BOOLEAN
InstallGraphicResTypes (void) InstallGraphicResTypes (void)
{ {
InstallResTypeVectors ("GFXRES", GetCelFileData, _ReleaseCelData); InstallResTypeVectors ("GFXRES", GetCelFileData, _ReleaseCelData, NULL);
InstallResTypeVectors ("FONTRES", GetFontFileData, _ReleaseFontData); InstallResTypeVectors ("FONTRES", GetFontFileData, _ReleaseFontData, NULL);
return (TRUE); return (TRUE);
} }
+4 -2
View File
@@ -41,6 +41,7 @@ extern const char *_cur_resfile_name;
typedef void (ResourceLoadFun) (const char *pathname, RESOURCE_DATA *resdata); typedef void (ResourceLoadFun) (const char *pathname, RESOURCE_DATA *resdata);
typedef BOOLEAN (ResourceFreeFun) (void *handle); typedef BOOLEAN (ResourceFreeFun) (void *handle);
typedef void (ResourceStringFun) (RESOURCE_DATA *handle, char *buf, unsigned int size);
typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len); typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len);
@@ -60,7 +61,7 @@ BOOLEAN DeleteResFile (uio_DirHandle *dir, const char *filename);
RESOURCE_INDEX InitResourceSystem (); RESOURCE_INDEX InitResourceSystem ();
void UninitResourceSystem (void); void UninitResourceSystem (void);
BOOLEAN InstallResTypeVectors (const char *res_type, ResourceLoadFun *loadFun, ResourceFreeFun *freeFun); BOOLEAN InstallResTypeVectors (const char *res_type, ResourceLoadFun *loadFun, ResourceFreeFun *freeFun, ResourceStringFun *stringFun);
void *res_GetResource (RESOURCE res); void *res_GetResource (RESOURCE res);
void *res_DetachResource (RESOURCE res); void *res_DetachResource (RESOURCE res);
BOOLEAN FreeResource (RESOURCE res); BOOLEAN FreeResource (RESOURCE res);
@@ -69,7 +70,8 @@ DWORD res_GetIntResource (RESOURCE res);
BOOLEAN res_GetBooleanResource (RESOURCE res); BOOLEAN res_GetBooleanResource (RESOURCE res);
const char *res_GetResourceType (RESOURCE res); const char *res_GetResourceType (RESOURCE res);
void LoadResourceIndex (uio_DirHandle *dir, const char *filename); void LoadResourceIndex (uio_DirHandle *dir, const char *filename, const char *prefix);
void SaveResourceIndex (uio_DirHandle *dir, const char *rmpfile, const char *root, BOOLEAN strip_root);
void *GetResourceData (uio_Stream *fp, DWORD length); void *GetResourceData (uio_Stream *fp, DWORD length);
+1
View File
@@ -28,6 +28,7 @@ typedef struct resource_handlers
const char *resType; const char *resType;
ResourceLoadFun *loadFun; ResourceLoadFun *loadFun;
ResourceFreeFun *freeFun; ResourceFreeFun *freeFun;
ResourceStringFun *toString;
} ResourceHandlers; } ResourceHandlers;
typedef struct resource_desc typedef struct resource_desc
+76 -10
View File
@@ -144,19 +144,19 @@ process_resource_desc (const char *key, const char *value)
} }
} }
void static void
UseDescriptorAsRes (const char *descriptor, RESOURCE_DATA *resdata) UseDescriptorAsRes (const char *descriptor, RESOURCE_DATA *resdata)
{ {
resdata->ptr = (void *)descriptor; resdata->ptr = (void *)descriptor;
} }
void static void
DescriptorToInt (const char *descriptor, RESOURCE_DATA *resdata) DescriptorToInt (const char *descriptor, RESOURCE_DATA *resdata)
{ {
resdata->num = atoi (descriptor); resdata->num = atoi (descriptor);
} }
void static void
DescriptorToBoolean (const char *descriptor, RESOURCE_DATA *resdata) DescriptorToBoolean (const char *descriptor, RESOURCE_DATA *resdata)
{ {
if (!strcasecmp (descriptor, "true")) if (!strcasecmp (descriptor, "true"))
@@ -169,6 +169,25 @@ DescriptorToBoolean (const char *descriptor, RESOURCE_DATA *resdata)
} }
} }
static void
RawDescriptor (RESOURCE_DATA *resdata, char *buf, unsigned int size)
{
snprintf (buf, size, "%s", (char *)resdata->ptr);
}
static void
IntToString (RESOURCE_DATA *resdata, char *buf, unsigned int size)
{
snprintf (buf, size, "%d", resdata->num);
}
static void
BooleanToString (RESOURCE_DATA *resdata, char *buf, unsigned int size)
{
snprintf (buf, size, "%s", resdata->num ? "true" : "false");
}
RESOURCE_INDEX RESOURCE_INDEX
InitResourceSystem (void) InitResourceSystem (void)
{ {
@@ -176,10 +195,10 @@ InitResourceSystem (void)
_set_current_index_header (ndx); _set_current_index_header (ndx);
InstallResTypeVectors ("UNKNOWNRES", UseDescriptorAsRes, NULL); InstallResTypeVectors ("UNKNOWNRES", UseDescriptorAsRes, NULL, NULL);
InstallResTypeVectors ("STRING", UseDescriptorAsRes, NULL); InstallResTypeVectors ("STRING", UseDescriptorAsRes, NULL, RawDescriptor);
InstallResTypeVectors ("INT32", DescriptorToInt, NULL); InstallResTypeVectors ("INT32", DescriptorToInt, NULL, IntToString);
InstallResTypeVectors ("BOOLEAN", DescriptorToBoolean, NULL); InstallResTypeVectors ("BOOLEAN", DescriptorToBoolean, NULL, BooleanToString);
InstallGraphicResTypes (); InstallGraphicResTypes ();
InstallStringTableResType (); InstallStringTableResType ();
InstallAudioResTypes (); InstallAudioResTypes ();
@@ -190,9 +209,55 @@ InitResourceSystem (void)
} }
void void
LoadResourceIndex (uio_DirHandle *dir, const char *rmpfile) LoadResourceIndex (uio_DirHandle *dir, const char *rmpfile, const char *prefix)
{ {
PropFile_from_filename (dir, rmpfile, process_resource_desc, NULL); PropFile_from_filename (dir, rmpfile, process_resource_desc, prefix);
}
void
SaveResourceIndex (uio_DirHandle *dir, const char *rmpfile, const char *root, BOOLEAN strip_root)
{
uio_Stream *f;
CharHashTable_Iterator *it;
unsigned int prefix_len;
f = res_OpenResFile (dir, rmpfile, "wb");
if (!f) {
/* TODO: Warning message */
return;
}
prefix_len = root ? strlen (root) : 0;
for (it = CharHashTable_getIterator (_get_current_index_header ()->map);
!CharHashTable_iteratorDone (it);
it = CharHashTable_iteratorNext (it)) {
char *key = CharHashTable_iteratorKey (it);
if (!root || !strncmp (root, key, prefix_len)) {
ResourceDesc *value = CharHashTable_iteratorValue (it);
if (!value) {
log_add(log_Warning, "Resource %s had no value", key);
} else if (!value->vtable) {
log_add(log_Warning, "Resource %s had no type", key);
} else if (value->vtable->toString) {
char buf[256];
value->vtable->toString (&value->resdata, buf, 256);
buf[255]=0;
if (root && strip_root) {
WriteResFile (key+prefix_len, 1, strlen (key) - prefix_len, f);
} else {
WriteResFile (key, 1, strlen (key), f);
}
PutResFileChar(' ', f);
PutResFileChar('=', f);
PutResFileChar(' ', f);
WriteResFile (value->vtable->resType, 1, strlen (value->vtable->resType), f);
PutResFileChar(':', f);
WriteResFile (buf, 1, strlen (buf), f);
PutResFileNewline(f);
}
}
}
res_CloseResFile (f);
CharHashTable_freeIterator (it);
} }
void void
@@ -204,7 +269,7 @@ UninitResourceSystem (void)
BOOLEAN BOOLEAN
InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun, InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun,
ResourceFreeFun *freeFun) ResourceFreeFun *freeFun, ResourceStringFun *stringFun)
{ {
ResourceHandlers *handlers; ResourceHandlers *handlers;
ResourceDesc *result; ResourceDesc *result;
@@ -223,6 +288,7 @@ InstallResTypeVectors (const char *resType, ResourceLoadFun *loadFun,
} }
handlers->loadFun = loadFun; handlers->loadFun = loadFun;
handlers->freeFun = freeFun; handlers->freeFun = freeFun;
handlers->toString = stringFun;
handlers->resType = resType; handlers->resType = resType;
result = HMalloc (sizeof (ResourceDesc)); result = HMalloc (sizeof (ResourceDesc));
+2 -2
View File
@@ -33,8 +33,8 @@ GetMusicFileData (const char *pathname, RESOURCE_DATA *resdata)
BOOLEAN BOOLEAN
InstallAudioResTypes (void) InstallAudioResTypes (void)
{ {
InstallResTypeVectors ("SNDRES", GetSoundBankFileData, _ReleaseSoundBankData); InstallResTypeVectors ("SNDRES", GetSoundBankFileData, _ReleaseSoundBankData, NULL);
InstallResTypeVectors ("MUSICRES", GetMusicFileData, _ReleaseMusicData); InstallResTypeVectors ("MUSICRES", GetMusicFileData, _ReleaseMusicData, NULL);
return (TRUE); return (TRUE);
} }
+3 -3
View File
@@ -33,9 +33,9 @@ GetBinaryTableFileData (const char *pathname, RESOURCE_DATA *resdata)
BOOLEAN BOOLEAN
InstallStringTableResType (void) InstallStringTableResType (void)
{ {
InstallResTypeVectors ("STRTAB", GetStringTableFileData, FreeResourceData); InstallResTypeVectors ("STRTAB", GetStringTableFileData, FreeResourceData, NULL);
InstallResTypeVectors ("BINTAB", GetBinaryTableFileData, FreeResourceData); InstallResTypeVectors ("BINTAB", GetBinaryTableFileData, FreeResourceData, NULL);
InstallResTypeVectors ("CONVERSATION", _GetConversationData, FreeResourceData); InstallResTypeVectors ("CONVERSATION", _GetConversationData, FreeResourceData, NULL);
return TRUE; return TRUE;
} }
+1 -1
View File
@@ -159,7 +159,7 @@ err:
BOOLEAN BOOLEAN
InstallVideoResType (void) InstallVideoResType (void)
{ {
InstallResTypeVectors ("3DOVID", GetLegacyVideoData, FreeLegacyVideoData); InstallResTypeVectors ("3DOVID", GetLegacyVideoData, FreeLegacyVideoData, NULL);
return TRUE; return TRUE;
} }