From 06846de000ac45855ca41e131fabd1bf13a8cdb7 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Mon, 2 Mar 2026 08:27:09 +0100 Subject: [PATCH] base: cleanup --- base/sources/backends/vulkan_gpu.h | 1 - base/sources/iron.h | 168 ---------------------- base/sources/iron_file.c | 168 +++++++++++++++++++++- base/sources/iron_file.h | 9 ++ base/sources/iron_global.h | 11 ++ base/sources/iron_raycast.c | 0 base/sources/iron_raycast.h | 0 base/sources/ts/file.ts | 6 +- paint/sources/box_preferences.ts | 2 +- paint/sources/import_blend_material.ts | 2 +- paint/sources/neural_nodes/neural_node.ts | 2 +- 11 files changed, 190 insertions(+), 179 deletions(-) create mode 100644 base/sources/iron_raycast.c create mode 100644 base/sources/iron_raycast.h diff --git a/base/sources/backends/vulkan_gpu.h b/base/sources/backends/vulkan_gpu.h index a259f508..087163b7 100644 --- a/base/sources/backends/vulkan_gpu.h +++ b/base/sources/backends/vulkan_gpu.h @@ -3,7 +3,6 @@ #include #include #include -#include #include typedef struct gpu_pipeline_impl { diff --git a/base/sources/iron.h b/base/sources/iron.h index 8530bb4f..1ef4093e 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -159,16 +159,6 @@ buffer_t *embed_get(char *key) { } #endif -#define f64 double -#define i64 int64_t -#define u64 uint64_t -#define f32 float -#define i32 int32_t -#define u32 uint32_t -#define i16 int16_t -#define u16 uint16_t -#define i8 int8_t -#define u8 uint8_t #define string_t char #define any void * #define any_ptr void ** @@ -1306,25 +1296,6 @@ void _gpu_begin(gpu_texture_t *render_target, any_array_t *additional, gpu_textu } } -void iron_file_save_bytes(string_t *path, buffer_t *bytes, u64 length) { - u64 byte_length = length > 0 ? length : (u64)bytes->length; - if (byte_length > (u64)bytes->length) { - byte_length = (u64)bytes->length; - } - -#ifdef IRON_WINDOWS - MultiByteToWideChar(CP_UTF8, 0, path, -1, temp_wstring, 1024); - FILE *file = _wfopen(temp_wstring, L"wb"); -#else - FILE *file = fopen(path, "wb"); -#endif - if (file == NULL) { - return; - } - fwrite(bytes->buffer, 1, byte_length, file); - fclose(file); -} - i32 iron_sys_command(string_t *cmd) { #ifdef IRON_WINDOWS @@ -1359,56 +1330,6 @@ i32 iron_sys_command(string_t *cmd) { return result; } -typedef struct _callback_data { - int32_t size; - char url[512]; - void (*func)(char *, buffer_t *); -} _callback_data_t; - -void _https_callback(const char *body, void *callback_data) { - _callback_data_t *cbd = (_callback_data_t *)callback_data; - buffer_t *buffer = NULL; - if (body != NULL) { - buffer = malloc(sizeof(buffer_t)); - buffer->length = cbd->size > 0 ? cbd->size : strlen(body); - buffer->buffer = malloc(buffer->length); - memcpy(buffer->buffer, body, buffer->length); - } - cbd->func(cbd->url, buffer); - free(cbd); -} - -void iron_file_download(string_t *url, void (*callback)(char *, buffer_t *), i32 size, string_t *dst_path) { - _callback_data_t *cbd = malloc(sizeof(_callback_data_t)); - cbd->size = size; - strcpy(cbd->url, url); - cbd->func = callback; - - char url_base[512]; - char url_path[512]; - int i = 0; - for (; i < strlen(url) - 8; ++i) { - if (url[i + 8] == '/') { - break; - } - url_base[i] = url[i + 8]; // Strip https:// - } - url_base[i] = 0; - int j = 0; - if (strlen(url_base) < strlen(url) - 8) { - ++i; // Skip / - } - for (; j < strlen(url) - 8 - i; ++j) { - if (url[i + 8 + j] == 0) { - break; - } - url_path[j] = url[i + 8 + j]; - } - url_path[j] = 0; - - iron_net_request(url_base, url_path, NULL, 443, IRON_HTTPS_GET, &_https_callback, cbd, dst_path); -} - bool _save_and_quit_callback_internal(); bool _save_and_quit_callback(void *data) { @@ -1507,95 +1428,6 @@ char *iron_save_dialog(char *filter_list, char *default_path) { #endif -char *iron_read_directory(char *path) { - char *files = temp_string; - files[0] = 0; - - directory dir = open_dir(path); - if (dir.handle == NULL) { - return files; - } - - while (true) { - file f = read_next_file(&dir); - if (!f.valid) { - break; - } - -#ifdef IRON_WINDOWS - char file_path[512]; - strcpy(file_path, path); - strcat(file_path, "\\"); - strcat(file_path, f.name); - if (FILE_ATTRIBUTE_HIDDEN & GetFileAttributesA(file_path)) { - continue; // Skip hidden files - } -#endif - - if (files[0] != '\0') { - strcat(files, "\n"); - } - strcat(files, f.name); - } - close_dir(&dir); - return files; -} - -void iron_create_directory(char *path) { -#ifdef IRON_IOS - IOSCreateDirectory(path); -#elif defined(IRON_WINDOWS) - char cmd[1024]; - strcpy(cmd, "mkdir \""); - strcat(cmd, path); - strcat(cmd, "\""); - iron_sys_command(cmd); -#else - char cmd[1024]; - strcpy(cmd, "mkdir -p \""); - strcat(cmd, path); - strcat(cmd, "\""); - iron_sys_command(cmd); -#endif -} - -bool iron_is_directory(char *path) { -#ifdef IRON_WINDOWS - DWORD attribs = GetFileAttributesA(path); - return attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY); -#else - struct stat st; - return stat(path, &st) == 0 && S_ISDIR(st.st_mode); -#endif -} - -bool iron_file_exists(char *path) { - iron_file_reader_t reader; - if (iron_file_reader_open(&reader, path, IRON_FILE_TYPE_ASSET)) { - iron_file_reader_close(&reader); - return true; - } - return false; -} - -void iron_delete_file(char *path) { -#ifdef IRON_IOS - IOSDeleteFile(path); -#elif defined(IRON_WINDOWS) - char cmd[1024]; - strcpy(cmd, "del /f \""); - strcat(cmd, path); - strcat(cmd, "\""); - iron_sys_command(cmd); -#else - char cmd[1024]; - strcpy(cmd, "rm \""); - strcat(cmd, path); - strcat(cmd, "\""); - iron_sys_command(cmd); -#endif -} - #ifdef WITH_COMPRESS buffer_t *iron_inflate(buffer_t *bytes, bool raw) { unsigned char *inflated = NULL; diff --git a/base/sources/iron_file.c b/base/sources/iron_file.c index 5b30d480..f5beb276 100644 --- a/base/sources/iron_file.c +++ b/base/sources/iron_file.c @@ -1,6 +1,9 @@ #include "iron_file.h" #include "iron_system.h" +#include "iron_string.h" +#include "iron_net.h" +#include "libs/kong/dir.h" #ifdef IRON_ANDROID #include #endif @@ -10,11 +13,14 @@ #ifdef IRON_WINDOWS #include #include -#endif -#ifdef IRON_WINDOWS #include +#else +#include #endif +i32 iron_sys_command(char *cmd); +extern char temp_string[1024 * 128]; + static char *fileslocation = NULL; #ifdef IRON_WINDOWS static wchar_t wfilepath[1001]; @@ -292,3 +298,161 @@ void iron_file_writer_write(iron_file_writer_t *writer, void *data, int size) { fwrite(data, 1, size, (FILE *)writer->file); #endif } + +char *iron_read_directory(char *path) { + char *files = temp_string; + files[0] = 0; + + directory dir = open_dir(path); + if (dir.handle == NULL) { + return files; + } + + while (true) { + file f = read_next_file(&dir); + if (!f.valid) { + break; + } + +#ifdef IRON_WINDOWS + char file_path[512]; + strcpy(file_path, path); + strcat(file_path, "\\"); + strcat(file_path, f.name); + if (FILE_ATTRIBUTE_HIDDEN & GetFileAttributesA(file_path)) { + continue; // Skip hidden files + } +#endif + + if (files[0] != '\0') { + strcat(files, "\n"); + } + strcat(files, f.name); + } + close_dir(&dir); + return files; +} + +void iron_create_directory(char *path) { +#ifdef IRON_IOS + IOSCreateDirectory(path); +#elif defined(IRON_WINDOWS) + char cmd[1024]; + strcpy(cmd, "mkdir \""); + strcat(cmd, path); + strcat(cmd, "\""); + iron_sys_command(cmd); +#else + char cmd[1024]; + strcpy(cmd, "mkdir -p \""); + strcat(cmd, path); + strcat(cmd, "\""); + iron_sys_command(cmd); +#endif +} + +bool iron_is_directory(char *path) { +#ifdef IRON_WINDOWS + DWORD attribs = GetFileAttributesA(path); + return attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY); +#else + struct stat st; + return stat(path, &st) == 0 && S_ISDIR(st.st_mode); +#endif +} + +bool iron_file_exists(char *path) { + iron_file_reader_t reader; + if (iron_file_reader_open(&reader, path, IRON_FILE_TYPE_ASSET)) { + iron_file_reader_close(&reader); + return true; + } + return false; +} + +void iron_delete_file(char *path) { +#ifdef IRON_IOS + IOSDeleteFile(path); +#elif defined(IRON_WINDOWS) + char cmd[1024]; + strcpy(cmd, "del /f \""); + strcat(cmd, path); + strcat(cmd, "\""); + iron_sys_command(cmd); +#else + char cmd[1024]; + strcpy(cmd, "rm \""); + strcat(cmd, path); + strcat(cmd, "\""); + iron_sys_command(cmd); +#endif +} + +void iron_file_save_bytes(char *path, buffer_t *bytes, u64 length) { + u64 byte_length = length > 0 ? length : (u64)bytes->length; + if (byte_length > (u64)bytes->length) { + byte_length = (u64)bytes->length; + } + +#ifdef IRON_WINDOWS + MultiByteToWideChar(CP_UTF8, 0, path, -1, temp_wstring, 1024); + FILE *file = _wfopen(temp_wstring, L"wb"); +#else + FILE *file = fopen(path, "wb"); +#endif + if (file == NULL) { + return; + } + fwrite(bytes->buffer, 1, byte_length, file); + fclose(file); +} + +typedef struct _callback_data { + int32_t size; + char url[512]; + void (*func)(char *, buffer_t *); +} _callback_data_t; + +void _https_callback(const char *body, void *callback_data) { + _callback_data_t *cbd = (_callback_data_t *)callback_data; + buffer_t *buffer = NULL; + if (body != NULL) { + buffer = malloc(sizeof(buffer_t)); + buffer->length = cbd->size > 0 ? cbd->size : strlen(body); + buffer->buffer = malloc(buffer->length); + memcpy(buffer->buffer, body, buffer->length); + } + cbd->func(cbd->url, buffer); + free(cbd); +} + +void iron_file_download(char *url, void (*callback)(char *, buffer_t *), i32 size, char *dst_path) { + _callback_data_t *cbd = malloc(sizeof(_callback_data_t)); + cbd->size = size; + strcpy(cbd->url, url); + cbd->func = callback; + + char url_base[512]; + char url_path[512]; + int i = 0; + for (; i < strlen(url) - 8; ++i) { + if (url[i + 8] == '/') { + break; + } + url_base[i] = url[i + 8]; // Strip https:// + } + url_base[i] = 0; + int j = 0; + if (strlen(url_base) < strlen(url) - 8) { + ++i; // Skip / + } + for (; j < strlen(url) - 8 - i; ++j) { + if (url[i + 8 + j] == 0) { + break; + } + url_path[j] = url[i + 8 + j]; + } + url_path[j] = 0; + + iron_net_request(url_base, url_path, NULL, 443, IRON_HTTPS_GET, &_https_callback, cbd, dst_path); +} diff --git a/base/sources/iron_file.h b/base/sources/iron_file.h index 39b0b087..81f205c2 100644 --- a/base/sources/iron_file.h +++ b/base/sources/iron_file.h @@ -1,6 +1,7 @@ #pragma once #include "iron_global.h" +#include "iron_array.h" #include #include #include @@ -61,3 +62,11 @@ typedef struct iron_file_writer { bool iron_file_writer_open(iron_file_writer_t *writer, const char *filepath); void iron_file_writer_write(iron_file_writer_t *writer, void *data, int size); void iron_file_writer_close(iron_file_writer_t *writer); + +char *iron_read_directory(char *path); +void iron_create_directory(char *path); +bool iron_is_directory(char *path); +bool iron_file_exists(char *path); +void iron_delete_file(char *path); +void iron_file_save_bytes(char *path, buffer_t *bytes, u64 length); +void iron_file_download(char *url, void (*callback)(char *, buffer_t *), i32 size, char *dst_path); diff --git a/base/sources/iron_global.h b/base/sources/iron_global.h index f38c0bb8..1829c10c 100644 --- a/base/sources/iron_global.h +++ b/base/sources/iron_global.h @@ -25,3 +25,14 @@ #endif int kickstart(int argc, char **argv); + +typedef float f32; +typedef double f64; +typedef int8_t i8; +typedef uint8_t u8; +typedef int16_t i16; +typedef uint16_t u16; +typedef int32_t i32; +typedef uint32_t u32; +typedef int64_t i64; +typedef uint64_t u64; diff --git a/base/sources/iron_raycast.c b/base/sources/iron_raycast.c new file mode 100644 index 00000000..e69de29b diff --git a/base/sources/iron_raycast.h b/base/sources/iron_raycast.h new file mode 100644 index 00000000..e69de29b diff --git a/base/sources/ts/file.ts b/base/sources/ts/file.ts index d058cf0d..b1b3bc86 100644 --- a/base/sources/ts/file.ts +++ b/base/sources/ts/file.ts @@ -76,10 +76,6 @@ function file_read_directory(path: string): string[] { return files; } -function file_create_directory(path: string) { - iron_create_directory(path); -} - function file_copy(src_path: string, dst_path: string) { iron_sys_command(file_cmd_copy + " \"" + src_path + "\" \"" + dst_path + "\""); } @@ -120,7 +116,7 @@ function file_cache_cloud(path: string, done: (s: string) => void) { let file_dir: string = substring(dest, 0, string_last_index_of(dest, PATH_SEP)); if (file_read_directory(file_dir)[0] == "") { - file_create_directory(file_dir); + iron_create_directory(file_dir); } /// if arm_windows path = string_replace_all(path, "\\", "/"); diff --git a/paint/sources/box_preferences.ts b/paint/sources/box_preferences.ts index c2941feb..3f958071 100644 --- a/paint/sources/box_preferences.ts +++ b/paint/sources/box_preferences.ts @@ -641,7 +641,7 @@ function box_preferences_neural_tab() { ui_row(row); if (ui_icon_button(tr("Models Directory..."), icon_t.FOLDER)) { if (file_read_directory(neural_node_dir())[0] == "") { - file_create_directory(neural_node_dir()); + iron_create_directory(neural_node_dir()); } file_start(neural_node_dir()); } diff --git a/paint/sources/import_blend_material.ts b/paint/sources/import_blend_material.ts index 7100653d..c3d4f6d5 100644 --- a/paint/sources/import_blend_material.ts +++ b/paint/sources/import_blend_material.ts @@ -40,7 +40,7 @@ function _import_blend_material() { save = path_data(); } save += "blender"; - file_create_directory(save); + iron_create_directory(save); let py: string = "\ import bpy;\n\ diff --git a/paint/sources/neural_nodes/neural_node.ts b/paint/sources/neural_nodes/neural_node.ts index 189be491..a7063ec0 100644 --- a/paint/sources/neural_nodes/neural_node.ts +++ b/paint/sources/neural_nodes/neural_node.ts @@ -150,7 +150,7 @@ function neural_node_download(url: string) { function neural_node_download_models(models: string[]) { if (file_read_directory(neural_node_dir())[0] == "") { - file_create_directory(neural_node_dir()); + iron_create_directory(neural_node_dir()); } /// if arm_windows