Skip to content

Add OS::get_cwd virtual #87789

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ Error OS::set_cwd(const String &p_cwd) {
return ERR_CANT_OPEN;
}

String OS::get_cwd() const {
return get_executable_path();
}

Dictionary OS::get_memory_info() const {
Dictionary meminfo;

Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class OS {
virtual Error shell_open(String p_uri);
virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder = true);
virtual Error set_cwd(const String &p_cwd);
virtual String get_cwd() const;
Comment on lines 181 to +182
Copy link
Member

Choose a reason for hiding this comment

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

Note: neither set_cwd, not newly added get_cwd are bound, and therefore it will be accessible only from the engine C++ code (not from GDScript or GDExtension). If you want't it to be accessible, a wrapper should be added to the CoreBind (OS is a special case and its methods can't be bound directly).

Here's links to one of such wrappers, for example (OS.get_name):

Wrapper definition:

String get_name() const;

Wrapper implementation:

godot/core/core_bind.cpp

Lines 351 to 353 in 9adb7c7

String OS::get_name() const {
return ::OS::get_singleton()->get_name();
}

Wrapper binding:

ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name);

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 noticed that set_cwd is not bound, so I don't plan to get_cwd the corresponding one.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure what's the purpose of adding get_cwd() if it can't be used from script and not used by the engine.

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 hope it will provide an intuitive way to get the current working directory in future engine development, and it will correspond to set_cwd.
Maybe it could also be used to gradually replace the original code that used DirAccess to get the working path?


virtual bool has_environment(const String &p_var) const = 0;
virtual String get_environment(const String &p_var) const = 0;
Expand Down
11 changes: 11 additions & 0 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,17 @@ Error OS_Unix::set_cwd(const String &p_cwd) {
return OK;
}

String OS_Unix::get_cwd() const {
String cwd;
char ret[PATH_MAX];
char *temp = getcwd(ret, PATH_MAX);
ERR_FAIL_NULL_V(temp, "");
if (cwd.parse_utf8(ret) != OK) {
cwd = ret; // No utf8, maybe latin1?
}
return cwd;
}

bool OS_Unix::has_environment(const String &p_var) const {
return getenv(p_var.utf8().get_data()) != nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/unix/os_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class OS_Unix : public OS {
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false) override;

virtual Error set_cwd(const String &p_cwd) override;
virtual String get_cwd() const override;

virtual String get_name() const override;
virtual String get_distribution_name() const override;
Expand Down
15 changes: 15 additions & 0 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,21 @@ Error OS_Windows::set_cwd(const String &p_cwd) {
return OK;
}

String OS_Windows::get_cwd() const {
const DWORD size = GetCurrentDirectoryW(0, nullptr);

Char16String buffer;
buffer.resize((int)size);
if (GetCurrentDirectoryW(size, (wchar_t *)buffer.ptrw()) == 0) {
return OS::get_cwd();
}

String result;
result.parse_utf16(buffer.ptr());

return result;
}

Vector<String> OS_Windows::get_system_fonts() const {
if (!dwrite_init) {
return Vector<String>();
Expand Down
1 change: 1 addition & 0 deletions platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class OS_Windows : public OS {
virtual double get_unix_time() const override;

virtual Error set_cwd(const String &p_cwd) override;
virtual String get_cwd() const override;

virtual void delay_usec(uint32_t p_usec) const override;
virtual uint64_t get_ticks_usec() const override;
Expand Down
Loading