plugins.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "../../../base/sources/plugins/plugin_api.h"
  2. #include "iron_array.h"
  3. #include "iron_map.h"
  4. #include "iron_ui.h"
  5. #include "iron_ui_nodes.h"
  6. void proc_uv_unwrap(void *mesh);
  7. FN(proc_uv_unwrap) {
  8. uint64_t mesh;
  9. JS_ToBigUint64(ctx, &mesh, argv[0]);
  10. proc_uv_unwrap((void *)mesh);
  11. return JS_UNDEFINED;
  12. }
  13. void plugin_uv_unwrap_button();
  14. FN(plugin_uv_unwrap_button) {
  15. plugin_uv_unwrap_button();
  16. return JS_UNDEFINED;
  17. }
  18. void *io_svg_parse(char *buf);
  19. FN(io_svg_parse) {
  20. size_t len;
  21. void *ab = JS_GetArrayBuffer(ctx, &len, argv[0]);
  22. return JS_NewBigUint64(ctx, (uint64_t)io_svg_parse(ab));
  23. }
  24. void *io_exr_parse(char *buf, size_t len);
  25. FN(io_exr_parse) {
  26. size_t len;
  27. void *ab = JS_GetArrayBuffer(ctx, &len, argv[0]);
  28. return JS_NewBigUint64(ctx, (uint64_t)io_exr_parse(ab, len));
  29. }
  30. void *io_gltf_parse(char *buf, size_t size, const char *path);
  31. FN(io_gltf_parse) {
  32. size_t len;
  33. void *ab = JS_GetArrayBuffer(ctx, &len, argv[0]);
  34. const char *path = JS_ToCString(ctx, argv[1]);
  35. return JS_NewBigUint64(ctx, (uint64_t)io_gltf_parse(ab, len, path));
  36. }
  37. void *io_fbx_parse(char *buf, size_t size);
  38. FN(io_fbx_parse) {
  39. size_t len;
  40. void *ab = JS_GetArrayBuffer(ctx, &len, argv[0]);
  41. return JS_NewBigUint64(ctx, (uint64_t)io_fbx_parse(ab, len));
  42. }
  43. VOID_FN_STR(console_log)
  44. VOID_FN_STR(console_info)
  45. PTR_FN(plugin_create)
  46. VOID_FN_PTR_CB(plugin_notify_on_ui)
  47. VOID_FN_PTR_CB(plugin_notify_on_update)
  48. VOID_FN_PTR_CB(plugin_notify_on_delete)
  49. static JSObject *ui_files_cb;
  50. static void ui_files_done(char *path) {
  51. JSValue path_val = JS_NewString(js_ctx, path);
  52. JSValue argv[] = {path_val};
  53. js_call_arg(ui_files_cb, 1, argv);
  54. }
  55. void ui_files_show(char *s, bool b0, bool b1, void (*f)(char *));
  56. FN(ui_files_show) {
  57. char *filters = (char *)JS_ToCString(ctx, argv[0]);
  58. bool is_save = JS_ToBool(ctx, argv[1]);
  59. bool open_multiple = JS_ToBool(ctx, argv[2]);
  60. ui_files_cb = malloc(sizeof(JSValue));
  61. JSValue dup = JS_DupValue(ctx, argv[3]);
  62. memcpy(ui_files_cb, &dup, sizeof(JSValue));
  63. ui_files_show(filters, is_save, open_multiple, ui_files_done);
  64. return JS_UNDEFINED;
  65. }
  66. void ui_box_show_message(char *s0, char *s1, bool b);
  67. extern bool ui_box_click_to_hide;
  68. FN(ui_box_show_message) {
  69. char *title = (char *)JS_ToCString(ctx, argv[0]);
  70. char *message = (char *)JS_ToCString(ctx, argv[1]);
  71. ui_box_show_message(title, message, true);
  72. ui_box_click_to_hide = false;
  73. return JS_UNDEFINED;
  74. }
  75. VOID_FN_CB(context_set_viewport_shader)
  76. void node_shader_add_constant(void *p, char *s0, char *s1, bool b);
  77. FN(node_shader_add_constant) {
  78. uint64_t p;
  79. JS_ToBigUint64(ctx, &p, argv[0]);
  80. char *s0 = (char *)JS_ToCString(ctx, argv[1]);
  81. char *s1 = (char *)JS_ToCString(ctx, argv[2]);
  82. node_shader_add_constant((void *)p, s0, s1, false);
  83. return JS_UNDEFINED;
  84. }
  85. VOID_FN_PTR_STR(node_shader_write_frag)
  86. extern char *project_filepath;
  87. FN(project_filepath_get) {
  88. return JS_NewString(ctx, project_filepath);
  89. }
  90. void project_save(bool b);
  91. FN(project_save) {
  92. project_save(false);
  93. return JS_UNDEFINED;
  94. }
  95. extern any_array_t *nodes_material_categories;
  96. extern any_array_t *nodes_material_list;
  97. FN(nodes_material_category_add) {
  98. char *category_name = (char *)JS_ToCString(ctx, argv[0]);
  99. any_array_push(nodes_material_categories, category_name);
  100. size_t len;
  101. void *ab = JS_GetArrayBuffer(ctx, &len, argv[1]);
  102. buffer_t b = {.buffer = ab, .length = len, .capacity = len};
  103. any_array_push(nodes_material_list, armpack_decode(&b));
  104. return JS_UNDEFINED;
  105. }
  106. FN(nodes_material_category_remove) {
  107. char *category_name = (char *)JS_ToCString(ctx, argv[0]);
  108. int i = array_index_of(nodes_material_categories, category_name);
  109. array_splice(nodes_material_list, i, 1);
  110. array_splice(nodes_material_categories, i, 1);
  111. return JS_UNDEFINED;
  112. }
  113. extern any_map_t *parser_material_custom_nodes;
  114. FN(parser_material_custom_nodes_set) {
  115. char *node_type = (char *)JS_ToCString(ctx, argv[0]);
  116. JSValue *p = malloc(sizeof(JSValue));
  117. JSValue dup = JS_DupValue(ctx, argv[1]);
  118. memcpy(p, &dup, sizeof(JSValue));
  119. any_map_set(parser_material_custom_nodes, node_type, p);
  120. return JS_UNDEFINED;
  121. }
  122. FN(parser_material_custom_nodes_delete) {
  123. char *node_type = (char *)JS_ToCString(ctx, argv[0]);
  124. map_delete(parser_material_custom_nodes, node_type);
  125. return JS_UNDEFINED;
  126. }
  127. extern void *parser_material_kong;
  128. FN(parser_material_kong_get) {
  129. return JS_NewBigUint64(ctx, (uint64_t)parser_material_kong);
  130. }
  131. char *parser_material_parse_value_input(void *inp, bool vector_as_grayscale);
  132. FN(parser_material_parse_value_input) {
  133. uint64_t *node;
  134. JS_ToBigUint64(ctx, &node, argv[0]);
  135. int64_t i;
  136. JS_ToInt64(ctx, &i, argv[1]);
  137. char *s = parser_material_parse_value_input(((ui_node_t *)node)->inputs->buffer[i], false);
  138. return JS_NewString(ctx, s);
  139. }
  140. extern any_array_t *nodes_brush_categories;
  141. extern any_array_t *nodes_brush_list;
  142. void nodes_brush_list_init();
  143. FN(nodes_brush_category_add) {
  144. char *category_name = (char *)JS_ToCString(ctx, argv[0]);
  145. any_array_push(nodes_brush_categories, category_name);
  146. size_t len;
  147. void *ab = JS_GetArrayBuffer(ctx, &len, argv[1]);
  148. buffer_t b = {.buffer = ab, .length = len, .capacity = len};
  149. nodes_brush_list_init();
  150. any_array_push(nodes_brush_list, armpack_decode(&b));
  151. return JS_UNDEFINED;
  152. }
  153. FN(nodes_brush_category_remove) {
  154. char *category_name = (char *)JS_ToCString(ctx, argv[0]);
  155. int i = array_index_of(nodes_brush_categories, category_name);
  156. array_splice(nodes_brush_list, i, 1);
  157. array_splice(nodes_brush_categories, i, 1);
  158. return JS_UNDEFINED;
  159. }
  160. extern any_map_t *parser_logic_custom_nodes;
  161. FN(parser_logic_custom_nodes_set) {
  162. char *node_type = (char *)JS_ToCString(ctx, argv[0]);
  163. JSValue *p = malloc(sizeof(JSValue));
  164. JSValue dup = JS_DupValue(ctx, argv[1]);
  165. memcpy(p, &dup, sizeof(JSValue));
  166. any_map_set(parser_logic_custom_nodes, node_type, p);
  167. return JS_UNDEFINED;
  168. }
  169. FN(parser_logic_custom_nodes_delete) {
  170. char *node_type = (char *)JS_ToCString(ctx, argv[0]);
  171. map_delete(parser_logic_custom_nodes, node_type);
  172. return JS_UNDEFINED;
  173. }
  174. extern any_map_t *util_mesh_unwrappers;
  175. FN(util_mesh_unwrappers_set) {
  176. char *plugin_name = (char *)JS_ToCString(ctx, argv[0]);
  177. JSValue *p = malloc(sizeof(JSValue));
  178. JSValue dup = JS_DupValue(ctx, argv[1]);
  179. memcpy(p, &dup, sizeof(JSValue));
  180. any_map_set(util_mesh_unwrappers, plugin_name, p);
  181. return JS_UNDEFINED;
  182. }
  183. FN(util_mesh_unwrappers_delete) {
  184. char *plugin_name = (char *)JS_ToCString(ctx, argv[0]);
  185. map_delete(util_mesh_unwrappers, plugin_name);
  186. return JS_UNDEFINED;
  187. }
  188. extern any_map_t *path_mesh_importers;
  189. extern char_ptr_array_t *path_mesh_formats;
  190. FN(path_mesh_importers_set) {
  191. char *format_name = (char *)JS_ToCString(ctx, argv[0]);
  192. JSValue *p = malloc(sizeof(JSValue));
  193. JSValue dup = JS_DupValue(ctx, argv[1]);
  194. memcpy(p, &dup, sizeof(JSValue));
  195. any_map_set(path_mesh_importers, format_name, p);
  196. any_array_push(path_mesh_formats, format_name);
  197. return JS_UNDEFINED;
  198. }
  199. FN(path_mesh_importers_delete) {
  200. char *format_name = (char *)JS_ToCString(ctx, argv[0]);
  201. map_delete(path_mesh_importers, format_name);
  202. array_splice(path_mesh_formats, char_ptr_array_index_of(path_mesh_formats, format_name), 1);
  203. return JS_UNDEFINED;
  204. }
  205. extern any_map_t *path_texture_importers;
  206. extern char_ptr_array_t *path_texture_formats;
  207. FN(path_texture_importers_set) {
  208. char *format_name = (char *)JS_ToCString(ctx, argv[0]);
  209. JSValue *p = malloc(sizeof(JSValue));
  210. JSValue dup = JS_DupValue(ctx, argv[1]);
  211. memcpy(p, &dup, sizeof(JSValue));
  212. any_map_set(path_texture_importers, format_name, p);
  213. any_array_push(path_texture_formats, format_name);
  214. return JS_UNDEFINED;
  215. }
  216. FN(path_texture_importers_delete) {
  217. char *format_name = (char *)JS_ToCString(ctx, argv[0]);
  218. map_delete(path_texture_importers, format_name);
  219. array_splice(path_texture_formats, char_ptr_array_index_of(path_texture_formats, format_name), 1);
  220. return JS_UNDEFINED;
  221. }
  222. void plugin_embed() {
  223. JSValue global_obj = JS_GetGlobalObject(js_ctx);
  224. BIND(proc_uv_unwrap, 1);
  225. BIND(plugin_uv_unwrap_button, 0);
  226. BIND(io_svg_parse, 1);
  227. BIND(io_exr_parse, 1);
  228. BIND(io_gltf_parse, 2);
  229. BIND(io_fbx_parse, 1);
  230. BIND(console_log, 1);
  231. BIND(console_info, 1);
  232. BIND(plugin_create, 0);
  233. BIND(plugin_notify_on_ui, 2);
  234. BIND(plugin_notify_on_update, 2);
  235. BIND(plugin_notify_on_delete, 2);
  236. BIND(ui_files_show, 4);
  237. BIND(ui_box_show_message, 2);
  238. BIND(context_set_viewport_shader, 1);
  239. BIND(node_shader_add_constant, 3);
  240. BIND(node_shader_write_frag, 2);
  241. BIND(project_filepath_get, 0);
  242. BIND(project_save, 0);
  243. BIND(nodes_material_category_add, 2);
  244. BIND(nodes_material_category_remove, 1);
  245. BIND(parser_material_custom_nodes_set, 2);
  246. BIND(parser_material_custom_nodes_delete, 1);
  247. BIND(parser_material_kong_get, 0);
  248. BIND(parser_material_parse_value_input, 2);
  249. BIND(nodes_brush_category_add, 2);
  250. BIND(nodes_brush_category_remove, 1);
  251. BIND(parser_logic_custom_nodes_set, 2);
  252. BIND(parser_logic_custom_nodes_delete, 1);
  253. BIND(util_mesh_unwrappers_set, 2);
  254. BIND(util_mesh_unwrappers_delete, 1);
  255. BIND(path_mesh_importers_set, 2);
  256. BIND(path_mesh_importers_delete, 1);
  257. BIND(path_texture_importers_set, 2);
  258. BIND(path_texture_importers_delete, 1);
  259. JS_FreeValue(js_ctx, global_obj);
  260. }