project_dialog.cpp 38 KB

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