editor_import_plugin.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. GDVIRTUAL_CALL(_get_importer_name, ret);
  36. return ret;
  37. }
  38. String EditorImportPlugin::get_visible_name() const {
  39. String ret;
  40. GDVIRTUAL_CALL(_get_visible_name, ret);
  41. return ret;
  42. }
  43. void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {
  44. Vector<String> extensions;
  45. GDVIRTUAL_CALL(_get_recognized_extensions, extensions);
  46. for (int i = 0; i < extensions.size(); i++) {
  47. p_extensions->push_back(extensions[i]);
  48. }
  49. }
  50. String EditorImportPlugin::get_preset_name(int p_idx) const {
  51. String ret;
  52. GDVIRTUAL_CALL(_get_preset_name, p_idx, ret);
  53. return ret;
  54. }
  55. int EditorImportPlugin::get_preset_count() const {
  56. int ret;
  57. if (GDVIRTUAL_CALL(_get_preset_count, ret)) {
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. String EditorImportPlugin::get_save_extension() const {
  63. String ret;
  64. GDVIRTUAL_CALL(_get_save_extension, ret);
  65. return ret;
  66. }
  67. String EditorImportPlugin::get_resource_type() const {
  68. String ret;
  69. GDVIRTUAL_CALL(_get_resource_type, ret);
  70. return ret;
  71. }
  72. float EditorImportPlugin::get_priority() const {
  73. float ret;
  74. if (GDVIRTUAL_CALL(_get_priority, ret)) {
  75. return ret;
  76. }
  77. return 1.0;
  78. }
  79. int EditorImportPlugin::get_import_order() const {
  80. int ret;
  81. if (GDVIRTUAL_CALL(_get_import_order, ret)) {
  82. return ret;
  83. }
  84. return IMPORT_ORDER_DEFAULT;
  85. }
  86. int EditorImportPlugin::get_format_version() const {
  87. int ret = 0;
  88. if (GDVIRTUAL_CALL(_get_format_version, ret)) {
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {
  94. Array needed = { "name", "default_value" };
  95. TypedArray<Dictionary> options;
  96. GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options);
  97. for (int i = 0; i < options.size(); i++) {
  98. Dictionary d = options[i];
  99. ERR_FAIL_COND(!d.has_all(needed));
  100. String name = d["name"];
  101. Variant default_value = d["default_value"];
  102. PropertyHint hint = PROPERTY_HINT_NONE;
  103. if (d.has("property_hint")) {
  104. hint = (PropertyHint)d["property_hint"].operator int64_t();
  105. }
  106. String hint_string;
  107. if (d.has("hint_string")) {
  108. hint_string = d["hint_string"];
  109. }
  110. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  111. if (d.has("usage")) {
  112. usage = d["usage"];
  113. }
  114. ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);
  115. r_options->push_back(option);
  116. }
  117. }
  118. bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  119. Dictionary d;
  120. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  121. while (E) {
  122. d[E->key] = E->value;
  123. ++E;
  124. }
  125. bool visible = false;
  126. if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {
  127. return visible;
  128. }
  129. return true;
  130. }
  131. 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) {
  132. Dictionary options;
  133. TypedArray<String> platform_variants, gen_files;
  134. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  135. while (E) {
  136. options[E->key] = E->value;
  137. ++E;
  138. }
  139. Error err = OK;
  140. GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err);
  141. for (int i = 0; i < platform_variants.size(); i++) {
  142. r_platform_variants->push_back(platform_variants[i]);
  143. }
  144. for (int i = 0; i < gen_files.size(); i++) {
  145. r_gen_files->push_back(gen_files[i]);
  146. }
  147. return err;
  148. }
  149. bool EditorImportPlugin::can_import_threaded() const {
  150. bool ret = false;
  151. if (GDVIRTUAL_CALL(_can_import_threaded, ret)) {
  152. return ret;
  153. } else {
  154. return ResourceImporter::can_import_threaded();
  155. }
  156. }
  157. Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  158. HashMap<StringName, Variant> options;
  159. for (const KeyValue<Variant, Variant> &kv : p_custom_options) {
  160. options.insert(kv.key, kv.value);
  161. }
  162. return append_import_external_resource(p_file, options, p_custom_importer, p_generator_parameters);
  163. }
  164. 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) {
  165. ERR_FAIL_COND_V_MSG(!EditorFileSystem::get_singleton()->is_importing(), ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");
  166. return EditorFileSystem::get_singleton()->reimport_append(p_file, p_custom_options, p_custom_importer, p_generator_parameters);
  167. }
  168. void EditorImportPlugin::_bind_methods() {
  169. GDVIRTUAL_BIND(_get_importer_name)
  170. GDVIRTUAL_BIND(_get_visible_name)
  171. GDVIRTUAL_BIND(_get_preset_count)
  172. GDVIRTUAL_BIND(_get_preset_name, "preset_index")
  173. GDVIRTUAL_BIND(_get_recognized_extensions)
  174. GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")
  175. GDVIRTUAL_BIND(_get_save_extension)
  176. GDVIRTUAL_BIND(_get_resource_type)
  177. GDVIRTUAL_BIND(_get_priority)
  178. GDVIRTUAL_BIND(_get_import_order)
  179. GDVIRTUAL_BIND(_get_format_version)
  180. GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")
  181. GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");
  182. GDVIRTUAL_BIND(_can_import_threaded);
  183. 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()));
  184. }