scene_loader.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* scene_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "scene_loader.h"
  30. #include "globals.h"
  31. #include "path_remap.h"
  32. #ifdef OLD_SCENE_FORMAT_ENABLED
  33. SceneFormatLoader *SceneLoader::loader[MAX_LOADERS];
  34. int SceneLoader::loader_count=0;
  35. void SceneInteractiveLoader::_bind_methods() {
  36. ObjectTypeDB::bind_method(_MD("get_scene"),&SceneInteractiveLoader::get_scene);
  37. ObjectTypeDB::bind_method(_MD("poll"),&SceneInteractiveLoader::poll);
  38. ObjectTypeDB::bind_method(_MD("get_stage"),&SceneInteractiveLoader::get_stage);
  39. ObjectTypeDB::bind_method(_MD("get_stage_count"),&SceneInteractiveLoader::get_stage_count);
  40. }
  41. class SceneInteractiveLoaderDefault : public SceneInteractiveLoader {
  42. OBJ_TYPE( SceneInteractiveLoaderDefault, SceneInteractiveLoader );
  43. public:
  44. Node *scene;
  45. virtual void set_local_path(const String& p_local_path) { scene->set_filename(p_local_path); }
  46. virtual Node *get_scene() { return scene; }
  47. virtual Error poll() { return ERR_FILE_EOF; }
  48. virtual int get_stage() const { return 1; }
  49. virtual int get_stage_count() const { return 1; }
  50. SceneInteractiveLoaderDefault() {}
  51. };
  52. Ref<SceneInteractiveLoader> SceneFormatLoader::load_interactive(const String &p_path,bool p_root_scene_hint) {
  53. Node *scene = load(p_path,p_root_scene_hint);
  54. if (!scene)
  55. return Ref<SceneInteractiveLoader>();
  56. Ref<SceneInteractiveLoaderDefault> sil = Ref<SceneInteractiveLoaderDefault>( memnew( SceneInteractiveLoaderDefault ));
  57. sil->scene=scene;
  58. return sil;
  59. }
  60. bool SceneFormatLoader::recognize(const String& p_extension) const {
  61. List<String> extensions;
  62. get_recognized_extensions(&extensions);
  63. for (List<String>::Element *E=extensions.front();E;E=E->next()) {
  64. if (E->get().nocasecmp_to(p_extension.extension())==0)
  65. return true;
  66. }
  67. return false;
  68. }
  69. Ref<SceneInteractiveLoader> SceneLoader::load_interactive(const String &p_path,bool p_save_root_state) {
  70. String local_path=Globals::get_singleton()->localize_path(p_path);
  71. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  72. String extension=remapped_path.extension();
  73. for (int i=0;i<loader_count;i++) {
  74. if (!loader[i]->recognize(extension))
  75. continue;
  76. Ref<SceneInteractiveLoader> il = loader[i]->load_interactive(remapped_path,p_save_root_state);
  77. if (il.is_null() && remapped_path!=local_path)
  78. il = loader[i]->load_interactive(local_path,p_save_root_state);
  79. ERR_EXPLAIN("Error loading scene: "+local_path);
  80. ERR_FAIL_COND_V(il.is_null(),Ref<SceneInteractiveLoader>());
  81. il->set_local_path(local_path);
  82. return il;
  83. }
  84. ERR_EXPLAIN("No loader found for scene: "+p_path);
  85. ERR_FAIL_V(Ref<SceneInteractiveLoader>());
  86. return Ref<SceneInteractiveLoader>();
  87. }
  88. Node* SceneLoader::load(const String &p_path,bool p_root_scene_hint) {
  89. String local_path=Globals::get_singleton()->localize_path(p_path);
  90. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  91. String extension=remapped_path.extension();
  92. for (int i=0;i<loader_count;i++) {
  93. if (!loader[i]->recognize(extension))
  94. continue;
  95. Node*node = loader[i]->load(remapped_path,p_root_scene_hint);
  96. if (!node && remapped_path!=local_path)
  97. node = loader[i]->load(local_path,p_root_scene_hint);
  98. ERR_EXPLAIN("Error loading scene: "+local_path);
  99. ERR_FAIL_COND_V(!node,NULL);
  100. node->set_filename(local_path);
  101. return node;
  102. }
  103. ERR_EXPLAIN("No loader found for scene: "+p_path);
  104. ERR_FAIL_V(NULL);
  105. }
  106. void SceneLoader::get_recognized_extensions(List<String> *p_extensions) {
  107. for (int i=0;i<loader_count;i++) {
  108. loader[i]->get_recognized_extensions(p_extensions);
  109. }
  110. }
  111. void SceneLoader::add_scene_format_loader(SceneFormatLoader *p_format_loader) {
  112. ERR_FAIL_COND( loader_count >= MAX_LOADERS );
  113. loader[loader_count++]=p_format_loader;
  114. }
  115. #endif