pluginscript_loader.cpp 4.5 KB

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