From 561f91b97dbc8beb8018c64414aab4ddda5de2b4 Mon Sep 17 00:00:00 2001 From: Pixelsuft Date: Tue, 4 Jun 2024 16:18:15 +0700 Subject: [PATCH] wip --- .gitignore | 1 + src/ui-mobile.c | 38 ++++++++++++++++++++++++++++++++------ src/util.c | 6 +++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 7f38792..e46e720 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ vs *.o *.a *.exe +*.apk *.dll *.zip *.rar diff --git a/src/ui-mobile.c b/src/ui-mobile.c index 20028e3..ea1b54e 100644 --- a/src/ui-mobile.c +++ b/src/ui-mobile.c @@ -257,6 +257,36 @@ void mobui_place_elems(void) { page.cfg_btn.base.set_rect(&page.cfg_btn, &tr3); } +void mobui_copy_config(void) { + void* in_f = h_fopen("default_mobile.conf", "rb"); + if (in_f == NULL) + return; + int64_t sz = h_fsize(in_f); + if (sz <= 0) { + fclose(in_f); + return; + } + void* buf = h_malloc((size_t)sz + 1); + if (buf == NULL) { + fclose(in_f); + return; + } + h_fread(buf, 1, (size_t)sz, in_f); + h_fclose(in_f); + char out_path[1024 * 10]; + size_t path_inp_len = strlen(page.path_inp.text); + memcpy(out_path, page.path_inp.text, path_inp_len); + strcpy(out_path + path_inp_len, "/halfix.conf"); + void* out_f = h_fopen(out_path, "wb"); + if (out_f == NULL) { + h_free(buf); + return; + } + h_fwrite(buf, 1, (size_t)sz, out_f); + h_fclose(out_f); + h_free(buf); +} + void mobui_run_main(void) { int running = 1; SDL_Event ev; @@ -342,12 +372,7 @@ void mobui_run_main(void) { } if (page.cfg_btn.was_pressed) { page.cfg_btn.was_pressed = 0; - char cmd_buf[10 * 1024] = "cp default_mobile.conf \""; - size_t path_len = strlen(page.path_inp.text); - memcpy(cmd_buf + 24, page.path_inp.text, path_len + 1); - cmd_buf[24 + path_len] = '"'; - cmd_buf[25 + path_len] = '\0'; - system(cmd_buf); + mobui_copy_config(); } SDL_RenderPresent(ren); } @@ -388,6 +413,7 @@ void mobui_init(void) { page.elems[2] = (mobui_elem*)&page.cfg_btn; page.elem_count = 10; #ifndef MOBILE_WIP + // IDK which is right... SDL_AndroidRequestPermission("READ_EXTERNAL_STORAGE"); SDL_AndroidRequestPermission("permission.READ_EXTERNAL_STORAGE"); SDL_AndroidRequestPermission("android.permission.READ_EXTERNAL_STORAGE"); diff --git a/src/util.c b/src/util.c index 04347c3..214ccca 100644 --- a/src/util.c +++ b/src/util.c @@ -77,11 +77,11 @@ void h_free(void* ptr) { void* h_fopen(const char* fp, const char* mode) { #if defined(_WIN32) && !defined(PREFER_SDL2) && !defined(PREFER_STD) - int can_write = mode[0] == 'r' && mode[1] == 'b' && mode[2] == '+'; // Hack + int can_write = (mode[0] == 'r' && mode[1] == 'b' && mode[2] == '+') || (mode[0] == 'w'); // Hack #ifdef FILES_WIN32_USE_ANSI HANDLE res = CreateFileA( fp, GENERIC_READ | (can_write ? GENERIC_WRITE : 0), FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL + (mode[0] == 'w') ? CREATE_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); #else int count = MultiByteToWideChar(CP_UTF8, 0, fp, (int)strlen(fp), NULL, 0); @@ -98,7 +98,7 @@ void* h_fopen(const char* fp, const char* mode) { fp_buf[encode_res] = L'\0'; HANDLE res = CreateFileW( fp_buf, GENERIC_READ | (can_write ? GENERIC_WRITE : 0), FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL + (mode[0] == 'w') ? CREATE_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); h_free(fp_buf); #endif