pluginscript_loader.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* pluginscript_loader.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. // Godot imports
  31. #include "core/os/file_access.h"
  32. // Pythonscript imports
  33. #include "pluginscript_language.h"
  34. #include "pluginscript_loader.h"
  35. #include "pluginscript_script.h"
  36. ResourceFormatLoaderPluginScript::ResourceFormatLoaderPluginScript(PluginScriptLanguage *language) {
  37. _language = language;
  38. }
  39. RES ResourceFormatLoaderPluginScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
  40. if (r_error)
  41. *r_error = ERR_FILE_CANT_OPEN;
  42. PluginScript *script = memnew(PluginScript);
  43. script->init(_language);
  44. Ref<PluginScript> scriptres(script);
  45. Error err = script->load_source_code(p_path);
  46. ERR_FAIL_COND_V(err != OK, RES());
  47. script->set_path(p_original_path);
  48. script->reload();
  49. if (r_error)
  50. *r_error = OK;
  51. return scriptres;
  52. }
  53. void ResourceFormatLoaderPluginScript::get_recognized_extensions(List<String> *p_extensions) const {
  54. p_extensions->push_back(_language->get_extension());
  55. }
  56. bool ResourceFormatLoaderPluginScript::handles_type(const String &p_type) const {
  57. return p_type == "Script" || p_type == _language->get_type();
  58. }
  59. String ResourceFormatLoaderPluginScript::get_resource_type(const String &p_path) const {
  60. String el = p_path.get_extension().to_lower();
  61. if (el == _language->get_extension())
  62. return _language->get_type();
  63. return "";
  64. }
  65. ResourceFormatSaverPluginScript::ResourceFormatSaverPluginScript(PluginScriptLanguage *language) {
  66. _language = language;
  67. }
  68. Error ResourceFormatSaverPluginScript::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  69. Ref<PluginScript> sqscr = p_resource;
  70. ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
  71. String source = sqscr->get_source_code();
  72. Error err;
  73. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  74. ERR_FAIL_COND_V(err, err);
  75. file->store_string(source);
  76. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  77. memdelete(file);
  78. return ERR_CANT_CREATE;
  79. }
  80. file->close();
  81. memdelete(file);
  82. return OK;
  83. }
  84. void ResourceFormatSaverPluginScript::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  85. if (Object::cast_to<PluginScript>(*p_resource)) {
  86. p_extensions->push_back(_language->get_extension());
  87. }
  88. }
  89. bool ResourceFormatSaverPluginScript::recognize(const RES &p_resource) const {
  90. return Object::cast_to<PluginScript>(*p_resource) != NULL;
  91. }