Skip to content

Commit

Permalink
desktop-ui: cleanup resource search paths
Browse files Browse the repository at this point in the history
As well as simplifying the code a little, this also  fixes an oversight where users could not override application resources on macOS.
  • Loading branch information
LukeUsher committed Feb 21, 2024
1 parent d025359 commit 5cdefb0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
51 changes: 24 additions & 27 deletions desktop-ui/desktop-ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,36 @@ namespace ruby {
}

auto locate(const string& name) -> string {
// First, check the application directory
// This allows ares to function in 'portable' mode
// First check each path for the presence of the file we are looking for in the following order
// allowing users to override the default resources if they wish to do so.

// 1. The application directory
string location = {Path::program(), name};
if(inode::exists(location)) return location;

// On macOS, also check the AppBundle Resource path
#if defined(PLATFORM_MACOS)
location = {Path::resources(), name};
if(inode::exists(location)) return location;
#endif

// Check the userData directory, this is the default
// on non-windows platforms for any resouces that did not
// ship with the executable.
// On Windows, this allows settings from to be carried over
// from previous versions (pre-portable)
// 2. The user data directory
location = {Path::userData(), "ares/", name};
if(inode::exists(location)) return location;

// On non-windows platforms, this time check the shared
// data directory, on Windows, default to program dir,
// this ensures Portable mode is the default on Windows platforms.
#if !defined(PLATFORM_WINDOWS)
string shared_location = {Path::sharedData(), "ares/", name};
if(inode::exists(shared_location)) return shared_location;

// On non-windows platforms, after exhausting other options,
// default to userData.
directory::create({Path::userData(), "ares/"});
return {Path::userData(), "ares/", name};
#else
return {Path::program(), name};
#endif
// 3. The shared data directory
location = {Path::sharedData(), "ares/", name};
if(inode::exists(location)) return location;

// 4. The application bundle resource directory (macOS only)
#if defined(PLATFORM_MACOS)
location = {Path::resources(), name};
if(inode::exists(location)) return location;
#endif

// If the file was not found in any of the above locations, we may be intending to create it
// We must return a path to a user writable directory; on Windows, this is the executable directory
#if defined(PLATFORM_WINDOWS)
return {Path::program(), name};
#endif

// On other platforms, this is the "user data" directory
directory::create({Path::userData(), "ares/"});
return {Path::userData(), "ares/", name};
}

#include <nall/main.hpp>
Expand Down
45 changes: 21 additions & 24 deletions mia/mia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,37 @@ function<string ()> homeLocation = [] { return string{Path::user(), "Emulation/S
function<string ()> saveLocation = [] { return string{}; };
vector<string> media;

auto locate(const string& name) -> string {
// First, check the application directory
// This allows ares to function in 'portable' mode
auto locate(const string &name) -> string {
// First check each path for the presence of the file we are looking for in the following order
// allowing users to override the default resources if they wish to do so.

// 1. The application directory
string location = {Path::program(), name};
if(inode::exists(location)) return location;
if (inode::exists(location)) return location;

// 2. The user data directory
location = {Path::userData(), "ares/", name};
if (inode::exists(location)) return location;

// 3. The shared data directory
location = {Path::sharedData(), "ares/", name};
if (inode::exists(location)) return location;

// On macOS, also check the AppBundle Resource path
// 4. The application bundle resource directory (macOS only)
#if defined(PLATFORM_MACOS)
location = {Path::resources(), name};
if(inode::exists(location)) return location;
#endif

// Check the userData directory, this is the default
// on non-windows platforms for any resouces that did not
// ship with the executable.
// On Windows, this allows settings from to be carried over
// from previous versions (pre-portable)
location = {Path::userData(), "ares/", name};
if(inode::exists(location)) return location;

// On non-windows platforms, this time check the shared
// data directory, on Windows, default to program dir
// this ensures Portable mode is the default on Windows platforms.
#if !defined(PLATFORM_WINDOWS)
string shared_location = {Path::sharedData(), "ares/", name};
if(inode::exists(shared_location)) return shared_location;
// If the file was not found in any of the above locations, we may be intending to create it
// We must return a path to a user writable directory; on Windows, this is the executable directory
#if defined(PLATFORM_WINDOWS)
return {Path::program(), name};
#endif

// On non-windows platforms, after exhausting other options,
// default to userData
// On other platforms, this is the "user data" directory
directory::create({Path::userData(), "ares/"});
return {Path::userData(), "ares/", name};
#else
return {Path::program(), name};
#endif
}

auto operator+=(string& lhs, const string& rhs) -> string& {
Expand Down

0 comments on commit 5cdefb0

Please sign in to comment.