register_types.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*************************************************************************/
  2. /* register_types.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 "register_types.h"
  31. #include "core/io/file_access_encrypted.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/file_access.h"
  34. #include "editor/gdscript_highlighter.h"
  35. #include "gdscript.h"
  36. #include "gdscript_tokenizer.h"
  37. GDScriptLanguage *script_language_gd = NULL;
  38. Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
  39. Ref<ResourceFormatSaverGDScript> resource_saver_gd;
  40. #ifdef TOOLS_ENABLED
  41. #include "editor/editor_export.h"
  42. #include "editor/editor_node.h"
  43. #include "editor/editor_settings.h"
  44. class EditorExportGDScript : public EditorExportPlugin {
  45. GDCLASS(EditorExportGDScript, EditorExportPlugin);
  46. public:
  47. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  48. if (!p_path.ends_with(".gd"))
  49. return;
  50. Vector<uint8_t> file = FileAccess::get_file_as_array(p_path);
  51. if (file.empty())
  52. return;
  53. String txt;
  54. txt.parse_utf8((const char *)file.ptr(), file.size());
  55. file = GDScriptTokenizerBuffer::parse_code_string(txt);
  56. if (file.empty())
  57. return;
  58. add_file(p_path.get_basename() + ".gdc", file, true);
  59. }
  60. };
  61. static void _editor_init() {
  62. Ref<EditorExportGDScript> gd_export;
  63. gd_export.instance();
  64. EditorExport::get_singleton()->add_export_plugin(gd_export);
  65. }
  66. #endif
  67. void register_gdscript_types() {
  68. ClassDB::register_class<GDScript>();
  69. ClassDB::register_virtual_class<GDScriptFunctionState>();
  70. script_language_gd = memnew(GDScriptLanguage);
  71. ScriptServer::register_language(script_language_gd);
  72. resource_loader_gd.instance();
  73. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  74. resource_saver_gd.instance();
  75. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  76. #ifdef TOOLS_ENABLED
  77. ScriptEditor::register_create_syntax_highlighter_function(GDScriptSyntaxHighlighter::create);
  78. EditorNode::add_init_callback(_editor_init);
  79. #endif
  80. }
  81. void unregister_gdscript_types() {
  82. ScriptServer::unregister_language(script_language_gd);
  83. if (script_language_gd)
  84. memdelete(script_language_gd);
  85. if (resource_loader_gd.is_valid()) {
  86. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  87. resource_loader_gd.unref();
  88. }
  89. if (resource_saver_gd.is_valid()) {
  90. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  91. resource_saver_gd.unref();
  92. }
  93. }