summaryrefslogtreecommitdiff
path: root/editor/export_template_manager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/export_template_manager.cpp')
-rw-r--r--editor/export_template_manager.cpp221
1 files changed, 166 insertions, 55 deletions
diff --git a/editor/export_template_manager.cpp b/editor/export_template_manager.cpp
index 97ccfb0db1..318fee01f2 100644
--- a/editor/export_template_manager.cpp
+++ b/editor/export_template_manager.cpp
@@ -52,18 +52,16 @@ void ExportTemplateManager::_update_template_list() {
DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir());
- d->list_dir_begin();
Set<String> templates;
-
+ d->list_dir_begin();
if (err == OK) {
- bool isdir;
- String c = d->get_next(&isdir);
+ String c = d->get_next();
while (c != String()) {
- if (isdir && !c.begins_with(".")) {
+ if (d->current_is_dir() && !c.begins_with(".")) {
templates.insert(c);
}
- c = d->get_next(&isdir);
+ c = d->get_next();
}
}
d->list_dir_end();
@@ -71,6 +69,15 @@ void ExportTemplateManager::_update_template_list() {
memdelete(d);
String current_version = VERSION_FULL_CONFIG;
+ // Downloadable export templates are only available for stable and official alpha/beta/RC builds
+ // (which always have a number following their status, e.g. "alpha1").
+ // Therefore, don't display download-related features when using a development version
+ // (whose builds aren't numbered).
+ const bool downloads_available =
+ String(VERSION_STATUS) != String("dev") &&
+ String(VERSION_STATUS) != String("alpha") &&
+ String(VERSION_STATUS) != String("beta") &&
+ String(VERSION_STATUS) != String("rc");
Label *current = memnew(Label);
current->set_h_size_flags(SIZE_EXPAND_FILL);
@@ -78,10 +85,14 @@ void ExportTemplateManager::_update_template_list() {
if (templates.has(current_version)) {
current->add_color_override("font_color", get_color("success_color", "Editor"));
- Button *redownload = memnew(Button);
- redownload->set_text(TTR("Re-Download"));
- current_hb->add_child(redownload);
- redownload->connect("pressed", this, "_download_template", varray(current_version));
+
+ // Only display a redownload button if it can be downloaded in the first place
+ if (downloads_available) {
+ Button *redownload = memnew(Button);
+ redownload->set_text(TTR("Redownload"));
+ current_hb->add_child(redownload);
+ redownload->connect("pressed", this, "_download_template", varray(current_version));
+ }
Button *uninstall = memnew(Button);
uninstall->set_text(TTR("Uninstall"));
@@ -93,6 +104,12 @@ void ExportTemplateManager::_update_template_list() {
current->add_color_override("font_color", get_color("error_color", "Editor"));
Button *redownload = memnew(Button);
redownload->set_text(TTR("Download"));
+
+ if (!downloads_available) {
+ redownload->set_disabled(true);
+ redownload->set_tooltip(TTR("Official export templates aren't available for development builds."));
+ }
+
redownload->connect("pressed", this, "_download_template", varray(current_version));
current_hb->add_child(redownload);
current->set_text(current_version + " " + TTR("(Missing)"));
@@ -144,42 +161,27 @@ void ExportTemplateManager::_uninstall_template(const String &p_version) {
void ExportTemplateManager::_uninstall_template_confirm() {
- DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
- Error err = d->change_dir(EditorSettings::get_singleton()->get_templates_dir());
-
- ERR_FAIL_COND(err != OK);
-
- err = d->change_dir(to_remove);
-
- ERR_FAIL_COND(err != OK);
-
- Vector<String> files;
-
- d->list_dir_begin();
-
- bool isdir;
- String c = d->get_next(&isdir);
- while (c != String()) {
- if (!isdir) {
- files.push_back(c);
- }
- c = d->get_next(&isdir);
- }
+ DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
+ const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir();
+ Error err = da->change_dir(templates_dir);
+ ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir + "'.");
+ err = da->change_dir(to_remove);
+ ERR_FAIL_COND_MSG(err != OK, "Could not access templates directory at '" + templates_dir.plus_file(to_remove) + "'.");
- d->list_dir_end();
-
- for (int i = 0; i < files.size(); i++) {
- d->remove(files[i]);
- }
+ err = da->erase_contents_recursive();
+ ERR_FAIL_COND_MSG(err != OK, "Could not remove all templates in '" + templates_dir.plus_file(to_remove) + "'.");
- d->change_dir("..");
- d->remove(to_remove);
+ da->change_dir("..");
+ err = da->remove(to_remove);
+ ERR_FAIL_COND_MSG(err != OK, "Could not remove templates directory at '" + templates_dir.plus_file(to_remove) + "'.");
_update_template_list();
}
bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_progress) {
+ // unzClose() will take care of closing the file stored in the unzFile,
+ // so we don't need to `memdelete(fa)` in this method.
FileAccess *fa = NULL;
zlib_filefunc_def io = zipio_create_io_from_file(&fa);
@@ -244,7 +246,7 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(version);
- DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
+ DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = d->make_dir_recursive(template_path);
if (err != OK) {
EditorNode::get_singleton()->show_warning(TTR("Error creating path for templates:") + "\n" + template_path);
@@ -252,8 +254,6 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
return false;
}
- memdelete(d);
-
ret = unzGoToFirstFile(pkg);
EditorProgress *p = NULL;
@@ -308,17 +308,20 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
p->step(TTR("Importing:") + " " + file, fc);
}
- FileAccess *f = FileAccess::open(template_path.plus_file(file), FileAccess::WRITE);
+ String to_write = template_path.plus_file(file);
+ FileAccessRef f = FileAccess::open(to_write, FileAccess::WRITE);
if (!f) {
ret = unzGoToNextFile(pkg);
fc++;
- ERR_CONTINUE(!f);
+ ERR_CONTINUE_MSG(true, "Can't open file from path '" + String(to_write) + "'.");
}
f->store_buffer(data.ptr(), data.size());
- memdelete(f);
+#ifndef WINDOWS_ENABLED
+ FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
+#endif
ret = unzGoToNextFile(pkg);
fc++;
@@ -331,7 +334,6 @@ bool ExportTemplateManager::_install_from_file(const String &p_file, bool p_use_
unzClose(pkg);
_update_template_list();
-
return true;
}
@@ -401,9 +403,7 @@ void ExportTemplateManager::_http_download_templates_completed(int p_status, int
} break;
case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
case HTTPRequest::RESULT_CONNECTION_ERROR:
- case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
- template_list_state->set_text(TTR("Can't connect."));
- } break;
+ case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
case HTTPRequest::RESULT_CANT_CONNECT: {
template_list_state->set_text(TTR("Can't connect."));
@@ -424,14 +424,16 @@ void ExportTemplateManager::_http_download_templates_completed(int p_status, int
String path = download_templates->get_download_file();
template_list_state->set_text(TTR("Download Complete."));
template_downloader->hide();
- int ret = _install_from_file(path, false);
+ bool ret = _install_from_file(path, false);
if (ret) {
- Error err = OS::get_singleton()->move_to_trash(path);
+ // Clean up downloaded file.
+ DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
+ Error err = da->remove(path);
if (err != OK) {
- EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + path + "\n");
+ EditorNode::get_singleton()->add_io_error(TTR("Cannot remove temporary file:") + "\n" + path + "\n");
}
} else {
- WARN_PRINTS(vformat(TTR("Templates installation failed. The problematic templates archives can be found at '%s'."), path));
+ EditorNode::get_singleton()->add_io_error(vformat(TTR("Templates installation failed.\nThe problematic templates archives can be found at '%s'."), path));
}
}
} break;
@@ -460,7 +462,7 @@ void ExportTemplateManager::_begin_template_download(const String &p_url) {
Error err = download_templates->request(p_url);
if (err != OK) {
- EditorNode::get_singleton()->show_warning(TTR("Error requesting url: ") + p_url);
+ EditorNode::get_singleton()->show_warning(TTR("Error requesting URL:") + " " + p_url);
return;
}
@@ -530,7 +532,6 @@ void ExportTemplateManager::_notification(int p_what) {
template_list_state->set_text(status);
if (errored) {
set_process(false);
- ;
}
}
@@ -541,6 +542,116 @@ void ExportTemplateManager::_notification(int p_what) {
}
}
+bool ExportTemplateManager::can_install_android_template() {
+
+ const String templates_dir = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
+ return FileAccess::exists(templates_dir.plus_file("android_source.zip")) &&
+ FileAccess::exists(templates_dir.plus_file("android_release.apk")) &&
+ FileAccess::exists(templates_dir.plus_file("android_debug.apk"));
+}
+
+Error ExportTemplateManager::install_android_template() {
+
+ // To support custom Android builds, we install the Java source code and buildsystem
+ // from android_source.zip to the project's res://android folder.
+
+ DirAccessRef da = DirAccess::open("res://");
+ ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
+
+ // Make res://android dir (if it does not exist).
+ da->make_dir("android");
+ {
+ // Add an empty .gdignore file to avoid scan.
+ FileAccessRef f = FileAccess::open("res://android/.gdignore", FileAccess::WRITE);
+ ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
+ f->store_line("");
+ f->close();
+ }
+ {
+ // Add version, to ensure building won't work if template and Godot version don't match.
+ FileAccessRef f = FileAccess::open("res://android/.build_version", FileAccess::WRITE);
+ ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
+ f->store_line(VERSION_FULL_CONFIG);
+ f->close();
+ }
+
+ Error err = da->make_dir_recursive("android/build");
+ ERR_FAIL_COND_V(err != OK, err);
+
+ // Uncompress source template.
+
+ const String &templates_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(VERSION_FULL_CONFIG);
+ const String &source_zip = templates_path.plus_file("android_source.zip");
+ ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);
+
+ FileAccess *src_f = NULL;
+ zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
+
+ unzFile pkg = unzOpen2(source_zip.utf8().get_data(), &io);
+ ERR_FAIL_COND_V_MSG(!pkg, ERR_CANT_OPEN, "Android sources not in ZIP format.");
+
+ int ret = unzGoToFirstFile(pkg);
+ int total_files = 0;
+ // Count files to unzip.
+ while (ret == UNZ_OK) {
+ total_files++;
+ ret = unzGoToNextFile(pkg);
+ }
+ ret = unzGoToFirstFile(pkg);
+
+ ProgressDialog::get_singleton()->add_task("uncompress_src", TTR("Uncompressing Android Build Sources"), total_files);
+
+ Set<String> dirs_tested;
+ int idx = 0;
+ while (ret == UNZ_OK) {
+
+ // Get file path.
+ unz_file_info info;
+ char fpath[16384];
+ ret = unzGetCurrentFileInfo(pkg, &info, fpath, 16384, NULL, 0, NULL, 0);
+
+ String path = fpath;
+ String base_dir = path.get_base_dir();
+
+ if (!path.ends_with("/")) {
+ Vector<uint8_t> data;
+ data.resize(info.uncompressed_size);
+
+ // Read.
+ unzOpenCurrentFile(pkg);
+ unzReadCurrentFile(pkg, data.ptrw(), data.size());
+ unzCloseCurrentFile(pkg);
+
+ if (!dirs_tested.has(base_dir)) {
+ da->make_dir_recursive(String("android/build").plus_file(base_dir));
+ dirs_tested.insert(base_dir);
+ }
+
+ String to_write = String("res://android/build").plus_file(path);
+ FileAccess *f = FileAccess::open(to_write, FileAccess::WRITE);
+ if (f) {
+ f->store_buffer(data.ptr(), data.size());
+ memdelete(f);
+#ifndef WINDOWS_ENABLED
+ FileAccess::set_unix_permissions(to_write, (info.external_fa >> 16) & 0x01FF);
+#endif
+ } else {
+ ERR_PRINTS("Can't uncompress file: " + to_write);
+ }
+ }
+
+ ProgressDialog::get_singleton()->task_step("uncompress_src", path, idx);
+
+ idx++;
+ ret = unzGoToNextFile(pkg);
+ }
+
+ ProgressDialog::get_singleton()->end_task("uncompress_src");
+ unzClose(pkg);
+
+ return OK;
+}
+
void ExportTemplateManager::_bind_methods() {
ClassDB::bind_method("_download_template", &ExportTemplateManager::_download_template);
@@ -579,7 +690,7 @@ ExportTemplateManager::ExportTemplateManager() {
remove_confirm->connect("confirmed", this, "_uninstall_template_confirm");
template_open = memnew(FileDialog);
- template_open->set_title(TTR("Select template file"));
+ template_open->set_title(TTR("Select Template File"));
template_open->add_filter("*.tpz ; Godot Export Templates");
template_open->set_access(FileDialog::ACCESS_FILESYSTEM);
template_open->set_mode(FileDialog::MODE_OPEN_FILE);