xmake-cli.patch 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
  2. index 8753607d4..500bca5c0 100644
  3. --- a/core/src/xmake/engine.c
  4. +++ b/core/src/xmake/engine.c
  5. @@ -98,6 +98,11 @@ typedef struct __xm_engine_t
  6. #ifdef XM_EMBED_ENABLE
  7. // the temporary directory
  8. tb_char_t tmpdir[TB_PATH_MAXN];
  9. +
  10. + // the embed files
  11. + tb_byte_t const* embeddata[32];
  12. + tb_size_t embedsize[32];
  13. + tb_size_t embedcount;
  14. #endif
  15. }xm_engine_t;
  16. @@ -812,8 +817,15 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t*
  17. do
  18. {
  19. #ifdef XM_EMBED_ENABLE
  20. - // get it from the temporary directory
  21. - tb_strlcpy(path, engine->tmpdir, maxn);
  22. + tb_size_t embedcount = engine->embedcount;
  23. + if (embedcount)
  24. + {
  25. + tb_uint32_t crc32 = 0;
  26. + for (tb_size_t i = 0; i < embedcount; i++)
  27. + crc32 += tb_crc32_make(engine->embeddata[i], engine->embedsize[i], 0);
  28. + tb_snprintf(path, maxn, "%s/%x", engine->tmpdir, crc32);
  29. + }
  30. + else tb_strlcpy(path, engine->tmpdir, maxn);
  31. ok = tb_true;
  32. break;
  33. #endif
  34. @@ -1189,14 +1201,8 @@ static tb_pointer_t xm_engine_lua_realloc(tb_pointer_t udata, tb_pointer_t data,
  35. #endif
  36. #ifdef XM_EMBED_ENABLE
  37. -static tb_bool_t xm_engine_extract_programfiles(xm_engine_t* engine, tb_char_t const* programdir)
  38. +static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t* engine, tb_char_t const* programdir, tb_byte_t const* data, tb_size_t size)
  39. {
  40. - tb_file_info_t info = {0};
  41. - if (tb_file_info(programdir, &info)) return tb_true;
  42. -
  43. - tb_byte_t const* data = g_xmake_xmz_data;
  44. - tb_size_t size = sizeof(g_xmake_xmz_data);
  45. -
  46. // do decompress
  47. tb_bool_t ok = tb_false;
  48. LZ4F_errorCode_t code;
  49. @@ -1289,6 +1295,28 @@ static tb_bool_t xm_engine_extract_programfiles(xm_engine_t* engine, tb_char_t c
  50. tb_buffer_exit(&result);
  51. return ok;
  52. }
  53. +
  54. +static tb_bool_t xm_engine_extract_programfiles(xm_engine_t* engine, tb_char_t const* programdir)
  55. +{
  56. + tb_file_info_t info = {0};
  57. + if (!tb_file_info(programdir, &info))
  58. + {
  59. + tb_byte_t const* data = g_xmake_xmz_data;
  60. + tb_size_t size = sizeof(g_xmake_xmz_data);
  61. + if (!xm_engine_extract_programfiles_impl(engine, programdir, data, size))
  62. + return tb_false;
  63. +
  64. + tb_size_t embedcount = engine->embedcount;
  65. + for (tb_size_t i = 0; i < embedcount; i++)
  66. + {
  67. + data = engine->embeddata[i];
  68. + size = engine->embedsize[i];
  69. + if (!xm_engine_extract_programfiles_impl(engine, programdir, data, size))
  70. + return tb_false;
  71. + }
  72. + }
  73. + return tb_true;
  74. +}
  75. #endif
  76. /* //////////////////////////////////////////////////////////////////////////////////////
  77. @@ -1560,6 +1588,18 @@ tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const* module, luaL
  78. xm_lua_register(engine->lua, tb_null, funcs);
  79. lua_rawset(engine->lua, -3);
  80. }
  81. +#ifdef XM_EMBED_ENABLE
  82. +tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t self, tb_byte_t const* data, tb_size_t size)
  83. +{
  84. + // check
  85. + xm_engine_t* engine = (xm_engine_t*)self;
  86. + tb_assert_and_check_return(engine && engine->embedcount < tb_arrayn(engine->embedsize) && data && size);
  87. +
  88. + engine->embeddata[engine->embedcount] = data;
  89. + engine->embedsize[engine->embedcount] = size;
  90. + engine->embedcount++;
  91. +}
  92. +#endif
  93. tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv, xm_engine_lni_initalizer_cb_t lni_initalizer)
  94. {
  95. tb_int_t ok = -1;
  96. @@ -1575,3 +1615,4 @@ tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, t
  97. }
  98. return ok;
  99. }
  100. +
  101. diff --git a/core/src/xmake/engine.h b/core/src/xmake/engine.h
  102. index cc5a533ca..901e3997e 100644
  103. --- a/core/src/xmake/engine.h
  104. +++ b/core/src/xmake/engine.h
  105. @@ -79,6 +79,14 @@ tb_int_t xm_engine_main(xm_engine_ref_t engine, tb_int_t argc
  106. */
  107. tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t const* module, luaL_Reg const funcs[]);
  108. +/*! add the embed files
  109. + *
  110. + * @param name the engine name
  111. + * @param data the embedfiles data
  112. + * @param size the data size
  113. + */
  114. +tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t engine, tb_byte_t const* data, tb_size_t size);
  115. +
  116. /*! run main entry of the engine singleton
  117. *
  118. * @param name the engine name
  119. diff --git a/core/src/xmake/prefix.h b/core/src/xmake/prefix.h
  120. index 5d6fba582..3c5c0679f 100644
  121. --- a/core/src/xmake/prefix.h
  122. +++ b/core/src/xmake/prefix.h
  123. @@ -117,14 +117,21 @@ static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx)
  124. static __tb_inline__ tb_void_t xm_lua_register(lua_State *lua, tb_char_t const* libname, luaL_Reg const* l)
  125. {
  126. #if LUA_VERSION_NUM >= 504
  127. - lua_getglobal(lua, libname);
  128. - if (lua_isnil(lua, -1))
  129. + if (libname)
  130. {
  131. - lua_pop(lua, 1);
  132. - lua_newtable(lua);
  133. + lua_getglobal(lua, libname);
  134. + if (lua_isnil(lua, -1))
  135. + {
  136. + lua_pop(lua, 1);
  137. + lua_newtable(lua);
  138. + }
  139. + luaL_setfuncs(lua, l, 0);
  140. + lua_setglobal(lua, libname);
  141. + }
  142. + else
  143. + {
  144. + luaL_setfuncs(lua, l, 0);
  145. }
  146. - luaL_setfuncs(lua, l, 0);
  147. - lua_setglobal(lua, libname);
  148. #else
  149. luaL_register(lua, libname, l);
  150. #endif