Browse Source

Downloaded templates from project manager can now be installed.

Juan Linietsky 9 years ago
parent
commit
31ebac2bfb

+ 26 - 0
tools/editor/asset_library_editor_plugin.cpp

@@ -444,6 +444,13 @@ void EditorAssetLibraryItemDownload::_close() {
 void EditorAssetLibraryItemDownload::_install() {
 void EditorAssetLibraryItemDownload::_install() {
 
 
 	String file = download->get_download_file();
 	String file = download->get_download_file();
+
+	if (external_install) {
+		emit_signal("install_asset",file,title->get_text());
+		return;
+	}
+
+
 	asset_installer->open(file,1);
 	asset_installer->open(file,1);
 }
 }
 
 
@@ -465,6 +472,8 @@ void EditorAssetLibraryItemDownload::_bind_methods() {
 	ObjectTypeDB::bind_method("_close",&EditorAssetLibraryItemDownload::_close);
 	ObjectTypeDB::bind_method("_close",&EditorAssetLibraryItemDownload::_close);
 	ObjectTypeDB::bind_method("_make_request",&EditorAssetLibraryItemDownload::_make_request);
 	ObjectTypeDB::bind_method("_make_request",&EditorAssetLibraryItemDownload::_make_request);
 
 
+	ADD_SIGNAL(MethodInfo("install_asset",PropertyInfo(Variant::STRING,"zip_path"),PropertyInfo(Variant::STRING,"name")));
+
 }
 }
 
 
 EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
 EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
@@ -530,6 +539,8 @@ EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
 
 
 	prev_status=-1;
 	prev_status=-1;
 
 
+	external_install=false;
+
 
 
 }
 }
 
 
@@ -611,6 +622,11 @@ void EditorAssetLibrary::_install_asset() {
 	downloads_hb->add_child(download);
 	downloads_hb->add_child(download);
 	download->configure(description->get_title(),description->get_asset_id(),description->get_preview_icon(),description->get_download_url(),description->get_sha256());
 	download->configure(description->get_title(),description->get_asset_id(),description->get_preview_icon(),description->get_download_url(),description->get_sha256());
 
 
+	if (templates_only) {
+		download->set_external_install(true);
+		download->connect("install_asset",this,"_install_external_asset");
+	}
+
 }
 }
 
 
 const char* EditorAssetLibrary::sort_key[SORT_MAX]={
 const char* EditorAssetLibrary::sort_key[SORT_MAX]={
@@ -1259,6 +1275,11 @@ void EditorAssetLibrary::_manage_plugins() {
 
 
 
 
 
 
+void EditorAssetLibrary::_install_external_asset(String p_zip_path,String p_title) {
+
+	emit_signal("install_asset",p_zip_path,p_title);
+}
+
 void EditorAssetLibrary::_bind_methods() {
 void EditorAssetLibrary::_bind_methods() {
 
 
 	ObjectTypeDB::bind_method("_http_request_completed",&EditorAssetLibrary::_http_request_completed);
 	ObjectTypeDB::bind_method("_http_request_completed",&EditorAssetLibrary::_http_request_completed);
@@ -1274,6 +1295,11 @@ void EditorAssetLibrary::_bind_methods() {
 	ObjectTypeDB::bind_method("_repository_changed",&EditorAssetLibrary::_repository_changed);
 	ObjectTypeDB::bind_method("_repository_changed",&EditorAssetLibrary::_repository_changed);
 	ObjectTypeDB::bind_method("_support_toggled",&EditorAssetLibrary::_support_toggled);
 	ObjectTypeDB::bind_method("_support_toggled",&EditorAssetLibrary::_support_toggled);
 	ObjectTypeDB::bind_method("_rerun_search",&EditorAssetLibrary::_rerun_search);
 	ObjectTypeDB::bind_method("_rerun_search",&EditorAssetLibrary::_rerun_search);
+	ObjectTypeDB::bind_method("_install_external_asset",&EditorAssetLibrary::_install_external_asset);
+
+
+
+	ADD_SIGNAL(MethodInfo("install_asset",PropertyInfo(Variant::STRING,"zip_path"),PropertyInfo(Variant::STRING,"name")));
 
 
 }
 }
 
 

+ 5 - 0
tools/editor/asset_library_editor_plugin.h

@@ -155,6 +155,8 @@ class EditorAssetLibraryItemDownload : public PanelContainer {
 
 
 	int asset_id;
 	int asset_id;
 
 
+	bool external_install;
+
 	EditorAssetInstaller *asset_installer;
 	EditorAssetInstaller *asset_installer;
 
 
 	void _close();
 	void _close();
@@ -168,6 +170,7 @@ protected:
 	static void _bind_methods();
 	static void _bind_methods();
 public:
 public:
 
 
+	void set_external_install(bool p_enable) { external_install=p_enable; }
 	int get_asset_id() { return asset_id; }
 	int get_asset_id() { return asset_id; }
 	void configure(const String& p_title,int p_asset_id,const Ref<Texture>& p_preview, const String& p_download_url, const String& p_sha256_hash);
 	void configure(const String& p_title,int p_asset_id,const Ref<Texture>& p_preview, const String& p_download_url, const String& p_sha256_hash);
 	EditorAssetLibraryItemDownload();
 	EditorAssetLibraryItemDownload();
@@ -301,6 +304,8 @@ class EditorAssetLibrary : public PanelContainer {
 	void _repository_changed(int p_repository_id);
 	void _repository_changed(int p_repository_id);
 	void _support_toggled(int p_support);
 	void _support_toggled(int p_support);
 
 
+	void _install_external_asset(String p_zip_path,String p_title);
+
 friend class EditorAssetLibraryItemDescription;
 friend class EditorAssetLibraryItemDescription;
 friend class EditorAssetLibraryItem;
 friend class EditorAssetLibraryItem;
 protected:
 protected:

+ 183 - 34
tools/editor/project_manager.cpp

@@ -49,17 +49,30 @@
 #include "editor_initialize_ssl.h"
 #include "editor_initialize_ssl.h"
 #include "editor_scale.h"
 #include "editor_scale.h"
 
 
+#include "io/zip_io.h"
+
 class NewProjectDialog : public ConfirmationDialog {
 class NewProjectDialog : public ConfirmationDialog {
 
 
 	OBJ_TYPE(NewProjectDialog,ConfirmationDialog);
 	OBJ_TYPE(NewProjectDialog,ConfirmationDialog);
 
 
+public:
+
+	enum Mode {
+		MODE_NEW,
+		MODE_IMPORT,
+		MODE_INSTALL
+	};
+private:
 
 
-	bool import_mode;
+	Mode mode;
 	Label *pp,*pn;
 	Label *pp,*pn;
 	Label *error;
 	Label *error;
 	LineEdit *project_path;
 	LineEdit *project_path;
 	LineEdit *project_name;
 	LineEdit *project_name;
 	FileDialog *fdialog;
 	FileDialog *fdialog;
+	String zip_path;
+	String zip_title;
+	AcceptDialog *dialog_error;
 
 
 	bool _test_path() {
 	bool _test_path() {
 
 
@@ -72,7 +85,7 @@ class NewProjectDialog : public ConfirmationDialog {
 			return false;
 			return false;
 		}
 		}
 
 
-		if (!import_mode) {
+		if (mode!=MODE_IMPORT) {
 
 
 			if (d->file_exists("engine.cfg")) {
 			if (d->file_exists("engine.cfg")) {
 
 
@@ -109,7 +122,7 @@ class NewProjectDialog : public ConfirmationDialog {
 			if (lidx!=-1) {
 			if (lidx!=-1) {
 				sp=sp.substr(lidx+1,sp.length());
 				sp=sp.substr(lidx+1,sp.length());
 			}
 			}
-			if (sp=="" && import_mode )
+			if (sp=="" && mode==MODE_IMPORT )
 				sp=TTR("Imported Project");
 				sp=TTR("Imported Project");
 
 
 			project_name->set_text(sp);
 			project_name->set_text(sp);
@@ -119,7 +132,7 @@ class NewProjectDialog : public ConfirmationDialog {
 	void _file_selected(const String& p_path) {
 	void _file_selected(const String& p_path) {
 
 
 		String p = p_path;
 		String p = p_path;
-		if (import_mode) {
+		if (mode==MODE_IMPORT) {
 			if (p.ends_with("engine.cfg")) {
 			if (p.ends_with("engine.cfg")) {
 
 
 				p=p.get_base_dir();
 				p=p.get_base_dir();
@@ -141,7 +154,7 @@ class NewProjectDialog : public ConfirmationDialog {
 
 
 	void _browse_path() {
 	void _browse_path() {
 
 
-		if (import_mode) {
+		if (mode==MODE_IMPORT) {
 
 
 			fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
 			fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
 			fdialog->clear_filters();
 			fdialog->clear_filters();
@@ -163,7 +176,7 @@ class NewProjectDialog : public ConfirmationDialog {
 
 
 		String dir;
 		String dir;
 
 
-		if (import_mode) {
+		if (mode==MODE_IMPORT) {
 			dir=project_path->get_text();
 			dir=project_path->get_text();
 
 
 
 
@@ -179,26 +192,130 @@ class NewProjectDialog : public ConfirmationDialog {
 			dir=d->get_current_dir();
 			dir=d->get_current_dir();
 			memdelete(d);
 			memdelete(d);
 
 
-			FileAccess *f = FileAccess::open(dir.plus_file("/engine.cfg"),FileAccess::WRITE);
-			if (!f) {
-				error->set_text(TTR("Couldn't create engine.cfg in project path."));
-			} else {
+			if (mode==MODE_NEW) {
+
+
+
+
+				FileAccess *f = FileAccess::open(dir.plus_file("/engine.cfg"),FileAccess::WRITE);
+				if (!f) {
+					error->set_text(TTR("Couldn't create engine.cfg in project path."));
+				} else {
+
+					f->store_line("; Engine configuration file.");
+					f->store_line("; It's best to edit using the editor UI, not directly,");
+					f->store_line("; becausethe parameters that go here are not obvious.");
+					f->store_line("; ");
+					f->store_line("; Format: ");
+					f->store_line(";   [section] ; section goes between []");
+					f->store_line(";   param=value ; assign values to parameters");
+					f->store_line("\n");
+					f->store_line("[application]");
+					f->store_line("name=\""+project_name->get_text()+"\"");
+					f->store_line("icon=\"res://icon.png\"");
+
+					memdelete(f);
+
+					ResourceSaver::save(dir.plus_file("/icon.png"),get_icon("DefaultProjectIcon","EditorIcons"));
+				}
+
+			} else if (mode==MODE_INSTALL) {
+
+
+				FileAccess *src_f=NULL;
+				zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
+
+				unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
+				if (!pkg) {
+
+					dialog_error->set_text("Error opening package file, not in zip format.");
+					return;
+				}
+
+				int ret = unzGoToFirstFile(pkg);
+
+				Vector<String> failed_files;
+
+				int idx=0;
+				while(ret==UNZ_OK) {
+
+					//get filename
+					unz_file_info info;
+					char fname[16384];
+					ret = unzGetCurrentFileInfo(pkg,&info,fname,16384,NULL,0,NULL,0);
+
+					String path=fname;
+
+					int depth=1; //stuff from github comes with tag
+					bool skip=false;
+					while(depth>0) {
+						int pp = path.find("/");
+						if (pp==-1) {
+							skip=true;
+							break;
+						}
+						path=path.substr(pp+1,path.length());
+						depth--;
+					}
+
+
+					if (skip || path==String()) {
+						//
+					} else if (path.ends_with("/")) { // a dir
+
+						path=path.substr(0,path.length()-1);
+
+						DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
+						da->make_dir(dir.plus_file(path));
+						memdelete(da);
+
+					} else {
+
+						Vector<uint8_t> data;
+						data.resize(info.uncompressed_size);
+
+						//read
+						unzOpenCurrentFile(pkg);
+						unzReadCurrentFile(pkg,data.ptr(),data.size());
+						unzCloseCurrentFile(pkg);
+
+						FileAccess *f=FileAccess::open(dir.plus_file(path),FileAccess::WRITE);
+
+						if (f) {
+							f->store_buffer(data.ptr(),data.size());
+							memdelete(f);
+						} else {
+							failed_files.push_back(path);
+						}
+
+
+					}
+
+					idx++;
+					ret = unzGoToNextFile(pkg);
+				}
+
+				unzClose(pkg);
+
+				if (failed_files.size()) {
+					String msg=TTR("The following files failed extraction from package:")+"\n\n";
+					for(int i=0;i<failed_files.size();i++) {
+
+						if (i>15) {
+							msg+="\nAnd "+itos(failed_files.size()-i)+" more files.";
+							break;
+						}
+						msg+=failed_files[i]+"\n";
+					}
+
+					dialog_error->set_text(msg);
+					dialog_error->popup_centered_minsize();
+
+				} else {
+					dialog_error->set_text(TTR("Package Installed Successfully!"));
+					dialog_error->popup_centered_minsize();
+				}
 
 
-				f->store_line("; Engine configuration file.");
-				f->store_line("; It's best to edit using the editor UI, not directly,");
-				f->store_line("; becausethe parameters that go here are not obvious.");
-				f->store_line("; ");
-				f->store_line("; Format: ");
-				f->store_line(";   [section] ; section goes between []");
-				f->store_line(";   param=value ; assign values to parameters");
-				f->store_line("\n");
-				f->store_line("[application]");
-				f->store_line("name=\""+project_name->get_text()+"\"");
-				f->store_line("icon=\"res://icon.png\"");
-
-				memdelete(f);
-
-				ResourceSaver::save(dir.plus_file("/icon.png"),get_icon("DefaultProjectIcon","EditorIcons"));
 			}
 			}
 
 
 
 
@@ -233,10 +350,16 @@ protected:
 
 
 public:
 public:
 
 
+	void set_zip_path(const String& p_path) {
+		zip_path=p_path;
+	}
+	void set_zip_title(const String& p_title) {
+		zip_title=p_title;
+	}
 
 
-	void set_import_mode(bool p_import ) {
+	void set_mode(Mode p_mode) {
 
 
-		import_mode=p_import;
+		mode=p_mode;
 	}
 	}
 
 
 	void show_dialog() {
 	void show_dialog() {
@@ -245,7 +368,7 @@ public:
 		project_path->clear();
 		project_path->clear();
 		project_name->clear();
 		project_name->clear();
 
 
-		if (import_mode) {
+		if (mode==MODE_IMPORT) {
 			set_title(TTR("Import Existing Project"));
 			set_title(TTR("Import Existing Project"));
 			get_ok()->set_text(TTR("Import"));
 			get_ok()->set_text(TTR("Import"));
 			pp->set_text(TTR("Project Path (Must Exist):"));
 			pp->set_text(TTR("Project Path (Must Exist):"));
@@ -253,9 +376,10 @@ public:
 			pn->hide();
 			pn->hide();
 			project_name->hide();
 			project_name->hide();
 
 
-			popup_centered(Size2(500,125));
+			popup_centered(Size2(500,125)*EDSCALE);
+
+		} else if (mode==MODE_NEW){
 
 
-		} else {
 			set_title(TTR("Create New Project"));
 			set_title(TTR("Create New Project"));
 			get_ok()->set_text(TTR("Create"));
 			get_ok()->set_text(TTR("Create"));
 			pp->set_text(TTR("Project Path:"));
 			pp->set_text(TTR("Project Path:"));
@@ -263,7 +387,16 @@ public:
 			pn->show();
 			pn->show();
 			project_name->show();
 			project_name->show();
 
 
-			popup_centered(Size2(500,145));
+			popup_centered(Size2(500,145)*EDSCALE);
+		} else if (mode==MODE_INSTALL){
+
+			set_title(TTR("Install Project: ")+zip_title);
+			get_ok()->set_text(TTR("Install"));
+			pp->set_text(TTR("Project Path:"));
+			pn->hide();
+			project_name->hide();
+
+			popup_centered(Size2(500,125)*EDSCALE);
 
 
 		}
 		}
 
 
@@ -329,7 +462,10 @@ public:
 		fdialog->connect("dir_selected", this,"_path_selected");
 		fdialog->connect("dir_selected", this,"_path_selected");
 		fdialog->connect("file_selected", this,"_file_selected");
 		fdialog->connect("file_selected", this,"_file_selected");
 		set_hide_on_ok(false);
 		set_hide_on_ok(false);
-		import_mode=false;
+		mode=MODE_NEW;
+
+		dialog_error = memnew( AcceptDialog );
+		add_child(dialog_error);
 	}
 	}
 
 
 
 
@@ -616,6 +752,8 @@ void ProjectManager::_load_recent_projects() {
 	run_btn->set_disabled(selected_list.size()<1 || (selected_list.size()==1 && single_selected_main==""));
 	run_btn->set_disabled(selected_list.size()<1 || (selected_list.size()==1 && single_selected_main==""));
 
 
 	EditorSettings::get_singleton()->save();
 	EditorSettings::get_singleton()->save();
+
+	tabs->set_current_tab(0);
 }
 }
 
 
 void ProjectManager::_open_project_confirm() {
 void ProjectManager::_open_project_confirm() {
@@ -755,14 +893,14 @@ void ProjectManager::_scan_projects() {
 
 
 void ProjectManager::_new_project()  {
 void ProjectManager::_new_project()  {
 
 
-	npdialog->set_import_mode(false);
+	npdialog->set_mode(NewProjectDialog::MODE_NEW);
 	npdialog->show_dialog();
 	npdialog->show_dialog();
 }
 }
 
 
 
 
 void ProjectManager::_import_project()  {
 void ProjectManager::_import_project()  {
 
 
-	npdialog->set_import_mode(true);
+	npdialog->set_mode(NewProjectDialog::MODE_IMPORT);
 	npdialog->show_dialog();
 	npdialog->show_dialog();
 }
 }
 
 
@@ -800,6 +938,15 @@ void ProjectManager::_exit_dialog()  {
 	get_tree()->quit();
 	get_tree()->quit();
 }
 }
 
 
+
+void ProjectManager::_install_project(const String& p_zip_path,const String& p_title) {
+
+	npdialog->set_mode(NewProjectDialog::MODE_INSTALL);
+	npdialog->set_zip_path(p_zip_path);
+	npdialog->set_zip_title(p_title);
+	npdialog->show_dialog();
+}
+
 void ProjectManager::_bind_methods() {
 void ProjectManager::_bind_methods() {
 
 
 	ObjectTypeDB::bind_method("_open_project",&ProjectManager::_open_project);
 	ObjectTypeDB::bind_method("_open_project",&ProjectManager::_open_project);
@@ -817,6 +964,7 @@ void ProjectManager::_bind_methods() {
 	ObjectTypeDB::bind_method("_panel_draw",&ProjectManager::_panel_draw);
 	ObjectTypeDB::bind_method("_panel_draw",&ProjectManager::_panel_draw);
 	ObjectTypeDB::bind_method("_panel_input",&ProjectManager::_panel_input);
 	ObjectTypeDB::bind_method("_panel_input",&ProjectManager::_panel_input);
 	ObjectTypeDB::bind_method("_favorite_pressed",&ProjectManager::_favorite_pressed);
 	ObjectTypeDB::bind_method("_favorite_pressed",&ProjectManager::_favorite_pressed);
+	ObjectTypeDB::bind_method("_install_project",&ProjectManager::_install_project);
 
 
 
 
 }
 }
@@ -1016,6 +1164,7 @@ ProjectManager::ProjectManager() {
 	gui_base->add_child(multi_run_ask);
 	gui_base->add_child(multi_run_ask);
 
 
 
 
+	asset_library->connect("install_asset",this,"_install_project");
 
 
 	OS::get_singleton()->set_low_processor_usage_mode(true);
 	OS::get_singleton()->set_low_processor_usage_mode(true);
 
 

+ 2 - 0
tools/editor/project_manager.h

@@ -88,6 +88,8 @@ class ProjectManager : public Control {
 	void _load_recent_projects();
 	void _load_recent_projects();
 	void _scan_dir(DirAccess *da,float pos, float total,List<String> *r_projects);
 	void _scan_dir(DirAccess *da,float pos, float total,List<String> *r_projects);
 
 
+	void _install_project(const String& p_zip_path,const String& p_title);
+
 	void _panel_draw(Node *p_hb);
 	void _panel_draw(Node *p_hb);
 	void _panel_input(const InputEvent& p_ev,Node *p_hb);
 	void _panel_input(const InputEvent& p_ev,Node *p_hb);
 	void _favorite_pressed(Node *p_hb);
 	void _favorite_pressed(Node *p_hb);