editor_import_plugin.cpp 8.5 KB

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