editor_import_plugin.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. int EditorImportPlugin::get_format_version() const {
  102. int ret = 0;
  103. if (GDVIRTUAL_CALL(_get_format_version, ret)) {
  104. return ret;
  105. }
  106. return 0;
  107. }
  108. void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {
  109. Array needed;
  110. needed.push_back("name");
  111. needed.push_back("default_value");
  112. TypedArray<Dictionary> options;
  113. if (GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options)) {
  114. for (int i = 0; i < options.size(); i++) {
  115. Dictionary d = options[i];
  116. ERR_FAIL_COND(!d.has_all(needed));
  117. String name = d["name"];
  118. Variant default_value = d["default_value"];
  119. PropertyHint hint = PROPERTY_HINT_NONE;
  120. if (d.has("property_hint")) {
  121. hint = (PropertyHint)d["property_hint"].operator int64_t();
  122. }
  123. String hint_string;
  124. if (d.has("hint_string")) {
  125. hint_string = d["hint_string"];
  126. }
  127. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  128. if (d.has("usage")) {
  129. usage = d["usage"];
  130. }
  131. ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);
  132. r_options->push_back(option);
  133. }
  134. return;
  135. }
  136. ERR_FAIL_MSG("Unimplemented _get_import_options in add-on.");
  137. }
  138. bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  139. Dictionary d;
  140. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  141. while (E) {
  142. d[E->key] = E->value;
  143. ++E;
  144. }
  145. bool visible = false;
  146. if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {
  147. return visible;
  148. }
  149. ERR_FAIL_V_MSG(false, "Unimplemented _get_option_visibility in add-on.");
  150. }
  151. 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) {
  152. Dictionary options;
  153. TypedArray<String> platform_variants, gen_files;
  154. HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
  155. while (E) {
  156. options[E->key] = E->value;
  157. ++E;
  158. }
  159. Error err = OK;
  160. if (GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err)) {
  161. for (int i = 0; i < platform_variants.size(); i++) {
  162. r_platform_variants->push_back(platform_variants[i]);
  163. }
  164. for (int i = 0; i < gen_files.size(); i++) {
  165. r_gen_files->push_back(gen_files[i]);
  166. }
  167. return err;
  168. }
  169. ERR_FAIL_V_MSG(ERR_METHOD_NOT_FOUND, "Unimplemented _import in add-on.");
  170. }
  171. bool EditorImportPlugin::can_import_threaded() const {
  172. bool ret = false;
  173. if (GDVIRTUAL_CALL(_can_import_threaded, ret)) {
  174. return ret;
  175. } else {
  176. return ResourceImporter::can_import_threaded();
  177. }
  178. }
  179. Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
  180. HashMap<StringName, Variant> options;
  181. List<Variant> keys;
  182. p_custom_options.get_key_list(&keys);
  183. for (const Variant &K : keys) {
  184. options.insert(K, p_custom_options[K]);
  185. }
  186. return append_import_external_resource(p_file, options, p_custom_importer, p_generator_parameters);
  187. }
  188. 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) {
  189. return EditorFileSystem::get_singleton()->reimport_append(p_file, p_custom_options, p_custom_importer, p_generator_parameters);
  190. }
  191. void EditorImportPlugin::_bind_methods() {
  192. GDVIRTUAL_BIND(_get_importer_name)
  193. GDVIRTUAL_BIND(_get_visible_name)
  194. GDVIRTUAL_BIND(_get_preset_count)
  195. GDVIRTUAL_BIND(_get_preset_name, "preset_index")
  196. GDVIRTUAL_BIND(_get_recognized_extensions)
  197. GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")
  198. GDVIRTUAL_BIND(_get_save_extension)
  199. GDVIRTUAL_BIND(_get_resource_type)
  200. GDVIRTUAL_BIND(_get_priority)
  201. GDVIRTUAL_BIND(_get_import_order)
  202. GDVIRTUAL_BIND(_get_format_version)
  203. GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")
  204. GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");
  205. GDVIRTUAL_BIND(_can_import_threaded);
  206. 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()));
  207. }