From 8d67fa3b08d8154002240ea5e0922429ecc4c8d1 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 24 Jan 2023 18:10:42 +0800 Subject: [PATCH] Organize CMake, add safe fopen Tidy up CMake. Also provide abstraction to fopen_s only available to Windows. --- CMakeLists.txt | 199 +++++++++++++--------- src/packages/game/output/video/IZ_video.c | 5 +- src/packages/game/output/video/IZ_video.h | 1 + src/packages/io/IZ_io.c | 26 +++ src/packages/io/IZ_io.h | 11 ++ src/packages/net/svc/IZ_wsserver.c | 2 +- src/packages/net/svc/IZ_wsserver.h | 1 + 7 files changed, 158 insertions(+), 87 deletions(-) create mode 100644 src/packages/io/IZ_io.c create mode 100644 src/packages/io/IZ_io.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ce5e972..695e469 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,10 @@ cmake_minimum_required(VERSION 3.21) - -# Set your project name here project(izanagi C) - set(CMAKE_C_STANDARD 11) +# +# Set platform +# if (WIN32) # TODO set arch on target instead of generator set(PLATFORM WINDOWS) @@ -31,6 +31,9 @@ else() ) endif () +# +# Add configurations +# add_definitions( -DIZ_APP_NAME="Izanagi" -DIZ_APP_DESCRIPTION="Run and gun game" @@ -42,6 +45,9 @@ if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") add_definitions(-DIZ_DEBUG) endif() +# +# Specify directories +# include_directories( "${PROJECT_SOURCE_DIR}/dependencies/SDL2/include" "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image" @@ -78,15 +84,18 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") ) endif() +# +# Define executables +# add_executable( game dependencies/minIni/dev/minIni.h dependencies/minIni/dev/minIni.c dependencies/getopt-for-windows/getopt.h dependencies/getopt-for-windows/getopt.c - src/packages/game/output/video/IZ_video.h - src/packages/game/output/video/IZ_video.c - src/packages/common/IZ_common.h + src/packages/game/output/video/IZ_video.h + src/packages/game/output/video/IZ_video.c + src/packages/common/IZ_common.h src/packages/game/input/IZ_action.h src/packages/game/IZ_app.h src/packages/game/IZ_app.c @@ -95,9 +104,9 @@ add_executable( src/packages/game/input/IZ_joystick.h src/packages/game/input/IZ_keyboard.c src/packages/game/input/IZ_keyboard.h - src/packages/config/IZ_config.c - src/packages/config/IZ_config.h - src/packages/game/geometry/IZ_vector2d.c + src/packages/config/IZ_config.c + src/packages/config/IZ_config.h + src/packages/game/geometry/IZ_vector2d.c src/packages/game/geometry/IZ_vector2d.h src/packages/game/geometry/IZ_rect.c src/packages/game/geometry/IZ_rect.h @@ -115,15 +124,32 @@ add_executable( src/packages/game/input/IZ_midi.h src/packages/game/data/IZ_list.c src/packages/game/data/IZ_list.h - src/packages/net/svc/IZ_wsclient.c - src/packages/net/svc/IZ_wsclient.h - src/packages/log/IZ_log.c - src/packages/log/IZ_log.h - src/packages/midi/IZ_midi.c - src/packages/midi/IZ_midi.h - src/packages/net/core/IZ_websocket.h - src/packages/net/core/IZ_websocket.c - src/packages/net/IZ_net_client.c src/packages/net/IZ_net_client.h src/packages/game/IZ_app_net.c src/packages/game/IZ_app_net.h src/packages/game/IZ_app_video.c src/packages/game/IZ_app_video.h src/packages/game/IZ_subsystem.h src/packages/game/IZ_app_input.c src/packages/game/IZ_app_input.h src/packages/game/IZ_app_config.c src/packages/game/IZ_app_config.h src/packages/game/asset/IZ_asset.c src/packages/game/asset/IZ_asset.h src/packages/string/IZ_string.c src/packages/string/IZ_string.h) + src/packages/net/svc/IZ_wsclient.c + src/packages/net/svc/IZ_wsclient.h + src/packages/log/IZ_log.c + src/packages/log/IZ_log.h + src/packages/midi/IZ_midi.c + src/packages/midi/IZ_midi.h + src/packages/net/core/IZ_websocket.h + src/packages/net/core/IZ_websocket.c + src/packages/net/IZ_net_client.c + src/packages/net/IZ_net_client.h + src/packages/game/IZ_app_net.c + src/packages/game/IZ_app_net.h + src/packages/game/IZ_app_video.c + src/packages/game/IZ_app_video.h + src/packages/game/IZ_subsystem.h + src/packages/game/IZ_app_input.c + src/packages/game/IZ_app_input.h + src/packages/game/IZ_app_config.c + src/packages/game/IZ_app_config.h + src/packages/game/asset/IZ_asset.c + src/packages/game/asset/IZ_asset.h + src/packages/string/IZ_string.c + src/packages/string/IZ_string.h + src/packages/io/IZ_io.c + src/packages/io/IZ_io.h +) if (WIN32) target_link_libraries( @@ -140,14 +166,15 @@ if (WIN32) elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") target_link_libraries( game - SDL2main - SDL2 - SDL2_image - SDL2_ttf - portmidi - # libcrypto - # libssl - websockets + SDL2main + SDL2 + SDL2_image + SDL2_ttf + portmidi +# FIXME +# libcrypto +# libssl + websockets ) endif() @@ -157,7 +184,7 @@ add_executable( src/packages/test/IZ_mock.h src/packages/test/IZ_test.h - src/packages/game/geometry/IZ_rect.h + src/packages/game/geometry/IZ_rect.h src/packages/game/geometry/IZ_rect.c src/packages/game/geometry/IZ_vector2d.h src/packages/game/geometry/IZ_vector2d.c @@ -177,7 +204,7 @@ add_executable( __mocks__/SDL_stdinc.mock.h __mocks__/portmidi.mock.h - src/packages/config/IZ_config.h + src/packages/config/IZ_config.h src/packages/game/input/IZ_keyboard.h src/packages/game/input/IZ_keyboard.c @@ -188,8 +215,8 @@ add_executable( src/packages/game/input/IZ_midi.h src/packages/game/input/IZ_midi.c - src/packages/midi/IZ_midi.c - src/packages/midi/IZ_midi.h + src/packages/midi/IZ_midi.c + src/packages/midi/IZ_midi.h src/packages/game/input/input.test.c ) @@ -199,15 +226,12 @@ add_executable( dependencies/bdd-for-c/bdd-for-c.h src/packages/test/IZ_mock.h src/packages/test/IZ_test.h - __mocks__/minIni.mock.h __mocks__/SDL_stdinc.mock.h __mocks__/SDL_render.mock.h - - src/packages/config/IZ_config.h - - src/packages/game/output/video/IZ_video.h - src/packages/game/output/video/IZ_video.c + src/packages/config/IZ_config.h + src/packages/game/output/video/IZ_video.h + src/packages/game/output/video/IZ_video.c src/packages/game/output/output.test.c ) @@ -216,7 +240,6 @@ add_executable( dependencies/bdd-for-c/bdd-for-c.h src/packages/test/IZ_mock.h src/packages/test/IZ_test.h - src/packages/game/memory/IZ_pool.h src/packages/game/memory/IZ_pool.c src/packages/game/memory/memory.test.c @@ -229,7 +252,6 @@ add_executable( dependencies/bdd-for-c/bdd-for-c.h src/packages/test/IZ_mock.h src/packages/test/IZ_test.h - src/packages/game/data/IZ_list.h src/packages/game/data/IZ_list.c src/packages/game/data/data.test.c @@ -259,14 +281,10 @@ add_executable( src/packages/server/db/IZ_repo.c src/packages/server/db/IZ_repo.h src/packages/server/IZ_subsystem.h - src/packages/string/IZ_string.c src/packages/string/IZ_string.h) - -add_executable( - asset-inv - dependencies/minIni/dev/minIni.h - dependencies/minIni/dev/minIni.c - src/packages/common/IZ_common.h - src/packages/asset-inv/main.c + src/packages/string/IZ_string.c + src/packages/string/IZ_string.h + src/packages/io/IZ_io.c + src/packages/io/IZ_io.h ) target_link_libraries( @@ -278,69 +296,80 @@ target_link_libraries( websockets ) +add_executable( + asset-inv + dependencies/minIni/dev/minIni.h + dependencies/minIni/dev/minIni.c + src/packages/common/IZ_common.h + src/packages/asset-inv/main.c +) + +# +# Add DLLs and required assets to the build directory +# if (WIN32) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll" + $) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}/SDL2_image.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}/SDL2_image.dll" + $) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}/SDL2_ttf.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}/SDL2_ttf.dll" + $) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release/portmidi.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release/portmidi.dll" + $) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll" + $) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll" + $) add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll" + $) add_custom_command(TARGET server POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}/SDL2.dll" + $) add_custom_command(TARGET server POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/bin/Release/websockets.dll" + $) add_custom_command(TARGET server POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libssl-1_1-x64.dll" + $) add_custom_command(TARGET server POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll" # <--this is in-file - $) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/bin/libcrypto-1_1-x64.dll" + $) endif () add_custom_command(TARGET game POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/assets" # <--this is in-file - $/assets) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${PROJECT_SOURCE_DIR}/assets" + $/assets) add_custom_command(TARGET asset-inv POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory # which executes "cmake - E copy_if_different..." - "${PROJECT_SOURCE_DIR}/assets" # <--this is in-file - $/assets) # <--this is out-file path + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${PROJECT_SOURCE_DIR}/assets" + $/assets) diff --git a/src/packages/game/output/video/IZ_video.c b/src/packages/game/output/video/IZ_video.c index 306f95a..81c0e24 100644 --- a/src/packages/game/output/video/IZ_video.c +++ b/src/packages/game/output/video/IZ_video.c @@ -122,7 +122,10 @@ u16 IZ_VideoGetNextFreeSpriteSlot(IZ_VideoState* state, IZ_VideoSpritePriority p void IZ_VideoLoadSprite(IZ_VideoState* state, IZ_VideoLoadSpriteParams params, IZ_SpriteSlot* out) { char full_path[2048]; sprintf(full_path, "%s/%s", params.dir, params.filename); - FILE* f = fopen(full_path, "r"); + FILE* f; + if (IZ_fopen(&f, full_path, "r")) { + return; + } u32 sprite_length_bytes = ini_getl(params.dir, params.filename, 0, "assets.ini"); u8* sprite = malloc(sprite_length_bytes + 1); fread(sprite, 1, sprite_length_bytes, f); diff --git a/src/packages/game/output/video/IZ_video.h b/src/packages/game/output/video/IZ_video.h index b0eec01..3232adb 100644 --- a/src/packages/game/output/video/IZ_video.h +++ b/src/packages/game/output/video/IZ_video.h @@ -9,6 +9,7 @@ #include "../../../net/IZ_net_client.h" #include "../../../config/IZ_config.h" #include "../../../common/IZ_common.h" +#include "../../../io/IZ_io.h" #include "../../../string/IZ_string.h" #include "../../geometry/IZ_vector2d.h" #include "../../input/IZ_input.h" diff --git a/src/packages/io/IZ_io.c b/src/packages/io/IZ_io.c new file mode 100644 index 0000000..6b1b645 --- /dev/null +++ b/src/packages/io/IZ_io.c @@ -0,0 +1,26 @@ +#include "IZ_io.h" + +int IZ_sprintf(char* buffer, size_t buffer_size, const char* format, ...) { +#if defined IZ_WIN64 + va_list args; + va_start(args, format); + int result = sprintf_s(buffer, buffer_size, format, args); + va_end(args); + return result; +#else + va_list args; + va_start(args, format); + int result = sprintf(buffer, format, args); + va_end(args); + return result; +#endif +} + +errno_t IZ_fopen(struct _iobuf** file, const char* filename, const char* mode) { +#if defined IZ_WIN64 + return fopen_s(file, filename, mode); +#else + *file = fopen(filename, mode); + return *file == NULL ? 1 : 0; +#endif +} diff --git a/src/packages/io/IZ_io.h b/src/packages/io/IZ_io.h new file mode 100644 index 0000000..b12ed6a --- /dev/null +++ b/src/packages/io/IZ_io.h @@ -0,0 +1,11 @@ +#ifndef IZ_IO_H +#define IZ_IO_H + +#include +#include + +int IZ_sprintf(char*, size_t, const char*, ...); + +errno_t IZ_fopen(struct _iobuf**, const char*, const char*); + +#endif diff --git a/src/packages/net/svc/IZ_wsserver.c b/src/packages/net/svc/IZ_wsserver.c index 75dc891..1eca05e 100644 --- a/src/packages/net/svc/IZ_wsserver.c +++ b/src/packages/net/svc/IZ_wsserver.c @@ -32,7 +32,7 @@ IZ_ProcedureResult IZ_WSServerCallback( const char* IZ_WSServerTestPath(const char* base_dir, const char* file) { static char test_path[32]; - sprintf_s(test_path, 32, "%s/%s", base_dir, file); + IZ_sprintf(test_path, 32, "%s/%s", base_dir, file); struct stat stats; stat(test_path, &stats); if (stats.st_mode & S_IREAD) { diff --git a/src/packages/net/svc/IZ_wsserver.h b/src/packages/net/svc/IZ_wsserver.h index d3fa5ac..1497c21 100644 --- a/src/packages/net/svc/IZ_wsserver.h +++ b/src/packages/net/svc/IZ_wsserver.h @@ -4,6 +4,7 @@ #include #include #include "../../common/IZ_common.h" +#include "../../io/IZ_io.h" #include "../core/IZ_websocket.h" #ifndef S_ISDIR