From d2d92394a70c541f5502cc9de62c4b23460fd467 Mon Sep 17 00:00:00 2001 From: oneechanhax Date: Tue, 1 May 2018 09:44:29 -0500 Subject: [PATCH] Cmake embed! --- embed.cmake | 23 +++++++++++++++ embed.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 embed.cmake create mode 100644 embed.cpp diff --git a/embed.cmake b/embed.cmake new file mode 100644 index 0000000..a1cc2e4 --- /dev/null +++ b/embed.cmake @@ -0,0 +1,23 @@ + +# +# +# Purpose, make a macro to automaticly convert files to headers with cross platform support +# +# + +macro(embed_files) + if(NOT TARGET cmake-embed) + project(cmake-embed) + add_executable(nekohook embed.cpp) + endif() + for() + foreach(i IN LISTS ARGN) + get_filename_component(WORKING_DIR ${i} DIRECTORY) + add_custom_command( + TARGET ${i}.h + PRE_BUILD + cmake-embed "${i}" "${i}.h" + WORKING_DIRECTORY ${WORKING_DIR} + ) + endforeach() +endmacro() diff --git a/embed.cpp b/embed.cpp new file mode 100644 index 0000000..5edfb94 --- /dev/null +++ b/embed.cpp @@ -0,0 +1,80 @@ + +/* + * + * A Simple file embeder made in cpp for nekohook + * + */ + + #include // c lib + #include // reading/writing to files + #include // string utils kill me + + int main(int argC, const char** argS) { + // check args + if (argC < 2) { + printf("Usage: %s \n", argS[0]); + return 1; + } + // Ensure we have an out + const char* out_file_path; + if (argS[2]) + out_file_path = argS[2]; + else { + // Deleting this isnt needed, it will be destroyed on program end + const char header_append[] = ".h"; + char* tmp = new char[strlen(argS[1]) + sizeof(header_append) + 1]; + strcpy(tmp, argS[1]); + strcat(tmp, header_append); + out_file_path = tmp; + } + + // Open the files we need, check both for errors + FILE* in_file = fopen(argS[1], "r"); + if (!in_file) { + printf("Cannot open file: \"%s\"\n", argS[1]); + return 1; + } + FILE* out_file = fopen(out_file_path, "w"); + if (!out_file) { + printf("Cannot open file for writing: \"%s\"\n", out_file_path); + return 1; + } + + // Print header preamble + fprintf(out_file, + "/*\n" + " *\n" + " * This file is auto-generated for nekohook!\n" + " *\n" + " * Original file: %s\n" + " *\n" + " */\n" + "\n", argS[1]); + + // Start the embed array + char* in_path = strcpy(new char[strlen(argS[1]) + 1], argS[1]); + while (char* period = strstr(in_path, ".")) + *period = '_'; + fprintf(out_file, "const unsigned char %s[] = {\n\t", in_path); + + // Start reading the file and print to the header + unsigned char buf[256]; + int tick = 1; + while (size_t i = fread(buf, sizeof(char), sizeof(buf), in_file)) { + for(size_t ii = 0; ii < i; ii++) { + fprintf(out_file, "0x%02x, ", buf[ii]); + if (tick++ >= 10) { + fputs("\n\t", out_file); + tick = 1; + } + } + } + + // Finish up + if (tick) + fputs("\n", out_file); + fputs("};\n", out_file); + + return 0; + } + \ No newline at end of file