Skip to content

Commit

Permalink
OSX: Fix base/pref/config paths on Tiger
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Dec 10, 2024
1 parent 8dab6c9 commit faf70b0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#include "track.h"
#include "utils/console.h"
#include "utils/display.h"
#include "utils/file_util.h"
#include "utils/language.h"
#include "utils/parse_int.hpp"
#include "utils/paths.h"
Expand Down Expand Up @@ -2557,6 +2558,11 @@ void setOnInitialized(void (*callback)())

int DiabloMain(int argc, char **argv)
{
#if defined(__APPLE__) && defined(USE_SDL1)
if (argc > 0 && argv[0] != nullptr) {
paths::SetBasePath(Dirname(argv[0]));
}
#endif
#ifdef _DEBUG
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
#endif
Expand Down
2 changes: 1 addition & 1 deletion Source/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ std::vector<std::string> GetMPQSearchPaths()
if (paths[0] == paths[1] || (paths.size() == 3 && (paths[0] == paths[2] || paths[1] == paths[2])))
paths.pop_back();

#if defined(__unix__) && !defined(__ANDROID__)
#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__)
// `XDG_DATA_HOME` is usually the root path of `paths::PrefPath()`, so we only
// add `XDG_DATA_DIRS`.
const char *xdgDataDirs = std::getenv("XDG_DATA_DIRS");
Expand Down
14 changes: 9 additions & 5 deletions Source/utils/paths.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "utils/paths.h"

#include <optional>
#include <string>
#include <string_view>

#include <SDL.h>

#include "appfat.h"
Expand Down Expand Up @@ -131,31 +135,31 @@ const std::string &AssetsPath()
#elif defined(__3DS__) || defined(__SWITCH__)
assetsPath.emplace("romfs:/");
#else
assetsPath.emplace(FromSDL(SDL_GetBasePath()) + ("assets" DIRECTORY_SEPARATOR_STR));
assetsPath.emplace(BasePath() + (DIRECTORY_SEPARATOR_STR "assets" DIRECTORY_SEPARATOR_STR));
#endif
}
return *assetsPath;
}

void SetBasePath(const std::string &path)
void SetBasePath(std::string_view path)
{
basePath = path;
AddTrailingSlash(*basePath);
}

void SetPrefPath(const std::string &path)
void SetPrefPath(std::string_view path)
{
prefPath = path;
AddTrailingSlash(*prefPath);
}

void SetConfigPath(const std::string &path)
void SetConfigPath(std::string_view path)
{
configPath = path;
AddTrailingSlash(*configPath);
}

void SetAssetsPath(const std::string &path)
void SetAssetsPath(std::string_view path)
{
assetsPath = path;
AddTrailingSlash(*assetsPath);
Expand Down
10 changes: 5 additions & 5 deletions Source/utils/paths.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <optional>
#include <string>
#include <string_view>

namespace devilution {

Expand All @@ -12,10 +12,10 @@ const std::string &PrefPath();
const std::string &ConfigPath();
const std::string &AssetsPath();

void SetBasePath(const std::string &path);
void SetPrefPath(const std::string &path);
void SetConfigPath(const std::string &path);
void SetAssetsPath(const std::string &path);
void SetBasePath(std::string_view path);
void SetPrefPath(std::string_view path);
void SetConfigPath(std::string_view path);
void SetAssetsPath(std::string_view path);

} // namespace paths

Expand Down
2 changes: 1 addition & 1 deletion Source/utils/sdl2_to_1_2_backports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ char *SDL_GetPrefPath(const char *org, const char *app)
SDL_SetError("neither XDG_DATA_HOME nor HOME environment is set");
return NULL;
}
#if defined(__unix__) || defined(__unix)
#if defined(__unix__) || defined(__unix) || defined(__APPLE__)
append = "/.local/share/";
#else
append = "/";
Expand Down
8 changes: 8 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "diablo.h"
#include "options.h"
#include "utils/file_util.h"
#include "utils/paths.h"

int main(int argc, char **argv)
{
Expand All @@ -13,6 +15,12 @@ int main(int argc, char **argv)
devilution::sgOptions.Graphics.hardwareCursor.SetValue(false);
#endif

#if defined(__APPLE__) && defined(USE_SDL1)
if (argc > 0 && argv[0] != nullptr) {
devilution::paths::SetBasePath(devilution::Dirname(argv[0]));
}
#endif

testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit faf70b0

Please sign in to comment.