Add initial support for the XDG Base Directory spec

Spec version 0.7 from https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
(latest as of this commit).

Three virtual methods are added to OS for the various XDG paths we will use:
- OS::get_data_path gives XDG_DATA_HOME, or if missing:
  ~/.local/share on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_config_path gives XDG_CONFIG_HOME, or if missing:
  ~/.config on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_cache_path gives XDG_CACHE_HOME, or if missing:
  ~/.cache on X11, ~/Library/Caches on macOS and %APPDATA% on Windows

So for Windows there are no changes, for Linux we follow the full split spec
and for macOS stuff will move from ~/.godot to ~/Library/Application Support/Godot.

Support for system-wide installation of templates on Unix was removed for now,
as it's a bit hackish and I don't think anyone uses it.

user:// will still be OS::get_data_path() + "/godot/app_userdata/$name" by
default, but when using the application/config/use_shared_user_dir option
it will now use XDG_DATA_HOME/$name, e.g. ~/.local/share/MyGame.

For now everything still goes in EditorSettings::get_settings_dir(), but
this will be changed in a later commit to make use of the new splitting
where relevant.

Part of #3513.
This commit is contained in:
Rémi Verschelde 2017-11-17 17:11:41 +01:00
parent ad199c3964
commit 32c12a92a5
17 changed files with 360 additions and 118 deletions

View File

@ -33,6 +33,7 @@
#include "input.h"
#include "os/file_access.h"
#include "project_settings.h"
#include "version_generated.gen.h"
#include <stdarg.h>
@ -262,16 +263,7 @@ String OS::get_locale() const {
return "en";
}
String OS::get_resource_dir() const {
return ProjectSettings::get_singleton()->get_resource_path();
}
String OS::get_system_dir(SystemDir p_dir) const {
return ".";
}
// Helper function used by OS_Unix and OS_Windows
String OS::get_safe_application_name() const {
String an = ProjectSettings::get_singleton()->get("application/config/name");
Vector<String> invalid_char = String("\\ / : * ? \" < > |").split(" ");
@ -281,11 +273,51 @@ String OS::get_safe_application_name() const {
return an;
}
// Path to data, config, cache, etc. OS-specific folders
// Get properly capitalized engine name for system paths
String OS::get_godot_dir_name() const {
// Default to lowercase, so only override when different case is needed
return String(_MKSTR(VERSION_SHORT_NAME)).to_lower();
}
// OS equivalent of XDG_DATA_HOME
String OS::get_data_path() const {
return ".";
}
// OS equivalent of XDG_CONFIG_HOME
String OS::get_config_path() const {
return ".";
}
// OS equivalent of XDG_CACHE_HOME
String OS::get_cache_path() const {
return ".";
}
// OS specific path for user://
String OS::get_user_data_dir() const {
return ".";
};
// Absolute path to res://
String OS::get_resource_dir() const {
return ProjectSettings::get_singleton()->get_resource_path();
}
// Access system-specific dirs like Documents, Downloads, etc.
String OS::get_system_dir(SystemDir p_dir) const {
return ".";
}
Error OS::shell_open(String p_uri) {
return ERR_UNAVAILABLE;
};

View File

@ -200,7 +200,6 @@ public:
virtual void set_low_processor_usage_mode(bool p_enabled);
virtual bool is_in_low_processor_usage_mode() const;
virtual String get_installed_templates_path() const { return ""; }
virtual String get_executable_path() const;
virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false) = 0;
virtual Error kill(const ProcessID &p_pid) = 0;
@ -334,11 +333,15 @@ public:
virtual String get_locale() const;
String get_safe_application_name() const;
virtual String get_godot_dir_name() const;
virtual String get_data_path() const;
virtual String get_config_path() const;
virtual String get_cache_path() const;
virtual String get_user_data_dir() const;
virtual String get_resource_dir() const;
virtual Error move_to_trash(const String &p_path) { return FAILED; }
enum SystemDir {
SYSTEM_DIR_DESKTOP,
SYSTEM_DIR_DCIM,
@ -352,6 +355,8 @@ public:
virtual String get_system_dir(SystemDir p_dir) const;
virtual Error move_to_trash(const String &p_path) { return FAILED; }
virtual void set_no_window_mode(bool p_enable);
virtual bool is_no_window_mode_enabled() const;

View File

@ -2,16 +2,6 @@
Import('env')
g_set_p = '#ifdef UNIX_ENABLED\n'
g_set_p += '#include "os_unix.h"\n'
g_set_p += 'String OS_Unix::get_global_settings_path() const {\n'
g_set_p += '\treturn "' + env["unix_global_settings_path"] + '";\n'
g_set_p += '}\n'
g_set_p += '#endif'
f = open("os_unix_global_settings_path.gen.cpp", "w")
f.write(g_set_p)
f.close()
env.add_source_files(env.drivers_sources, "*.cpp")
env["check_c_headers"] = [ [ "mntent.h", "HAVE_MNTENT" ] ]

View File

@ -456,30 +456,19 @@ int OS_Unix::get_processor_count() const {
String OS_Unix::get_user_data_dir() const {
String an = get_safe_application_name();
if (an != "") {
if (has_environment("HOME")) {
bool use_godot = ProjectSettings::get_singleton()->get("application/config/use_shared_user_dir");
if (use_godot)
return get_environment("HOME") + "/.godot/app_userdata/" + an;
else
return get_environment("HOME") + "/." + an;
String appname = get_safe_application_name();
if (appname != "") {
bool use_godot_dir = ProjectSettings::get_singleton()->get("application/config/use_shared_user_dir");
if (use_godot_dir) {
return get_data_path().plus_file(get_godot_dir_name()).plus_file("app_userdata").plus_file(appname);
} else {
return get_data_path().plus_file(appname);
}
}
return ProjectSettings::get_singleton()->get_resource_path();
}
String OS_Unix::get_installed_templates_path() const {
String p = get_global_settings_path();
if (p != "")
return p + "/templates/";
else
return "";
}
String OS_Unix::get_executable_path() const {
#ifdef __linux__

View File

@ -62,8 +62,6 @@ protected:
String stdin_buf;
String get_global_settings_path() const;
public:
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
virtual String get_stdin_string(bool p_block);
@ -108,11 +106,8 @@ public:
virtual void debug_break();
virtual String get_installed_templates_path() const;
virtual String get_executable_path() const;
virtual String get_user_data_dir() const;
//virtual void run( MainLoop * p_main_loop );
};
class UnixTerminalLogger : public StdLogger {

View File

@ -336,33 +336,18 @@ Error EditorExportPlatform::_save_zip_file(void *p_userdata, const String &p_pat
String EditorExportPlatform::find_export_template(String template_file_name, String *err) const {
String base_name = itos(VERSION_MAJOR) + "." + itos(VERSION_MINOR) + "-" + _MKSTR(VERSION_STATUS) + "/" + template_file_name;
String user_file = EditorSettings::get_singleton()->get_settings_dir() + "/templates/" + base_name;
String system_file = OS::get_singleton()->get_installed_templates_path();
bool has_system_path = (system_file != "");
system_file = system_file.plus_file(base_name);
String current_version = itos(VERSION_MAJOR) + "." + itos(VERSION_MINOR) + "-" + _MKSTR(VERSION_STATUS) + VERSION_MODULE_CONFIG;
String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(current_version).plus_file(template_file_name);
// Prefer user file
if (FileAccess::exists(user_file)) {
return user_file;
}
// Now check system file
if (has_system_path) {
if (FileAccess::exists(system_file)) {
return system_file;
}
if (FileAccess::exists(template_path)) {
return template_path;
}
// Not found
if (err) {
*err += "No export template found at \"" + user_file + "\"";
if (has_system_path)
*err += "\n or \"" + system_file + "\".";
else
*err += ".";
*err += "No export template found at \"" + template_path + "\".";
}
return String(); // not found
return String();
}
bool EditorExportPlatform::exists_export_template(String template_file_name, String *err) const {

View File

@ -640,8 +640,13 @@ void EditorSettings::create() {
DirAccess *dir = NULL;
String data_path;
String data_dir;
String config_path;
String config_dir;
String cache_path;
String cache_dir;
Ref<ConfigFile> extra_config = memnew(ConfigFile);
String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();
@ -658,28 +663,74 @@ void EditorSettings::create() {
memdelete(d);
if (self_contained) {
// editor is self contained
// editor is self contained, all in same folder
data_path = exe_path;
data_dir = data_path.plus_file("editor_data");
config_path = exe_path;
config_dir = "editor_data";
config_dir = data_dir;
cache_path = exe_path;
cache_dir = data_dir.plus_file("cache");
} else {
if (OS::get_singleton()->has_environment("APPDATA")) {
// Most likely under windows, save here
config_path = OS::get_singleton()->get_environment("APPDATA");
config_dir = String(_MKSTR(VERSION_SHORT_NAME)).capitalize();
} else if (OS::get_singleton()->has_environment("HOME")) {
config_path = OS::get_singleton()->get_environment("HOME");
config_dir = "." + String(_MKSTR(VERSION_SHORT_NAME)).to_lower();
// Typically XDG_DATA_HOME or %APPDATA%
data_path = OS::get_singleton()->get_data_path();
data_dir = data_path.plus_file(OS::get_singleton()->get_godot_dir_name());
// Can be different from data_path e.g. on Linux or macOS
config_path = OS::get_singleton()->get_config_path();
config_dir = config_path.plus_file(OS::get_singleton()->get_godot_dir_name());
// Can be different from above paths, otherwise a subfolder of data_dir
cache_path = OS::get_singleton()->get_cache_path();
if (cache_path == data_path) {
cache_dir = data_dir.plus_file("cache");
} else {
cache_dir = cache_path.plus_file(OS::get_singleton()->get_godot_dir_name());
}
};
}
ClassDB::register_class<EditorSettings>(); //otherwise it can't be unserialized
String config_file_path;
if (config_path != "") {
if (data_path != "" && config_path != "" && cache_path != "") {
// Validate/create data dir and subdirectories
dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
if (dir->change_dir(data_path) != OK) {
ERR_PRINT("Cannot find path for data directory!");
memdelete(dir);
goto fail;
}
if (dir->change_dir(data_dir) != OK) {
dir->make_dir(data_dir);
if (dir->change_dir(data_dir) != OK) {
ERR_PRINT("Cannot create data directory!");
memdelete(dir);
goto fail;
}
}
// Validate/create cache dir
if (dir->change_dir(cache_path) != OK) {
ERR_PRINT("Cannot find path for cache directory!");
memdelete(dir);
goto fail;
}
if (dir->change_dir(cache_dir) != OK) {
dir->make_dir(cache_dir);
if (dir->change_dir(cache_dir) != OK) {
ERR_PRINT("Cannot create cache directory!");
memdelete(dir);
goto fail;
}
}
// Validate/create config dir and subdirectories
if (dir->change_dir(config_path) != OK) {
ERR_PRINT("Cannot find path for config directory!");
memdelete(dir);
@ -695,10 +746,17 @@ void EditorSettings::create() {
}
}
// FIXME: Move to data dir
if (dir->change_dir("templates") != OK) {
dir->make_dir("templates");
} else {
dir->change_dir("..");
}
// FIXME: Move to cache dir
if (dir->change_dir("tmp") != OK) {
dir->make_dir("tmp");
} else {
dir->change_dir("..");
}
@ -715,42 +773,33 @@ void EditorSettings::create() {
}
_create_script_templates(dir->get_current_dir() + "/script_templates");
if (dir->change_dir("tmp") != OK) {
dir->make_dir("tmp");
} else {
dir->change_dir("..");
}
// FIXME: Rename to "projects"
if (dir->change_dir("config") != OK) {
dir->make_dir("config");
} else {
dir->change_dir("..");
}
dir->change_dir("config");
// Validate/create project-specific config dir
dir->change_dir("config");
String project_config_dir = ProjectSettings::get_singleton()->get_resource_path();
if (project_config_dir.ends_with("/"))
project_config_dir = config_path.substr(0, project_config_dir.size() - 1);
project_config_dir = project_config_dir.get_file() + "-" + project_config_dir.md5_text();
if (dir->change_dir(project_config_dir)) {
if (dir->change_dir(project_config_dir) != OK) {
dir->make_dir(project_config_dir);
} else {
dir->change_dir("..");
}
dir->change_dir("..");
// path at least is validated, so validate config file
String config_file_name = "editor_settings-" + String(_MKSTR(VERSION_MAJOR)) + ".tres";
config_file_path = config_path + "/" + config_dir + "/" + config_file_name;
// Validate editor config file
String config_file_name = "editor_settings.tres";
config_file_path = config_dir.plus_file(config_file_name);
if (!dir->file_exists(config_file_name)) {
goto fail;
}
@ -766,7 +815,9 @@ void EditorSettings::create() {
singleton->save_changed_setting = true;
singleton->config_file_path = config_file_path;
singleton->project_config_dir = project_config_dir;
singleton->settings_dir = config_path + "/" + config_dir;
singleton->settings_dir = config_dir;
singleton->data_dir = data_dir;
singleton->cache_dir = cache_dir;
if (OS::get_singleton()->is_stdout_verbose()) {
@ -796,7 +847,9 @@ fail:
singleton = Ref<EditorSettings>(memnew(EditorSettings));
singleton->save_changed_setting = true;
singleton->config_file_path = config_file_path;
singleton->settings_dir = config_path + "/" + config_dir;
singleton->settings_dir = config_dir;
singleton->data_dir = data_dir;
singleton->cache_dir = cache_dir;
singleton->_load_defaults(extra_config);
singleton->setup_language();
singleton->setup_network();
@ -966,7 +1019,19 @@ void EditorSettings::add_property_hint(const PropertyInfo &p_hint) {
hints[p_hint.name] = p_hint;
}
// Settings paths and saved metadata
// Data directories
String EditorSettings::get_data_dir() const {
return data_dir;
}
String EditorSettings::get_templates_dir() const {
return get_data_dir().plus_file("templates");
}
// Config directories
String EditorSettings::get_settings_dir() const {
@ -975,9 +1040,29 @@ String EditorSettings::get_settings_dir() const {
String EditorSettings::get_project_settings_dir() const {
// FIXME: Rename to "projects"
return get_settings_dir().plus_file("config").plus_file(project_config_dir);
}
String EditorSettings::get_text_editor_themes_dir() const {
return get_settings_dir().plus_file("text_editor_themes");
}
String EditorSettings::get_script_templates_dir() const {
return get_settings_dir().plus_file("script_templates");
}
// Cache directory
String EditorSettings::get_cache_dir() const {
return cache_dir;
}
// Metadata
void EditorSettings::set_project_metadata(const String &p_section, const String &p_key, Variant p_data) {
Ref<ConfigFile> cf = memnew(ConfigFile);
String path = get_project_settings_dir().plus_file("project_metadata.cfg");

View File

@ -93,9 +93,11 @@ private:
Map<String, Ref<ShortCut> > shortcuts;
String resource_path;
String settings_dir;
String data_dir;
String cache_dir;
String config_file_path;
String settings_path;
String project_config_path;
String project_config_dir;
Vector<String> favorite_dirs;
Vector<String> recent_dirs;
@ -147,8 +149,13 @@ public:
void set_resource_clipboard(const Ref<Resource> &p_resource) { clipboard = p_resource; }
Ref<Resource> get_resource_clipboard() const { return clipboard; }
String get_data_dir() const;
String get_templates_dir() const;
String get_settings_dir() const;
String get_project_settings_dir() const;
String get_text_editor_themes_dir() const;
String get_script_templates_dir() const;
String get_cache_dir() const;
void set_project_metadata(const String &p_section, const String &p_key, Variant p_data);
Variant get_project_metadata(const String &p_section, const String &p_key, Variant p_default);

View File

@ -57,7 +57,7 @@ String _get_expected_build_config() {
String _get_mono_user_dir() {
#ifdef TOOLS_ENABLED
if (EditorSettings::get_singleton()) {
return EditorSettings::get_singleton()->get_settings_dir().plus_file("mono");
return EditorSettings::get_singleton()->get_data_dir().plus_file("mono");
} else {
String settings_path;
@ -68,13 +68,7 @@ String _get_mono_user_dir() {
// contain yourself
settings_path = exe_dir.plus_file("editor_data");
} else {
if (OS::get_singleton()->has_environment("APPDATA")) {
String app_data = OS::get_singleton()->get_environment("APPDATA").replace("\\", "/");
settings_path = app_data.plus_file(String(_MKSTR(VERSION_SHORT_NAME)).capitalize());
} else if (OS::get_singleton()->has_environment("HOME")) {
String home = OS::get_singleton()->get_environment("HOME");
settings_path = home.plus_file("." + String(_MKSTR(VERSION_SHORT_NAME)).to_lower());
}
settings_path = OS::get_singleton()->get_data_path().plus_file(OS::get_singleton()->get_godot_dir_name());
}
return settings_path.plus_file("mono");

View File

@ -314,3 +314,36 @@ bool OS_Haiku::_check_internal_feature_support(const String &p_feature) {
return p_feature == "pc" || p_feature == "s3tc";
}
String OS_Haiku::get_config_path() const {
if (has_environment("XDG_CONFIG_HOME")) {
return get_environment("XDG_CONFIG_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file(".config");
} else {
return ".";
}
}
String OS_Haiku::get_data_path() const {
if (has_environment("XDG_DATA_HOME")) {
return get_environment("XDG_DATA_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file(".local/share");
} else {
return get_config_path();
}
}
String OS_Haiku::get_cache_path() const {
if (has_environment("XDG_CACHE_HOME")) {
return get_environment("XDG_CACHE_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file(".cache");
} else {
return get_config_path();
}
}

View File

@ -117,6 +117,10 @@ public:
virtual int get_power_percent_left();
virtual bool _check_internal_feature_support(const String &p_feature);
virtual String get_config_path() const;
virtual String get_data_path() const;
virtual String get_cache_path() const;
};
#endif

View File

@ -154,6 +154,11 @@ public:
virtual MainLoop *get_main_loop() const;
virtual String get_config_path() const;
virtual String get_data_path() const;
virtual String get_cache_path() const;
virtual String get_godot_dir_name() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual bool can_draw() const;

View File

@ -36,6 +36,7 @@
#include "print_string.h"
#include "sem_osx.h"
#include "servers/visual/visual_server_raster.h"
#include "version_generated.gen.h"
#include <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
@ -1340,6 +1341,43 @@ MainLoop *OS_OSX::get_main_loop() const {
return main_loop;
}
String OS_OSX::get_config_path() const {
if (has_environment("XDG_CONFIG_HOME")) {
return get_environment("XDG_CONFIG_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file("Library/Application Support");
} else {
return ".";
}
}
String OS_OSX::get_data_path() const {
if (has_environment("XDG_DATA_HOME")) {
return get_environment("XDG_DATA_HOME");
} else {
return get_config_path();
}
}
String OS_OSX::get_cache_path() const {
if (has_environment("XDG_CACHE_HOME")) {
return get_environment("XDG_CACHE_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file("Library/Caches");
} else {
return get_config_path();
}
}
// Get properly capitalized engine name for system paths
String OS_OSX::get_godot_dir_name() const {
return String(_MKSTR(VERSION_SHORT_NAME)).capitalize();
}
String OS_OSX::get_system_dir(SystemDir p_dir) const {
NSSearchPathDirectory id = 0;

View File

@ -47,6 +47,7 @@
#include "servers/visual/visual_server_wrap_mt.h"
#include "stream_peer_winsock.h"
#include "tcp_server_winsock.h"
#include "version_generated.gen.h"
#include "windows_terminal_logger.h"
#include <process.h>
@ -2131,6 +2132,43 @@ MainLoop *OS_Windows::get_main_loop() const {
return main_loop;
}
String OS_Windows::get_config_path() const {
if (has_environment("XDG_CONFIG_HOME")) { // unlikely, but after all why not?
return get_environment("XDG_CONFIG_HOME");
} else if (has_environment("APPDATA")) {
return get_environment("APPDATA");
} else {
return ".";
}
}
String OS_Windows::get_data_path() const {
if (has_environment("XDG_DATA_HOME")) {
return get_environment("XDG_DATA_HOME");
} else {
return get_config_path();
}
}
String OS_Windows::get_cache_path() const {
if (has_environment("XDG_CACHE_HOME")) {
return get_environment("XDG_CACHE_HOME");
} else if (has_environment("TEMP")) {
return get_environment("TEMP");
} else {
return get_config_path();
}
}
// Get properly capitalized engine name for system paths
String OS_Windows::get_godot_dir_name() const {
return String(_MKSTR(VERSION_SHORT_NAME)).capitalize();
}
String OS_Windows::get_system_dir(SystemDir p_dir) const {
int id;
@ -2167,18 +2205,17 @@ String OS_Windows::get_system_dir(SystemDir p_dir) const {
ERR_FAIL_COND_V(res != S_OK, String());
return String(szPath);
}
String OS_Windows::get_user_data_dir() const {
String an = get_safe_application_name();
if (an != "") {
String appname = get_safe_application_name();
if (appname != "") {
if (has_environment("APPDATA")) {
bool use_godot = ProjectSettings::get_singleton()->get("application/config/use_shared_user_dir");
if (!use_godot)
return (OS::get_singleton()->get_environment("APPDATA") + "/" + an).replace("\\", "/");
else
return (OS::get_singleton()->get_environment("APPDATA") + "/Godot/app_userdata/" + an).replace("\\", "/");
bool use_godot_dir = ProjectSettings::get_singleton()->get("application/config/use_shared_user_dir");
if (use_godot_dir) {
return get_data_path().plus_file(get_godot_dir_name()).plus_file("app_userdata").plus_file(appname).replace("\\", "/");
} else {
return get_data_path().plus_file(appname).replace("\\", "/");
}
}

View File

@ -253,8 +253,14 @@ public:
virtual void enable_for_stealing_focus(ProcessID pid);
virtual void move_window_to_foreground();
virtual String get_user_data_dir() const;
virtual String get_config_path() const;
virtual String get_data_path() const;
virtual String get_cache_path() const;
virtual String get_godot_dir_name() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_user_data_dir() const;
virtual void release_rendering_thread();
virtual void make_rendering_thread();

View File

@ -1941,6 +1941,39 @@ bool OS_X11::_check_internal_feature_support(const String &p_feature) {
return p_feature == "pc" || p_feature == "s3tc";
}
String OS_X11::get_config_path() const {
if (has_environment("XDG_CONFIG_HOME")) {
return get_environment("XDG_CONFIG_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file(".config");
} else {
return ".";
}
}
String OS_X11::get_data_path() const {
if (has_environment("XDG_DATA_HOME")) {
return get_environment("XDG_DATA_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file(".local/share");
} else {
return get_config_path();
}
}
String OS_X11::get_cache_path() const {
if (has_environment("XDG_CACHE_HOME")) {
return get_environment("XDG_CACHE_HOME");
} else if (has_environment("HOME")) {
return get_environment("HOME").plus_file(".cache");
} else {
return get_config_path();
}
}
String OS_X11::get_system_dir(SystemDir p_dir) const {
String xdgparam;

View File

@ -213,6 +213,10 @@ public:
virtual void make_rendering_thread();
virtual void swap_buffers();
virtual String get_config_path() const;
virtual String get_data_path() const;
virtual String get_cache_path() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual Error shell_open(String p_uri);