Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e8b2343

Browse files
committedMar 22, 2025·
Add OS::get_cwd virtual
1 parent 9adb7c7 commit e8b2343

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed
 

‎core/os/os.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ Error OS::set_cwd(const String &p_cwd) {
327327
return ERR_CANT_OPEN;
328328
}
329329

330+
String OS::get_cwd() const {
331+
return get_executable_path();
332+
}
333+
330334
Dictionary OS::get_memory_info() const {
331335
Dictionary meminfo;
332336

‎core/os/os.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class OS {
179179
virtual Error shell_open(String p_uri);
180180
virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder = true);
181181
virtual Error set_cwd(const String &p_cwd);
182+
virtual String get_cwd() const;
182183

183184
virtual bool has_environment(const String &p_var) const = 0;
184185
virtual String get_environment(const String &p_var) const = 0;

‎drivers/unix/os_unix.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,17 @@ Error OS_Unix::set_cwd(const String &p_cwd) {
700700
return OK;
701701
}
702702

703+
String OS_Unix::get_cwd() const {
704+
String cwd;
705+
char ret[PATH_MAX];
706+
char *temp = getcwd(ret, PATH_MAX);
707+
ERR_FAIL_NULL_V(temp, "");
708+
if (cwd.parse_utf8(ret) != OK) {
709+
cwd = ret; // No utf8, maybe latin1?
710+
}
711+
return cwd;
712+
}
713+
703714
bool OS_Unix::has_environment(const String &p_var) const {
704715
return getenv(p_var.utf8().get_data()) != nullptr;
705716
}

‎drivers/unix/os_unix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class OS_Unix : public OS {
6060
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false) override;
6161

6262
virtual Error set_cwd(const String &p_cwd) override;
63+
virtual String get_cwd() const override;
6364

6465
virtual String get_name() const override;
6566
virtual String get_distribution_name() const override;

‎platform/windows/os_windows.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,13 @@ Error OS_Windows::set_cwd(const String &p_cwd) {
914914
return OK;
915915
}
916916

917+
String OS_Windows::get_cwd() const {
918+
WCHAR ret[2048];
919+
GetCurrentDirectoryW(2048, ret);
920+
String cwd = String::utf16((const char16_t *)ret);
921+
return cwd;
922+
}
923+
917924
Vector<String> OS_Windows::get_system_fonts() const {
918925
if (!dwrite_init) {
919926
return Vector<String>();

‎platform/windows/os_windows.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class OS_Windows : public OS {
181181
virtual double get_unix_time() const override;
182182

183183
virtual Error set_cwd(const String &p_cwd) override;
184+
virtual String get_cwd() const override;
184185

185186
virtual void delay_usec(uint32_t p_usec) const override;
186187
virtual uint64_t get_ticks_usec() const override;

0 commit comments

Comments
 (0)
Please sign in to comment.