| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  javascript_tools_editor_plugin.cpp                                   */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											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).   */ | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(TOOLS_ENABLED) && defined(JAVASCRIPT_ENABLED)
 | 
					
						
							|  |  |  | #include "javascript_tools_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/config/engine.h"
 | 
					
						
							|  |  |  | #include "core/config/project_settings.h"
 | 
					
						
							| 
									
										
										
										
											2021-06-11 14:51:48 +02:00
										 |  |  | #include "core/io/dir_access.h"
 | 
					
						
							|  |  |  | #include "core/io/file_access.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <emscripten/emscripten.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 18:33:20 +02:00
										 |  |  | // JavaScript functions defined in library_godot_editor_tools.js
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							| 
									
										
										
										
											2021-06-14 12:32:03 +02:00
										 |  |  | extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime); | 
					
						
							| 
									
										
										
										
											2020-10-23 18:33:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | static void _javascript_editor_init_callback() { | 
					
						
							|  |  |  | 	EditorNode::get_singleton()->add_editor_plugin(memnew(JavaScriptToolsEditorPlugin(EditorNode::get_singleton()))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void JavaScriptToolsEditorPlugin::initialize() { | 
					
						
							|  |  |  | 	EditorNode::add_init_callback(_javascript_editor_init_callback); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JavaScriptToolsEditorPlugin::JavaScriptToolsEditorPlugin(EditorNode *p_editor) { | 
					
						
							| 
									
										
										
										
											2020-12-16 00:04:21 +01:00
										 |  |  | 	add_tool_menu_item("Download Project Source", callable_mp(this, &JavaScriptToolsEditorPlugin::_download_zip)); | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) { | 
					
						
							|  |  |  | 	if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) { | 
					
						
							|  |  |  | 		WARN_PRINT("Project download is only available in Editor mode"); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	String resource_path = ProjectSettings::get_singleton()->get_resource_path(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	FileAccess *src_f; | 
					
						
							|  |  |  | 	zlib_filefunc_def io = zipio_create_io_from_file(&src_f); | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 	zipFile zip = zipOpen2("/tmp/project.zip", APPEND_STATUS_CREATE, nullptr, &io); | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 	String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/"; | 
					
						
							|  |  |  | 	_zip_recursive(resource_path, base_path, zip); | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 	zipClose(zip, nullptr); | 
					
						
							| 
									
										
										
										
											2021-05-19 15:53:04 +02:00
										 |  |  | 	FileAccess *f = FileAccess::open("/tmp/project.zip", FileAccess::READ); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_MSG(!f, "Unable to create zip file"); | 
					
						
							|  |  |  | 	Vector<uint8_t> buf; | 
					
						
							| 
									
										
										
										
											2021-05-25 07:58:49 +01:00
										 |  |  | 	buf.resize(f->get_length()); | 
					
						
							| 
									
										
										
										
											2021-05-19 15:53:04 +02:00
										 |  |  | 	f->get_buffer(buf.ptrw(), buf.size()); | 
					
						
							|  |  |  | 	godot_js_os_download_buffer(buf.ptr(), buf.size(), "project.zip", "application/zip"); | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) { | 
					
						
							|  |  |  | 	FileAccess *f = FileAccess::open(p_path, FileAccess::READ); | 
					
						
							|  |  |  | 	if (!f) { | 
					
						
							|  |  |  | 		WARN_PRINT("Unable to open file for zipping: " + p_path); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	Vector<uint8_t> data; | 
					
						
							| 
									
										
										
										
											2021-05-25 07:58:49 +01:00
										 |  |  | 	uint64_t len = f->get_length(); | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 	data.resize(len); | 
					
						
							|  |  |  | 	f->get_buffer(data.ptrw(), len); | 
					
						
							|  |  |  | 	f->close(); | 
					
						
							|  |  |  | 	memdelete(f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String path = p_path.replace_first(p_base_path, ""); | 
					
						
							|  |  |  | 	zipOpenNewFileInZip(p_zip, | 
					
						
							|  |  |  | 			path.utf8().get_data(), | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 			nullptr, | 
					
						
							|  |  |  | 			nullptr, | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 			0, | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 			nullptr, | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 			0, | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 			nullptr, | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 			Z_DEFLATED, | 
					
						
							|  |  |  | 			Z_DEFAULT_COMPRESSION); | 
					
						
							|  |  |  | 	zipWriteInFileInZip(p_zip, data.ptr(), data.size()); | 
					
						
							|  |  |  | 	zipCloseFileInZip(p_zip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) { | 
					
						
							|  |  |  | 	DirAccess *dir = DirAccess::open(p_path); | 
					
						
							|  |  |  | 	if (!dir) { | 
					
						
							|  |  |  | 		WARN_PRINT("Unable to open dir for zipping: " + p_path); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	dir->list_dir_begin(); | 
					
						
							|  |  |  | 	String cur = dir->get_next(); | 
					
						
							| 
									
										
										
										
											2020-12-29 09:54:59 +01:00
										 |  |  | 	while (!cur.is_empty()) { | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 		String cs = p_path.plus_file(cur); | 
					
						
							|  |  |  | 		if (cur == "." || cur == ".." || cur == ".import") { | 
					
						
							|  |  |  | 			// Skip
 | 
					
						
							|  |  |  | 		} else if (dir->current_is_dir()) { | 
					
						
							|  |  |  | 			String path = cs.replace_first(p_base_path, "") + "/"; | 
					
						
							|  |  |  | 			zipOpenNewFileInZip(p_zip, | 
					
						
							|  |  |  | 					path.utf8().get_data(), | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 					nullptr, | 
					
						
							|  |  |  | 					nullptr, | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 					0, | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 					nullptr, | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 					0, | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 					nullptr, | 
					
						
							| 
									
										
										
										
											2020-09-17 18:01:36 +02:00
										 |  |  | 					Z_DEFLATED, | 
					
						
							|  |  |  | 					Z_DEFAULT_COMPRESSION); | 
					
						
							|  |  |  | 			zipCloseFileInZip(p_zip); | 
					
						
							|  |  |  | 			_zip_recursive(cs, p_base_path, p_zip); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			_zip_file(cs, p_base_path, p_zip); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cur = dir->get_next(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |