improve assets build script to support any file extensions in data folder.

This commit is contained in:
xianjimli 2019-03-20 11:16:26 +08:00
parent 369c68dfb7
commit db616f27fe
21 changed files with 194 additions and 32 deletions

View File

@ -118,6 +118,8 @@
#include "assets/inc/ui/scroll_view_v.data" #include "assets/inc/ui/scroll_view_v.data"
#include "assets/inc/xml/test.data" #include "assets/inc/xml/test.data"
#include "assets/inc/data/test.data" #include "assets/inc/data/test.data"
#include "assets/inc/data/app.data"
#include "assets/inc/data/abc.data"
#ifdef WITH_STB_IMAGE #ifdef WITH_STB_IMAGE
#include "assets/inc/images/ani6.res" #include "assets/inc/images/ani6.res"
#include "assets/inc/images/checked.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, style_dialog_warn);
assets_manager_add(rm, font_default); assets_manager_add(rm, font_default);
assets_manager_add(rm, data_test); assets_manager_add(rm, data_test);
assets_manager_add(rm, data_app);
assets_manager_add(rm, data_abc);
#ifdef WITH_VGCANVAS #ifdef WITH_VGCANVAS
assets_manager_add(rm, image_pointer_4); assets_manager_add(rm, image_pointer_4);
assets_manager_add(rm, image_china); assets_manager_add(rm, image_china);

View File

@ -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*/

View File

@ -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*/

View File

@ -1,4 +1,4 @@
const unsigned char data_test[] = { 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, 0x08,0x00,0x04,0x01,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x74,0x65,0x73,0x74,0x2e,0x64,0x61,0x74,
0x61,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,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*/ 0x00,0x00,0x00,0x00,0x61,0x62,0x63,0x64,0x0a,0x00,0x00,0x00,0x00,};/*53*/

View File

@ -0,0 +1 @@
test

View File

@ -0,0 +1,3 @@
{
"name":"for test"
}

View File

@ -33,6 +33,7 @@
* [如何设置字体的全局缩放比例](how_to_scale_font.md) * [如何设置字体的全局缩放比例](how_to_scale_font.md)
* [如何实现"back"键和"home"键](how_to_impl_back_and_home_key.md) * [如何实现"back"键和"home"键](how_to_impl_back_and_home_key.md)
* [如何实现自定义的软键盘](how_to_impl_custom_soft_keyboard.md) * [如何实现自定义的软键盘](how_to_impl_custom_soft_keyboard.md)
* [如何编译Windows 32位版本](how_to_build_windows_32bit_version.md)
### 3.内部原理 ### 3.内部原理
* [AWTK脚本绑定原理](script_binding.md) * [AWTK脚本绑定原理](script_binding.md)

View File

@ -112,6 +112,10 @@ widget_t* win = window_open(name);
资源名称一般就是资源的文件名不带文件扩展名。比如图片名为test.png那资源名称就是test如果因为某种原因把test.png换成了test.jpg对代码并无影响。 资源名称一般就是资源的文件名不带文件扩展名。比如图片名为test.png那资源名称就是test如果因为某种原因把test.png换成了test.jpg对代码并无影响。
> 对于DATA类型的资源由于其扩展名不固定所以需要带扩展名才能访问。
>
> 比如资源文件名为"app.json",不能用"app"访问,而是用"app.json"才能访问。
对于图片和UI资源名称AWTK还支持一种高级用法。想想下面几种情况 对于图片和UI资源名称AWTK还支持一种高级用法。想想下面几种情况
* 需要支持不同的分辨率。而在不同分辨率里,要使用不同的背景图片。 * 需要支持不同的分辨率。而在不同分辨率里,要使用不同的背景图片。
@ -169,4 +173,4 @@ widget_t* win = window_open(name);
依次查找flag\_US和flag\_none两个图片。 依次查找flag\_US和flag\_none两个图片。
> 变量名可以使用[system\_info中的成员变量](https://github.com/zlgopen/awtk/blob/master/docs/manual/system_info_t.md) > 变量名可以使用[system\_info中的成员变量](https://github.com/zlgopen/awtk/blob/master/docs/manual/system_info_t.md)

View File

@ -1,4 +1,8 @@
# 最新动态 # 最新动态
* 2019/03/20
* 支持编译Windows 32 位版本(感谢陈谭提供补丁)
* 修改资源生成脚本对于data数据不限制文件名(DATA类型的资源需带扩展名才能访问)。
* 2019/03/19 * 2019/03/19
* 增加window\_close\_force函数。 * 增加window\_close\_force函数。
* 修改window\_manager\_close\_window多次关闭同一个窗口的问题。 * 修改window\_manager\_close\_window多次关闭同一个窗口的问题。

View File

@ -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'
```

View File

@ -33,6 +33,7 @@
* [如何设置字体的全局缩放比例](how_to_scale_font.md) * [如何设置字体的全局缩放比例](how_to_scale_font.md)
* [如何实现"back"键和"home"键](how_to_impl_back_and_home_key.md) * [如何实现"back"键和"home"键](how_to_impl_back_and_home_key.md)
* [如何实现自定义的软键盘](how_to_impl_custom_soft_keyboard.md) * [如何实现自定义的软键盘](how_to_impl_custom_soft_keyboard.md)
* [如何编译Windows 32位版本](how_to_build_windows_32bit_version.md)
### 3.内部原理 ### 3.内部原理
* [AWTK脚本绑定原理](script_binding.md) * [AWTK脚本绑定原理](script_binding.md)

View File

@ -260,11 +260,7 @@ asset_info_t* assets_manager_load(assets_manager_t* rm, asset_type_t type, const
break; break;
} }
case ASSET_TYPE_DATA: { case ASSET_TYPE_DATA: {
if ((info = try_load_assets(res_root, name, ".bin", type, ASSET_TYPE_DATA)) != NULL) { if ((info = try_load_assets(res_root, name, "", type, ASSET_TYPE_DATA)) != NULL) {
break;
}
if ((info = try_load_assets(res_root, name, ".dat", type, ASSET_TYPE_DATA)) != NULL) {
break; break;
} }
break; break;

View File

@ -98,6 +98,18 @@ typedef enum _asset_font_type_t {
ASSET_TYPE_FONT_BMP ASSET_TYPE_FONT_BMP
} asset_font_type_t; } 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 * @enum asset_script_type_t
* *
@ -120,7 +132,7 @@ typedef enum _asset_image_type_t {
ASSET_TYPE_IMAGE_PNG, ASSET_TYPE_IMAGE_PNG,
ASSET_TYPE_IMAGE_JPG, ASSET_TYPE_IMAGE_JPG,
ASSET_TYPE_IMAGE_BSVG, ASSET_TYPE_IMAGE_BSVG,
ASSET_TYPE_IMAGE_GIF /*not supported yet*/ ASSET_TYPE_IMAGE_GIF
} asset_image_type_t; } asset_image_type_t;
/** /**

View File

@ -458,7 +458,7 @@ int tk_sscanf(const char* str, const char* format, ...) {
return ret; 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; char* p = NULL;
const char* name = filename; const char* name = filename;
return_value_if_fail(filename != NULL && str != NULL, RET_BAD_PARAMS); 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); tk_strncpy(str, name, size - 1);
p = strchr(str, '.');
if (p != NULL) { if (remove_extname) {
*p = '\0'; p = strchr(str, '.');
if (p != NULL) {
*p = '\0';
}
} }
return RET_OK; 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 "<?include" #define INCLUDE_XML "<?include"
ret_t xml_file_expand(const char* filename, str_t* s, const char* data) { ret_t xml_file_expand(const char* filename, str_t* s, const char* data) {

View File

@ -59,6 +59,7 @@ void* tk_pixel_copy(void* dst, const void* src, uint32_t size, uint8_t bpp);
int tk_snprintf(char* str, size_t size, const char* format, ...); int tk_snprintf(char* str, size_t size, const char* format, ...);
int tk_sscanf(const char* str, const char* format, ...); int tk_sscanf(const char* str, const char* format, ...);
ret_t filename_to_name(const char* filename, char* str, uint32_t size); 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);
/*expand include process instruction to file content: <?include filename="view_me.inc" ?>*/ /*expand include process instruction to file content: <?include filename="view_me.inc" ?>*/
ret_t xml_file_expand_read(const char* filename, str_t* s); ret_t xml_file_expand_read(const char* filename, str_t* s);

View File

@ -21,6 +21,7 @@ INCLUDE_PATH = [TK_ROOT,
env['CPPPATH'] = INCLUDE_PATH env['CPPPATH'] = INCLUDE_PATH
env['LIBS'] = ['assets', 'image_gen', 'theme_gen', 'font_gen', 'str_gen', 'common'] + env['LIBS'] env['LIBS'] = ['assets', 'image_gen', 'theme_gen', 'font_gen', 'str_gen', 'common'] + env['LIBS']
env['LINKFLAGS'] = env['OS_SUBSYSTEM_CONSOLE'] + env['LINKFLAGS'];
SOURCES = [ SOURCES = [
os.path.join(GTEST_ROOT, 'src/gtest-all.cc'), os.path.join(GTEST_ROOT, 'src/gtest-all.cc'),

View File

@ -73,20 +73,6 @@ TEST(AssetsManager, clearCache) {
assets_manager_destroy(rm); 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) { TEST(AssetsManager, xml) {
const asset_info_t* r = NULL; const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager(); assets_manager_t* rm = assets_manager();
@ -99,3 +85,43 @@ TEST(AssetsManager, xml) {
ASSERT_EQ(r != NULL, true); ASSERT_EQ(r != NULL, true);
#endif /*WITH_FS_RES*/ #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*/
}

View File

@ -174,6 +174,16 @@ TEST(Utils, filename_to_name) {
ASSERT_EQ(string(name), string("test")); 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) { TEST(Utils, tk_skip_to_num) {
ASSERT_EQ(tk_atoi(tk_skip_to_num("123")), 123); ASSERT_EQ(tk_atoi(tk_skip_to_num("123")), 123);
ASSERT_EQ(tk_atoi(tk_skip_to_num("abc123")), 123); ASSERT_EQ(tk_atoi(tk_skip_to_num("abc123")), 123);

View File

@ -82,11 +82,24 @@ int unique(wchar_t* str, int size) {
return d - str; 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, ret_t output_c_source(const char* filename, const char* prefix, const char* name, uint8_t* buff,
uint32_t size) { uint32_t size) {
uint32_t i = 0; uint32_t i = 0;
FILE* fp = NULL; FILE* fp = NULL;
char str[TK_NAME_LEN + 1]; 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); return_value_if_fail(filename != NULL && buff != NULL, RET_BAD_PARAMS);
if (name == NULL) { if (name == NULL) {
@ -94,9 +107,11 @@ ret_t output_c_source(const char* filename, const char* prefix, const char* name
name = str; name = str;
} }
log_debug("filename=%s prefix=%s name=%s size=%u\n", filename, prefix, name, size);
fp = fopen(filename, "wb+"); fp = fopen(filename, "wb+");
if (fp != NULL) { 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++) { for (i = 0; i < size; i++) {
if ((i % 20) == 0) { if ((i % 20) == 0) {
fprintf(fp, "\n"); fprintf(fp, "\n");
@ -113,8 +128,8 @@ ret_t output_c_source(const char* filename, const char* prefix, const char* name
return RET_FAIL; return RET_FAIL;
} }
ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff, ret_t output_res_c_source_ex(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff,
uint32_t size) { uint32_t size, const char* name) {
asset_info_t* res = NULL; asset_info_t* res = NULL;
uint32_t total_size = sizeof(asset_info_t) + size; uint32_t total_size = sizeof(asset_info_t) + size;
const key_type_value_t* kv = asset_type_find_by_value(type); 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->refcount = 0;
res->subtype = subtype; res->subtype = subtype;
memcpy(res->data, buff, size); 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); output_c_source(filename, kv->name, res->name, (uint8_t*)res, total_size);
free(res); free(res);
return RET_OK; 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) { const char* skip_to(const char* p, char c) {
while (*p && *p != c) p++; while (*p && *p != c) p++;

View File

@ -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, ret_t output_res_c_source(const char* filename, uint16_t type, uint16_t subtype, uint8_t* buff,
uint32_t size); 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* to_lower(char* str);
const char* skip_to(const char* p, char c); const char* skip_to(const char* p, char c);

View File

@ -61,7 +61,30 @@ int main(int argc, char** argv) {
} else if (end_with(in_filename, ".xml")) { } else if (end_with(in_filename, ".xml")) {
output_res_c_source(out_filename, ASSET_TYPE_XML, 0, input_buff, size); output_res_c_source(out_filename, ASSET_TYPE_XML, 0, input_buff, size);
} else { } 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); TKMEM_FREE(input_buff);