diff --git a/bin2h/CMakeLists.txt b/bin2h/CMakeLists.txt new file mode 100644 index 00000000..bca86b88 --- /dev/null +++ b/bin2h/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.7.2) + +if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9)) + cmake_policy(SET CMP0069 NEW) +endif() + +project(bin2h LANGUAGES C) + +add_executable(bin2h "bin2h.c") + +set_target_properties(bin2h PROPERTIES + C_STANDARD 90 + C_STANDARD_REQUIRED ON + C_EXTENSIONS OFF +) + +# MSVC tweak +if(MSVC) + target_compile_definitions(bin2h PRIVATE _CRT_SECURE_NO_WARNINGS) # Shut up those stupid warnings +endif() + +# Enable link-time optimisation if available +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9)) + include(CheckIPOSupported) + check_ipo_supported(RESULT result) + if(result) + set_target_properties(bin2h PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() + endif() +endif() + +install(TARGETS bin2h RUNTIME DESTINATION bin) diff --git a/bin2h/bin2h.c b/bin2h/bin2h.c new file mode 100644 index 00000000..11c1de1e --- /dev/null +++ b/bin2h/bin2h.c @@ -0,0 +1,92 @@ +/*Bin2h by -C-u-c-k-y- Clownypants*/ +/*Converts files to the .h's expected by Cave Story Engine for resources.*/ + +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int result = 0; + + if (argc > 2) + { + char *last_forward_slash; + char *last_back_slash; + char *last_path_seperator; + char *filename_pointer; + char *dot; + size_t filename_length; + char *filename; + FILE *in_file; + FILE *out_file; + + last_forward_slash = strrchr(argv[1], '/'); + last_back_slash = strrchr(argv[1], '\\'); + + last_path_seperator = last_forward_slash > last_back_slash ? last_forward_slash : last_back_slash; + + filename_pointer = (last_path_seperator == NULL) ? argv[1] : last_path_seperator + 1; + dot = strchr(filename_pointer, '.'); + filename_length = (dot == NULL) ? strlen(filename_pointer) : (size_t)(dot - filename_pointer); + + filename = malloc(filename_length + 1); + memcpy(filename, filename_pointer, filename_length); + filename[filename_length] = '\0'; + + in_file = fopen(argv[1], "rb"); + out_file = fopen(argv[2], "w"); + + if (in_file == NULL) + { + printf("Couldn't open '%s'\n", argv[1]); + result = 1; + } + else if (out_file == NULL) + { + printf("Couldn't open '%s'\n", argv[2]); + result = 1; + } + else + { + long in_file_size; + unsigned char *in_file_buffer; + unsigned char *in_file_pointer; + long i; + + fseek(in_file, 0, SEEK_END); + in_file_size = ftell(in_file); + rewind(in_file); + in_file_buffer = malloc(in_file_size); + fread(in_file_buffer, 1, in_file_size, in_file); + fclose(in_file); + in_file_pointer = in_file_buffer; + + setvbuf(out_file, NULL, _IOFBF, 0x10000); + + fprintf(out_file, "#pragma once\n\nstatic const unsigned char r%s[0x%lX] = {\n\t", filename, in_file_size); + + for (i = 0; i < in_file_size - 1; ++i) + { + if (i % 32 == 32-1) + fprintf(out_file, "%d,\n\t", *in_file_pointer++); + else + fprintf(out_file, "%d,", *in_file_pointer++); + } + + fprintf(out_file, "%d\n};\n", *in_file_pointer++); + + fclose(out_file); + free(in_file_buffer); + } + + free(filename); + } + else + { + result = 1; + } + + return result; +}