changeset 1040:ebabf5aefb76

Work on resource handling.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 17:17:36 +0200
parents 54970cd5acf0
children d0f80f6a0c65
files src/dmres.c src/dmres.h
diffstat 2 files changed, 21 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmres.c	Sun Mar 01 17:03:50 2015 +0200
+++ b/src/dmres.c	Sun Mar 01 17:17:36 2015 +0200
@@ -23,7 +23,7 @@
 #endif
 
 
-DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size)
+DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags)
 {
     DMResource *node = dmMalloc0(sizeof(DMResource));
     if (node == NULL)
@@ -33,6 +33,7 @@
     node->filename = dm_strdup(filename);
     node->rawSize = size;
     
+    node->flags = flags;
     return node;
 }
 
@@ -484,8 +485,8 @@
         if (node->size != node->length)
         {
             ret = dmError(DMERR_INVALID_DATA,
-                "Node '%s' raw size and length fields differ for uncompressed node.\n",
-                handle->filename);
+                "Node '%s' raw size and length fields differ for uncompressed node: %d <> %d.\n",
+                handle->filename, node->size, node->length);
             goto out;
         }
         if (fread(handle->rawData, sizeof(Uint8), node->size, handle->lib->packFile->file) != node->size)
@@ -761,7 +762,7 @@
         if (lib->flags & DRF_USE_STDIO)
         {
             // Hmm.. does not exist? Fall back to a stdio file
-            *phandle = handle = dmResourceNew(lib, filename, 0);
+            *phandle = handle = dmResourceNew(lib, filename, 0, 0);
             if (handle == NULL)
                 return DMERR_MALLOC;
 
@@ -805,10 +806,10 @@
     // Check master directory for resource
     if ((*phandle = handle = dmResourceFind(lib, filename)) == NULL)
     {
-        if ((*phandle = handle = dmResourceNew(lib, filename, size)) == NULL)
+        if ((*phandle = handle = dmResourceNew(
+            lib, filename, size, DMF_LOADED_RAW | DMF_UNALLOCATED)) == NULL)
             return DMERR_MALLOC;
 
-        handle->flags   = DMF_LOADED_RAW | DMF_UNALLOCATED;
         handle->fops    = &dfMemIOFileOps;
         handle->rawData = buf;
         dmResourceInsert(lib, handle);
@@ -825,7 +826,7 @@
 int dmf_create_stdio(const char *filename, const char *mode, DMResource **phandle)
 {
     DMResource *handle;
-    if ((*phandle = handle = dmResourceNew(NULL, filename, 0)) == NULL)
+    if ((*phandle = handle = dmResourceNew(NULL, filename, 0, 0)) == NULL)
         return DMERR_MALLOC;
 
     handle->fops  = &dfStdioFileOps;
@@ -846,7 +847,7 @@
 int dmf_create_stdio_stream(FILE *fh, DMResource **phandle)
 {
     DMResource *handle;
-    if ((*phandle = handle = dmResourceNew(NULL, "", 0)) == NULL)
+    if ((*phandle = handle = dmResourceNew(NULL, "", 0, 0)) == NULL)
         return DMERR_MALLOC;
 
     handle->fops = &dfStdioFHOps;
@@ -1004,7 +1005,7 @@
             }
 
             if (S_ISREG(sbuf.st_mode))
-                node = dmResourceNew(lib, dh->d_name, sbuf.st_size);
+                node = dmResourceNew(lib, dh->d_name, sbuf.st_size, 0);
         }
 
         if (node != NULL)
@@ -1075,11 +1076,11 @@
             // Initialize resources from a PACK file
             for (node = lib->packFile->entries; node != NULL; node = node->next)
             {
-                DMResource *res = dmResourceNew(lib, node->filename, node->size);
+                DMResource *res = dmResourceNew(lib, node->filename, node->size, node->flags & DMF_PACK_MASK);
                 if (res == NULL)
                 {
                     return dmError(DMERR_INIT_FAIL,
-                        "Could not allocate memory for resource node '%s' [0x%08x], %d.\n",
+                        "Could not allocate memory for resource node '%s' [0x%08x], %d bytes.\n",
                         node->filename, node->flags, node->size);
                 }
 
--- a/src/dmres.h	Sun Mar 01 17:03:50 2015 +0200
+++ b/src/dmres.h	Sun Mar 01 17:17:36 2015 +0200
@@ -44,12 +44,14 @@
 
 enum
 {
-    DMF_PERSIST     = 0x00001, // Persist loaded RAW resource
-    DMF_TEMPORARY   = 0x00002,
-    DMF_UNALLOCATED = 0x00004, // The raw data is not allocated, so do not free it
-    DMF_LOADED_RAW  = 0x01000, // Raw data has been loaded
-    DMF_LOADED_RES  = 0x02000, // Resource has been loaded
-    DMF_COMPRESSED  = 0x10000, // Resource is compressed in PACK file, otherwise raw
+    DMF_PERSIST     = 0x0001, // Persist loaded RAW resource
+    DMF_COMPRESSED  = 0x0002, // Resource is compressed in PACK file, otherwise raw
+    DMF_TEMPORARY   = 0x0004,
+    DMF_PACK_MASK   = 0x00ff, // Mask for flags that may be specified in PACK
+
+    DMF_UNALLOCATED = 0x1000, // The raw data is not allocated, so do not free it
+    DMF_LOADED_RAW  = 0x2000, // Raw data has been loaded
+    DMF_LOADED_RES  = 0x4000, // Resource has been loaded
 };
 
 
@@ -144,7 +146,7 @@
 void         dmResourcesPrune(DMResourceLib *lib, const int agems, int const flags);
 int          dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total);
 
-DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size);
+DMResource * dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags);
 void         dmResourceFree(DMResource *node);
 void         dmResourceInsert(DMResourceLib *lib, DMResource * node);
 void         dmResourceDelete(DMResourceLib *lib, DMResource * node);