From 6d0b82682e1e0728cb3171c61abf68c4400833dd Mon Sep 17 00:00:00 2001 From: _ <_> Date: Mon, 23 Apr 2018 17:15:56 -0700 Subject: [PATCH 1/4] Added getheader() functions for tex and geo component --- include/devil1geo.h | 7 +++++-- include/devil1tex.h | 3 +++ src/devil1geo.c | 10 ++++++++++ src/devil1tex.c | 13 ++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/devil1geo.h b/include/devil1geo.h index 7b6b3d2..6d08f1e 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -85,6 +85,9 @@ typedef struct { // input: pointer to struct void (* const printcoordinate)(struct Coordinate*, unsigned int); +// input: pointer to struct, file data + bool (* const getheader) (struct Header*, const char*); + // input: pointer of pointer to struct, order, file data // ** = 'pass by reference' of a pointer to struct bool (* const getmeshheader) (struct MeshHeader**, @@ -96,11 +99,11 @@ typedef struct { unsigned int offset, const char * const); -// input: pointer to struct, order, file data +// input: pointer to struct, order, file data, file size bool (* const getmesh) (struct Mesh*, unsigned int i, const char*, - unsigned int filesize); + unsigned int); } fn_devil1geo; extern fn_devil1geo const DEVIL1GEO; diff --git a/include/devil1tex.h b/include/devil1tex.h index 9013b2b..b002a0a 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -45,6 +45,9 @@ typedef struct { // input: pointer to struct void (* const printbatchdesc)(struct TextureBatchDescriptor*); +// input: pointer to struct, file data + bool (* const getheader) (struct TexturePack*, const char*); + // input: pointer of pointer to struct, order, file data, file size // ** = 'pass by reference' of a pointer to struct bool (* const getbatchdesc) (struct TextureBatchDescriptor**, diff --git a/src/devil1geo.c b/src/devil1geo.c index b329072..d62e13b 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -9,6 +9,7 @@ static void printmeshbatch(struct Batch*); static void printcoordinate(struct Coordinate*, unsigned int); +static bool getgheader(struct Header*, const char*); static bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); @@ -20,6 +21,7 @@ fn_devil1geo const DEVIL1GEO = {printgheader, printmeshheader, printmeshbatch, printcoordinate, + getgheader, getmeshheader, getmeshbatch, getmesh}; @@ -74,6 +76,14 @@ static void printcoordinate(struct Coordinate *p, unsigned int count) { } } +static bool getgheader(struct Header* h, const char* filedata) { + if (filedata == NULL) { + return false; + } + h = (struct Header*)filedata; + return true; +} + static bool getmeshheader(struct MeshHeader **hs, unsigned int i, const char * const filedata) { diff --git a/src/devil1tex.c b/src/devil1tex.c index 5b4d8dd..1bbf2e9 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -8,8 +8,10 @@ static void printtph(struct TexturePack*); // Print Texture Batch Descriptor. static void printtbd(struct TextureBatchDescriptor*); -// Get Texture Batch Descriptor. +// Get Texture Pack Header +static bool getpackheader(struct TexturePack*, const char*); +// Get Texture Batch Descriptor. static bool gettexdescriptor(struct TextureBatchDescriptor**, unsigned int, const char *, @@ -30,6 +32,7 @@ static bool unpacktexbatch(struct Texture*, fn_devil1tex const DEVIL1TEX = {printtph, printtbd, + getpackheader, gettexdescriptor, gettexbatch, unpacktexbatch}; @@ -55,6 +58,14 @@ static void printtbd(struct TextureBatchDescriptor *bd) { } } +static bool getpackheader(struct TexturePack* p, const char *filedata) { + if (filedata == NULL) { + return false; + } + p = (struct TexturePack*)filedata; + return true; +} + static bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, unsigned int i, const char *filedata, From 046e1f8617ba27b0cedb3de78e9260888f22ab4a Mon Sep 17 00:00:00 2001 From: _ <_> Date: Mon, 23 Apr 2018 17:28:39 -0700 Subject: [PATCH 2/4] Replaced old casts with function calls --- demo/extractmesh.c | 4 ++-- demo/extracttexture.c | 3 +-- src/devil1geo.c | 6 +++--- src/devil1tex.c | 7 +++---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/demo/extractmesh.c b/demo/extractmesh.c index 71dbe23..ba49f9b 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -59,10 +59,10 @@ void writemesh(const struct MeshHeader *mh, void extractmeshes(const char *filedata, const char *filename, unsigned int filesize) { - if (filedata == NULL || filesize <= 0) { + struct Header *h = NULL; + if (!(DEVIL1GEO.getheader(h, filedata))|| filesize <= 0) { return; } - struct Header *h = (struct Header*)filedata; struct MeshHeader *mh = NULL; struct Mesh m; m.b = NULL; diff --git a/demo/extracttexture.c b/demo/extracttexture.c index f686ff7..839ad84 100644 --- a/demo/extracttexture.c +++ b/demo/extracttexture.c @@ -8,10 +8,9 @@ void extracttextures(const char *filedata, struct Texture *t = NULL; struct TextureBatchDescriptor *d = NULL; char * fmt = NULL; - if (filedata == NULL || filesize == 0) { + if (!(DEVIL1TEX.getheader(p, filedata)) || filesize == 0) { return; } - p = (struct TexturePack*)filedata; fmt = (char*)malloc(strlen(filename) + 3 + 4); unsigned int i; unsigned int j; diff --git a/src/devil1geo.c b/src/devil1geo.c index d62e13b..70f5bd8 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -87,11 +87,11 @@ static bool getgheader(struct Header* h, const char* filedata) { static bool getmeshheader(struct MeshHeader **hs, unsigned int i, const char * const filedata) { - bool done = false; - if (hs == NULL || filedata == NULL) { + bool done = false; + struct Header *h = NULL; + if (hs == NULL || !(getgheader(h, filedata))) { return done; } - struct Header *h = (struct Header*)filedata; if (h -> numMesh < i) { return done; } diff --git a/src/devil1tex.c b/src/devil1tex.c index 1bbf2e9..92bc717 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -9,7 +9,7 @@ static void printtph(struct TexturePack*); static void printtbd(struct TextureBatchDescriptor*); // Get Texture Pack Header -static bool getpackheader(struct TexturePack*, const char*); +static inline bool getpackheader(struct TexturePack*, const char*); // Get Texture Batch Descriptor. static bool gettexdescriptor(struct TextureBatchDescriptor**, @@ -58,7 +58,7 @@ static void printtbd(struct TextureBatchDescriptor *bd) { } } -static bool getpackheader(struct TexturePack* p, const char *filedata) { +inline static bool getpackheader(struct TexturePack* p, const char *filedata) { if (filedata == NULL) { return false; } @@ -86,10 +86,9 @@ static bool gettexbatch(struct TextureBatch **batch, const char *filedata, unsigned int filesize) { struct TexturePack *p = NULL; - if (filedata == NULL) { // no data to get batch from. + if (!(getpackheader(p, filedata))) { return false; } - p = (struct TexturePack*)filedata; if (i > p -> batchNumber) { // no such batch in texture pack return false; } From 9a14430a8839432cbef8dfb575c92ae29686feb1 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Mon, 23 Apr 2018 17:43:04 -0700 Subject: [PATCH 3/4] Fixed segmentation fault by clarifying pointer of pointer --- demo/extractmesh.c | 2 +- demo/extracttexture.c | 2 +- include/devil1geo.h | 2 +- include/devil1tex.h | 2 +- src/devil1geo.c | 8 ++++---- src/devil1tex.c | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/demo/extractmesh.c b/demo/extractmesh.c index ba49f9b..d15dde7 100644 --- a/demo/extractmesh.c +++ b/demo/extractmesh.c @@ -60,7 +60,7 @@ void extractmeshes(const char *filedata, const char *filename, unsigned int filesize) { struct Header *h = NULL; - if (!(DEVIL1GEO.getheader(h, filedata))|| filesize <= 0) { + if (!(DEVIL1GEO.getheader(&h, filedata))|| filesize <= 0) { return; } struct MeshHeader *mh = NULL; diff --git a/demo/extracttexture.c b/demo/extracttexture.c index 839ad84..8a187c3 100644 --- a/demo/extracttexture.c +++ b/demo/extracttexture.c @@ -8,7 +8,7 @@ void extracttextures(const char *filedata, struct Texture *t = NULL; struct TextureBatchDescriptor *d = NULL; char * fmt = NULL; - if (!(DEVIL1TEX.getheader(p, filedata)) || filesize == 0) { + if (!(DEVIL1TEX.getheader(&p, filedata)) || filesize == 0) { return; } fmt = (char*)malloc(strlen(filename) + 3 + 4); diff --git a/include/devil1geo.h b/include/devil1geo.h index 6d08f1e..7293041 100644 --- a/include/devil1geo.h +++ b/include/devil1geo.h @@ -86,7 +86,7 @@ typedef struct { void (* const printcoordinate)(struct Coordinate*, unsigned int); // input: pointer to struct, file data - bool (* const getheader) (struct Header*, const char*); + bool (* const getheader) (struct Header**, const char*); // input: pointer of pointer to struct, order, file data // ** = 'pass by reference' of a pointer to struct diff --git a/include/devil1tex.h b/include/devil1tex.h index b002a0a..f45be35 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -46,7 +46,7 @@ typedef struct { void (* const printbatchdesc)(struct TextureBatchDescriptor*); // input: pointer to struct, file data - bool (* const getheader) (struct TexturePack*, const char*); + bool (* const getheader) (struct TexturePack**, const char*); // input: pointer of pointer to struct, order, file data, file size // ** = 'pass by reference' of a pointer to struct diff --git a/src/devil1geo.c b/src/devil1geo.c index 70f5bd8..72357af 100644 --- a/src/devil1geo.c +++ b/src/devil1geo.c @@ -9,7 +9,7 @@ static void printmeshbatch(struct Batch*); static void printcoordinate(struct Coordinate*, unsigned int); -static bool getgheader(struct Header*, const char*); +static bool getgheader(struct Header**, const char*); static bool getmeshheader(struct MeshHeader**, unsigned int i, const char * const); @@ -76,11 +76,11 @@ static void printcoordinate(struct Coordinate *p, unsigned int count) { } } -static bool getgheader(struct Header* h, const char* filedata) { +static bool getgheader(struct Header** h, const char* filedata) { if (filedata == NULL) { return false; } - h = (struct Header*)filedata; + (*h) = (struct Header*)filedata; return true; } @@ -89,7 +89,7 @@ static bool getmeshheader(struct MeshHeader **hs, const char * const filedata) { bool done = false; struct Header *h = NULL; - if (hs == NULL || !(getgheader(h, filedata))) { + if (hs == NULL || !(getgheader(&h, filedata))) { return done; } if (h -> numMesh < i) { diff --git a/src/devil1tex.c b/src/devil1tex.c index 92bc717..f284cd8 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -9,7 +9,7 @@ static void printtph(struct TexturePack*); static void printtbd(struct TextureBatchDescriptor*); // Get Texture Pack Header -static inline bool getpackheader(struct TexturePack*, const char*); +static inline bool getpackheader(struct TexturePack**, const char*); // Get Texture Batch Descriptor. static bool gettexdescriptor(struct TextureBatchDescriptor**, @@ -58,11 +58,11 @@ static void printtbd(struct TextureBatchDescriptor *bd) { } } -inline static bool getpackheader(struct TexturePack* p, const char *filedata) { +inline static bool getpackheader(struct TexturePack** p, const char *filedata) { if (filedata == NULL) { return false; } - p = (struct TexturePack*)filedata; + (*p) = (struct TexturePack*)filedata; return true; } @@ -86,7 +86,7 @@ static bool gettexbatch(struct TextureBatch **batch, const char *filedata, unsigned int filesize) { struct TexturePack *p = NULL; - if (!(getpackheader(p, filedata))) { + if (!(getpackheader(&p, filedata))) { return false; } if (i > p -> batchNumber) { // no such batch in texture pack From 438ba04efb87ad19a850582462cfd359a816e61d Mon Sep 17 00:00:00 2001 From: _ <_> Date: Mon, 23 Apr 2018 17:59:46 -0700 Subject: [PATCH 4/4] Added bindings for new functions --- bindings/py3devil1.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bindings/py3devil1.py b/bindings/py3devil1.py index 2e2aa92..ddccd4c 100755 --- a/bindings/py3devil1.py +++ b/bindings/py3devil1.py @@ -65,6 +65,10 @@ class Devil1TEX_FN(ctypes.Structure): ("printbatchdesc", ctypes.CFUNCTYPE( None, ctypes.POINTER(TextureBatchDescriptor))), + ("getheader", ctypes.CFUNCTYPE( + ctypes.c_bool, + ctypes.POINTER(ctypes.POINTER(TexturePack)), + ctypes.c_char_p)), ("getbatchdesc", ctypes.CFUNCTYPE( ctypes.c_bool, ctypes.POINTER(TextureBatchDescriptor), @@ -179,6 +183,10 @@ class Devil1GEO_FN(ctypes.Structure): ("printcoordinate", ctypes.CFUNCTYPE( None, ctypes.POINTER(Coordinate))), + ("getheader", ctypes.CFUNCTYPE( + None, + ctypes.POINTER(ctypes.POINTER(Header)), + ctypes.c_char_p)), ("getmeshheader", ctypes.CFUNCTYPE( ctypes.c_bool, ctypes.POINTER(MeshHeader),