Skip to content

Commit

Permalink
Add libretro assets directory search path, and have environment varia…
Browse files Browse the repository at this point in the history
…bles override configured values (#17054)

* platform_win32: Fix sizeof for DEFAULT_DIR_PLAYLIST.

* frontend/drivers/platform_win32.c (frontend_win32_env_get): Check
the size of correct destination array.

* frontend: Honor the LIBRETRO_ASSETS_DIRECTORY environment variable.

This builds on 763fcd8 ("unix, win32: Allow set the default
libretro_directory via environment variable") to also allow specifying
the assets directory via an environment variable.

* frontend/drivers/platform_unix.c (frontend_unix_get_env)
<libretro_assets_directory> New variable. Use it to set
DEFAULT_DIR_ASSETS, when available.
* frontend/drivers/platform_win32.c (frontend_win32_env_get): Likewise.

* platform: Honor the LIBRETRO_DIRECTORY environment variable.

Until now, and unlike the defaut core directory, the default
core *info* directory would be hard-coded relative to the installation
directory. Honor the LIBRETRO_DIRECTORY environment variable the same
instead.

* frontend/drivers/platform_unix.c (frontend_unix_get_env): Set
DEFAULT_DIR_CORE_INFO default to the value of the LIBRETRO_DIRECTORY
environment variable, if available.
* frontend/drivers/platform_win32.c (frontend_win32_env_get): Likewise.

* platform: Honor the LIBRETRO_AUTOCONFIG_DIRECTORY environment variable.

* frontend/drivers/platform_unix.c
(libretro_autoconfig_directory): New variable.
(frontend_unix_get_env): Set DEFAULT_DIR_AUTOCONFIG to the value of
the LIBRETRO_AUTOCONFIG_DIRECTORY environment variable, if available.
* frontend/drivers/platform_win32.c: Likewise.

* platform: Honor the LIBRETRO_VIDEO_FILTER_DIRECTORY environment variable.

* frontend/drivers/platform_unix.c
(libretro_video_filter_directory): New variable.
(frontend_unix_get_env): Set DEFAULT_DIR_VIDEO_FILTER to the value of
the LIBRETRO_VIDEO_FILTER_DIRECTORY environment variable, if available.
* frontend/drivers/platform_win32.c: Likewise.

* platform: Honor the LIBRETRO_VIDEO_SHADER_DIRECTORY environment variable.

* frontend/drivers/platform_unix.c
(libretro_video_shader_directory): New variable.
(frontend_unix_get_env): Set DEFAULT_DIR_SHADER to the value of
the LIBRETRO_VIDEO_SHADER_DIRECTORY environment variable, if available.
* frontend/drivers/platform_win32.c: Likewise.

* platform: Honor the LIBRETRO_SYSTEM_DIRECTORY environment variable.

* frontend/drivers/platform_unix.c
(libretro_system_directory): New variable.
(frontend_unix_get_env): Set DEFAULT_DIR_SYSTEM to the value of
the LIBRETRO_SYSTEM_DIRECTORY environment variable, if available.
* frontend/drivers/platform_win32.c: Likewise.

* configuration: Have environment variables override configuration.

Because the configuration file is systematically written when
RetroArch terminates, persisting any previous default/configured
value, setting the LIBRETRO_DIRECTORY, LIBRETRO_ASSETS_DIRECTORY, etc.
environment variables would not have an effect unless the
retroarch.cfg configuration file was cleared.

This seems to go against the common expectation that environment
variables are set by users to *override* the default behavior or
configuration of an application.

* configuration.c (config_load_file) <libretro_directory>
<libretro_assets_directory, libretro_autoconfig_directory>
<libretro_system_directory, libretro_video_filter_directory>
<libretro_video_shader_directory>: New variables. Use the values of
the LIBRETRO_DIRECTORY, LIBRETRO_ASSETS_DIRECTORY,
LIBRETRO_AUTOCONFIG_DIRECTORY, LIBRETRO_SYSTEM_DIRECTORY,
LIBRETRO_VIDEO_FILTER_DIRECTORY and LIBRETRO_VIDEO_SHADER_DIRECTORY
environment variables instead of their corresponding configured
values, when set.
* docs/retroarch.6: Document the environment variables honored and
their behavior.
  • Loading branch information
Apteryks authored Jan 17, 2025
1 parent b211ada commit 69ceb95
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 34 deletions.
50 changes: 41 additions & 9 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -3568,6 +3568,12 @@ static bool config_load_file(global_t *global,
{
unsigned i;
char tmp_str[PATH_MAX_LENGTH];
char* libretro_directory = NULL;
char* libretro_assets_directory = NULL;
char* libretro_autoconfig_directory = NULL;
char* libretro_system_directory = NULL;
char* libretro_video_filter_directory = NULL;
char* libretro_video_shader_directory = NULL;
static bool first_load = true;
bool without_overrides = false;
unsigned msg_color = 0;
Expand Down Expand Up @@ -3849,19 +3855,33 @@ static bool config_load_file(global_t *global,
strlcpy(path_settings[i].ptr, tmp_str, PATH_MAX_LENGTH);
}

#if !IOS
if (config_get_path(conf, "libretro_directory", tmp_str, sizeof(tmp_str)))
configuration_set_string(settings,
settings->paths.directory_libretro, tmp_str);
#endif

#ifdef RARCH_CONSOLE
if (conf)
video_driver_load_settings(global, conf);
#endif

/* Post-settings load */

libretro_directory = getenv("LIBRETRO_DIRECTORY");
if (libretro_directory) {
configuration_set_string(settings,
settings->paths.directory_libretro, libretro_directory);
configuration_set_string(settings,
settings->paths.path_libretro_info, libretro_directory);
}

libretro_autoconfig_directory = getenv("LIBRETRO_AUTOCONFIG_DIRECTORY");
if (libretro_autoconfig_directory)
configuration_set_string(settings,
settings->paths.directory_autoconfig,
libretro_autoconfig_directory);

libretro_system_directory = getenv("LIBRETRO_SYSTEM_DIRECTORY");
if (libretro_system_directory)
configuration_set_string(settings,
settings->paths.directory_system,
libretro_system_directory);

if ( (rarch_flags & RARCH_FLAGS_HAS_SET_USERNAME)
&& (override_username))
{
Expand Down Expand Up @@ -4026,15 +4046,27 @@ static bool config_load_file(global_t *global,
*settings->paths.path_menu_wallpaper = '\0';
if (string_is_equal(settings->paths.path_rgui_theme_preset, "default"))
*settings->paths.path_rgui_theme_preset = '\0';
if (string_is_equal(settings->paths.directory_video_shader, "default"))
libretro_video_shader_directory = getenv("LIBRETRO_VIDEO_SHADER_DIRECTORY");
if (libretro_video_shader_directory) { /* override configuration value */
configuration_set_string(settings, settings->paths.directory_video_shader,
libretro_video_shader_directory);
} else if (string_is_equal(settings->paths.directory_video_shader, "default"))
*settings->paths.directory_video_shader = '\0';
if (string_is_equal(settings->paths.directory_video_filter, "default"))
libretro_video_filter_directory = getenv("LIBRETRO_VIDEO_FILTER_DIRECTORY");
if (libretro_video_filter_directory) { /* override configuration value */
configuration_set_string(settings, settings->paths.directory_video_filter,
libretro_video_filter_directory);
} else if (string_is_equal(settings->paths.directory_video_filter, "default"))
*settings->paths.directory_video_filter = '\0';
if (string_is_equal(settings->paths.directory_audio_filter, "default"))
*settings->paths.directory_audio_filter = '\0';
if (string_is_equal(settings->paths.directory_core_assets, "default"))
*settings->paths.directory_core_assets = '\0';
if (string_is_equal(settings->paths.directory_assets, "default"))
libretro_assets_directory = getenv("LIBRETRO_ASSETS_DIRECTORY");
if (libretro_assets_directory) { /* override configuration value */
configuration_set_string(settings,
settings->paths.directory_assets, libretro_assets_directory);
} else if (string_is_equal(settings->paths.directory_assets, "default"))
*settings->paths.directory_assets = '\0';
#ifdef _3DS
if (string_is_equal(settings->paths.directory_bottom_assets, "default"))
Expand Down
41 changes: 40 additions & 1 deletion docs/retroarch.6
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" retroarch.6:

.TH "RETROARCH" "6" "November 1, 2011" "RETROARCH" "System Manager's Manual: retroarch"
.TH "RETROARCH" "6" "January 16, 2025" "RETROARCH" "System Manager's Manual: retroarch"

.SH NAME

Expand Down Expand Up @@ -239,3 +239,42 @@ Disables all kinds of content patching.
.TP
\fB-D, --detach\fR
Detach from the current console. This is currently only relevant for Microsoft Windows.

.SH ENVIRONMENT
\fBretroarch\fR honors the following environment variables:

.TP
\fBLIBRETRO_DIRECTORY\fR
Specify the directory where RetroArch looks for core and info files,
overriding the value of the "libretro_directory" configuration file
option.

.TP
\fBLIBRETRO_ASSETS_DIRECTORY\fR
Specify the directory where RetroArch looks for assets, overriding
the value of the "assets_directory" configuration file
option.

.TP
\fBLIBRETRO_AUTOCONFIG_DIRECTORY\fR
Specify the directory where RetroArch looks for controller
auto-configuration files, overriding the value of the
"joypad_autoconfig_dir" configuration file option.

.TP
\fBLIBRETRO_SYSTEM_DIRECTORY\fR
Specify the directory where RetroArch looks for system files,
overriding the value of the "system_directory" configuration file
option.

.TP
\fBLIBRETRO_VIDEO_FILTER_DIRECTORY\fR
Specify the directory where RetroArch looks for video filters,
overriding the value of the "video_filter_dir" configuration file
option.

.TP
\fBLIBRETRO_VIDEO_SHADER_DIRECTORY\fR
Specify the directory where RetroArch looks for video shaders,
overriding the value of the "video_shader_dir" configuration file
option.
52 changes: 41 additions & 11 deletions frontend/drivers/platform_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,11 @@ static void frontend_unix_get_env(int *argc,
{
unsigned i;
const char* libretro_directory = getenv("LIBRETRO_DIRECTORY");
const char* libretro_assets_directory = getenv("LIBRETRO_ASSETS_DIRECTORY");
const char* libretro_autoconfig_directory = getenv("LIBRETRO_AUTOCONFIG_DIRECTORY");
const char* libretro_system_directory = getenv("LIBRETRO_SYSTEM_DIRECTORY");
const char* libretro_video_filter_directory = getenv("LIBRETRO_VIDEO_FILTER_DIRECTORY");
const char* libretro_video_shader_directory = getenv("LIBRETRO_VIDEO_SHADER_DIRECTORY");
#ifdef ANDROID
int32_t major, minor, rel;
char device_model[PROP_VALUE_MAX] = {0};
Expand Down Expand Up @@ -1764,20 +1769,31 @@ static void frontend_unix_get_env(int *argc,
"cores", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
else
#endif
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_CORE_INFO], base_path,
"cores", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
if (!string_is_empty(libretro_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_CORE_INFO], libretro_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
else
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_CORE_INFO], base_path,
"cores", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
#endif
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG], base_path,
"autoconfig", sizeof(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG]));

if (!string_is_empty(libretro_autoconfig_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG],
libretro_autoconfig_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG]));
else
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG], base_path,
"autoconfig", sizeof(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG]));
#ifdef ASSETS_DIR
if (path_is_directory(ASSETS_DIR "/assets"))
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_ASSETS],
ASSETS_DIR,
"assets", sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
else
#endif
if (path_is_directory("/usr/local/share/retroarch/assets"))
if (!string_is_empty(libretro_assets_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_ASSETS], libretro_assets_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
else if (path_is_directory("/usr/local/share/retroarch/assets"))
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_ASSETS],
"/usr/local/share/retroarch",
"assets", sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
Expand Down Expand Up @@ -1837,7 +1853,11 @@ static void frontend_unix_get_env(int *argc,
"filters/video", sizeof(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER]));
else
#endif
if (path_is_directory("/usr/local/share/retroarch/filters/video"))
if (!string_is_empty(libretro_video_filter_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER],
libretro_video_filter_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER]));
else if (path_is_directory("/usr/local/share/retroarch/filters/video"))
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER],
"/usr/local/share/retroarch",
"filters/video", sizeof(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER]));
Expand Down Expand Up @@ -1871,8 +1891,13 @@ static void frontend_unix_get_env(int *argc,
"records", sizeof(g_defaults.dirs[DEFAULT_DIR_RECORD_OUTPUT]));
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_DATABASE], base_path,
"database/rdb", sizeof(g_defaults.dirs[DEFAULT_DIR_DATABASE]));
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_SHADER], base_path,
"shaders", sizeof(g_defaults.dirs[DEFAULT_DIR_SHADER]));
if (!string_is_empty(libretro_video_shader_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_SHADER],
libretro_video_shader_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_SHADER]));
else
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_SHADER], base_path,
"shaders", sizeof(g_defaults.dirs[DEFAULT_DIR_SHADER]));
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_CHEATS], base_path,
"cheats", sizeof(g_defaults.dirs[DEFAULT_DIR_CHEATS]));
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_OVERLAY], base_path,
Expand All @@ -1891,8 +1916,13 @@ static void frontend_unix_get_env(int *argc,
"saves", sizeof(g_defaults.dirs[DEFAULT_DIR_SRAM]));
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_SAVESTATE], base_path,
"states", sizeof(g_defaults.dirs[DEFAULT_DIR_SAVESTATE]));
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_SYSTEM], base_path,
"system", sizeof(g_defaults.dirs[DEFAULT_DIR_SYSTEM]));
if (!string_is_empty(libretro_system_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_SYSTEM],
libretro_system_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_SYSTEM]));
else
fill_pathname_join(g_defaults.dirs[DEFAULT_DIR_SYSTEM], base_path,
"system", sizeof(g_defaults.dirs[DEFAULT_DIR_SYSTEM]));
#endif

#ifndef IS_SALAMANDER
Expand Down
60 changes: 47 additions & 13 deletions frontend/drivers/platform_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,39 @@ static void frontend_win32_env_get(int *argc, char *argv[],
{
const char *tmp_dir = getenv("TMP");
const char *libretro_directory = getenv("LIBRETRO_DIRECTORY");
const char *libretro_assets_directory = getenv("LIBRETRO_ASSETS_DIRECTORY");
const char* libretro_autoconfig_directory = getenv("LIBRETRO_AUTOCONFIG_DIRECTORY");
const char* libretro_system_directory = getenv("LIBRETRO_SYSTEM_DIRECTORY");
const char* libretro_video_filter_directory = getenv("LIBRETRO_VIDEO_FILTER_DIRECTORY");
const char* libretro_video_shader_directory = getenv("LIBRETRO_VIDEO_SHADER_DIRECTORY");
if (!string_is_empty(tmp_dir))
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_CACHE],
tmp_dir, sizeof(g_defaults.dirs[DEFAULT_DIR_CACHE]));

gfx_set_dwm();

fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_ASSETS],
":\\assets", sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
if (!string_is_empty(libretro_assets_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_ASSETS], libretro_assets_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
else
fill_pathname_expand_special(
g_defaults.dirs[DEFAULT_DIR_ASSETS],
":\\assets", sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_AUDIO_FILTER],
":\\filters\\audio", sizeof(g_defaults.dirs[DEFAULT_DIR_AUDIO_FILTER]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER],
":\\filters\\video", sizeof(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER]));
if (!string_is_empty(libretro_video_filter_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER],
libretro_video_filter_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER]));
else
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER],
":\\filters\\video", sizeof(g_defaults.dirs[DEFAULT_DIR_VIDEO_FILTER]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_CHEATS],
":\\cheats", sizeof(g_defaults.dirs[DEFAULT_DIR_CHEATS]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_DATABASE],
":\\database\\rdb", sizeof(g_defaults.dirs[DEFAULT_DIR_DATABASE]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_PLAYLIST],
":\\playlists", sizeof(g_defaults.dirs[DEFAULT_DIR_ASSETS]));
":\\playlists", sizeof(g_defaults.dirs[DEFAULT_DIR_PLAYLIST]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_RECORD_CONFIG],
":\\config\\record", sizeof(g_defaults.dirs[DEFAULT_DIR_RECORD_CONFIG]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_RECORD_OUTPUT],
Expand All @@ -606,12 +621,26 @@ static void frontend_win32_env_get(int *argc, char *argv[],
else
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_CORE],
":\\cores", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_CORE_INFO],
":\\info", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG],
":\\autoconfig", sizeof(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_SHADER],
":\\shaders", sizeof(g_defaults.dirs[DEFAULT_DIR_SHADER]));
if (!string_is_empty(libretro_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_CORE_INFO], libretro_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
else
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_CORE_INFO],
":\\info", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_INFO]));
if (!string_is_empty(libretro_autoconfig_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG],
libretro_autoconfig_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG]));
else
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG],
":\\autoconfig", sizeof(g_defaults.dirs[DEFAULT_DIR_AUTOCONFIG]));
if (!string_is_empty(libretro_video_filter_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_SHADER],
libretro_video_shader_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_SHADER]));
else
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_SHADER],
":\\shaders", sizeof(g_defaults.dirs[DEFAULT_DIR_SHADER]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_CORE_ASSETS],
":\\downloads", sizeof(g_defaults.dirs[DEFAULT_DIR_CORE_ASSETS]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_SCREENSHOT],
Expand All @@ -620,8 +649,13 @@ static void frontend_win32_env_get(int *argc, char *argv[],
":\\saves", sizeof(g_defaults.dirs[DEFAULT_DIR_SRAM]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_SAVESTATE],
":\\states", sizeof(g_defaults.dirs[DEFAULT_DIR_SAVESTATE]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_SYSTEM],
":\\system", sizeof(g_defaults.dirs[DEFAULT_DIR_SYSTEM]));
if (!string_is_empty(libretro_system_directory))
strlcpy(g_defaults.dirs[DEFAULT_DIR_SYSTEM],
libretro_system_directory,
sizeof(g_defaults.dirs[DEFAULT_DIR_SYSTEM]));
else
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_SYSTEM],
":\\system", sizeof(g_defaults.dirs[DEFAULT_DIR_SYSTEM]));
fill_pathname_expand_special(g_defaults.dirs[DEFAULT_DIR_LOGS],
":\\logs", sizeof(g_defaults.dirs[DEFAULT_DIR_LOGS]));

Expand Down

0 comments on commit 69ceb95

Please sign in to comment.