awtk/tools/res_gen/main.c

245 lines
9.2 KiB
C
Raw Normal View History

2018-04-01 08:12:52 +08:00
/**
* File: main.c
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2018-04-01 08:12:52 +08:00
* Brief: res to c source
*
2024-03-27 11:36:42 +08:00
* Copyright (c) 2018 - 2024 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-04-01 08:12:52 +08:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-04-01 Li XianJing <xianjimli@hotmail.com> created
*
*/
2020-03-23 12:16:07 +08:00
#include "tkc/fs.h"
#include "tkc/path.h"
#include "tkc/mem.h"
2018-04-01 08:12:52 +08:00
#include "common/utils.h"
2018-08-24 07:45:37 +08:00
#include "base/assets_manager.h"
2024-03-04 08:54:03 +08:00
static ret_t gen_one(const char* in_filename, const char* out_filename, const char* theme,
str_t* res_name) {
2020-03-23 12:16:07 +08:00
if (!exit_if_need_not_update(in_filename, out_filename)) {
uint32_t size = 0;
uint8_t* input_buff = NULL;
input_buff = (uint8_t*)read_file(in_filename, &size);
return_value_if_fail(input_buff != NULL, RET_FAIL);
if (case_end_with(in_filename, ".ttf")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".ttf", "");
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_FONT, ASSET_TYPE_FONT_TTF, input_buff,
2024-03-04 08:54:03 +08:00
size, res_name->str);
} else if (case_end_with(in_filename, ".png")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".png", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_PNG,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".bmp")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".bmp", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_BMP,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".jpg")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".jpg", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_JPG,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".jpeg")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".jpeg", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_JPG,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".gif")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".gif", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_GIF,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".lz4")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".lz4", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_LZ4,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".webp")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".webp", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_WEBP,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".js")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".js", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_SCRIPT, ASSET_TYPE_SCRIPT_JS,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".lua")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".lua", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_SCRIPT, ASSET_TYPE_SCRIPT_LUA,
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".py")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".py", "");
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_SCRIPT, ASSET_TYPE_SCRIPT_PYTHON,
2024-03-04 08:54:03 +08:00
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".xml")) {
2023-11-13 18:25:24 +08:00
str_replace(res_name, ".xml", "");
2024-03-04 08:54:03 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_XML, 0, input_buff, size,
res_name->str);
2020-03-23 12:16:07 +08:00
} else if (strstr(in_filename, "images") != NULL) {
output_res_c_source(out_filename, theme, ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_OTHER, input_buff,
size);
2021-11-23 10:42:33 +08:00
} else if (strstr(in_filename, "flows") != NULL) {
output_res_c_source(out_filename, theme, ASSET_TYPE_FLOW, 0, input_buff, size);
2020-03-23 12:16:07 +08:00
} else {
if (case_end_with(in_filename, ".txt")) {
2020-03-23 12:16:07 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_DATA, ASSET_TYPE_DATA_TEXT,
2023-11-13 18:25:24 +08:00
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".json")) {
2020-03-23 12:16:07 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_DATA, ASSET_TYPE_DATA_JSON,
2023-11-13 18:25:24 +08:00
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".bin")) {
2020-03-23 12:16:07 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_DATA, ASSET_TYPE_DATA_BIN,
2023-11-13 18:25:24 +08:00
input_buff, size, res_name->str);
} else if (case_end_with(in_filename, ".dat")) {
2020-03-23 12:16:07 +08:00
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_DATA, ASSET_TYPE_DATA_DAT,
2023-11-13 18:25:24 +08:00
input_buff, size, res_name->str);
2020-03-23 12:16:07 +08:00
} else {
output_res_c_source_ex(out_filename, theme, ASSET_TYPE_DATA, ASSET_TYPE_DATA_NONE,
2023-11-13 18:25:24 +08:00
input_buff, size, res_name->str);
2020-03-23 12:16:07 +08:00
}
}
TKMEM_FREE(input_buff);
}
return RET_OK;
}
2021-01-15 18:36:34 +08:00
static ret_t gen_folder(const char* in_foldername, const char* out_foldername, const char* theme,
2023-11-13 18:25:24 +08:00
const char* extname, const char* dir_name, bool_t data_folder) {
2023-04-14 18:10:22 +08:00
fs_item_t item;
2020-03-23 12:16:07 +08:00
ret_t ret = RET_OK;
char in_name[MAX_PATH] = {0};
char out_name[MAX_PATH] = {0};
2023-03-08 18:08:14 +08:00
fs_dir_t* dir = fs_open_dir(os_fs(), in_foldername);
2020-05-04 15:28:46 +08:00
while (fs_dir_read(dir, &item) != RET_FAIL) {
2020-03-23 12:16:07 +08:00
if (item.is_reg_file) {
str_t str_name;
2023-11-13 18:25:24 +08:00
str_t res_name;
2020-03-23 12:16:07 +08:00
char ext_array[MAX_PATH] = {0};
path_extname(item.name, ext_array, MAX_PATH);
2024-03-04 08:54:03 +08:00
2023-11-13 18:25:24 +08:00
str_init(&res_name, 0);
2020-03-23 12:16:07 +08:00
str_init(&str_name, 0);
str_set(&str_name, item.name);
2021-01-15 18:36:34 +08:00
if (!data_folder) {
2020-03-23 16:34:45 +08:00
str_replace(&str_name, ext_array, "");
2020-03-23 12:16:07 +08:00
}
2020-03-23 16:34:45 +08:00
filter_name(str_name.str);
str_append(&str_name, extname);
2020-03-23 12:16:07 +08:00
2023-11-13 18:25:24 +08:00
str_append(&res_name, dir_name);
str_append(&res_name, item.name);
2021-01-15 18:36:34 +08:00
path_build(in_name, MAX_PATH, in_foldername, item.name, NULL);
path_build(out_name, MAX_PATH, out_foldername, str_name.str, NULL);
2023-11-13 18:25:24 +08:00
ret = gen_one(in_name, out_name, theme, &res_name);
2020-03-23 12:16:07 +08:00
str_reset(&str_name);
2023-11-13 18:25:24 +08:00
str_reset(&res_name);
2020-03-23 12:16:07 +08:00
if (ret == RET_FAIL) {
2021-01-12 14:35:35 +08:00
printf(
"gen fail, filename = %s! desc = the resource file is empty, please confirm that the "
"resource file has saved data.!\n",
in_name);
2020-03-23 12:16:07 +08:00
break;
}
2023-03-08 18:08:14 +08:00
} else if (item.is_dir && !tk_str_eq(item.name, ".") && !tk_str_eq(item.name, "..")) {
2023-11-13 18:25:24 +08:00
str_t res_name;
2023-03-08 18:08:14 +08:00
path_build(in_name, MAX_PATH, in_foldername, item.name, NULL);
path_build(out_name, MAX_PATH, out_foldername, item.name, NULL);
if (!fs_dir_exist(os_fs(), out_name)) {
fs_create_dir(os_fs(), out_name);
}
2023-11-13 18:25:24 +08:00
str_init(&res_name, 0);
str_append(&res_name, item.name);
str_append(&res_name, "/");
ret = gen_folder(in_name, out_name, theme, extname, res_name.str, data_folder);
str_reset(&res_name);
2023-03-08 18:08:14 +08:00
if (ret != RET_OK) {
break;
}
2020-03-23 12:16:07 +08:00
}
}
fs_dir_close(dir);
return ret;
}
2021-01-15 18:36:34 +08:00
bool_t is_data_folder(const char* folder_name) {
2020-03-23 12:16:07 +08:00
char basename[MAX_PATH] = {0};
2021-01-15 18:36:34 +08:00
path_basename(folder_name, basename, MAX_PATH);
2020-03-23 12:16:07 +08:00
return tk_str_eq(basename, "data");
}
2018-04-01 08:12:52 +08:00
int wmain(int argc, wchar_t* argv[]) {
2018-11-02 11:51:14 +08:00
const char* in_filename = NULL;
const char* out_filename = NULL;
2020-06-17 17:59:38 +08:00
platform_prepare();
2018-04-01 08:12:52 +08:00
2020-03-23 12:16:07 +08:00
if (argc < 3) {
printf("Usage: %S in_filename out_filename [theme] [out_extname]\n", argv[0]);
2018-04-01 08:12:52 +08:00
return 0;
}
str_t in_file;
str_t out_file;
2020-03-23 12:16:07 +08:00
str_t str_extname;
str_t theme_name;
str_init(&in_file, 0);
str_init(&out_file, 0);
str_from_wstr(&in_file, argv[1]);
str_from_wstr(&out_file, argv[2]);
2020-03-23 12:16:07 +08:00
str_init(&theme_name, 0);
if (argc > 3) {
str_from_wstr(&theme_name, argv[3]);
}
str_init(&str_extname, 0);
if (argc > 4) {
str_from_wstr(&str_extname, argv[4]);
}
in_filename = in_file.str;
out_filename = out_file.str;
2018-04-01 08:12:52 +08:00
2020-03-23 12:16:07 +08:00
fs_stat_info_t in_stat_info;
fs_stat_info_t out_stat_info;
fs_stat(os_fs(), in_filename, &in_stat_info);
fs_stat(os_fs(), out_filename, &out_stat_info);
if (in_stat_info.is_dir == TRUE && out_stat_info.is_dir == TRUE) {
2023-11-13 18:25:24 +08:00
gen_folder(in_filename, out_filename, theme_name.str, str_extname.str, "",
2021-01-15 18:36:34 +08:00
is_data_folder(in_filename));
2020-03-23 12:16:07 +08:00
} else if (in_stat_info.is_reg_file == TRUE) {
2023-11-13 18:25:24 +08:00
str_t res_name;
char name[MAX_PATH + 1] = {0};
path_basename_ex(in_filename, TRUE, name, sizeof(name));
str_init(&res_name, 0);
str_append(&res_name, name);
if (gen_one(in_filename, out_filename, theme_name.str, &res_name) == RET_FAIL) {
2021-01-12 14:35:35 +08:00
printf(
"gen fail, filename = %s! desc = the resource file is empty, please confirm that the "
"resource file has saved data.!\n",
in_filename);
}
2023-11-13 18:25:24 +08:00
str_reset(&res_name);
2020-03-23 12:16:07 +08:00
} else {
GEN_ERROR(in_filename);
2018-04-01 08:12:52 +08:00
}
str_reset(&in_file);
str_reset(&out_file);
2020-03-23 12:16:07 +08:00
str_reset(&theme_name);
str_reset(&str_extname);
2018-04-01 08:12:52 +08:00
printf("done\n");
return 0;
}
#include "common/main.inc"