project_dialog.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /**************************************************************************/
  2. /* project_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "project_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/zip_io.h"
  34. #include "core/version.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_vcs_interface.h"
  38. #include "editor/gui/editor_file_dialog.h"
  39. #include "editor/themes/editor_icons.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/gui/check_box.h"
  42. #include "scene/gui/check_button.h"
  43. #include "scene/gui/line_edit.h"
  44. #include "scene/gui/option_button.h"
  45. #include "scene/gui/separator.h"
  46. #include "scene/gui/texture_rect.h"
  47. void ProjectDialog::_set_message(const String &p_msg, MessageType p_type, InputType p_input_type) {
  48. msg->set_text(p_msg);
  49. get_ok_button()->set_disabled(p_type == MESSAGE_ERROR);
  50. Ref<Texture2D> new_icon;
  51. switch (p_type) {
  52. case MESSAGE_ERROR: {
  53. msg->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  54. new_icon = get_editor_theme_icon(SNAME("StatusError"));
  55. } break;
  56. case MESSAGE_WARNING: {
  57. msg->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  58. new_icon = get_editor_theme_icon(SNAME("StatusWarning"));
  59. } break;
  60. case MESSAGE_SUCCESS: {
  61. msg->add_theme_color_override("font_color", get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
  62. new_icon = get_editor_theme_icon(SNAME("StatusSuccess"));
  63. } break;
  64. }
  65. if (p_input_type == PROJECT_PATH) {
  66. project_status_rect->set_texture(new_icon);
  67. } else if (p_input_type == INSTALL_PATH) {
  68. install_status_rect->set_texture(new_icon);
  69. }
  70. }
  71. static bool is_zip_file(Ref<DirAccess> p_d, const String &p_path) {
  72. return p_path.get_extension() == "zip" && p_d->file_exists(p_path);
  73. }
  74. void ProjectDialog::_validate_path() {
  75. _set_message("", MESSAGE_SUCCESS, PROJECT_PATH);
  76. _set_message("", MESSAGE_SUCCESS, INSTALL_PATH);
  77. if (project_name->get_text().strip_edges().is_empty()) {
  78. _set_message(TTR("It would be a good idea to name your project."), MESSAGE_ERROR);
  79. return;
  80. }
  81. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  82. String path = project_path->get_text().simplify_path();
  83. String target_path = path;
  84. InputType target_path_input_type = PROJECT_PATH;
  85. if (mode == MODE_IMPORT) {
  86. if (path.get_file().strip_edges() == "project.godot") {
  87. path = path.get_base_dir();
  88. project_path->set_text(path);
  89. }
  90. if (is_zip_file(d, path)) {
  91. zip_path = path;
  92. } else if (is_zip_file(d, path.strip_edges())) {
  93. zip_path = path.strip_edges();
  94. } else {
  95. zip_path = "";
  96. }
  97. if (!zip_path.is_empty()) {
  98. target_path = install_path->get_text().simplify_path();
  99. target_path_input_type = INSTALL_PATH;
  100. create_dir->show();
  101. install_path_container->show();
  102. Ref<FileAccess> io_fa;
  103. zlib_filefunc_def io = zipio_create_io(&io_fa);
  104. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  105. if (!pkg) {
  106. _set_message(TTR("Invalid \".zip\" project file; it is not in ZIP format."), MESSAGE_ERROR);
  107. unzClose(pkg);
  108. return;
  109. }
  110. int ret = unzGoToFirstFile(pkg);
  111. while (ret == UNZ_OK) {
  112. unz_file_info info;
  113. char fname[16384];
  114. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  115. ERR_FAIL_COND_MSG(ret != UNZ_OK, "Failed to get current file info.");
  116. String name = String::utf8(fname);
  117. if (name.get_file() == "project.godot") {
  118. break; // ret == UNZ_OK.
  119. }
  120. ret = unzGoToNextFile(pkg);
  121. }
  122. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  123. _set_message(TTR("Invalid \".zip\" project file; it doesn't contain a \"project.godot\" file."), MESSAGE_ERROR);
  124. unzClose(pkg);
  125. return;
  126. }
  127. unzClose(pkg);
  128. } else if (d->dir_exists(path) && d->file_exists(path.path_join("project.godot"))) {
  129. zip_path = "";
  130. create_dir->hide();
  131. install_path_container->hide();
  132. _set_message(TTR("Valid project found at path."), MESSAGE_SUCCESS);
  133. } else {
  134. create_dir->hide();
  135. install_path_container->hide();
  136. _set_message(TTR("Please choose a \"project.godot\", a directory with one, or a \".zip\" file."), MESSAGE_ERROR);
  137. return;
  138. }
  139. }
  140. if (target_path.is_empty() || target_path.is_relative_path()) {
  141. _set_message(TTR("The path specified is invalid."), MESSAGE_ERROR, target_path_input_type);
  142. return;
  143. }
  144. if (target_path.get_file() != OS::get_singleton()->get_safe_dir_name(target_path.get_file())) {
  145. _set_message(TTR("The directory name specified contains invalid characters or trailing whitespace."), MESSAGE_ERROR, target_path_input_type);
  146. return;
  147. }
  148. String working_dir = d->get_current_dir();
  149. String executable_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  150. if (target_path == working_dir || target_path == executable_dir) {
  151. _set_message(TTR("Creating a project at the engine's working directory or executable directory is not allowed, as it would prevent the project manager from starting."), MESSAGE_ERROR, target_path_input_type);
  152. return;
  153. }
  154. // TODO: The following 5 lines could be simplified if OS.get_user_home_dir() or SYSTEM_DIR_HOME is implemented. See: https://github.com/godotengine/godot-proposals/issues/4851.
  155. #ifdef WINDOWS_ENABLED
  156. String home_dir = OS::get_singleton()->get_environment("USERPROFILE");
  157. #else
  158. String home_dir = OS::get_singleton()->get_environment("HOME");
  159. #endif
  160. String documents_dir = OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DOCUMENTS);
  161. if (target_path == home_dir || target_path == documents_dir) {
  162. _set_message(TTR("You cannot save a project at the selected path. Please create a subfolder or choose a new path."), MESSAGE_ERROR, target_path_input_type);
  163. return;
  164. }
  165. is_folder_empty = true;
  166. if (mode == MODE_NEW || mode == MODE_INSTALL || (mode == MODE_IMPORT && target_path_input_type == InputType::INSTALL_PATH)) {
  167. if (create_dir->is_pressed()) {
  168. if (!d->dir_exists(target_path.get_base_dir())) {
  169. _set_message(TTR("The parent directory of the path specified doesn't exist."), MESSAGE_ERROR, target_path_input_type);
  170. return;
  171. }
  172. if (d->dir_exists(target_path)) {
  173. // The path is not necessarily empty here, but we will update the message later if it isn't.
  174. _set_message(TTR("The project folder already exists and is empty."), MESSAGE_SUCCESS, target_path_input_type);
  175. } else {
  176. _set_message(TTR("The project folder will be automatically created."), MESSAGE_SUCCESS, target_path_input_type);
  177. }
  178. } else {
  179. if (!d->dir_exists(target_path)) {
  180. _set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR, target_path_input_type);
  181. return;
  182. }
  183. // The path is not necessarily empty here, but we will update the message later if it isn't.
  184. _set_message(TTR("The project folder exists and is empty."), MESSAGE_SUCCESS, target_path_input_type);
  185. }
  186. // Check if the directory is empty. Not an error, but we want to warn the user.
  187. if (d->change_dir(target_path) == OK) {
  188. d->list_dir_begin();
  189. String n = d->get_next();
  190. while (!n.is_empty()) {
  191. if (n[0] != '.') {
  192. // Allow `.`, `..` (reserved current/parent folder names)
  193. // and hidden files/folders to be present.
  194. // For instance, this lets users initialize a Git repository
  195. // and still be able to create a project in the directory afterwards.
  196. is_folder_empty = false;
  197. break;
  198. }
  199. n = d->get_next();
  200. }
  201. d->list_dir_end();
  202. if (!is_folder_empty) {
  203. _set_message(TTR("The selected path is not empty. Choosing an empty folder is highly recommended."), MESSAGE_WARNING, target_path_input_type);
  204. }
  205. }
  206. }
  207. }
  208. String ProjectDialog::_get_target_path() {
  209. if (mode == MODE_NEW || mode == MODE_INSTALL) {
  210. return project_path->get_text();
  211. } else if (mode == MODE_IMPORT) {
  212. return install_path->get_text();
  213. } else {
  214. ERR_FAIL_V("");
  215. }
  216. }
  217. void ProjectDialog::_set_target_path(const String &p_text) {
  218. if (mode == MODE_NEW || mode == MODE_INSTALL) {
  219. project_path->set_text(p_text);
  220. } else if (mode == MODE_IMPORT) {
  221. install_path->set_text(p_text);
  222. } else {
  223. ERR_FAIL();
  224. }
  225. }
  226. void ProjectDialog::_update_target_auto_dir() {
  227. String new_auto_dir;
  228. if (mode == MODE_NEW || mode == MODE_INSTALL) {
  229. new_auto_dir = project_name->get_text();
  230. } else if (mode == MODE_IMPORT) {
  231. new_auto_dir = project_path->get_text().get_file().get_basename();
  232. }
  233. int naming_convention = (int)EDITOR_GET("project_manager/directory_naming_convention");
  234. switch (naming_convention) {
  235. case 0: // No convention
  236. break;
  237. case 1: // kebab-case
  238. new_auto_dir = new_auto_dir.to_lower().replace(" ", "-");
  239. break;
  240. case 2: // snake_case
  241. new_auto_dir = new_auto_dir.to_snake_case();
  242. break;
  243. case 3: // camelCase
  244. new_auto_dir = new_auto_dir.to_camel_case();
  245. break;
  246. case 4: // PascalCase
  247. new_auto_dir = new_auto_dir.to_pascal_case();
  248. break;
  249. case 5: // Title Case
  250. new_auto_dir = new_auto_dir.capitalize();
  251. break;
  252. default:
  253. ERR_FAIL_MSG("Invalid directory naming convention.");
  254. break;
  255. }
  256. new_auto_dir = OS::get_singleton()->get_safe_dir_name(new_auto_dir);
  257. if (create_dir->is_pressed()) {
  258. String target_path = _get_target_path();
  259. if (target_path.get_file() == auto_dir) {
  260. // Update target dir name to new project name / ZIP name.
  261. target_path = target_path.get_base_dir().path_join(new_auto_dir);
  262. }
  263. _set_target_path(target_path);
  264. }
  265. auto_dir = new_auto_dir;
  266. }
  267. void ProjectDialog::_create_dir_toggled(bool p_pressed) {
  268. String target_path = _get_target_path();
  269. if (create_dir->is_pressed()) {
  270. // (Re-)append target dir name.
  271. if (last_custom_target_dir.is_empty()) {
  272. target_path = target_path.path_join(auto_dir);
  273. } else {
  274. target_path = target_path.path_join(last_custom_target_dir);
  275. }
  276. } else {
  277. // Save and remove target dir name.
  278. if (target_path.get_file() == auto_dir) {
  279. last_custom_target_dir = "";
  280. } else {
  281. last_custom_target_dir = target_path.get_file();
  282. }
  283. target_path = target_path.get_base_dir();
  284. }
  285. _set_target_path(target_path);
  286. _validate_path();
  287. }
  288. void ProjectDialog::_project_name_changed() {
  289. if (mode == MODE_NEW || mode == MODE_INSTALL) {
  290. _update_target_auto_dir();
  291. }
  292. _validate_path();
  293. }
  294. void ProjectDialog::_project_path_changed() {
  295. if (mode == MODE_IMPORT) {
  296. _update_target_auto_dir();
  297. }
  298. _validate_path();
  299. }
  300. void ProjectDialog::_install_path_changed() {
  301. _validate_path();
  302. }
  303. void ProjectDialog::_browse_project_path() {
  304. if (mode == MODE_IMPORT && install_path->is_visible_in_tree()) {
  305. // Select last ZIP file.
  306. fdialog_project->set_current_path(project_path->get_text());
  307. } else if ((mode == MODE_NEW || mode == MODE_INSTALL) && create_dir->is_pressed()) {
  308. // Select parent directory of project path.
  309. fdialog_project->set_current_dir(project_path->get_text().get_base_dir());
  310. } else {
  311. // Select project path.
  312. fdialog_project->set_current_dir(project_path->get_text());
  313. }
  314. if (mode == MODE_IMPORT) {
  315. fdialog_project->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_ANY);
  316. fdialog_project->clear_filters();
  317. fdialog_project->add_filter("project.godot", vformat("%s %s", VERSION_NAME, TTR("Project")));
  318. fdialog_project->add_filter("*.zip", TTR("ZIP File"));
  319. } else {
  320. fdialog_project->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  321. }
  322. fdialog_project->popup_file_dialog();
  323. }
  324. void ProjectDialog::_browse_install_path() {
  325. ERR_FAIL_COND_MSG(mode != MODE_IMPORT, "Install path is only used for MODE_IMPORT.");
  326. if (create_dir->is_pressed()) {
  327. // Select parent directory of install path.
  328. fdialog_install->set_current_dir(install_path->get_text().get_base_dir());
  329. } else {
  330. // Select install path.
  331. fdialog_install->set_current_dir(install_path->get_text());
  332. }
  333. fdialog_install->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  334. fdialog_install->popup_file_dialog();
  335. }
  336. void ProjectDialog::_project_path_selected(const String &p_path) {
  337. if (create_dir->is_pressed() && (mode == MODE_NEW || mode == MODE_INSTALL)) {
  338. // Replace parent directory, but keep target dir name.
  339. project_path->set_text(p_path.path_join(project_path->get_text().get_file()));
  340. } else {
  341. project_path->set_text(p_path);
  342. }
  343. _project_path_changed();
  344. if (install_path->is_visible_in_tree()) {
  345. // ZIP is selected; focus install path.
  346. install_path->grab_focus();
  347. } else {
  348. get_ok_button()->grab_focus();
  349. }
  350. }
  351. void ProjectDialog::_install_path_selected(const String &p_path) {
  352. ERR_FAIL_COND_MSG(mode != MODE_IMPORT, "Install path is only used for MODE_IMPORT.");
  353. if (create_dir->is_pressed()) {
  354. // Replace parent directory, but keep target dir name.
  355. install_path->set_text(p_path.path_join(install_path->get_text().get_file()));
  356. } else {
  357. install_path->set_text(p_path);
  358. }
  359. _install_path_changed();
  360. get_ok_button()->grab_focus();
  361. }
  362. void ProjectDialog::_renderer_selected() {
  363. ERR_FAIL_NULL(renderer_button_group->get_pressed_button());
  364. String renderer_type = renderer_button_group->get_pressed_button()->get_meta(SNAME("rendering_method"));
  365. if (renderer_type == "forward_plus") {
  366. renderer_info->set_text(
  367. String::utf8("• ") + TTR("Supports desktop platforms only.") +
  368. String::utf8("\n• ") + TTR("Advanced 3D graphics available.") +
  369. String::utf8("\n• ") + TTR("Can scale to large complex scenes.") +
  370. String::utf8("\n• ") + TTR("Uses RenderingDevice backend.") +
  371. String::utf8("\n• ") + TTR("Slower rendering of simple scenes."));
  372. } else if (renderer_type == "mobile") {
  373. renderer_info->set_text(
  374. String::utf8("• ") + TTR("Supports desktop + mobile platforms.") +
  375. String::utf8("\n• ") + TTR("Less advanced 3D graphics.") +
  376. String::utf8("\n• ") + TTR("Less scalable for complex scenes.") +
  377. String::utf8("\n• ") + TTR("Uses RenderingDevice backend.") +
  378. String::utf8("\n• ") + TTR("Fast rendering of simple scenes."));
  379. } else if (renderer_type == "gl_compatibility") {
  380. renderer_info->set_text(
  381. String::utf8("• ") + TTR("Supports desktop, mobile + web platforms.") +
  382. String::utf8("\n• ") + TTR("Least advanced 3D graphics (currently work-in-progress).") +
  383. String::utf8("\n• ") + TTR("Intended for low-end/older devices.") +
  384. String::utf8("\n• ") + TTR("Uses OpenGL 3 backend (OpenGL 3.3/ES 3.0/WebGL2).") +
  385. String::utf8("\n• ") + TTR("Fastest rendering of simple scenes."));
  386. } else {
  387. WARN_PRINT("Unknown renderer type. Please report this as a bug on GitHub.");
  388. }
  389. }
  390. void ProjectDialog::_nonempty_confirmation_ok_pressed() {
  391. is_folder_empty = true;
  392. ok_pressed();
  393. }
  394. void ProjectDialog::ok_pressed() {
  395. // Before we create a project, check that the target folder is empty.
  396. // If not, we need to ask the user if they're sure they want to do this.
  397. if (!is_folder_empty) {
  398. ConfirmationDialog *cd = memnew(ConfirmationDialog);
  399. cd->set_title(TTR("Warning: This folder is not empty"));
  400. cd->set_text(TTR("You are about to create a Godot project in a non-empty folder.\nThe entire contents of this folder will be imported as project resources!\n\nAre you sure you wish to continue?"));
  401. cd->get_ok_button()->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_nonempty_confirmation_ok_pressed));
  402. get_parent()->add_child(cd);
  403. cd->popup_centered();
  404. return;
  405. }
  406. String path = project_path->get_text();
  407. if (mode == MODE_NEW) {
  408. if (create_dir->is_pressed()) {
  409. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  410. if (!d->dir_exists(path) && d->make_dir(path) != OK) {
  411. _set_message(TTR("Couldn't create project directory, check permissions."), MESSAGE_ERROR);
  412. return;
  413. }
  414. }
  415. PackedStringArray project_features = ProjectSettings::get_required_features();
  416. ProjectSettings::CustomMap initial_settings;
  417. // Be sure to change this code if/when renderers are changed.
  418. // Default values are "forward_plus" for the main setting, "mobile" for the mobile override,
  419. // and "gl_compatibility" for the web override.
  420. String renderer_type = renderer_button_group->get_pressed_button()->get_meta(SNAME("rendering_method"));
  421. initial_settings["rendering/renderer/rendering_method"] = renderer_type;
  422. EditorSettings::get_singleton()->set("project_manager/default_renderer", renderer_type);
  423. EditorSettings::get_singleton()->save();
  424. if (renderer_type == "forward_plus") {
  425. project_features.push_back("Forward Plus");
  426. } else if (renderer_type == "mobile") {
  427. project_features.push_back("Mobile");
  428. } else if (renderer_type == "gl_compatibility") {
  429. project_features.push_back("GL Compatibility");
  430. // Also change the default rendering method for the mobile override.
  431. initial_settings["rendering/renderer/rendering_method.mobile"] = "gl_compatibility";
  432. } else {
  433. WARN_PRINT("Unknown renderer type. Please report this as a bug on GitHub.");
  434. }
  435. project_features.sort();
  436. initial_settings["application/config/features"] = project_features;
  437. initial_settings["application/config/name"] = project_name->get_text().strip_edges();
  438. initial_settings["application/config/icon"] = "res://icon.svg";
  439. Error err = ProjectSettings::get_singleton()->save_custom(path.path_join("project.godot"), initial_settings, Vector<String>(), false);
  440. if (err != OK) {
  441. _set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  442. return;
  443. }
  444. // Store default project icon in SVG format.
  445. Ref<FileAccess> fa_icon = FileAccess::open(path.path_join("icon.svg"), FileAccess::WRITE, &err);
  446. if (err != OK) {
  447. _set_message(TTR("Couldn't create icon.svg in project path."), MESSAGE_ERROR);
  448. return;
  449. }
  450. fa_icon->store_string(get_default_project_icon());
  451. EditorVCSInterface::create_vcs_metadata_files(EditorVCSInterface::VCSMetadata(vcs_metadata_selection->get_selected()), path);
  452. }
  453. // Two cases for importing a ZIP.
  454. switch (mode) {
  455. case MODE_IMPORT: {
  456. if (zip_path.is_empty()) {
  457. break;
  458. }
  459. path = install_path->get_text().simplify_path();
  460. [[fallthrough]];
  461. }
  462. case MODE_INSTALL: {
  463. ERR_FAIL_COND(zip_path.is_empty());
  464. Ref<FileAccess> io_fa;
  465. zlib_filefunc_def io = zipio_create_io(&io_fa);
  466. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  467. if (!pkg) {
  468. dialog_error->set_text(TTR("Error opening package file, not in ZIP format."));
  469. dialog_error->popup_centered();
  470. return;
  471. }
  472. // Find the first directory with a "project.godot".
  473. String zip_root;
  474. int ret = unzGoToFirstFile(pkg);
  475. while (ret == UNZ_OK) {
  476. unz_file_info info;
  477. char fname[16384];
  478. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  479. ERR_FAIL_COND_MSG(ret != UNZ_OK, "Failed to get current file info.");
  480. String name = String::utf8(fname);
  481. if (name.get_file() == "project.godot") {
  482. zip_root = name.get_base_dir();
  483. break;
  484. }
  485. ret = unzGoToNextFile(pkg);
  486. }
  487. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  488. _set_message(TTR("Invalid \".zip\" project file; it doesn't contain a \"project.godot\" file."), MESSAGE_ERROR);
  489. unzClose(pkg);
  490. return;
  491. }
  492. if (create_dir->is_pressed()) {
  493. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  494. if (!d->dir_exists(path) && d->make_dir(path) != OK) {
  495. _set_message(TTR("Couldn't create project directory, check permissions."), MESSAGE_ERROR);
  496. return;
  497. }
  498. }
  499. ret = unzGoToFirstFile(pkg);
  500. Vector<String> failed_files;
  501. while (ret == UNZ_OK) {
  502. //get filename
  503. unz_file_info info;
  504. char fname[16384];
  505. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  506. ERR_FAIL_COND_MSG(ret != UNZ_OK, "Failed to get current file info.");
  507. String rel_path = String::utf8(fname).trim_prefix(zip_root);
  508. if (rel_path.is_empty()) { // Root.
  509. } else if (rel_path.ends_with("/")) { // Directory.
  510. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  511. da->make_dir(path.path_join(rel_path));
  512. } else { // File.
  513. Vector<uint8_t> uncomp_data;
  514. uncomp_data.resize(info.uncompressed_size);
  515. unzOpenCurrentFile(pkg);
  516. ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
  517. ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", rel_path));
  518. unzCloseCurrentFile(pkg);
  519. Ref<FileAccess> f = FileAccess::open(path.path_join(rel_path), FileAccess::WRITE);
  520. if (f.is_valid()) {
  521. f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
  522. } else {
  523. failed_files.push_back(rel_path);
  524. }
  525. }
  526. ret = unzGoToNextFile(pkg);
  527. }
  528. unzClose(pkg);
  529. if (failed_files.size()) {
  530. String err_msg = TTR("The following files failed extraction from package:") + "\n\n";
  531. for (int i = 0; i < failed_files.size(); i++) {
  532. if (i > 15) {
  533. err_msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  534. break;
  535. }
  536. err_msg += failed_files[i] + "\n";
  537. }
  538. dialog_error->set_text(err_msg);
  539. dialog_error->popup_centered();
  540. return;
  541. }
  542. } break;
  543. default: {
  544. } break;
  545. }
  546. if (mode == MODE_RENAME || mode == MODE_INSTALL) {
  547. // Load project.godot as ConfigFile to set the new name.
  548. ConfigFile cfg;
  549. String project_godot = path.path_join("project.godot");
  550. Error err = cfg.load(project_godot);
  551. if (err != OK) {
  552. dialog_error->set_text(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err));
  553. dialog_error->popup_centered();
  554. return;
  555. }
  556. cfg.set_value("application", "config/name", project_name->get_text().strip_edges());
  557. err = cfg.save(project_godot);
  558. if (err != OK) {
  559. dialog_error->set_text(vformat(TTR("Couldn't save project at '%s' (error %d)."), project_godot, err));
  560. dialog_error->popup_centered();
  561. return;
  562. }
  563. }
  564. hide();
  565. if (mode == MODE_NEW || mode == MODE_IMPORT || mode == MODE_INSTALL) {
  566. emit_signal(SNAME("project_created"), path);
  567. } else if (mode == MODE_RENAME) {
  568. emit_signal(SNAME("projects_updated"));
  569. }
  570. }
  571. void ProjectDialog::set_zip_path(const String &p_path) {
  572. zip_path = p_path;
  573. }
  574. void ProjectDialog::set_zip_title(const String &p_title) {
  575. zip_title = p_title;
  576. }
  577. void ProjectDialog::set_mode(Mode p_mode) {
  578. mode = p_mode;
  579. }
  580. void ProjectDialog::set_project_name(const String &p_name) {
  581. project_name->set_text(p_name);
  582. }
  583. void ProjectDialog::set_project_path(const String &p_path) {
  584. project_path->set_text(p_path);
  585. }
  586. void ProjectDialog::ask_for_path_and_show() {
  587. // Workaround: for the file selection dialog content to be rendered we need to show its parent dialog.
  588. show_dialog();
  589. _browse_project_path();
  590. }
  591. void ProjectDialog::show_dialog() {
  592. if (mode == MODE_RENAME) {
  593. // Name and path are set in `ProjectManager::_rename_project`.
  594. project_path->set_editable(false);
  595. set_title(TTR("Rename Project"));
  596. set_ok_button_text(TTR("Rename"));
  597. create_dir->hide();
  598. project_status_rect->hide();
  599. project_browse->hide();
  600. name_container->show();
  601. install_path_container->hide();
  602. renderer_container->hide();
  603. default_files_container->hide();
  604. callable_mp((Control *)project_name, &Control::grab_focus).call_deferred();
  605. callable_mp(project_name, &LineEdit::select_all).call_deferred();
  606. } else {
  607. String proj = TTR("New Game Project");
  608. project_name->set_text(proj);
  609. project_path->set_editable(true);
  610. String fav_dir = EDITOR_GET("filesystem/directories/default_project_path");
  611. if (!fav_dir.is_empty()) {
  612. project_path->set_text(fav_dir);
  613. install_path->set_text(fav_dir);
  614. fdialog_project->set_current_dir(fav_dir);
  615. } else {
  616. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  617. project_path->set_text(d->get_current_dir());
  618. install_path->set_text(d->get_current_dir());
  619. fdialog_project->set_current_dir(d->get_current_dir());
  620. }
  621. create_dir->show();
  622. project_status_rect->show();
  623. project_browse->show();
  624. if (mode == MODE_IMPORT) {
  625. set_title(TTR("Import Existing Project"));
  626. set_ok_button_text(TTR("Import & Edit"));
  627. name_container->hide();
  628. install_path_container->hide();
  629. renderer_container->hide();
  630. default_files_container->hide();
  631. // Project path dialog is also opened; no need to change focus.
  632. } else if (mode == MODE_NEW) {
  633. set_title(TTR("Create New Project"));
  634. set_ok_button_text(TTR("Create & Edit"));
  635. name_container->show();
  636. install_path_container->hide();
  637. renderer_container->show();
  638. default_files_container->show();
  639. callable_mp((Control *)project_name, &Control::grab_focus).call_deferred();
  640. callable_mp(project_name, &LineEdit::select_all).call_deferred();
  641. } else if (mode == MODE_INSTALL) {
  642. set_title(TTR("Install Project:") + " " + zip_title);
  643. set_ok_button_text(TTR("Install & Edit"));
  644. project_name->set_text(zip_title);
  645. name_container->show();
  646. install_path_container->hide();
  647. renderer_container->hide();
  648. default_files_container->hide();
  649. callable_mp((Control *)project_path, &Control::grab_focus).call_deferred();
  650. }
  651. auto_dir = "";
  652. last_custom_target_dir = "";
  653. _update_target_auto_dir();
  654. if (create_dir->is_pressed()) {
  655. // Append `auto_dir` to target path.
  656. _create_dir_toggled(true);
  657. }
  658. }
  659. _validate_path();
  660. popup_centered(Size2(500, 0) * EDSCALE);
  661. }
  662. void ProjectDialog::_notification(int p_what) {
  663. switch (p_what) {
  664. case NOTIFICATION_THEME_CHANGED: {
  665. create_dir->set_icon(get_editor_theme_icon(SNAME("FolderCreate")));
  666. project_browse->set_icon(get_editor_theme_icon(SNAME("FolderBrowse")));
  667. install_browse->set_icon(get_editor_theme_icon(SNAME("FolderBrowse")));
  668. } break;
  669. }
  670. }
  671. void ProjectDialog::_bind_methods() {
  672. ADD_SIGNAL(MethodInfo("project_created"));
  673. ADD_SIGNAL(MethodInfo("projects_updated"));
  674. }
  675. ProjectDialog::ProjectDialog() {
  676. VBoxContainer *vb = memnew(VBoxContainer);
  677. add_child(vb);
  678. name_container = memnew(VBoxContainer);
  679. vb->add_child(name_container);
  680. Label *l = memnew(Label);
  681. l->set_text(TTR("Project Name:"));
  682. name_container->add_child(l);
  683. project_name = memnew(LineEdit);
  684. project_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  685. name_container->add_child(project_name);
  686. project_path_container = memnew(VBoxContainer);
  687. vb->add_child(project_path_container);
  688. HBoxContainer *pphb_label = memnew(HBoxContainer);
  689. project_path_container->add_child(pphb_label);
  690. l = memnew(Label);
  691. l->set_text(TTR("Project Path:"));
  692. l->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  693. pphb_label->add_child(l);
  694. create_dir = memnew(CheckButton);
  695. create_dir->set_text(TTR("Create Folder"));
  696. create_dir->set_pressed(true);
  697. pphb_label->add_child(create_dir);
  698. create_dir->connect("toggled", callable_mp(this, &ProjectDialog::_create_dir_toggled));
  699. HBoxContainer *pphb = memnew(HBoxContainer);
  700. project_path_container->add_child(pphb);
  701. project_path = memnew(LineEdit);
  702. project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  703. project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  704. pphb->add_child(project_path);
  705. install_path_container = memnew(VBoxContainer);
  706. vb->add_child(install_path_container);
  707. l = memnew(Label);
  708. l->set_text(TTR("Project Installation Path:"));
  709. install_path_container->add_child(l);
  710. HBoxContainer *iphb = memnew(HBoxContainer);
  711. install_path_container->add_child(iphb);
  712. install_path = memnew(LineEdit);
  713. install_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  714. install_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  715. iphb->add_child(install_path);
  716. // status icon
  717. project_status_rect = memnew(TextureRect);
  718. project_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  719. pphb->add_child(project_status_rect);
  720. project_browse = memnew(Button);
  721. project_browse->set_text(TTR("Browse"));
  722. project_browse->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_browse_project_path));
  723. pphb->add_child(project_browse);
  724. // install status icon
  725. install_status_rect = memnew(TextureRect);
  726. install_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  727. iphb->add_child(install_status_rect);
  728. install_browse = memnew(Button);
  729. install_browse->set_text(TTR("Browse"));
  730. install_browse->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_browse_install_path));
  731. iphb->add_child(install_browse);
  732. msg = memnew(Label);
  733. msg->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  734. msg->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  735. msg->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  736. vb->add_child(msg);
  737. // Renderer selection.
  738. renderer_container = memnew(VBoxContainer);
  739. vb->add_child(renderer_container);
  740. l = memnew(Label);
  741. l->set_text(TTR("Renderer:"));
  742. renderer_container->add_child(l);
  743. HBoxContainer *rshc = memnew(HBoxContainer);
  744. renderer_container->add_child(rshc);
  745. renderer_button_group.instantiate();
  746. // Left hand side, used for checkboxes to select renderer.
  747. Container *rvb = memnew(VBoxContainer);
  748. rshc->add_child(rvb);
  749. String default_renderer_type = "forward_plus";
  750. if (EditorSettings::get_singleton()->has_setting("project_manager/default_renderer")) {
  751. default_renderer_type = EditorSettings::get_singleton()->get_setting("project_manager/default_renderer");
  752. }
  753. Button *rs_button = memnew(CheckBox);
  754. rs_button->set_button_group(renderer_button_group);
  755. rs_button->set_text(TTR("Forward+"));
  756. #if defined(WEB_ENABLED)
  757. rs_button->set_disabled(true);
  758. #endif
  759. rs_button->set_meta(SNAME("rendering_method"), "forward_plus");
  760. rs_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_renderer_selected));
  761. rvb->add_child(rs_button);
  762. if (default_renderer_type == "forward_plus") {
  763. rs_button->set_pressed(true);
  764. }
  765. rs_button = memnew(CheckBox);
  766. rs_button->set_button_group(renderer_button_group);
  767. rs_button->set_text(TTR("Mobile"));
  768. #if defined(WEB_ENABLED)
  769. rs_button->set_disabled(true);
  770. #endif
  771. rs_button->set_meta(SNAME("rendering_method"), "mobile");
  772. rs_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_renderer_selected));
  773. rvb->add_child(rs_button);
  774. if (default_renderer_type == "mobile") {
  775. rs_button->set_pressed(true);
  776. }
  777. rs_button = memnew(CheckBox);
  778. rs_button->set_button_group(renderer_button_group);
  779. rs_button->set_text(TTR("Compatibility"));
  780. #if !defined(GLES3_ENABLED)
  781. rs_button->set_disabled(true);
  782. #endif
  783. rs_button->set_meta(SNAME("rendering_method"), "gl_compatibility");
  784. rs_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_renderer_selected));
  785. rvb->add_child(rs_button);
  786. #if defined(GLES3_ENABLED)
  787. if (default_renderer_type == "gl_compatibility") {
  788. rs_button->set_pressed(true);
  789. }
  790. #endif
  791. rshc->add_child(memnew(VSeparator));
  792. // Right hand side, used for text explaining each choice.
  793. rvb = memnew(VBoxContainer);
  794. rvb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  795. rshc->add_child(rvb);
  796. renderer_info = memnew(Label);
  797. renderer_info->set_modulate(Color(1, 1, 1, 0.7));
  798. rvb->add_child(renderer_info);
  799. _renderer_selected();
  800. l = memnew(Label);
  801. l->set_text(TTR("The renderer can be changed later, but scenes may need to be adjusted."));
  802. // Add some extra spacing to separate it from the list above and the buttons below.
  803. l->set_custom_minimum_size(Size2(0, 40) * EDSCALE);
  804. l->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  805. l->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  806. l->set_modulate(Color(1, 1, 1, 0.7));
  807. renderer_container->add_child(l);
  808. default_files_container = memnew(HBoxContainer);
  809. vb->add_child(default_files_container);
  810. l = memnew(Label);
  811. l->set_text(TTR("Version Control Metadata:"));
  812. default_files_container->add_child(l);
  813. vcs_metadata_selection = memnew(OptionButton);
  814. vcs_metadata_selection->set_custom_minimum_size(Size2(100, 20));
  815. vcs_metadata_selection->add_item(TTR("None"), (int)EditorVCSInterface::VCSMetadata::NONE);
  816. vcs_metadata_selection->add_item(TTR("Git"), (int)EditorVCSInterface::VCSMetadata::GIT);
  817. vcs_metadata_selection->select((int)EditorVCSInterface::VCSMetadata::GIT);
  818. default_files_container->add_child(vcs_metadata_selection);
  819. Control *spacer = memnew(Control);
  820. spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  821. default_files_container->add_child(spacer);
  822. fdialog_project = memnew(EditorFileDialog);
  823. fdialog_project->set_previews_enabled(false); //Crucial, otherwise the engine crashes.
  824. fdialog_project->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  825. fdialog_install = memnew(EditorFileDialog);
  826. fdialog_install->set_previews_enabled(false); //Crucial, otherwise the engine crashes.
  827. fdialog_install->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  828. add_child(fdialog_project);
  829. add_child(fdialog_install);
  830. project_name->connect("text_changed", callable_mp(this, &ProjectDialog::_project_name_changed).unbind(1));
  831. project_path->connect("text_changed", callable_mp(this, &ProjectDialog::_project_path_changed).unbind(1));
  832. install_path->connect("text_changed", callable_mp(this, &ProjectDialog::_install_path_changed).unbind(1));
  833. fdialog_project->connect("dir_selected", callable_mp(this, &ProjectDialog::_project_path_selected));
  834. fdialog_project->connect("file_selected", callable_mp(this, &ProjectDialog::_project_path_selected));
  835. fdialog_install->connect("dir_selected", callable_mp(this, &ProjectDialog::_install_path_selected));
  836. fdialog_install->connect("file_selected", callable_mp(this, &ProjectDialog::_install_path_selected));
  837. set_hide_on_ok(false);
  838. dialog_error = memnew(AcceptDialog);
  839. add_child(dialog_error);
  840. }