editor_import_plugin.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/script_language.h"
  32. EditorImportPlugin::EditorImportPlugin() {
  33. }
  34. String EditorImportPlugin::get_importer_name() const {
  35. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_importer_name")), "");
  36. return get_script_instance()->call("get_importer_name");
  37. }
  38. String EditorImportPlugin::get_visible_name() const {
  39. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_visible_name")), "");
  40. return get_script_instance()->call("get_visible_name");
  41. }
  42. void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {
  43. ERR_FAIL_COND(!(get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")));
  44. Array extensions = get_script_instance()->call("get_recognized_extensions");
  45. for (int i = 0; i < extensions.size(); i++) {
  46. p_extensions->push_back(extensions[i]);
  47. }
  48. }
  49. String EditorImportPlugin::get_preset_name(int p_idx) const {
  50. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_preset_name")), "");
  51. return get_script_instance()->call("get_preset_name", p_idx);
  52. }
  53. int EditorImportPlugin::get_preset_count() const {
  54. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_preset_count")), 0);
  55. return get_script_instance()->call("get_preset_count");
  56. }
  57. String EditorImportPlugin::get_save_extension() const {
  58. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_save_extension")), "");
  59. return get_script_instance()->call("get_save_extension");
  60. }
  61. String EditorImportPlugin::get_resource_type() const {
  62. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_resource_type")), "");
  63. return get_script_instance()->call("get_resource_type");
  64. }
  65. float EditorImportPlugin::get_priority() const {
  66. if (!(get_script_instance() && get_script_instance()->has_method("get_priority"))) {
  67. return ResourceImporter::get_priority();
  68. }
  69. return get_script_instance()->call("get_priority");
  70. }
  71. int EditorImportPlugin::get_import_order() const {
  72. if (!(get_script_instance() && get_script_instance()->has_method("get_import_order"))) {
  73. return ResourceImporter::get_import_order();
  74. }
  75. return get_script_instance()->call("get_import_order");
  76. }
  77. void EditorImportPlugin::get_import_options(List<ResourceImporter::ImportOption> *r_options, int p_preset) const {
  78. ERR_FAIL_COND(!(get_script_instance() && get_script_instance()->has_method("get_import_options")));
  79. Array needed;
  80. needed.push_back("name");
  81. needed.push_back("default_value");
  82. Array options = get_script_instance()->call("get_import_options", p_preset);
  83. for (int i = 0; i < options.size(); i++) {
  84. Dictionary d = options[i];
  85. ERR_FAIL_COND(!d.has_all(needed));
  86. String name = d["name"];
  87. Variant default_value = d["default_value"];
  88. PropertyHint hint = PROPERTY_HINT_NONE;
  89. if (d.has("property_hint")) {
  90. hint = (PropertyHint)d["property_hint"].operator int64_t();
  91. }
  92. String hint_string;
  93. if (d.has("hint_string")) {
  94. hint_string = d["hint_string"];
  95. }
  96. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  97. if (d.has("usage")) {
  98. usage = d["usage"];
  99. }
  100. ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);
  101. r_options->push_back(option);
  102. }
  103. }
  104. bool EditorImportPlugin::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  105. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("get_option_visibility")), true);
  106. Dictionary d;
  107. Map<StringName, Variant>::Element *E = p_options.front();
  108. while (E) {
  109. d[E->key()] = E->get();
  110. E = E->next();
  111. }
  112. return get_script_instance()->call("get_option_visibility", p_option, d);
  113. }
  114. Error EditorImportPlugin::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
  115. ERR_FAIL_COND_V(!(get_script_instance() && get_script_instance()->has_method("import")), ERR_UNAVAILABLE);
  116. Dictionary options;
  117. Array platform_variants, gen_files;
  118. Map<StringName, Variant>::Element *E = p_options.front();
  119. while (E) {
  120. options[E->key()] = E->get();
  121. E = E->next();
  122. }
  123. Error err = (Error)get_script_instance()->call("import", p_source_file, p_save_path, options, platform_variants, gen_files).operator int64_t();
  124. for (int i = 0; i < platform_variants.size(); i++) {
  125. r_platform_variants->push_back(platform_variants[i]);
  126. }
  127. for (int i = 0; i < gen_files.size(); i++) {
  128. r_gen_files->push_back(gen_files[i]);
  129. }
  130. return err;
  131. }
  132. void EditorImportPlugin::_bind_methods() {
  133. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_importer_name"));
  134. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_visible_name"));
  135. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "get_preset_count"));
  136. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_preset_name", PropertyInfo(Variant::INT, "preset")));
  137. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::ARRAY, "get_recognized_extensions"));
  138. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::ARRAY, "get_import_options", PropertyInfo(Variant::INT, "preset")));
  139. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_save_extension"));
  140. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_resource_type"));
  141. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::REAL, "get_priority"));
  142. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "get_import_order"));
  143. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "get_option_visibility", PropertyInfo(Variant::STRING, "option"), PropertyInfo(Variant::DICTIONARY, "options")));
  144. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "import", PropertyInfo(Variant::STRING, "source_file"), PropertyInfo(Variant::STRING, "save_path"), PropertyInfo(Variant::DICTIONARY, "options"), PropertyInfo(Variant::ARRAY, "platform_variants"), PropertyInfo(Variant::ARRAY, "gen_files")));
  145. }