From 52299176be9a99f7d834ae36216044058f731098 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Fri, 29 Jul 2022 18:02:55 +0200 Subject: [PATCH] MSVC support --- CMakeLists.txt | 4 ++-- README.md | 2 +- src/Model-Extractor.c | 20 ++++++++++---------- src/errors_types.h | 5 ++++- src/hob_struct.h | 38 ++++++++++++++++++++++++++------------ src/obj_exporter.c | 6 ++++++ src/options.h | 6 +++--- 7 files changed, 52 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8f120c..b75ccd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,10 +42,10 @@ add_executable(rse-model ${RSE_MOD_SRCS} ${RSE_MOD_HRDS}) set_target_properties(rse-model PROPERTIES OUTPUT_NAME ${RSE_MOD_NAME}) if(MSVC) # msvc does not append 'lib' - do it here to have consistent name - set_target_properties(rse-model PROPERTIES PREFIX "lib") + #set_target_properties(rse-model PROPERTIES PREFIX "lib") set_target_properties(rse-model PROPERTIES IMPORT_PREFIX "lib") endif() -target_link_libraries(rse-model m) +target_link_libraries(rse-model) # add GPG signature command #add_custom_command( diff --git a/README.md b/README.md index dda8db9..6a1b814 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Due to issue with copyrights, I can't provide samples... You need to extract HOB You can compile on both Windows (MinGW) or native Linux system thanks to CMake. -To compile, just clone and launch cmake: +To compile, just clone (**don't forget git submodules**) and launch cmake: ```shell cmake . diff --git a/src/Model-Extractor.c b/src/Model-Extractor.c index 1475bc1..75315b6 100644 --- a/src/Model-Extractor.c +++ b/src/Model-Extractor.c @@ -30,7 +30,7 @@ static unsigned int mainProcess(int args_cnt, char *args_value[]); static void createSubDir(char *dirName); -static int checkInputArgs(int arg_nbr, char *args[]); +static unsigned int checkInputArgs(int arg_nbr, char *args[]); static void cleanUpMemory(T_HOB* hobStruct); //int exportTextures(HMT_FILE *hmt_f, char *filename); static inline void dispHelp(); @@ -40,7 +40,7 @@ static inline void dispHelp(); * Global variables declaration */ -int _options; // Global options settings variable +unsigned int _options; // Global options settings variable /* @@ -103,8 +103,8 @@ static unsigned int mainProcess(int args_cnt, char *args_value[]) { return NO_ERROR; } -static int checkInputArgs(int arg_nbr, char *args[]) { - int _o = (OUTPUT_DIR | EXPORT_MTL); // Default options parameters +static unsigned int checkInputArgs(int arg_nbr, char *args[]) { + unsigned int _o = (OUTPUT_DIR | EXPORT_MTL); // Default options parameters char test[256]; int i; @@ -118,18 +118,18 @@ static int checkInputArgs(int arg_nbr, char *args[]) { } else if (strcmp(args[i], "-v") == 0) { _o |= VERBOSE_ENABLED; printf("[OPTN] Verbose enabled.\n"); - } else if (strcmp(args[i], "-no-subdir") == 0) { - _o &= ~OUTPUT_DIR; - printf("[OPTN] Export to current directory.\n"); - } else if (strcmp(args[i], "-mtl") == 0) { - _o &= ~EXPORT_MTL; - printf("[OPTN] No materials datas.\n"); } else if (strcmp(args[i], "-vv") == 0) { _o |= DEBUG_MODE; printf("[OPTN] Verbose enabled.\n"); } else if (strcmp(args[i], "-vvv") == 0) { _o |= GOD_MODE; printf("[OPTN] Verbose enabled.\n"); + } else if (strcmp(args[i], "-no-subdir") == 0) { + _o &= ~OUTPUT_DIR; + printf("[OPTN] Export to current directory.\n"); + } else if (strcmp(args[i], "-mtl") == 0) { + _o &= ~EXPORT_MTL; + printf("[OPTN] No materials datas.\n"); } else { printf("[ERR] Unknown option: %s\n", args[i]); } diff --git a/src/errors_types.h b/src/errors_types.h index e94fe6c..6d101ab 100644 --- a/src/errors_types.h +++ b/src/errors_types.h @@ -5,12 +5,15 @@ * Author: JackCarterSmith */ -#include "error.h" //TODO: use it as base for error ID +//#include "error.h" //TODO: use it as base for error ID #ifndef SRC_ERRORS_TYPES_H_ #define SRC_ERRORS_TYPES_H_ +#ifdef NO_ERROR +#undef NO_ERROR +#endif #define NO_ERROR 0 #define ERROR_GENERIC 1 #define ERROR_MEMORY 2 diff --git a/src/hob_struct.h b/src/hob_struct.h index 4b13fc2..28b96a8 100644 --- a/src/hob_struct.h +++ b/src/hob_struct.h @@ -16,6 +16,12 @@ * car = 8bits */ +#if defined(_MSC_VER) +#define PACK +#elif defined(__GNUC__) +#define PACK __attribute__((packed)) +#endif + /////////////////////////////////////////////////////////////////////////////// // HOB file structure /////////////////////////////////////////////////////////////////////////////// @@ -105,12 +111,16 @@ typedef struct hob { // Caution: the place of variable is important for correct mapping! /////////////////////////////////////////////////////////////////////////////// -typedef struct __attribute__((packed)) hobfile_header { +#if defined(_MSC_VER) +#pragma pack(push, 1) +#endif + +typedef struct PACK hobfile_header { unsigned int obj_count; unsigned int vertices_offset; } T_HOBFILE_HEADER; -typedef struct __attribute__((packed)) hobfile_obj_descriptor { +typedef struct PACK hobfile_obj_descriptor { unsigned char object_name[16]; unsigned int facegroup_offset; unsigned int object_parts_offset; @@ -143,17 +153,17 @@ typedef struct __attribute__((packed)) hobfile_obj_descriptor { float reserved17; } T_HOBFILE_OBJ_DESCRIPTOR; -typedef struct __attribute__((packed)) hobfile_facegroup_header { +typedef struct PACK hobfile_facegroup_header { unsigned short object_part_count; unsigned short facegroup_count; } T_HOBFILE_FACEGROUP_HEADER; -typedef struct __attribute__((packed)) hobfile_facegroup_offset { +typedef struct PACK hobfile_facegroup_offset { unsigned int unknown1; unsigned int facegroup_offset; } T_HOBFILE_FACEGROUP_OFFSET; -typedef struct __attribute__((packed)) hobfile_meshdef0 { +typedef struct PACK hobfile_meshdef0 { unsigned int offset1; unsigned int offset2; unsigned int prev_meshdef0_offset; @@ -201,7 +211,7 @@ typedef struct __attribute__((packed)) hobfile_meshdef0 { float transform_z; } T_HOBFILE_MESHDEF0; -typedef struct __attribute__((packed)) hobfile_meshdef1 { +typedef struct PACK hobfile_meshdef1 { unsigned int facedef_end_offset; unsigned int reserved1; // 20B of zeros @@ -231,7 +241,7 @@ typedef struct __attribute__((packed)) hobfile_meshdef1 { unsigned int reserved19; } T_HOBFILE_MESHDEF1; -typedef struct __attribute__((packed)) hobfile_faceblock { +typedef struct PACK hobfile_faceblock { unsigned int reserved1; // 8B of zeros unsigned int reserved2; @@ -239,7 +249,7 @@ typedef struct __attribute__((packed)) hobfile_faceblock { unsigned int faceCounts; } T_HOBFILE_FACEBLOCK; -typedef struct __attribute__((packed)) hobfile_faces_header { +typedef struct PACK hobfile_faces_header { unsigned int flags; unsigned char b1; unsigned char b2; @@ -250,29 +260,33 @@ typedef struct __attribute__((packed)) hobfile_faces_header { unsigned short vertexIndices[4]; // Relative to facegroup, the last value is equal to 0 when it's triangle shape. } T_HOBFILE_FACES_HEADER; -typedef struct __attribute__((packed)) hobfile_faces_extra_vertex_color { +typedef struct PACK hobfile_faces_extra_vertex_color { T_RGBA v1_rgba; T_RGBA v2_rgba; T_RGBA v3_rgba; T_RGBA v4_rgba; // Used with quad type face } T_HOBFILE_FACES_VERTEX_COLOR; -typedef struct __attribute__((packed)) hobfile_faces_extra_color { +typedef struct PACK hobfile_faces_extra_color { T_RGBA rgba; } T_HOBFILE_FACES_COLOR; -typedef struct __attribute__((packed)) hobfile_faces_extra_vertex_texture { +typedef struct PACK hobfile_faces_extra_vertex_texture { T_TEXCOORD v1_texcoord; // Should be divided (no shifting) by 4096 to get 0...1 range T_TEXCOORD v2_texcoord; T_TEXCOORD v3_texcoord; T_TEXCOORD v4_texcoord; // Used with quad type face } T_HOBFILE_FACES_VERTEX_TEXTURE; -typedef struct __attribute__((packed)) hobfile_vertex { +typedef struct PACK hobfile_vertex { short x; short y; short z; short w; } T_HOBFILE_VERTEX; +#if defined(_MSC_VER) +#pragma pack(pop) +#endif + #endif /* SRC_HOB_STRUCT_H_ */ diff --git a/src/obj_exporter.c b/src/obj_exporter.c index 00052de..bce78de 100644 --- a/src/obj_exporter.c +++ b/src/obj_exporter.c @@ -107,7 +107,9 @@ unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) { if (_options & EXPORT_MTL) { obj_write(objConstruct, objExport_path, mtlExport_path, 8); +#if defined(__GNUC__) //TODO: review MSVC file management or include and rewrite obj lib? if (_options & OUTPUT_DIR) mtlPathPatch(objExport_path, hob_objects->name); +#endif } else obj_write(objConstruct, objExport_path, NULL, 8); obj_delete(objConstruct); @@ -150,7 +152,11 @@ static void mtlPathPatch(const char* out_file, const char* obj_name) { // Begin rewrite file obj = fopen(out_file, "w"); fprintf(obj, "mtllib %s", _path); +#if defined(_MSC_VER) + fwrite(memFile, fileSize - pos , 1, obj); +#elif defined(__GNUC__) fwrite(memFile, fileSize - (pos + lines), 1, obj); +#endif free(memFile); } diff --git a/src/options.h b/src/options.h index cef9fef..296d57c 100644 --- a/src/options.h +++ b/src/options.h @@ -4,9 +4,9 @@ #define VERBOSE_ENABLED 0x0001 // Simple details about ID and other "light" things #define OUTPUT_DIR 0x0002 #define EXPORT_MTL 0x0004 -#define DEBUG_MODE 0x0011 // Output all values of faces, indices and vertices and others "heavy" things -#define GOD_MODE 0x1111 // Dev only. Output experimental values. +#define DEBUG_MODE 0x0010 // Output all values of faces, indices and vertices and others "heavy" things +#define GOD_MODE 0x0100 // Dev only. Output experimental values. -extern int _options; +extern unsigned int _options; #endif