diff --git a/engine/include/GearBox/Endian.h b/engine/include/GearBox/Endian.h new file mode 100644 index 0000000..132ff9a --- /dev/null +++ b/engine/include/GearBox/Endian.h @@ -0,0 +1,39 @@ +#ifndef __GEARBOX_ENDIAN_H__ +#define __GEARBOX_ENDIAN_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_M_IX86) || defined(__x86_64__) || defined(__i386__) +#define GBEndianHostLittle +#else +#define GBEndianHostBig +#endif + +#ifdef GBEndianHostLittle +GBDECL GBI16 GBEndianSwapI16BE(GBI16 in); +GBDECL GBI32 GBEndianSwapI32BE(GBI32 in); +GBDECL GBI64 GBEndianSwapI64BE(GBI64 in); + +GBDECL GBU16 GBEndianSwapU16BE(GBU16 in); +GBDECL GBU32 GBEndianSwapU32BE(GBU32 in); +GBDECL GBU64 GBEndianSwapU64BE(GBU64 in); +#else +#define GBEndianSwapI16BE(in) (in) +#define GBEndianSwapI32BE(in) (in) +#define GBEndianSwapI64BE(in) (in) + +#define GBEndianSwapU16BE(in) (in) +#define GBEndianSwapU32BE(in) (in) +#define GBEndianSwapU64BE(in) (in) +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/engine/include/GearBox/Foundation.h b/engine/include/GearBox/Foundation.h index 6950cb5..b6642b3 100644 --- a/engine/include/GearBox/Foundation.h +++ b/engine/include/GearBox/Foundation.h @@ -13,5 +13,6 @@ #include #include #include +#include #endif diff --git a/engine/include/GearBox/TypeDefs.h b/engine/include/GearBox/TypeDefs.h index 74b132e..006be09 100644 --- a/engine/include/GearBox/TypeDefs.h +++ b/engine/include/GearBox/TypeDefs.h @@ -3,6 +3,40 @@ #include +#if defined(_MSC_VER) || defined(__WATCOMC__) +typedef __int8_t GBI8; +typedef __int16_t GBI16; +typedef __int32_t GBI32; +typedef __int64_t GBI64; + +typedef unsigned __int8_t GBU8; +typedef unsigned __int16_t GBU16; +typedef unsigned __int32_t GBU32; +typedef unsigned __int64_t GBU64; +#elif __STDC_VERSION__ >= 199901L +#include + +typedef int8_t GBI8; +typedef int16_t GBI16; +typedef int32_t GBI32; +typedef int64_t GBI64; + +typedef uint8_t GBU8; +typedef uint16_t GBU16; +typedef uint32_t GBU32; +typedef uint64_t GBU64; +#else +typedef signed char GBI8; +typedef short GBI16; +typedef int GBI32; +typedef long GBI64; + +typedef unsigned char GBU8; +typedef unsigned short GBU16; +typedef unsigned int GBU32; +typedef unsigned long GBU64; +#endif + typedef struct _GBVersion GBVersion; typedef struct _GBEngineParam GBEngineParam; typedef struct _GBResourceKV GBResourceKV; diff --git a/engine/src/endian.c b/engine/src/endian.c new file mode 100644 index 0000000..465812b --- /dev/null +++ b/engine/src/endian.c @@ -0,0 +1,64 @@ +#include + +#ifdef GBEndianHostLittle +static void swap(void* from, void* to, int size) { + unsigned char* f = from; + unsigned char* t = to; + + int i; + + for(i = 0; i < size; i++) { + t[size - 1 - i] = f[i]; + } +} + +#define SWAP(in, out) swap(&in, &out, sizeof(in)) + +GBI16 GBEndianSwapI16BE(GBI16 in) { + GBI16 out; + + SWAP(in, out); + + return out; +} + +GBI32 GBEndianSwapI32BE(GBI32 in) { + GBI32 out; + + SWAP(in, out); + + return out; +} + +GBI64 GBEndianSwapI64BE(GBI64 in) { + GBI64 out; + + SWAP(in, out); + + return out; +} + +GBU16 GBEndianSwapU16BE(GBU16 in) { + GBU16 out; + + SWAP(in, out); + + return out; +} + +GBU32 GBEndianSwapU32BE(GBU32 in) { + GBU32 out; + + SWAP(in, out); + + return out; +} + +GBU64 GBEndianSwapU64BE(GBU64 in) { + GBU64 out; + + SWAP(in, out); + + return out; +} +#endif diff --git a/engine/src/resource.c b/engine/src/resource.c index 4ef301f..9fe2828 100644 --- a/engine/src/resource.c +++ b/engine/src/resource.c @@ -2,6 +2,7 @@ #include #include +#include #include @@ -44,25 +45,16 @@ void* GBResourceGet(GBResource resource, const char* name, unsigned int* size) { GBFileSeek(resource->file, 4); while(!(GBFileRead(resource->file, fn, 128) < 128 || fn[0] == 0)) { - unsigned int actsz = 0; - unsigned int cmpsz = 0; - unsigned char c; - int i; - void* d; + GBU32 actsz = 0; + GBU32 cmpsz = 0; + int i; + void* d; - for(i = 0; i < 4; i++) { - if(GBFileRead(resource->file, &c, 1) < 1) return NULL; + if(GBFileRead(resource->file, &actsz, 4) < 4) return NULL; + if(GBFileRead(resource->file, &cmpsz, 4) < 4) return NULL; - actsz = actsz << 8; - actsz = actsz | c; - } - - for(i = 0; i < 4; i++) { - if(GBFileRead(resource->file, &c, 1) < 1) return NULL; - - cmpsz = cmpsz << 8; - cmpsz = cmpsz | c; - } + actsz = GBEndianSwapU32BE(actsz); + cmpsz = GBEndianSwapU32BE(cmpsz); *size = actsz; diff --git a/engine/tools/gb_makepak/main.c b/engine/tools/gb_makepak/main.c index 0da820a..f56d170 100644 --- a/engine/tools/gb_makepak/main.c +++ b/engine/tools/gb_makepak/main.c @@ -73,7 +73,7 @@ static void pack(const char* fullpath, const char* path) { fwrite(output, 1, outsz, out); - printf("done (%6d -> %6d)\n", insz, outsz); + printf("done (%8d -> %8d)\n", insz, outsz); total++; totalact += insz;