Skip to content

Commit

Permalink
#1089: added prj2cart tool
Browse files Browse the repository at this point in the history
  • Loading branch information
nesbox committed Jun 27, 2020
1 parent df5441e commit 74d84d8
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 41 deletions.
17 changes: 11 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
22 changes: 22 additions & 0 deletions build/tools/bin2txt/bin2txt.c → build/tools/bin2txt.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// MIT License

// Copyright (c) 2020 Vadim Grigoruk @nesbox // [email protected]

// 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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand Down
82 changes: 82 additions & 0 deletions build/tools/cart2prj.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// MIT License

// Copyright (c) 2020 Vadim Grigoruk @nesbox // [email protected]

// 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 <stdio.h>
#include <stdlib.h>
#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 <cartridge> <project>\n");

return res;
}
82 changes: 82 additions & 0 deletions build/tools/prj2cart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// MIT License

// Copyright (c) 2020 Vadim Grigoruk @nesbox // [email protected]

// 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 <stdio.h>
#include <stdlib.h>
#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 <project> <cartridge>\n");

return res;
}
2 changes: 1 addition & 1 deletion src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
6 changes: 3 additions & 3 deletions src/project.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// SOFTWARE.

#include "project.h"
#include "studio.h"
#include "tools.h"

#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
25 changes: 2 additions & 23 deletions src/studio.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "dialog.h"
#include "menu.h"
#include "surf.h"
#include "project.h"

#include "fs.h"

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 0 additions & 7 deletions src/studio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) \
{ \
Expand Down Expand Up @@ -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);

Expand Down
23 changes: 23 additions & 0 deletions src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "tools.h"

#include <string.h>
#include <stdlib.h>

extern void tic_tool_poke4(void* addr, u32 index, u8 value);
extern u8 tic_tool_peek4(const void* addr, u32 index);
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 74d84d8

Please sign in to comment.