project_dialog.cpp 35 KB

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