From db616f27fead64f18f2ce89938a77bedb8c2ba94 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Wed, 20 Mar 2019 11:16:26 +0800 Subject: [PATCH] improve assets build script to support any file extensions in data folder. --- demos/assets.c | 4 ++ demos/assets/inc/data/abc.data | 4 ++ demos/assets/inc/data/app.data | 5 ++ demos/assets/inc/data/test.data | 4 +- demos/assets/raw/data/abc.any | 1 + demos/assets/raw/data/app.json | 3 ++ docs/README.md | 1 + docs/assets_manager.md | 6 ++- docs/changes.md | 4 ++ docs/how_to_build_windows_32bit_version.md | 29 ++++++++++++ docs/index.md | 1 + src/base/assets_manager.c | 6 +-- src/base/assets_manager.h | 14 +++++- src/tkc/utils.c | 15 ++++-- src/tkc/utils.h | 1 + tests/SConscript | 1 + tests/assets_manager_test.cc | 54 ++++++++++++++++------ tests/utils_test.cc | 10 ++++ tools/common/utils.c | 36 +++++++++++++-- tools/common/utils.h | 2 + tools/res_gen/main.c | 25 +++++++++- 21 files changed, 194 insertions(+), 32 deletions(-) create mode 100644 demos/assets/inc/data/abc.data create mode 100644 demos/assets/inc/data/app.data create mode 100644 demos/assets/raw/data/abc.any create mode 100644 demos/assets/raw/data/app.json create mode 100644 docs/how_to_build_windows_32bit_version.md diff --git a/demos/assets.c b/demos/assets.c index 255763a0e..ebb10ebeb 100644 --- a/demos/assets.c +++ b/demos/assets.c @@ -118,6 +118,8 @@ #include "assets/inc/ui/scroll_view_v.data" #include "assets/inc/xml/test.data" #include "assets/inc/data/test.data" +#include "assets/inc/data/app.data" +#include "assets/inc/data/abc.data" #ifdef WITH_STB_IMAGE #include "assets/inc/images/ani6.res" #include "assets/inc/images/checked.res" @@ -627,6 +629,8 @@ ret_t assets_init(void) { assets_manager_add(rm, style_dialog_warn); assets_manager_add(rm, font_default); assets_manager_add(rm, data_test); + assets_manager_add(rm, data_app); + assets_manager_add(rm, data_abc); #ifdef WITH_VGCANVAS assets_manager_add(rm, image_pointer_4); assets_manager_add(rm, image_china); diff --git a/demos/assets/inc/data/abc.data b/demos/assets/inc/data/abc.data new file mode 100644 index 000000000..0b2ebbcfb --- /dev/null +++ b/demos/assets/inc/data/abc.data @@ -0,0 +1,4 @@ +const unsigned char data_abc[] = { +0x08,0x00,0x00,0x01,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x61,0x62,0x63,0x2e,0x61,0x6e,0x79,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x74,0x65,0x73,0x74,0x0a,0x00,0x00,0x00,0x00,};/*53*/ diff --git a/demos/assets/inc/data/app.data b/demos/assets/inc/data/app.data new file mode 100644 index 000000000..2e242ed95 --- /dev/null +++ b/demos/assets/inc/data/app.data @@ -0,0 +1,5 @@ +const unsigned char data_app[] = { +0x08,0x00,0x03,0x01,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x61,0x70,0x70,0x2e,0x6a,0x73,0x6f,0x6e, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x7b,0x0a,0x20,0x20,0x22,0x6e,0x61,0x6d,0x65,0x22,0x3a,0x22,0x66,0x6f,0x72,0x20, +0x74,0x65,0x73,0x74,0x22,0x0a,0x7d,0x0a,0x00,0x00,0x00,0x00,};/*72*/ diff --git a/demos/assets/inc/data/test.data b/demos/assets/inc/data/test.data index 41845fccb..41b77819a 100644 --- a/demos/assets/inc/data/test.data +++ b/demos/assets/inc/data/test.data @@ -1,4 +1,4 @@ const unsigned char data_test[] = { -0x08,0x00,0x00,0x01,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x74,0x65,0x73,0x74,0x00,0x64,0x61,0x74, -0x61,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x08,0x00,0x04,0x01,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x74,0x65,0x73,0x74,0x2e,0x64,0x61,0x74, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x61,0x62,0x63,0x64,0x0a,0x00,0x00,0x00,0x00,};/*53*/ diff --git a/demos/assets/raw/data/abc.any b/demos/assets/raw/data/abc.any new file mode 100644 index 000000000..9daeafb98 --- /dev/null +++ b/demos/assets/raw/data/abc.any @@ -0,0 +1 @@ +test diff --git a/demos/assets/raw/data/app.json b/demos/assets/raw/data/app.json new file mode 100644 index 000000000..9e0c6ba57 --- /dev/null +++ b/demos/assets/raw/data/app.json @@ -0,0 +1,3 @@ +{ + "name":"for test" +} diff --git a/docs/README.md b/docs/README.md index 2a405a966..57294c4c2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -33,6 +33,7 @@ * [如何设置字体的全局缩放比例](how_to_scale_font.md) * [如何实现"back"键和"home"键](how_to_impl_back_and_home_key.md) * [如何实现自定义的软键盘](how_to_impl_custom_soft_keyboard.md) +* [如何编译Windows 32位版本](how_to_build_windows_32bit_version.md) ### 3.内部原理 * [AWTK脚本绑定原理](script_binding.md) diff --git a/docs/assets_manager.md b/docs/assets_manager.md index 6cc61b429..ee4ab7ca5 100644 --- a/docs/assets_manager.md +++ b/docs/assets_manager.md @@ -112,6 +112,10 @@ widget_t* win = window_open(name); 资源名称一般就是资源的文件名,不带文件扩展名。比如图片名为test.png,那资源名称就是test,如果因为某种原因,把test.png换成了test.jpg,对代码并无影响。 +> 对于DATA类型的资源,由于其扩展名不固定,所以需要带扩展名才能访问。 +> +> 比如资源文件名为"app.json",不能用"app"访问,而是用"app.json"才能访问。 + 对于图片和UI资源名称,AWTK还支持一种高级用法。想想下面几种情况: * 需要支持不同的分辨率。而在不同分辨率里,要使用不同的背景图片。 @@ -169,4 +173,4 @@ widget_t* win = window_open(name); 依次查找flag\_US和flag\_none两个图片。 -> 变量名可以使用[system\_info中的成员变量](https://github.com/zlgopen/awtk/blob/master/docs/manual/system_info_t.md) \ No newline at end of file +> 变量名可以使用[system\_info中的成员变量](https://github.com/zlgopen/awtk/blob/master/docs/manual/system_info_t.md) diff --git a/docs/changes.md b/docs/changes.md index 99b4cd19f..01012b9d2 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,4 +1,8 @@ # 最新动态 +* 2019/03/20 + * 支持编译Windows 32 位版本(感谢陈谭提供补丁) + * 修改资源生成脚本,对于data数据,不限制文件名(DATA类型的资源需带扩展名才能访问)。 + * 2019/03/19 * 增加window\_close\_force函数。 * 修改window\_manager\_close\_window多次关闭同一个窗口的问题。 diff --git a/docs/how_to_build_windows_32bit_version.md b/docs/how_to_build_windows_32bit_version.md new file mode 100644 index 000000000..e956da67d --- /dev/null +++ b/docs/how_to_build_windows_32bit_version.md @@ -0,0 +1,29 @@ +# 如何编译Windows 32位版本 + +* 1.在文件awtk\_config.py中设置TARGET\_ARCH为'x86': + +将: + +``` +#TARGET_ARCH='x86' +``` + +修改为: + +``` +TARGET_ARCH='x86' +``` + +* 2.对于XP,如果OpenGL渲染不正常。可以使用AGGE(或BGFX)试试。 + +将: + +``` +#NANOVG_BACKEND='AGGE' +``` + +修改为: + +``` +NANOVG_BACKEND='AGGE' +``` diff --git a/docs/index.md b/docs/index.md index 2a405a966..57294c4c2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -33,6 +33,7 @@ * [如何设置字体的全局缩放比例](how_to_scale_font.md) * [如何实现"back"键和"home"键](how_to_impl_back_and_home_key.md) * [如何实现自定义的软键盘](how_to_impl_custom_soft_keyboard.md) +* [如何编译Windows 32位版本](how_to_build_windows_32bit_version.md) ### 3.内部原理 * [AWTK脚本绑定原理](script_binding.md) diff --git a/src/base/assets_manager.c b/src/base/assets_manager.c index 4277f2510..c2774d432 100644 --- a/src/base/assets_manager.c +++ b/src/base/assets_manager.c @@ -260,11 +260,7 @@ asset_info_t* assets_manager_load(assets_manager_t* rm, asset_type_t type, const break; } case ASSET_TYPE_DATA: { - if ((info = try_load_assets(res_root, name, ".bin", type, ASSET_TYPE_DATA)) != NULL) { - break; - } - - if ((info = try_load_assets(res_root, name, ".dat", type, ASSET_TYPE_DATA)) != NULL) { + if ((info = try_load_assets(res_root, name, "", type, ASSET_TYPE_DATA)) != NULL) { break; } break; diff --git a/src/base/assets_manager.h b/src/base/assets_manager.h index 4c162d3a8..95ca80ad8 100644 --- a/src/base/assets_manager.h +++ b/src/base/assets_manager.h @@ -98,6 +98,18 @@ typedef enum _asset_font_type_t { ASSET_TYPE_FONT_BMP } asset_font_type_t; +/** + * @enum asset_data_type_t + * 字体资源类型定义。 + */ +typedef enum _asset_data_type_t { + ASSET_TYPE_DATA_NONE, + ASSET_TYPE_DATA_TEXT, + ASSET_TYPE_DATA_BIN, + ASSET_TYPE_DATA_JSON, + ASSET_TYPE_DATA_DAT +} asset_data_type_t; + /** * @enum asset_script_type_t * 脚本资源类型定义。 @@ -120,7 +132,7 @@ typedef enum _asset_image_type_t { ASSET_TYPE_IMAGE_PNG, ASSET_TYPE_IMAGE_JPG, ASSET_TYPE_IMAGE_BSVG, - ASSET_TYPE_IMAGE_GIF /*not supported yet*/ + ASSET_TYPE_IMAGE_GIF } asset_image_type_t; /** diff --git a/src/tkc/utils.c b/src/tkc/utils.c index a14011e5a..edacbf370 100644 --- a/src/tkc/utils.c +++ b/src/tkc/utils.c @@ -458,7 +458,7 @@ int tk_sscanf(const char* str, const char* format, ...) { return ret; } -ret_t filename_to_name(const char* filename, char* str, uint32_t size) { +ret_t filename_to_name_ex(const char* filename, char* str, uint32_t size, bool_t remove_extname) { char* p = NULL; const char* name = filename; return_value_if_fail(filename != NULL && str != NULL, RET_BAD_PARAMS); @@ -475,14 +475,21 @@ ret_t filename_to_name(const char* filename, char* str, uint32_t size) { } tk_strncpy(str, name, size - 1); - p = strchr(str, '.'); - if (p != NULL) { - *p = '\0'; + + if (remove_extname) { + p = strchr(str, '.'); + if (p != NULL) { + *p = '\0'; + } } return RET_OK; } +ret_t filename_to_name(const char* filename, char* str, uint32_t size) { + return filename_to_name_ex(filename, str, size, TRUE); +} + #define INCLUDE_XML "*/ ret_t xml_file_expand_read(const char* filename, str_t* s); diff --git a/tests/SConscript b/tests/SConscript index 86b2714a6..1da1b9d31 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -21,6 +21,7 @@ INCLUDE_PATH = [TK_ROOT, env['CPPPATH'] = INCLUDE_PATH env['LIBS'] = ['assets', 'image_gen', 'theme_gen', 'font_gen', 'str_gen', 'common'] + env['LIBS'] +env['LINKFLAGS'] = env['OS_SUBSYSTEM_CONSOLE'] + env['LINKFLAGS']; SOURCES = [ os.path.join(GTEST_ROOT, 'src/gtest-all.cc'), diff --git a/tests/assets_manager_test.cc b/tests/assets_manager_test.cc index 85d48c716..424f58b04 100644 --- a/tests/assets_manager_test.cc +++ b/tests/assets_manager_test.cc @@ -73,20 +73,6 @@ TEST(AssetsManager, clearCache) { assets_manager_destroy(rm); } -TEST(AssetsManager, data) { - const asset_info_t* r = NULL; - assets_manager_t* rm = assets_manager(); - -#ifdef WITH_FS_RES - r = assets_manager_ref(rm, ASSET_TYPE_DATA, "test"); - ASSERT_EQ(r != NULL, true); - assets_manager_unref(rm, r); -#else - r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "test"); - ASSERT_EQ(r != NULL, true); -#endif /*WITH_FS_RES*/ -} - TEST(AssetsManager, xml) { const asset_info_t* r = NULL; assets_manager_t* rm = assets_manager(); @@ -99,3 +85,43 @@ TEST(AssetsManager, xml) { ASSERT_EQ(r != NULL, true); #endif /*WITH_FS_RES*/ } + +TEST(AssetsManager, data) { + const asset_info_t* r = NULL; + assets_manager_t* rm = assets_manager(); + +#ifdef WITH_FS_RES + r = assets_manager_ref(rm, ASSET_TYPE_DATA, "test.dat"); + ASSERT_EQ(r != NULL, true); + assets_manager_unref(rm, r); +#else + r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "test.dat"); + ASSERT_EQ(r != NULL, true); +#endif /*WITH_FS_RES*/ +} + +TEST(AssetsManager, json) { + const asset_info_t* r = NULL; + assets_manager_t* rm = assets_manager(); +#ifdef WITH_FS_RES + r = assets_manager_ref(rm, ASSET_TYPE_DATA, "app.json"); + ASSERT_EQ(r != NULL, true); + assets_manager_unref(rm, r); +#else + r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "app.json"); + ASSERT_EQ(r != NULL, true); +#endif /*WITH_FS_RES*/ +} + +TEST(AssetsManager, any) { + const asset_info_t* r = NULL; + assets_manager_t* rm = assets_manager(); +#ifdef WITH_FS_RES + r = assets_manager_ref(rm, ASSET_TYPE_DATA, "abc.any"); + ASSERT_EQ(r != NULL, true); + assets_manager_unref(rm, r); +#else + r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "abc.any"); + ASSERT_EQ(r != NULL, true); +#endif /*WITH_FS_RES*/ +} diff --git a/tests/utils_test.cc b/tests/utils_test.cc index 5c3e17eff..af7dc2bcc 100644 --- a/tests/utils_test.cc +++ b/tests/utils_test.cc @@ -174,6 +174,16 @@ TEST(Utils, filename_to_name) { ASSERT_EQ(string(name), string("test")); } +TEST(Utils, filename_to_name_ex) { + char name[TK_NAME_LEN + 1]; + + filename_to_name_ex("test.png", name, TK_NAME_LEN, FALSE); + ASSERT_EQ(string(name), string("test.png")); + + filename_to_name_ex("/a/test.png", name, TK_NAME_LEN, TRUE); + ASSERT_EQ(string(name), string("test")); +} + TEST(Utils, tk_skip_to_num) { ASSERT_EQ(tk_atoi(tk_skip_to_num("123")), 123); ASSERT_EQ(tk_atoi(tk_skip_to_num("abc123")), 123); diff --git a/tools/common/utils.c b/tools/common/utils.c index 92f78cb08..3957e348e 100644 --- a/tools/common/utils.c +++ b/tools/common/utils.c @@ -82,11 +82,24 @@ int unique(wchar_t* str, int size) { return d - str; } +static const char* to_var_name(char var_name[2 * TK_NAME_LEN + 1], const char* prefix, + const char* name) { + tk_snprintf(var_name, 2 * TK_NAME_LEN, "%s_%s", prefix ? prefix : "", name); + + char* p = strrchr(var_name, '.'); + if (p != NULL) { + *p = '\0'; + } + + return var_name; +} + ret_t output_c_source(const char* filename, const char* prefix, const char* name, uint8_t* buff, uint32_t size) { uint32_t i = 0; FILE* fp = NULL; char str[TK_NAME_LEN + 1]; + char var_name[2 * TK_NAME_LEN + 1]; return_value_if_fail(filename != NULL && buff != NULL, RET_BAD_PARAMS); if (name == NULL) { @@ -94,9 +107,11 @@ ret_t output_c_source(const char* filename, const char* prefix, const char* name name = str; } + log_debug("filename=%s prefix=%s name=%s size=%u\n", filename, prefix, name, size); + fp = fopen(filename, "wb+"); if (fp != NULL) { - fprintf(fp, "const unsigned char %s_%s[] = {", prefix ? prefix : "", name); + fprintf(fp, "const unsigned char %s[] = {", to_var_name(var_name, prefix, name)); for (i = 0; i < size; i++) { if ((i % 20) == 0) { fprintf(fp, "\n"); @@ -113,8 +128,8 @@ ret_t output_c_source(const char* filename, const char* prefix, const char* name return RET_FAIL; } -ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff, - uint32_t size) { +ret_t output_res_c_source_ex(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff, + uint32_t size, const char* name) { asset_info_t* res = NULL; uint32_t total_size = sizeof(asset_info_t) + size; const key_type_value_t* kv = asset_type_find_by_value(type); @@ -129,13 +144,26 @@ ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype, res->refcount = 0; res->subtype = subtype; memcpy(res->data, buff, size); - filename_to_name(filename, res->name, sizeof(res->name)); + if (name != NULL) { + tk_strncpy(res->name, name, sizeof(res->name) - 1); + } else { + if (type == ASSET_TYPE_DATA) { + filename_to_name_ex(filename, res->name, sizeof(res->name), FALSE); + } else { + filename_to_name(filename, res->name, sizeof(res->name)); + } + } output_c_source(filename, kv->name, res->name, (uint8_t*)res, total_size); free(res); return RET_OK; } +ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff, + uint32_t size) { + return output_res_c_source_ex(filename, type, subtype, buff, size, NULL); +} + const char* skip_to(const char* p, char c) { while (*p && *p != c) p++; diff --git a/tools/common/utils.h b/tools/common/utils.h index 1f9f2c667..ba4b4281c 100644 --- a/tools/common/utils.h +++ b/tools/common/utils.h @@ -37,6 +37,8 @@ ret_t output_c_source(const char* filename, const char* prefix, const char* name ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff, uint32_t size); +ret_t output_res_c_source_ex(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff, + uint32_t size, const char* name); const char* to_lower(char* str); const char* skip_to(const char* p, char c); diff --git a/tools/res_gen/main.c b/tools/res_gen/main.c index 123cf7b5b..be262f221 100644 --- a/tools/res_gen/main.c +++ b/tools/res_gen/main.c @@ -61,7 +61,30 @@ int main(int argc, char** argv) { } else if (end_with(in_filename, ".xml")) { output_res_c_source(out_filename, ASSET_TYPE_XML, 0, input_buff, size); } else { - output_res_c_source(out_filename, ASSET_TYPE_DATA, 0, input_buff, size); + const char* name = strrchr(in_filename, '/'); + if (name == NULL) { + name = strrchr(in_filename, '\\'); + } + if (name != NULL) { + name++; + } + + if (end_with(in_filename, ".txt")) { + output_res_c_source_ex(out_filename, ASSET_TYPE_DATA, ASSET_TYPE_DATA_TEXT, input_buff, size, + name); + } else if (end_with(in_filename, ".json")) { + output_res_c_source_ex(out_filename, ASSET_TYPE_DATA, ASSET_TYPE_DATA_JSON, input_buff, size, + name); + } else if (end_with(in_filename, ".bin")) { + output_res_c_source_ex(out_filename, ASSET_TYPE_DATA, ASSET_TYPE_DATA_BIN, input_buff, size, + name); + } else if (end_with(in_filename, ".dat")) { + output_res_c_source_ex(out_filename, ASSET_TYPE_DATA, ASSET_TYPE_DATA_DAT, input_buff, size, + name); + } else { + output_res_c_source_ex(out_filename, ASSET_TYPE_DATA, ASSET_TYPE_DATA_NONE, input_buff, size, + name); + } } TKMEM_FREE(input_buff);