2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* register_types.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-24 13:53:33 +01:00
|
|
|
#include "register_types.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/io/file_access_encrypted.h"
|
|
|
|
#include "core/io/resource_loader.h"
|
2019-08-09 13:45:30 +02:00
|
|
|
#include "core/os/dir_access.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/file_access.h"
|
2017-11-16 18:38:18 +01:00
|
|
|
#include "gdscript.h"
|
2017-12-14 19:33:54 +01:00
|
|
|
#include "gdscript_tokenizer.h"
|
2014-02-25 13:31:47 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GDScriptLanguage *script_language_gd = NULL;
|
2018-06-11 02:59:53 +02:00
|
|
|
Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
|
|
|
|
Ref<ResourceFormatSaverGDScript> resource_saver_gd;
|
2014-02-25 13:31:47 +01:00
|
|
|
|
2017-12-14 19:33:54 +01:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
#include "editor/editor_export.h"
|
|
|
|
#include "editor/editor_node.h"
|
|
|
|
#include "editor/editor_settings.h"
|
2019-08-28 13:10:32 +02:00
|
|
|
#include "editor/gdscript_highlighter.h"
|
|
|
|
|
|
|
|
#ifndef GDSCRIPT_NO_LSP
|
|
|
|
#include "core/engine.h"
|
|
|
|
#include "language_server/gdscript_language_server.h"
|
|
|
|
#endif // !GDSCRIPT_NO_LSP
|
2017-12-14 19:33:54 +01:00
|
|
|
|
|
|
|
class EditorExportGDScript : public EditorExportPlugin {
|
|
|
|
GDCLASS(EditorExportGDScript, EditorExportPlugin);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
|
2018-12-23 19:28:29 +01:00
|
|
|
int script_mode = EditorExportPreset::MODE_SCRIPT_COMPILED;
|
|
|
|
String script_key;
|
|
|
|
|
|
|
|
const Ref<EditorExportPreset> &preset = get_export_preset();
|
|
|
|
|
|
|
|
if (preset.is_valid()) {
|
|
|
|
script_mode = preset->get_script_export_mode();
|
|
|
|
script_key = preset->get_script_encryption_key().to_lower();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p_path.ends_with(".gd") || script_mode == EditorExportPreset::MODE_SCRIPT_TEXT)
|
2017-12-14 19:33:54 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Vector<uint8_t> file = FileAccess::get_file_as_array(p_path);
|
|
|
|
if (file.empty())
|
|
|
|
return;
|
2018-12-23 19:28:29 +01:00
|
|
|
|
2017-12-14 19:33:54 +01:00
|
|
|
String txt;
|
|
|
|
txt.parse_utf8((const char *)file.ptr(), file.size());
|
|
|
|
file = GDScriptTokenizerBuffer::parse_code_string(txt);
|
|
|
|
|
2018-12-23 19:28:29 +01:00
|
|
|
if (!file.empty()) {
|
|
|
|
if (script_mode == EditorExportPreset::MODE_SCRIPT_ENCRYPTED) {
|
2019-01-10 12:53:12 +01:00
|
|
|
String tmp_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("script.gde");
|
2018-12-23 19:28:29 +01:00
|
|
|
FileAccess *fa = FileAccess::open(tmp_path, FileAccess::WRITE);
|
|
|
|
|
|
|
|
Vector<uint8_t> key;
|
|
|
|
key.resize(32);
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
int v = 0;
|
|
|
|
if (i * 2 < script_key.length()) {
|
|
|
|
CharType ct = script_key[i * 2];
|
|
|
|
if (ct >= '0' && ct <= '9')
|
|
|
|
ct = ct - '0';
|
|
|
|
else if (ct >= 'a' && ct <= 'f')
|
|
|
|
ct = 10 + ct - 'a';
|
|
|
|
v |= ct << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i * 2 + 1 < script_key.length()) {
|
|
|
|
CharType ct = script_key[i * 2 + 1];
|
|
|
|
if (ct >= '0' && ct <= '9')
|
|
|
|
ct = ct - '0';
|
|
|
|
else if (ct >= 'a' && ct <= 'f')
|
|
|
|
ct = 10 + ct - 'a';
|
|
|
|
v |= ct;
|
|
|
|
}
|
|
|
|
key.write[i] = v;
|
|
|
|
}
|
|
|
|
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
|
|
|
Error err = fae->open_and_parse(fa, key, FileAccessEncrypted::MODE_WRITE_AES256);
|
|
|
|
|
|
|
|
if (err == OK) {
|
|
|
|
fae->store_buffer(file.ptr(), file.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(fae);
|
|
|
|
|
|
|
|
file = FileAccess::get_file_as_array(tmp_path);
|
|
|
|
add_file(p_path.get_basename() + ".gde", file, true);
|
|
|
|
|
2019-08-09 13:45:30 +02:00
|
|
|
// Clean up temporary file.
|
|
|
|
DirAccess::remove_file_or_error(tmp_path);
|
|
|
|
|
2018-12-23 19:28:29 +01:00
|
|
|
} else {
|
|
|
|
add_file(p_path.get_basename() + ".gdc", file, true);
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 19:33:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void _editor_init() {
|
|
|
|
Ref<EditorExportGDScript> gd_export;
|
|
|
|
gd_export.instance();
|
|
|
|
EditorExport::get_singleton()->add_export_plugin(gd_export);
|
2019-06-26 14:21:42 +02:00
|
|
|
|
2019-08-28 13:10:32 +02:00
|
|
|
#ifndef GDSCRIPT_NO_LSP
|
2019-06-14 16:38:54 +02:00
|
|
|
register_lsp_types();
|
2019-06-26 14:21:42 +02:00
|
|
|
GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
|
|
|
|
EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
|
|
|
|
Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
|
2019-08-28 13:10:32 +02:00
|
|
|
#endif // !GDSCRIPT_NO_LSP
|
2017-12-14 19:33:54 +01:00
|
|
|
}
|
|
|
|
|
2019-08-28 13:10:32 +02:00
|
|
|
#endif // TOOLS_ENABLED
|
2017-12-14 19:33:54 +01:00
|
|
|
|
2014-02-24 13:53:33 +01:00
|
|
|
void register_gdscript_types() {
|
2017-01-03 03:03:46 +01:00
|
|
|
ClassDB::register_class<GDScript>();
|
2017-11-16 18:38:18 +01:00
|
|
|
ClassDB::register_virtual_class<GDScriptFunctionState>();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
script_language_gd = memnew(GDScriptLanguage);
|
2014-02-10 02:10:30 +01:00
|
|
|
ScriptServer::register_language(script_language_gd);
|
2018-06-11 02:59:53 +02:00
|
|
|
|
|
|
|
resource_loader_gd.instance();
|
2014-02-10 02:10:30 +01:00
|
|
|
ResourceLoader::add_resource_format_loader(resource_loader_gd);
|
2018-06-11 02:59:53 +02:00
|
|
|
|
|
|
|
resource_saver_gd.instance();
|
2014-02-10 02:10:30 +01:00
|
|
|
ResourceSaver::add_resource_format_saver(resource_saver_gd);
|
2017-12-14 19:33:54 +01:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2018-04-04 21:02:42 +02:00
|
|
|
ScriptEditor::register_create_syntax_highlighter_function(GDScriptSyntaxHighlighter::create);
|
2017-12-14 19:33:54 +01:00
|
|
|
EditorNode::add_init_callback(_editor_init);
|
2019-08-28 13:10:32 +02:00
|
|
|
#endif // TOOLS_ENABLED
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-08-27 21:07:15 +02:00
|
|
|
|
2014-02-24 13:53:33 +01:00
|
|
|
void unregister_gdscript_types() {
|
2016-06-26 15:53:34 +02:00
|
|
|
ScriptServer::unregister_language(script_language_gd);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (script_language_gd)
|
2017-03-05 16:44:50 +01:00
|
|
|
memdelete(script_language_gd);
|
2018-06-11 02:59:53 +02:00
|
|
|
|
2019-01-10 12:45:57 +01:00
|
|
|
ResourceLoader::remove_resource_format_loader(resource_loader_gd);
|
|
|
|
resource_loader_gd.unref();
|
2018-06-11 02:59:53 +02:00
|
|
|
|
2019-01-10 12:45:57 +01:00
|
|
|
ResourceSaver::remove_resource_format_saver(resource_saver_gd);
|
|
|
|
resource_saver_gd.unref();
|
2014-02-25 13:31:47 +01:00
|
|
|
}
|