Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --init cli argument to create new projects #25925

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions core/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,41 @@ bool ProjectSettings::has_custom_feature(const String &p_feature) const {
return custom_features.has(p_feature);
}

ProjectSettings::CustomMap ProjectSettings::get_default_settings(const String &project_name, const String &driver) {
ProjectSettings::CustomMap initial_settings;

if (driver == "GLES3") {
initial_settings["rendering/quality/driver/driver_name"] = "GLES3";
} else {
initial_settings["rendering/quality/driver/driver_name"] = "GLES2";
initial_settings["rendering/vram_compression/import_etc2"] = false;
initial_settings["rendering/vram_compression/import_etc"] = true;
}
initial_settings["application/config/name"] = project_name;
initial_settings["application/config/icon"] = "res://" + get_default_icon_name();
initial_settings["rendering/environment/default_environment"] = "res://" + get_default_env_name();

return initial_settings;
}

String ProjectSettings::get_default_env_content() {
String default_env_str;
default_env_str += "[gd_resource type=\"Environment\" load_steps=2 format=2]\n";
default_env_str += "[sub_resource type=\"ProceduralSky\" id=1]\n";
default_env_str += "[resource]\n";
default_env_str += "background_mode = 2\n";
default_env_str += "background_sky = SubResource( 1 )\n";
return default_env_str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct approach. This means that if we change anything in the 'real' default environment we now have to change it in two places. This should serialize a normal default environment instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved the code from project_manager.cpp as it was.
See:

f->store_line("[gd_resource type=\"Environment\" load_steps=2 format=2]");
f->store_line("[sub_resource type=\"ProceduralSky\" id=1]");
f->store_line("[resource]");
f->store_line("background_mode = 2");
f->store_line("background_sky = SubResource( 1 )");

Where can I find the default environment configuration?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that it was hard coded before, maybe default environment could be instanced and serialized to file that way?

}

String ProjectSettings::get_default_icon_name() {
return "icon.png";
}

String ProjectSettings::get_default_env_name() {
return "default_env.tres";
}

void ProjectSettings::_bind_methods() {

ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
Expand Down
5 changes: 5 additions & 0 deletions core/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class ProjectSettings : public Object {
public:
static const int CONFIG_VERSION = 4;

static CustomMap get_default_settings(const String &project_name, const String &driver = "GLES3");
static String get_default_env_content();
static String get_default_icon_name();
static String get_default_env_name();

void set_setting(const String &p_setting, const Variant &p_value);
Variant get_setting(const String &p_setting) const;

Expand Down
23 changes: 5 additions & 18 deletions editor/project_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,32 +472,19 @@ class ProjectDialog : public ConfirmationDialog {
} else {
if (mode == MODE_NEW) {

ProjectSettings::CustomMap initial_settings;
if (rasterizer_button_group->get_pressed_button()->get_meta("driver_name") == "GLES3") {
initial_settings["rendering/quality/driver/driver_name"] = "GLES3";
} else {
initial_settings["rendering/quality/driver/driver_name"] = "GLES2";
initial_settings["rendering/vram_compression/import_etc2"] = false;
initial_settings["rendering/vram_compression/import_etc"] = true;
}
initial_settings["application/config/name"] = project_name->get_text();
initial_settings["application/config/icon"] = "res://icon.png";
initial_settings["rendering/environment/default_environment"] = "res://default_env.tres";
ProjectSettings::CustomMap initial_settings = ProjectSettings::get_default_settings(project_name->get_text(),
rasterizer_button_group->get_pressed_button()->get_meta("driver_name"));

if (ProjectSettings::get_singleton()->save_custom(dir.plus_file("project.godot"), initial_settings, Vector<String>(), false) != OK) {
set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
} else {
ResourceSaver::save(dir.plus_file("icon.png"), get_icon("DefaultProjectIcon", "EditorIcons"));
ResourceSaver::save(dir.plus_file(ProjectSettings::get_default_icon_name()), get_icon("DefaultProjectIcon", "EditorIcons"));

FileAccess *f = FileAccess::open(dir.plus_file("default_env.tres"), FileAccess::WRITE);
FileAccess *f = FileAccess::open(dir.plus_file(ProjectSettings::get_default_env_name()), FileAccess::WRITE);
if (!f) {
set_message(TTR("Couldn't create project.godot in project path."), MESSAGE_ERROR);
} else {
f->store_line("[gd_resource type=\"Environment\" load_steps=2 format=2]");
f->store_line("[sub_resource type=\"ProceduralSky\" id=1]");
f->store_line("[resource]");
f->store_line("background_mode = 2");
f->store_line("background_sky = SubResource( 1 )");
f->store_line(ProjectSettings::get_default_env_content());
memdelete(f);
}
}
Expand Down
50 changes: 50 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ void Main::print_help(const char *p_binary) {
OS::get_singleton()->print("\n");

OS::get_singleton()->print("Standalone tools:\n");
OS::get_singleton()->print(" -i, --init <name> [<path>] Create a new project.\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a --path parameter. Should we reuse that instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make it more semantic.
I wonder if we should have a CLI parser to validate input and format so we can separate that from the logic based on the content. The actual code handling the cli arguments feels a bit convoluted and hardcoded.
That would make easier to have proper error handling and not opening the editor when you pass an invalid argument.

Copy link
Member

@Calinou Calinou Feb 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, mixing godot -i test test and I don't really know which one is path and which one is project name, so seems logical...

To salvage the problem with whether it should create a folder by default, maybe this:
godot --init test --path (would this be empty?)

would allow to create project named test and making a directory for it, while
godot --init test --path .

would create a project inside current directory.

Or just omitting the path argument would force to create a folder named the same as project:
godot --init test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the --init command should not take an optional path argument.
It should also not default to creating a folder, unless the path given does not exist, in which .ase it should try to create it.

So:

// Create "test" project in current working directory
godot --init test
// Create "test" project in previously created "myfolder" directory
mkdir myfolder
godot --init test --path myfolder
// Create "test" project in myother/folder subdirectory, creating them as needed
godot --init test --path myother/folder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be within #ifdef TOOLS_ENABLED (and same for the parsing of -i and --init).

OS::get_singleton()->print(" -s, --script <script> Run a script.\n");
OS::get_singleton()->print(" --check-only Only parse for errors and quit (use with --script).\n");
#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -427,6 +428,55 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
print_line(get_full_version_string());
goto error;

} else if (I->get() == "-i" || I->get() == "--init") { // Init new project

String dir = ".";
String project_name;

if (I->next()) {
project_name = I->next()->get();
N = I->next()->next();

// Parse the optional argument: the path of the project.
if (N && !N->get().begins_with("-")) {
String arg_str = N->get();
DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = d->make_dir_recursive(arg_str);

if (err == OK) {
dir = arg_str;
N = N->next();
} else {
OS::get_singleton()->print("Error attempting to create dir: %s.\n", arg_str.utf8().get_data());
goto error;
}
memdelete(d);
}
} else {
OS::get_singleton()->print("Missing init argument, aborting.\n");
goto error;
}
ProjectSettings::CustomMap initial_settings = ProjectSettings::get_default_settings(project_name);

if (ProjectSettings::get_singleton()->save_custom(dir.plus_file("project.godot"), initial_settings, Vector<String>(), false) != OK) {

OS::get_singleton()->print("Couldn't create project.godot in project path.\n");

} else {

Ref<Image> icon = memnew(Image(app_icon_png));
icon->save_png(dir.plus_file(ProjectSettings::get_default_icon_name()));

FileAccess *f = FileAccess::open(dir.plus_file(ProjectSettings::get_default_env_name()), FileAccess::WRITE);
if (!f) {
OS::get_singleton()->print("Couldn't create project.godot in project path.\n");
} else {
f->store_line(ProjectSettings::get_default_env_content());
memdelete(f);
}
}
goto error;

} else if (I->get() == "--resolution") { // force resolution

if (I->next()) {
Expand Down