From 74d84d84e5c10738e3052fd8c42f244cdcf54b86 Mon Sep 17 00:00:00 2001 From: nesbox Date: Sat, 27 Jun 2020 19:09:29 +0300 Subject: [PATCH] #1089: added prj2cart tool --- CMakeLists.txt | 17 +++--- build/tools/{bin2txt => }/bin2txt.c | 22 ++++++++ build/tools/cart2prj.c | 82 +++++++++++++++++++++++++++++ build/tools/prj2cart.c | 82 +++++++++++++++++++++++++++++ src/map.c | 2 +- src/music.c | 2 +- src/project.c | 6 +-- src/project.h | 7 +++ src/studio.c | 25 +-------- src/studio.h | 7 --- src/tools.c | 23 ++++++++ src/tools.h | 1 + 12 files changed, 235 insertions(+), 41 deletions(-) rename build/tools/{bin2txt => }/bin2txt.c (53%) create mode 100644 build/tools/cart2prj.c create mode 100644 build/tools/prj2cart.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c1ed38c0..9c228d271 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -572,18 +572,23 @@ endif () set(CMAKE_DISABLE_TESTING ON CACHE BOOL "" FORCE) add_subdirectory(${THIRDPARTY_DIR}/zip) +set(TOOLS_DIR ${CMAKE_SOURCE_DIR}/build/tools) + ################################ -# bin2txt +# bin2txt cart2prj prj2cart ################################ if(BUILD_DEMO_CARTS) - set(BIN2TXT_DIR ${CMAKE_SOURCE_DIR}/build/tools/bin2txt) - set(BIN2TXT_SRC - ${BIN2TXT_DIR}/bin2txt.c - ) - add_executable(bin2txt ${BIN2TXT_SRC}) + add_executable(cart2prj ${TOOLS_DIR}/cart2prj.c ${CMAKE_SOURCE_DIR}/src/project.c) + target_include_directories(cart2prj PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include) + target_link_libraries(cart2prj tic80core) + + add_executable(prj2cart ${TOOLS_DIR}/prj2cart.c ${CMAKE_SOURCE_DIR}/src/project.c) + target_include_directories(prj2cart PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include) + target_link_libraries(prj2cart tic80core) + add_executable(bin2txt ${TOOLS_DIR}/bin2txt.c) target_link_libraries(bin2txt zlib) file(GLOB DEMO_CARTS ${CMAKE_SOURCE_DIR}/demos/*.tic ) diff --git a/build/tools/bin2txt/bin2txt.c b/build/tools/bin2txt.c similarity index 53% rename from build/tools/bin2txt/bin2txt.c rename to build/tools/bin2txt.c index 7a1ffc068..3c3f43dfd 100644 --- a/build/tools/bin2txt/bin2txt.c +++ b/build/tools/bin2txt.c @@ -1,3 +1,25 @@ +// MIT License + +// Copyright (c) 2020 Vadim Grigoruk @nesbox // grigoruk@gmail.com + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include #include #include diff --git a/build/tools/cart2prj.c b/build/tools/cart2prj.c new file mode 100644 index 000000000..5e964ad8c --- /dev/null +++ b/build/tools/cart2prj.c @@ -0,0 +1,82 @@ +// MIT License + +// Copyright (c) 2020 Vadim Grigoruk @nesbox // grigoruk@gmail.com + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include +#include +#include "project.h" + +int main(int argc, char** argv) +{ + int res = -1; + + if(argc == 3) + { + FILE* cartFile = fopen(argv[1], "rb"); + + if(cartFile) + { + fseek(cartFile, 0, SEEK_END); + int size = ftell(cartFile); + fseek(cartFile, 0, SEEK_SET); + + unsigned char* buffer = (unsigned char*)malloc(size); + + if(buffer) + { + fread(buffer, size, 1, cartFile); + fclose(cartFile); + + tic_cartridge cart = {0}; + + tic_cart_load(&cart, buffer, size); + + FILE* project = fopen(argv[2], "wb"); + + if(project) + { + unsigned char* out = (unsigned char*)malloc(sizeof(tic_cartridge) * 3); + + if(out) + { + s32 outSize = tic_project_save(argv[2], out, &cart); + + fwrite(out, outSize, 1, project); + + free(out); + } + + fclose(project); + + res = 0; + } + else printf("cannot open project file\n"); + + free(buffer); + } + + } + else printf("cannot open cartridge file\n"); + } + else printf("usage: cart2prj \n"); + + return res; +} diff --git a/build/tools/prj2cart.c b/build/tools/prj2cart.c new file mode 100644 index 000000000..c30a747fe --- /dev/null +++ b/build/tools/prj2cart.c @@ -0,0 +1,82 @@ +// MIT License + +// Copyright (c) 2020 Vadim Grigoruk @nesbox // grigoruk@gmail.com + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include +#include +#include "project.h" + +int main(int argc, char** argv) +{ + int res = -1; + + if(argc == 3) + { + FILE* project = fopen(argv[1], "rb"); + + if(project) + { + fseek(project, 0, SEEK_END); + int size = ftell(project); + fseek(project, 0, SEEK_SET); + + unsigned char* buffer = (unsigned char*)malloc(size); + + if(buffer) + { + fread(buffer, size, 1, project); + fclose(project); + + tic_cartridge cart = {0}; + + tic_project_load(argv[1], buffer, size, &cart); + + FILE* cartFile = fopen(argv[2], "wb"); + + if(cartFile) + { + unsigned char* out = (unsigned char*)malloc(sizeof(tic_cartridge)); + + if(out) + { + int outSize = tic_cart_save(&cart, out); + + fwrite(out, outSize, 1, cartFile); + + free(out); + } + + fclose(cartFile); + + res = 0; + } + else printf("cannot open cartridge file\n"); + + free(buffer); + } + + } + else printf("cannot open project file\n"); + } + else printf("usage: prj2cart \n"); + + return res; +} diff --git a/src/map.c b/src/map.c index e621120da..38be1deb1 100644 --- a/src/map.c +++ b/src/map.c @@ -1047,7 +1047,7 @@ static void copyFromClipboard(Map* map) { u8* data = malloc(size); - str2buf(clipboard, strlen(clipboard), data, true); + tic_tool_str2buf(clipboard, strlen(clipboard), data, true); if(data[0] * data[1] == size - 2) { diff --git a/src/music.c b/src/music.c index 11774a471..7d501de04 100644 --- a/src/music.c +++ b/src/music.c @@ -685,7 +685,7 @@ static void copyFromClipboard(Music* music) { u8* data = malloc(size); - str2buf(clipboard, strlen(clipboard), data, true); + tic_tool_str2buf(clipboard, strlen(clipboard), data, true); ClipboardHeader header = {0}; diff --git a/src/project.c b/src/project.c index 8ebbe0348..df1d854c1 100644 --- a/src/project.c +++ b/src/project.c @@ -21,7 +21,7 @@ // SOFTWARE. #include "project.h" -#include "studio.h" +#include "tools.h" #include #include @@ -224,7 +224,7 @@ static bool loadBinarySection(const char* project, const char* comment, const ch if(index < count) { ptr += sizeof("-- 999:") - 1; - str2buf(ptr, size*2, (u8*)dst + size*index, flip); + tic_tool_str2buf(ptr, size*2, (u8*)dst + size*index, flip); ptr += size*2 + 1; ptr = getLineEnd(ptr); @@ -235,7 +235,7 @@ static bool loadBinarySection(const char* project, const char* comment, const ch else { ptr += sizeof("-- 999:") - 1; - str2buf(ptr, end - ptr, (u8*)dst, flip); + tic_tool_str2buf(ptr, end - ptr, (u8*)dst, flip); } done = true; diff --git a/src/project.h b/src/project.h index 70f319d3f..7cf6b0ad7 100644 --- a/src/project.h +++ b/src/project.h @@ -24,5 +24,12 @@ #include "cart.h" +#define PROJECT_LUA_EXT ".lua" +#define PROJECT_MOON_EXT ".moon" +#define PROJECT_JS_EXT ".js" +#define PROJECT_WREN_EXT ".wren" +#define PROJECT_SQUIRREL_EXT ".nut" +#define PROJECT_FENNEL_EXT ".fnl" + bool tic_project_load(const char* name, const char* data, s32 size, tic_cartridge* dst); s32 tic_project_save(const char* name, void* data, const tic_cartridge* cart); diff --git a/src/studio.c b/src/studio.c index 9a2dceadd..2fd3add45 100644 --- a/src/studio.c +++ b/src/studio.c @@ -36,6 +36,7 @@ #include "dialog.h" #include "menu.h" #include "surf.h" +#include "project.h" #include "fs.h" @@ -539,28 +540,6 @@ void toClipboard(const void* data, s32 size, bool flip) } } -void str2buf(const char* str, s32 size, void* buf, bool flip) -{ - char val[] = "0x00"; - const char* ptr = str; - - for(s32 i = 0; i < size/2; i++) - { - if(flip) - { - val[3] = *ptr++; - val[2] = *ptr++; - } - else - { - val[2] = *ptr++; - val[3] = *ptr++; - } - - ((u8*)buf)[i] = (u8)strtol(val, NULL, 16); - } -} - static void removeWhiteSpaces(char* str) { s32 i = 0; @@ -588,7 +567,7 @@ bool fromClipboard(void* data, s32 size, bool flip, bool remove_white_spaces) bool valid = strlen(clipboard) == size * 2; - if(valid) str2buf(clipboard, strlen(clipboard), data, flip); + if(valid) tic_tool_str2buf(clipboard, strlen(clipboard), data, flip); getSystem()->freeClipboardText(clipboard); diff --git a/src/studio.h b/src/studio.h index abc3243b1..e64b1e42a 100644 --- a/src/studio.h +++ b/src/studio.h @@ -57,12 +57,6 @@ #define KEYMAP_DAT_PATH TIC_LOCAL_VERSION KEYMAP_DAT #define CART_EXT ".tic" -#define PROJECT_LUA_EXT ".lua" -#define PROJECT_MOON_EXT ".moon" -#define PROJECT_JS_EXT ".js" -#define PROJECT_WREN_EXT ".wren" -#define PROJECT_SQUIRREL_EXT ".nut" -#define PROJECT_FENNEL_EXT ".fnl" #define SHOW_TOOLTIP(FORMAT, ...) \ { \ @@ -122,7 +116,6 @@ void exitStudio(); u32 zip(u8* dest, size_t destSize, const u8* source, size_t size); u32 unzip(u8* dest, size_t bufSize, const u8* source, size_t size); -void str2buf(const char* str, s32 size, void* buf, bool flip); void toClipboard(const void* data, s32 size, bool flip); bool fromClipboard(void* data, s32 size, bool flip, bool remove_white_spaces); diff --git a/src/tools.c b/src/tools.c index 970b1d73c..1054d0440 100644 --- a/src/tools.c +++ b/src/tools.c @@ -23,6 +23,7 @@ #include "tools.h" #include +#include extern void tic_tool_poke4(void* addr, u32 index, u8 value); extern u8 tic_tool_peek4(const void* addr, u32 index); @@ -157,3 +158,25 @@ bool tic_tool_is_noise(const tic_waveform* wave) return true; } + +void tic_tool_str2buf(const char* str, s32 size, void* buf, bool flip) +{ + char val[] = "0x00"; + const char* ptr = str; + + for(s32 i = 0; i < size/2; i++) + { + if(flip) + { + val[3] = *ptr++; + val[2] = *ptr++; + } + else + { + val[2] = *ptr++; + val[3] = *ptr++; + } + + ((u8*)buf)[i] = (u8)strtol(val, NULL, 16); + } +} diff --git a/src/tools.h b/src/tools.h index b507b418f..f8d0b5e59 100644 --- a/src/tools.h +++ b/src/tools.h @@ -82,3 +82,4 @@ bool tic_tool_has_ext(const char* name, const char* ext); s32 tic_tool_get_track_row_sfx(const tic_track_row* row); void tic_tool_set_track_row_sfx(tic_track_row* row, s32 sfx); bool tic_tool_is_noise(const tic_waveform* wave); +void tic_tool_str2buf(const char* str, s32 size, void* buf, bool flip);