editor_import_plugin.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*************************************************************************/
  2. /* editor_import_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. EditorImportPlugin::EditorImportPlugin() {
  33. }
  34. String EditorImportPlugin::get_importer_name() const {
  35. String ret;
  36. if (GDVIRTUAL_CALL(_get_importer_name, ret)) {
  37. return ret;
  38. }
  39. ERR_FAIL_V_MSG(String(), "Unimplemented _get_importer_name in add-on.");
  40. }
  41. String EditorImportPlugin::get_visible_name() const {
  42. String ret;
  43. if (GDVIRTUAL_CALL(_get_visible_name, ret)) {
  44. return ret;
  45. }
  46. ERR_FAIL_V_MSG(String(), "Unimplemented _get_visible_name in add-on.");
  47. }
  48. void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {
  49. Vector<String> extensions;
  50. if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {
  51. for (int i = 0; i < extensions.size(); i++) {
  52. p_extensions->push_back(extensions[i]);
  53. }
  54. return;
  55. }
  56. ERR_FAIL_MSG("Unimplemented _get_recognized_extensions in add-on.");
  57. }
  58. String EditorImportPlugin::get_preset_name(int p_idx) const {
  59. String ret;
  60. if (GDVIRTUAL_CALL(_get_preset_name, p_idx, ret)) {
  61. return ret;
  62. }
  63. ERR_FAIL_V_MSG(String(), "Unimplemented _get_preset_name in add-on.");
  64. }
  65. int EditorImportPlugin::get_preset_count() const {
  66. int ret;
  67. if (GDVIRTUAL_CALL(_get_preset_count, ret)) {
  68. return ret;
  69. }
  70. ERR_FAIL_V_MSG(-1, "Unimplemented _get_preset_count in add-on.");
  71. }
  72. String EditorImportPlugin::get_save_extension() const {
  73. String ret;
  74. if (GDVIRTUAL_CALL(_get_save_extension, ret)) {
  75. return ret;
  76. }
  77. ERR_FAIL_V_MSG(String(), "Unimplemented _get_save_extension in add-on.");
  78. }
  79. String EditorImportPlugin::get_resource_type() const {
  80. String ret;
  81. if (GDVIRTUAL_CALL(_get_resource_type, ret)) {
  82. return ret;
  83. }
  84. ERR_FAIL_V_MSG(String(), "Unimplemented _get_resource_type in add-on.");
  85. }
  86. float EditorImportPlugin::get_priority() const {
  87. float ret;
  88. if (GDVIRTUAL_CALL(_get_priority, ret)) {
  89. return ret;
  90. }
  91. ERR_FAIL_V_MSG(-1, "Unimplemented _get_priority in add-on.");
  92. }
  93. int EditorImportPlugin::get_import_order() const {
  94. int ret;
  95. if (GDVIRTUAL_CALL(_get_import_order, ret)) {
  96. return ret;
  97. }
  98. ERR_FAIL_V_MSG(-1, "Unimplemented _get_import_order in add-on.");
  99. }
  100. void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {
  101. Array needed;
  102. needed.push_back("name");
  103. needed.push_back("default_value");
  104. TypedArray<Dictionary> options;
  105. if (GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options)) {
  106. for (int i = 0; i < options.size(); i++) {
  107. Dictionary d = options[i];
  108. ERR_FAIL_COND(!d.has_all(needed));
  109. String name = d["name"];
  110. Variant default_value = d["default_value"];
  111. PropertyHint hint = PROPERTY_HINT_NONE;
  112. if (d.has("property_hint")) {
  113. hint = (PropertyHint)d["property_hint"].operator int64_t();
  114. }
  115. String hint_string;
  116. if (d.has("hint_string")) {
  117. hint_string = d["hint_string"];
  118. }
  119. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  120. if (d.has("usage")) {
  121. usage = d["usage"];
  122. }
  123. ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);
  124. r_options->push_back(option);
  125. }
  126. return;
  127. }
  128. ERR_FAIL_MSG("Unimplemented _get_import_options in add-on.");
  129. }
  130. bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  131. Dictionary d;
  132. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  133. while (E) {
  134. d[E->key] = E->value;
  135. ++E;
  136. }
  137. bool visible = false;
  138. if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {
  139. return visible;
  140. }
  141. ERR_FAIL_V_MSG(false, "Unimplemented _get_option_visibility in add-on.");
  142. }
  143. 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) {
  144. Dictionary options;
  145. TypedArray<String> platform_variants, gen_files;
  146. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  147. while (E) {
  148. options[E->key] = E->value;
  149. ++E;
  150. }
  151. int err = 0;
  152. if (GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err)) {
  153. Error ret_err = Error(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 ret_err;
  161. }
  162. ERR_FAIL_V_MSG(ERR_METHOD_NOT_FOUND, "Unimplemented _import in add-on.");
  163. }
  164. void EditorImportPlugin::_bind_methods() {
  165. GDVIRTUAL_BIND(_get_importer_name)
  166. GDVIRTUAL_BIND(_get_visible_name)
  167. GDVIRTUAL_BIND(_get_preset_count)
  168. GDVIRTUAL_BIND(_get_preset_name, "preset_index")
  169. GDVIRTUAL_BIND(_get_recognized_extensions)
  170. GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")
  171. GDVIRTUAL_BIND(_get_save_extension)
  172. GDVIRTUAL_BIND(_get_resource_type)
  173. GDVIRTUAL_BIND(_get_priority)
  174. GDVIRTUAL_BIND(_get_import_order)
  175. GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")
  176. GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");
  177. }