project_dialog.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. 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. // Strip any trailing slash.
  278. target_path = target_path.rstrip("/\\");
  279. // Save and remove target dir name.
  280. if (target_path.get_file() == auto_dir) {
  281. last_custom_target_dir = "";
  282. } else {
  283. last_custom_target_dir = target_path.get_file();
  284. }
  285. target_path = target_path.get_base_dir();
  286. }
  287. _set_target_path(target_path);
  288. _validate_path();
  289. }
  290. void ProjectDialog::_project_name_changed() {
  291. if (mode == MODE_NEW || mode == MODE_INSTALL) {
  292. _update_target_auto_dir();
  293. }
  294. _validate_path();
  295. }
  296. void ProjectDialog::_project_path_changed() {
  297. if (mode == MODE_IMPORT) {
  298. _update_target_auto_dir();
  299. }
  300. _validate_path();
  301. }
  302. void ProjectDialog::_install_path_changed() {
  303. _validate_path();
  304. }
  305. void ProjectDialog::_browse_project_path() {
  306. String path = project_path->get_text();
  307. if (path.is_empty()) {
  308. path = EDITOR_GET("filesystem/directories/default_project_path");
  309. }
  310. if (mode == MODE_IMPORT && install_path->is_visible_in_tree()) {
  311. // Select last ZIP file.
  312. fdialog_project->set_current_path(path);
  313. } else if ((mode == MODE_NEW || mode == MODE_INSTALL) && create_dir->is_pressed()) {
  314. // Select parent directory of project path.
  315. fdialog_project->set_current_dir(path.get_base_dir());
  316. } else {
  317. // Select project path.
  318. fdialog_project->set_current_dir(path);
  319. }
  320. if (mode == MODE_IMPORT) {
  321. fdialog_project->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_ANY);
  322. fdialog_project->clear_filters();
  323. fdialog_project->add_filter("project.godot", vformat("%s %s", VERSION_NAME, TTR("Project")));
  324. fdialog_project->add_filter("*.zip", TTR("ZIP File"));
  325. } else {
  326. fdialog_project->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  327. }
  328. hide();
  329. fdialog_project->popup_file_dialog();
  330. }
  331. void ProjectDialog::_browse_install_path() {
  332. ERR_FAIL_COND_MSG(mode != MODE_IMPORT, "Install path is only used for MODE_IMPORT.");
  333. if (create_dir->is_pressed()) {
  334. // Select parent directory of install path.
  335. fdialog_install->set_current_dir(install_path->get_text().get_base_dir());
  336. } else {
  337. // Select install path.
  338. fdialog_install->set_current_dir(install_path->get_text());
  339. }
  340. fdialog_install->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  341. fdialog_install->popup_file_dialog();
  342. }
  343. void ProjectDialog::_project_path_selected(const String &p_path) {
  344. show_dialog(false);
  345. if (create_dir->is_pressed() && (mode == MODE_NEW || mode == MODE_INSTALL)) {
  346. // Replace parent directory, but keep target dir name.
  347. project_path->set_text(p_path.path_join(project_path->get_text().get_file()));
  348. } else {
  349. project_path->set_text(p_path);
  350. }
  351. _project_path_changed();
  352. if (install_path->is_visible_in_tree()) {
  353. // ZIP is selected; focus install path.
  354. install_path->grab_focus();
  355. } else {
  356. get_ok_button()->grab_focus();
  357. }
  358. }
  359. void ProjectDialog::_install_path_selected(const String &p_path) {
  360. ERR_FAIL_COND_MSG(mode != MODE_IMPORT, "Install path is only used for MODE_IMPORT.");
  361. if (create_dir->is_pressed()) {
  362. // Replace parent directory, but keep target dir name.
  363. install_path->set_text(p_path.path_join(install_path->get_text().get_file()));
  364. } else {
  365. install_path->set_text(p_path);
  366. }
  367. _install_path_changed();
  368. get_ok_button()->grab_focus();
  369. }
  370. void ProjectDialog::_reset_name() {
  371. project_name->set_text(TTR("New Game Project"));
  372. }
  373. void ProjectDialog::_renderer_selected() {
  374. ERR_FAIL_NULL(renderer_button_group->get_pressed_button());
  375. String renderer_type = renderer_button_group->get_pressed_button()->get_meta(SNAME("rendering_method"));
  376. if (renderer_type == "forward_plus") {
  377. renderer_info->set_text(
  378. String::utf8("• ") + TTR("Supports desktop platforms only.") +
  379. String::utf8("\n• ") + TTR("Advanced 3D graphics available.") +
  380. String::utf8("\n• ") + TTR("Can scale to large complex scenes.") +
  381. String::utf8("\n• ") + TTR("Uses RenderingDevice backend.") +
  382. String::utf8("\n• ") + TTR("Slower rendering of simple scenes."));
  383. } else if (renderer_type == "mobile") {
  384. renderer_info->set_text(
  385. String::utf8("• ") + TTR("Supports desktop + mobile platforms.") +
  386. String::utf8("\n• ") + TTR("Less advanced 3D graphics.") +
  387. String::utf8("\n• ") + TTR("Less scalable for complex scenes.") +
  388. String::utf8("\n• ") + TTR("Uses RenderingDevice backend.") +
  389. String::utf8("\n• ") + TTR("Fast rendering of simple scenes."));
  390. } else if (renderer_type == "gl_compatibility") {
  391. renderer_info->set_text(
  392. String::utf8("• ") + TTR("Supports desktop, mobile + web platforms.") +
  393. String::utf8("\n• ") + TTR("Least advanced 3D graphics (currently work-in-progress).") +
  394. String::utf8("\n• ") + TTR("Intended for low-end/older devices.") +
  395. String::utf8("\n• ") + TTR("Uses OpenGL 3 backend (OpenGL 3.3/ES 3.0/WebGL2).") +
  396. String::utf8("\n• ") + TTR("Fastest rendering of simple scenes."));
  397. } else {
  398. WARN_PRINT("Unknown renderer type. Please report this as a bug on GitHub.");
  399. }
  400. }
  401. void ProjectDialog::_nonempty_confirmation_ok_pressed() {
  402. is_folder_empty = true;
  403. ok_pressed();
  404. }
  405. void ProjectDialog::ok_pressed() {
  406. // Before we create a project, check that the target folder is empty.
  407. // If not, we need to ask the user if they're sure they want to do this.
  408. if (!is_folder_empty) {
  409. ConfirmationDialog *cd = memnew(ConfirmationDialog);
  410. cd->set_title(TTR("Warning: This folder is not empty"));
  411. 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?"));
  412. cd->get_ok_button()->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_nonempty_confirmation_ok_pressed));
  413. get_parent()->add_child(cd);
  414. cd->popup_centered();
  415. return;
  416. }
  417. String path = project_path->get_text();
  418. if (mode == MODE_NEW) {
  419. if (create_dir->is_pressed()) {
  420. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  421. if (!d->dir_exists(path) && d->make_dir(path) != OK) {
  422. _set_message(TTR("Couldn't create project directory, check permissions."), MESSAGE_ERROR);
  423. return;
  424. }
  425. }
  426. PackedStringArray project_features = ProjectSettings::get_required_features();
  427. ProjectSettings::CustomMap initial_settings;
  428. // Be sure to change this code if/when renderers are changed.
  429. // Default values are "forward_plus" for the main setting, "mobile" for the mobile override,
  430. // and "gl_compatibility" for the web override.
  431. String renderer_type = renderer_button_group->get_pressed_button()->get_meta(SNAME("rendering_method"));
  432. initial_settings["rendering/renderer/rendering_method"] = renderer_type;
  433. EditorSettings::get_singleton()->set("project_manager/default_renderer", renderer_type);
  434. EditorSettings::get_singleton()->save();
  435. if (renderer_type == "forward_plus") {
  436. project_features.push_back("Forward Plus");
  437. } else if (renderer_type == "mobile") {
  438. project_features.push_back("Mobile");
  439. } else if (renderer_type == "gl_compatibility") {
  440. project_features.push_back("GL Compatibility");
  441. // Also change the default rendering method for the mobile override.
  442. initial_settings["rendering/renderer/rendering_method.mobile"] = "gl_compatibility";
  443. } else {
  444. WARN_PRINT("Unknown renderer type. Please report this as a bug on GitHub.");
  445. }
  446. project_features.sort();
  447. initial_settings["application/config/features"] = project_features;
  448. initial_settings["application/config/name"] = project_name->get_text().strip_edges();
  449. initial_settings["application/config/icon"] = "res://icon.svg";
  450. Error err = ProjectSettings::get_singleton()->save_custom(path.path_join("project.godot"), initial_settings, Vector<String>(), false);
  451. if (err != OK) {
  452. _set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
  453. return;
  454. }
  455. // Store default project icon in SVG format.
  456. Ref<FileAccess> fa_icon = FileAccess::open(path.path_join("icon.svg"), FileAccess::WRITE, &err);
  457. if (err != OK) {
  458. _set_message(TTR("Couldn't create icon.svg in project path."), MESSAGE_ERROR);
  459. return;
  460. }
  461. fa_icon->store_string(get_default_project_icon());
  462. EditorVCSInterface::create_vcs_metadata_files(EditorVCSInterface::VCSMetadata(vcs_metadata_selection->get_selected()), path);
  463. }
  464. // Two cases for importing a ZIP.
  465. switch (mode) {
  466. case MODE_IMPORT: {
  467. if (zip_path.is_empty()) {
  468. break;
  469. }
  470. path = install_path->get_text().simplify_path();
  471. [[fallthrough]];
  472. }
  473. case MODE_INSTALL: {
  474. ERR_FAIL_COND(zip_path.is_empty());
  475. Ref<FileAccess> io_fa;
  476. zlib_filefunc_def io = zipio_create_io(&io_fa);
  477. unzFile pkg = unzOpen2(zip_path.utf8().get_data(), &io);
  478. if (!pkg) {
  479. dialog_error->set_text(TTR("Error opening package file, not in ZIP format."));
  480. dialog_error->popup_centered();
  481. return;
  482. }
  483. // Find the first directory with a "project.godot".
  484. String zip_root;
  485. int ret = unzGoToFirstFile(pkg);
  486. while (ret == UNZ_OK) {
  487. unz_file_info info;
  488. char fname[16384];
  489. unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  490. ERR_FAIL_COND_MSG(ret != UNZ_OK, "Failed to get current file info.");
  491. String name = String::utf8(fname);
  492. if (name.get_file() == "project.godot") {
  493. zip_root = name.get_base_dir();
  494. break;
  495. }
  496. ret = unzGoToNextFile(pkg);
  497. }
  498. if (ret == UNZ_END_OF_LIST_OF_FILE) {
  499. _set_message(TTR("Invalid \".zip\" project file; it doesn't contain a \"project.godot\" file."), MESSAGE_ERROR);
  500. unzClose(pkg);
  501. return;
  502. }
  503. if (create_dir->is_pressed()) {
  504. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  505. if (!d->dir_exists(path) && d->make_dir(path) != OK) {
  506. _set_message(TTR("Couldn't create project directory, check permissions."), MESSAGE_ERROR);
  507. return;
  508. }
  509. }
  510. ret = unzGoToFirstFile(pkg);
  511. Vector<String> failed_files;
  512. while (ret == UNZ_OK) {
  513. //get filename
  514. unz_file_info info;
  515. char fname[16384];
  516. ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
  517. ERR_FAIL_COND_MSG(ret != UNZ_OK, "Failed to get current file info.");
  518. String rel_path = String::utf8(fname).trim_prefix(zip_root);
  519. if (rel_path.is_empty()) { // Root.
  520. } else if (rel_path.ends_with("/")) { // Directory.
  521. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  522. da->make_dir(path.path_join(rel_path));
  523. } else { // File.
  524. Vector<uint8_t> uncomp_data;
  525. uncomp_data.resize(info.uncompressed_size);
  526. unzOpenCurrentFile(pkg);
  527. ret = unzReadCurrentFile(pkg, uncomp_data.ptrw(), uncomp_data.size());
  528. ERR_BREAK_MSG(ret < 0, vformat("An error occurred while attempting to read from file: %s. This file will not be used.", rel_path));
  529. unzCloseCurrentFile(pkg);
  530. Ref<FileAccess> f = FileAccess::open(path.path_join(rel_path), FileAccess::WRITE);
  531. if (f.is_valid()) {
  532. f->store_buffer(uncomp_data.ptr(), uncomp_data.size());
  533. } else {
  534. failed_files.push_back(rel_path);
  535. }
  536. }
  537. ret = unzGoToNextFile(pkg);
  538. }
  539. unzClose(pkg);
  540. if (failed_files.size()) {
  541. String err_msg = TTR("The following files failed extraction from package:") + "\n\n";
  542. for (int i = 0; i < failed_files.size(); i++) {
  543. if (i > 15) {
  544. err_msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
  545. break;
  546. }
  547. err_msg += failed_files[i] + "\n";
  548. }
  549. dialog_error->set_text(err_msg);
  550. dialog_error->popup_centered();
  551. return;
  552. }
  553. } break;
  554. default: {
  555. } break;
  556. }
  557. if (mode == MODE_RENAME || mode == MODE_INSTALL) {
  558. // Load project.godot as ConfigFile to set the new name.
  559. ConfigFile cfg;
  560. String project_godot = path.path_join("project.godot");
  561. Error err = cfg.load(project_godot);
  562. if (err != OK) {
  563. dialog_error->set_text(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err));
  564. dialog_error->popup_centered();
  565. return;
  566. }
  567. cfg.set_value("application", "config/name", project_name->get_text().strip_edges());
  568. err = cfg.save(project_godot);
  569. if (err != OK) {
  570. dialog_error->set_text(vformat(TTR("Couldn't save project at '%s' (error %d)."), project_godot, err));
  571. dialog_error->popup_centered();
  572. return;
  573. }
  574. }
  575. hide();
  576. if (mode == MODE_NEW || mode == MODE_IMPORT || mode == MODE_INSTALL) {
  577. emit_signal(SNAME("project_created"), path, edit_check_box->is_pressed());
  578. } else if (mode == MODE_RENAME) {
  579. emit_signal(SNAME("projects_updated"));
  580. }
  581. }
  582. void ProjectDialog::set_zip_path(const String &p_path) {
  583. zip_path = p_path;
  584. }
  585. void ProjectDialog::set_zip_title(const String &p_title) {
  586. zip_title = p_title;
  587. }
  588. void ProjectDialog::set_mode(Mode p_mode) {
  589. mode = p_mode;
  590. }
  591. void ProjectDialog::set_project_name(const String &p_name) {
  592. project_name->set_text(p_name);
  593. }
  594. void ProjectDialog::set_project_path(const String &p_path) {
  595. project_path->set_text(p_path);
  596. }
  597. void ProjectDialog::ask_for_path_and_show() {
  598. _reset_name();
  599. _browse_project_path();
  600. }
  601. void ProjectDialog::show_dialog(bool p_reset_name) {
  602. if (mode == MODE_RENAME) {
  603. // Name and path are set in `ProjectManager::_rename_project`.
  604. project_path->set_editable(false);
  605. set_title(TTR("Rename Project"));
  606. set_ok_button_text(TTR("Rename"));
  607. create_dir->hide();
  608. project_status_rect->hide();
  609. project_browse->hide();
  610. edit_check_box->hide();
  611. name_container->show();
  612. install_path_container->hide();
  613. renderer_container->hide();
  614. default_files_container->hide();
  615. callable_mp((Control *)project_name, &Control::grab_focus).call_deferred();
  616. callable_mp(project_name, &LineEdit::select_all).call_deferred();
  617. } else {
  618. if (p_reset_name) {
  619. _reset_name();
  620. }
  621. project_path->set_editable(true);
  622. String fav_dir = EDITOR_GET("filesystem/directories/default_project_path");
  623. if (!fav_dir.is_empty()) {
  624. project_path->set_text(fav_dir);
  625. install_path->set_text(fav_dir);
  626. fdialog_project->set_current_dir(fav_dir);
  627. } else {
  628. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  629. project_path->set_text(d->get_current_dir());
  630. install_path->set_text(d->get_current_dir());
  631. fdialog_project->set_current_dir(d->get_current_dir());
  632. }
  633. create_dir->show();
  634. project_status_rect->show();
  635. project_browse->show();
  636. edit_check_box->show();
  637. if (mode == MODE_IMPORT) {
  638. set_title(TTR("Import Existing Project"));
  639. set_ok_button_text(TTR("Import"));
  640. name_container->hide();
  641. install_path_container->hide();
  642. renderer_container->hide();
  643. default_files_container->hide();
  644. // Project path dialog is also opened; no need to change focus.
  645. } else if (mode == MODE_NEW) {
  646. set_title(TTR("Create New Project"));
  647. set_ok_button_text(TTR("Create"));
  648. name_container->show();
  649. install_path_container->hide();
  650. renderer_container->show();
  651. default_files_container->show();
  652. callable_mp((Control *)project_name, &Control::grab_focus).call_deferred();
  653. callable_mp(project_name, &LineEdit::select_all).call_deferred();
  654. } else if (mode == MODE_INSTALL) {
  655. set_title(TTR("Install Project:") + " " + zip_title);
  656. set_ok_button_text(TTR("Install"));
  657. project_name->set_text(zip_title);
  658. name_container->show();
  659. install_path_container->hide();
  660. renderer_container->hide();
  661. default_files_container->hide();
  662. callable_mp((Control *)project_path, &Control::grab_focus).call_deferred();
  663. }
  664. auto_dir = "";
  665. last_custom_target_dir = "";
  666. _update_target_auto_dir();
  667. if (create_dir->is_pressed()) {
  668. // Append `auto_dir` to target path.
  669. _create_dir_toggled(true);
  670. }
  671. }
  672. _validate_path();
  673. popup_centered(Size2(500, 0) * EDSCALE);
  674. }
  675. void ProjectDialog::_notification(int p_what) {
  676. switch (p_what) {
  677. case NOTIFICATION_THEME_CHANGED: {
  678. create_dir->set_icon(get_editor_theme_icon(SNAME("FolderCreate")));
  679. project_browse->set_icon(get_editor_theme_icon(SNAME("FolderBrowse")));
  680. install_browse->set_icon(get_editor_theme_icon(SNAME("FolderBrowse")));
  681. } break;
  682. case NOTIFICATION_READY: {
  683. fdialog_project = memnew(EditorFileDialog);
  684. fdialog_project->set_previews_enabled(false); // Crucial, otherwise the engine crashes.
  685. fdialog_project->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  686. fdialog_project->connect("dir_selected", callable_mp(this, &ProjectDialog::_project_path_selected));
  687. fdialog_project->connect("file_selected", callable_mp(this, &ProjectDialog::_project_path_selected));
  688. fdialog_project->connect("canceled", callable_mp(this, &ProjectDialog::show_dialog).bind(false), CONNECT_DEFERRED);
  689. callable_mp((Node *)this, &Node::add_sibling).call_deferred(fdialog_project, false);
  690. } break;
  691. }
  692. }
  693. void ProjectDialog::_bind_methods() {
  694. ADD_SIGNAL(MethodInfo("project_created"));
  695. ADD_SIGNAL(MethodInfo("projects_updated"));
  696. }
  697. ProjectDialog::ProjectDialog() {
  698. VBoxContainer *vb = memnew(VBoxContainer);
  699. add_child(vb);
  700. name_container = memnew(VBoxContainer);
  701. vb->add_child(name_container);
  702. Label *l = memnew(Label);
  703. l->set_text(TTR("Project Name:"));
  704. name_container->add_child(l);
  705. project_name = memnew(LineEdit);
  706. project_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  707. name_container->add_child(project_name);
  708. project_path_container = memnew(VBoxContainer);
  709. vb->add_child(project_path_container);
  710. HBoxContainer *pphb_label = memnew(HBoxContainer);
  711. project_path_container->add_child(pphb_label);
  712. l = memnew(Label);
  713. l->set_text(TTR("Project Path:"));
  714. l->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  715. pphb_label->add_child(l);
  716. create_dir = memnew(CheckButton);
  717. create_dir->set_text(TTR("Create Folder"));
  718. create_dir->set_pressed(true);
  719. pphb_label->add_child(create_dir);
  720. create_dir->connect("toggled", callable_mp(this, &ProjectDialog::_create_dir_toggled));
  721. HBoxContainer *pphb = memnew(HBoxContainer);
  722. project_path_container->add_child(pphb);
  723. project_path = memnew(LineEdit);
  724. project_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  725. project_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  726. pphb->add_child(project_path);
  727. install_path_container = memnew(VBoxContainer);
  728. vb->add_child(install_path_container);
  729. l = memnew(Label);
  730. l->set_text(TTR("Project Installation Path:"));
  731. install_path_container->add_child(l);
  732. HBoxContainer *iphb = memnew(HBoxContainer);
  733. install_path_container->add_child(iphb);
  734. install_path = memnew(LineEdit);
  735. install_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  736. install_path->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  737. iphb->add_child(install_path);
  738. // status icon
  739. project_status_rect = memnew(TextureRect);
  740. project_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  741. pphb->add_child(project_status_rect);
  742. project_browse = memnew(Button);
  743. project_browse->set_text(TTR("Browse"));
  744. project_browse->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_browse_project_path));
  745. pphb->add_child(project_browse);
  746. // install status icon
  747. install_status_rect = memnew(TextureRect);
  748. install_status_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  749. iphb->add_child(install_status_rect);
  750. install_browse = memnew(Button);
  751. install_browse->set_text(TTR("Browse"));
  752. install_browse->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_browse_install_path));
  753. iphb->add_child(install_browse);
  754. msg = memnew(Label);
  755. msg->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  756. msg->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  757. msg->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  758. vb->add_child(msg);
  759. // Renderer selection.
  760. renderer_container = memnew(VBoxContainer);
  761. vb->add_child(renderer_container);
  762. l = memnew(Label);
  763. l->set_text(TTR("Renderer:"));
  764. renderer_container->add_child(l);
  765. HBoxContainer *rshc = memnew(HBoxContainer);
  766. renderer_container->add_child(rshc);
  767. renderer_button_group.instantiate();
  768. // Left hand side, used for checkboxes to select renderer.
  769. Container *rvb = memnew(VBoxContainer);
  770. rshc->add_child(rvb);
  771. String default_renderer_type = "forward_plus";
  772. if (EditorSettings::get_singleton()->has_setting("project_manager/default_renderer")) {
  773. default_renderer_type = EditorSettings::get_singleton()->get_setting("project_manager/default_renderer");
  774. }
  775. Button *rs_button = memnew(CheckBox);
  776. rs_button->set_button_group(renderer_button_group);
  777. rs_button->set_text(TTR("Forward+"));
  778. #if defined(WEB_ENABLED)
  779. rs_button->set_disabled(true);
  780. #endif
  781. rs_button->set_meta(SNAME("rendering_method"), "forward_plus");
  782. rs_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_renderer_selected));
  783. rvb->add_child(rs_button);
  784. if (default_renderer_type == "forward_plus") {
  785. rs_button->set_pressed(true);
  786. }
  787. rs_button = memnew(CheckBox);
  788. rs_button->set_button_group(renderer_button_group);
  789. rs_button->set_text(TTR("Mobile"));
  790. #if defined(WEB_ENABLED)
  791. rs_button->set_disabled(true);
  792. #endif
  793. rs_button->set_meta(SNAME("rendering_method"), "mobile");
  794. rs_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_renderer_selected));
  795. rvb->add_child(rs_button);
  796. if (default_renderer_type == "mobile") {
  797. rs_button->set_pressed(true);
  798. }
  799. rs_button = memnew(CheckBox);
  800. rs_button->set_button_group(renderer_button_group);
  801. rs_button->set_text(TTR("Compatibility"));
  802. #if !defined(GLES3_ENABLED)
  803. rs_button->set_disabled(true);
  804. #endif
  805. rs_button->set_meta(SNAME("rendering_method"), "gl_compatibility");
  806. rs_button->connect(SceneStringName(pressed), callable_mp(this, &ProjectDialog::_renderer_selected));
  807. rvb->add_child(rs_button);
  808. #if defined(GLES3_ENABLED)
  809. if (default_renderer_type == "gl_compatibility") {
  810. rs_button->set_pressed(true);
  811. }
  812. #endif
  813. rshc->add_child(memnew(VSeparator));
  814. // Right hand side, used for text explaining each choice.
  815. rvb = memnew(VBoxContainer);
  816. rvb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  817. rshc->add_child(rvb);
  818. renderer_info = memnew(Label);
  819. renderer_info->set_modulate(Color(1, 1, 1, 0.7));
  820. rvb->add_child(renderer_info);
  821. _renderer_selected();
  822. l = memnew(Label);
  823. l->set_text(TTR("The renderer can be changed later, but scenes may need to be adjusted."));
  824. // Add some extra spacing to separate it from the list above and the buttons below.
  825. l->set_custom_minimum_size(Size2(0, 40) * EDSCALE);
  826. l->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  827. l->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  828. l->set_modulate(Color(1, 1, 1, 0.7));
  829. renderer_container->add_child(l);
  830. default_files_container = memnew(HBoxContainer);
  831. vb->add_child(default_files_container);
  832. l = memnew(Label);
  833. l->set_text(TTR("Version Control Metadata:"));
  834. default_files_container->add_child(l);
  835. vcs_metadata_selection = memnew(OptionButton);
  836. vcs_metadata_selection->set_custom_minimum_size(Size2(100, 20));
  837. vcs_metadata_selection->add_item(TTR("None"), (int)EditorVCSInterface::VCSMetadata::NONE);
  838. vcs_metadata_selection->add_item(TTR("Git"), (int)EditorVCSInterface::VCSMetadata::GIT);
  839. vcs_metadata_selection->select((int)EditorVCSInterface::VCSMetadata::GIT);
  840. default_files_container->add_child(vcs_metadata_selection);
  841. Control *spacer = memnew(Control);
  842. spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  843. default_files_container->add_child(spacer);
  844. fdialog_install = memnew(EditorFileDialog);
  845. fdialog_install->set_previews_enabled(false); //Crucial, otherwise the engine crashes.
  846. fdialog_install->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  847. add_child(fdialog_install);
  848. Control *spacer2 = memnew(Control);
  849. spacer2->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  850. vb->add_child(spacer2);
  851. edit_check_box = memnew(CheckBox);
  852. edit_check_box->set_text(TTR("Edit Now"));
  853. edit_check_box->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  854. edit_check_box->set_pressed(true);
  855. vb->add_child(edit_check_box);
  856. project_name->connect(SceneStringName(text_changed), callable_mp(this, &ProjectDialog::_project_name_changed).unbind(1));
  857. project_path->connect(SceneStringName(text_changed), callable_mp(this, &ProjectDialog::_project_path_changed).unbind(1));
  858. install_path->connect(SceneStringName(text_changed), callable_mp(this, &ProjectDialog::_install_path_changed).unbind(1));
  859. fdialog_install->connect("dir_selected", callable_mp(this, &ProjectDialog::_install_path_selected));
  860. fdialog_install->connect("file_selected", callable_mp(this, &ProjectDialog::_install_path_selected));
  861. set_hide_on_ok(false);
  862. dialog_error = memnew(AcceptDialog);
  863. add_child(dialog_error);
  864. }