editor_import_plugin.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* editor_import_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_import_plugin.h"
  31. #include "core/object/script_language.h"
  32. #include "editor/file_system/editor_file_system.h"
  33. String EditorImportPlugin::get_importer_name() const {
  34. String ret;
  35. if (GDVIRTUAL_CALL(_get_importer_name, ret)) {
  36. return ret;
  37. }
  38. ERR_FAIL_V_MSG(String(), "Unimplemented _get_importer_name in add-on.");
  39. }
  40. String EditorImportPlugin::get_visible_name() const {
  41. String ret;
  42. if (GDVIRTUAL_CALL(_get_visible_name, ret)) {
  43. return ret;
  44. }
  45. ERR_FAIL_V_MSG(String(), "Unimplemented _get_visible_name in add-on.");
  46. }
  47. void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {
  48. Vector<String> extensions;
  49. if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {
  50. for (int i = 0; i < extensions.size(); i++) {
  51. p_extensions->push_back(extensions[i]);
  52. }
  53. return;
  54. }
  55. ERR_FAIL_MSG("Unimplemented _get_recognized_extensions in add-on.");
  56. }
  57. String EditorImportPlugin::get_preset_name(int p_idx) const {
  58. String ret;
  59. if (GDVIRTUAL_CALL(_get_preset_name, p_idx, ret)) {
  60. return ret;
  61. }
  62. ERR_FAIL_V_MSG(itos(p_idx), "Unimplemented _get_preset_name in add-on.");
  63. }
  64. int EditorImportPlugin::get_preset_count() const {
  65. int ret;
  66. if (GDVIRTUAL_CALL(_get_preset_count, ret)) {
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. String EditorImportPlugin::get_save_extension() const {
  72. String ret;
  73. if (GDVIRTUAL_CALL(_get_save_extension, ret)) {
  74. return ret;
  75. }
  76. ERR_FAIL_V_MSG(String(), "Unimplemented _get_save_extension in add-on.");
  77. }
  78. String EditorImportPlugin::get_resource_type() const {
  79. String ret;
  80. if (GDVIRTUAL_CALL(_get_resource_type, ret)) {
  81. return ret;
  82. }
  83. ERR_FAIL_V_MSG(String(), "Unimplemented _get_resource_type in add-on.");
  84. }
  85. float EditorImportPlugin::get_priority() const {
  86. float ret;
  87. if (GDVIRTUAL_CALL(_get_priority, ret)) {
  88. return ret;
  89. }
  90. return 1.0;
  91. }
  92. int EditorImportPlugin::get_import_order() const {
  93. int ret;
  94. if (GDVIRTUAL_CALL(_get_import_order, ret)) {
  95. return ret;
  96. }
  97. return IMPORT_ORDER_DEFAULT;
  98. }
  99. int EditorImportPlugin::get_format_version() const {
  100. int ret = 0;
  101. if (GDVIRTUAL_CALL(_get_format_version, ret)) {
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {
  107. Array needed = { "name", "default_value" };
  108. TypedArray<Dictionary> options;
  109. if (GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options)) {
  110. for (int i = 0; i < options.size(); i++) {
  111. Dictionary d = options[i];
  112. ERR_FAIL_COND(!d.has_all(needed));
  113. String name = d["name"];
  114. Variant default_value = d["default_value"];
  115. PropertyHint hint = PROPERTY_HINT_NONE;
  116. if (d.has("property_hint")) {
  117. hint = (PropertyHint)d["property_hint"].operator int64_t();
  118. }
  119. String hint_string;
  120. if (d.has("hint_string")) {
  121. hint_string = d["hint_string"];
  122. }
  123. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  124. if (d.has("usage")) {
  125. usage = d["usage"];
  126. }
  127. ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);
  128. r_options->push_back(option);
  129. }
  130. return;
  131. }
  132. ERR_FAIL_MSG("Unimplemented _get_import_options in add-on.");
  133. }
  134. bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  135. Dictionary d;
  136. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  137. while (E) {
  138. d[E->key] = E->value;
  139. ++E;
  140. }
  141. bool visible = false;
  142. if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {
  143. return visible;
  144. }
  145. return true;
  146. }
  147. Error EditorImportPlugin::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  148. Dictionary options;
  149. TypedArray<String> platform_variants, gen_files;
  150. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  151. while (E) {
  152. options[E->key] = E->value;
  153. ++E;
  154. }
  155. Error err = OK;
  156. if (GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err)) {
  157. for (int i = 0; i < platform_variants.size(); i++) {
  158. r_platform_variants->push_back(platform_variants[i]);
  159. }
  160. for (int i = 0; i < gen_files.size(); i++) {
  161. r_gen_files->push_back(gen_files[i]);
  162. }
  163. return err;
  164. }
  165. ERR_FAIL_V_MSG(ERR_METHOD_NOT_FOUND, "Unimplemented _import in add-on.");
  166. }
  167. bool EditorImportPlugin::can_import_threaded() const {
  168. bool ret = false;
  169. if (GDVIRTUAL_CALL(_can_import_threaded, ret)) {
  170. return ret;
  171. } else {
  172. return ResourceImporter::can_import_threaded();
  173. }
  174. }
  175. Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  176. HashMap<StringName, Variant> options;
  177. for (const KeyValue<Variant, Variant> &kv : p_custom_options) {
  178. options.insert(kv.key, kv.value);
  179. }
  180. return append_import_external_resource(p_file, options, p_custom_importer, p_generator_parameters);
  181. }
  182. Error EditorImportPlugin::append_import_external_resource(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  183. ERR_FAIL_COND_V_MSG(!EditorFileSystem::get_singleton()->is_importing(), ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");
  184. return EditorFileSystem::get_singleton()->reimport_append(p_file, p_custom_options, p_custom_importer, p_generator_parameters);
  185. }
  186. void EditorImportPlugin::_bind_methods() {
  187. GDVIRTUAL_BIND(_get_importer_name)
  188. GDVIRTUAL_BIND(_get_visible_name)
  189. GDVIRTUAL_BIND(_get_preset_count)
  190. GDVIRTUAL_BIND(_get_preset_name, "preset_index")
  191. GDVIRTUAL_BIND(_get_recognized_extensions)
  192. GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")
  193. GDVIRTUAL_BIND(_get_save_extension)
  194. GDVIRTUAL_BIND(_get_resource_type)
  195. GDVIRTUAL_BIND(_get_priority)
  196. GDVIRTUAL_BIND(_get_import_order)
  197. GDVIRTUAL_BIND(_get_format_version)
  198. GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")
  199. GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");
  200. GDVIRTUAL_BIND(_can_import_threaded);
  201. ClassDB::bind_method(D_METHOD("append_import_external_resource", "path", "custom_options", "custom_importer", "generator_parameters"), &EditorImportPlugin::_append_import_external_resource, DEFVAL(Dictionary()), DEFVAL(String()), DEFVAL(Variant()));
  202. }