From 756f28cd417710498e8fe0174c5ed2eb871cae16 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 01:36:28 -0700 Subject: [PATCH] Added texture pack headers correctly read in --- Makefile | 5 ++++- include/devil1tex.h | 50 +++++++++++++++++++++++++++++++++++++++++ src/devil1tex.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ test/main.c | 19 ++++++++++++++-- 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 include/devil1tex.h create mode 100644 src/devil1tex.c diff --git a/Makefile b/Makefile index bec6c8c..23ac41f 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,14 @@ CFLAGS= -I"include" all: main -main: devil1pld.o +main: devil1pld.o devil1tex.o $(CC) $^ test/main.c $(CFLAGS) -o $(EX) devil1pld.o: src/devil1pld.c $(CC) -c $^ $(CFLAGS) +devil1tex.o: src/devil1tex.c + $(CC) -c $^ $(CFLAGS) + clean: rm *.o $(EX) diff --git a/include/devil1tex.h b/include/devil1tex.h new file mode 100644 index 0000000..df279c0 --- /dev/null +++ b/include/devil1tex.h @@ -0,0 +1,50 @@ +#ifndef DEVIL1TEX_H +#define DEVIL1TEX_H + +#include + +#pragma pack(push, 1) +// disable struct padding +// to easily impose struct on plain data. + +struct TexturePack { + char id[4]; + int32_t batchNumber; + uint32_t firstBatchOffset; // + uint32_t unknownA; +}; + +struct TextureBatchDescriptor{ + int32_t batchIdx; + uint32_t hash; // + uint32_t texNumber; + uint32_t unknownA[8]; // + uint32_t textureSize; // + uint32_t unknownB[30]; +}; + +struct Texture { + // size of array is defined by descriptor + // textureSize + unsigned char *data; +}; + +struct TextureBatch { + // quantity of textures are defined by descriptor + // texNumber + struct Texture *batch; +}; + +#pragma pack(pop) +// re-enable stuct padding for whatever other reason. + +void gettph(struct TexturePack*, const char*); + +void printtph(struct TexturePack*); + +void gettbd(struct TextureBatchDescriptor*, + struct TexturePack *tph, + const char*, + unsigned int); + +#endif diff --git a/src/devil1tex.c b/src/devil1tex.c new file mode 100644 index 0000000..8bd8ab3 --- /dev/null +++ b/src/devil1tex.c @@ -0,0 +1,54 @@ +#include "devil1tex.h" +#include +#include + +// get texture pack header +void gettph (struct TexturePack *tp, const char *filedata) { +// Assume the Texture Pack Header is already allocated memory. + if (tp != NULL && filedata != NULL) { + memcpy(tp, filedata, sizeof(struct TexturePack)); + } + return; +} + +void printtph (struct TexturePack *tp) { + if (tp != NULL) { + printf("id: \t %c %c %c %c \n", tp -> id[3], + tp -> id[2], + tp -> id[1], + tp -> id[0]); + printf("batch#: \t %i \n", tp -> batchNumber); + printf("1st batch: \t %i \n", tp -> firstBatchOffset); + printf("unknownA: \t %i\n", tp -> unknownA); + } + return; +} + +// get texture pack descriptors +void gettbd (struct TextureBatchDescriptor *tbd, + struct TexturePack* tph, + const char *filedata, + unsigned int filesize) { + if (tbd == NULL || tph == NULL || filedata == NULL) { + // any of these being null will cause segmentation fault. + return; + } + // do some checks regarding the header + if (tph -> firstBatchOffset >= filesize || + tph -> firstBatchOffset <= 0) { + return; + } + unsigned int index = tph -> firstBatchOffset; + // Batch Descriptors are structured one after another. + // therefore the next one is at + // current index + sizeof(TextureBatchDescriptor) + unsigned int i = 0; + do { + index += (sizeof(struct TextureBatchDescriptor) * i); + printf("batch descriptor start: %x\n", index); + tbd = (struct TextureBatchDescriptor*)(filedata + index); + // + } while(i < (tph -> batchNumber)); + return; +} + diff --git a/test/main.c b/test/main.c index e5879e6..84e1a7d 100644 --- a/test/main.c +++ b/test/main.c @@ -1,9 +1,11 @@ -#include "devil1pld.h" #include #include #include #include +#include "devil1pld.h" +#include "devil1tex.h" + bool writedata(const char *basename, unsigned int id, const char *data, @@ -88,11 +90,24 @@ bool unpackpld (const char *filedata, return status; } + + int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); - unpackpld(buffer, bufsize, f); +// unpackpld(buffer, bufsize, f); + struct TexturePack *tp = (struct TexturePack*)malloc + (sizeof(struct TexturePack)); + printf("%d %d %d %d \n", buffer[0], + buffer[1], + buffer[2], + buffer[3]); + gettph(tp, buffer); + printf("%x %x \n", buffer + 0, tp -> id[0]); + printtph(tp); + + free(tp); free(buffer); return 0; }