Skip to content

Commit 0eb2c4e

Browse files
authored
Merge pull request #5513 from ab9rf/persistence-basepath
use proper save base path for cosaves
2 parents 749d3b0 + f866394 commit 0eb2c4e

File tree

4 files changed

+64
-34
lines changed

4 files changed

+64
-34
lines changed

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Template for new versions:
5656
## New Features
5757

5858
## Fixes
59+
- Honor the "portable mode" preference setting for locating save folders. Fixes DFHack cosaves not working.
5960

6061
## Misc Improvements
6162

library/include/modules/DFSDL.h

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,60 @@ namespace DFHack
2121
SDL_Rect* rect; // from which coords (NULL to draw whole surface)
2222
SDL_Rect* dstResize; // if not NULL dst rect will be resized (x/y/w/h will be added to original dst)
2323
};
24+
}
2425

2526
/**
2627
* The DFSDL module - provides access to SDL functions without actually
2728
* requiring build-time linkage to SDL
2829
* \ingroup grp_modules
2930
* \ingroup grp_dfsdl
3031
*/
31-
namespace DFSDL
32+
namespace DFHack::DFSDL
3233
{
34+
/**
35+
* Call this on DFHack init so we can load the SDL functions. Returns false on
36+
* failure.
37+
*/
38+
bool init(DFHack::color_ostream& out);
3339

34-
/**
35-
* Call this on DFHack init so we can load the SDL functions. Returns false on
36-
* failure.
37-
*/
38-
bool init(DFHack::color_ostream &out);
40+
/**
41+
* Call this when DFHack is being unloaded.
42+
*/
43+
void cleanup();
3944

40-
/**
41-
* Call this when DFHack is being unloaded.
42-
*/
43-
void cleanup();
45+
DFHACK_EXPORT SDL_Surface* DFIMG_Load(const char* file);
46+
DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
47+
DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurfaceFrom(void* pixels, int width, int height, int depth, int pitch, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
48+
DFHACK_EXPORT int DFSDL_UpperBlit(SDL_Surface* src, const SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
49+
DFHACK_EXPORT SDL_Surface* DFSDL_ConvertSurface(SDL_Surface* src, const SDL_PixelFormat* fmt, uint32_t flags);
50+
DFHACK_EXPORT void DFSDL_FreeSurface(SDL_Surface* surface);
51+
// DFHACK_EXPORT int DFSDL_SemWait(SDL_sem *sem);
52+
// DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem);
53+
DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event* event);
54+
DFHACK_EXPORT void DFSDL_free(void* ptr);
55+
DFHACK_EXPORT SDL_PixelFormat* DFSDL_AllocFormat(uint32_t pixel_format);
56+
DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format);
57+
DFHACK_EXPORT int DFSDL_ShowSimpleMessageBox(uint32_t flags, const char* title, const char* message, SDL_Window* window);
4458

45-
DFHACK_EXPORT SDL_Surface * DFIMG_Load(const char *file);
46-
DFHACK_EXPORT SDL_Surface * DFSDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
47-
DFHACK_EXPORT SDL_Surface * DFSDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
48-
DFHACK_EXPORT int DFSDL_UpperBlit(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
49-
DFHACK_EXPORT SDL_Surface * DFSDL_ConvertSurface(SDL_Surface *src, const SDL_PixelFormat *fmt, uint32_t flags);
50-
DFHACK_EXPORT void DFSDL_FreeSurface(SDL_Surface *surface);
51-
// DFHACK_EXPORT int DFSDL_SemWait(SDL_sem *sem);
52-
// DFHACK_EXPORT int DFSDL_SemPost(SDL_sem *sem);
53-
DFHACK_EXPORT int DFSDL_PushEvent(SDL_Event *event);
54-
DFHACK_EXPORT void DFSDL_free(void *ptr);
55-
DFHACK_EXPORT SDL_PixelFormat* DFSDL_AllocFormat(uint32_t pixel_format);
56-
DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format);
57-
DFHACK_EXPORT int DFSDL_ShowSimpleMessageBox(uint32_t flags, const char *title, const char *message, SDL_Window *window);
59+
// submitted and returned text is UTF-8
60+
// see wrapper functions below for cp-437 variants
61+
DFHACK_EXPORT char* DFSDL_GetClipboardText();
62+
DFHACK_EXPORT int DFSDL_SetClipboardText(const char* text);
5863

59-
// submitted and returned text is UTF-8
60-
// see wrapper functions below for cp-437 variants
61-
DFHACK_EXPORT char * DFSDL_GetClipboardText();
62-
DFHACK_EXPORT int DFSDL_SetClipboardText(const char *text);
64+
DFHACK_EXPORT char* DFSDL_GetPrefPath(const char* org, const char* app);
65+
DFHACK_EXPORT char* DFSDL_GetBasePath();
6366

6467
}
6568

66-
// System clipboard -- submitted and returned text must be in CP437
67-
DFHACK_EXPORT std::string getClipboardTextCp437();
68-
DFHACK_EXPORT bool setClipboardTextCp437(std::string text);
69+
namespace DFHack
70+
{
71+
72+
// System clipboard -- submitted and returned text must be in CP437
73+
DFHACK_EXPORT std::string getClipboardTextCp437();
74+
DFHACK_EXPORT bool setClipboardTextCp437(std::string text);
6975

70-
// interprets 0xa as newline instead of usual CP437 char
71-
DFHACK_EXPORT bool getClipboardTextCp437Multiline(std::vector<std::string> * lines);
72-
DFHACK_EXPORT bool setClipboardTextCp437Multiline(std::string text);
76+
// interprets 0xa as newline instead of usual CP437 char
77+
DFHACK_EXPORT bool getClipboardTextCp437Multiline(std::vector<std::string> * lines);
78+
DFHACK_EXPORT bool setClipboardTextCp437Multiline(std::string text);
7379

7480
}

library/modules/DFSDL.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ void (*g_SDL_free)(void *);
5858
SDL_PixelFormat* (*g_SDL_AllocFormat)(uint32_t pixel_format) = nullptr;
5959
SDL_Surface* (*g_SDL_CreateRGBSurfaceWithFormat)(uint32_t flags, int width, int height, int depth, uint32_t format) = nullptr;
6060
int (*g_SDL_ShowSimpleMessageBox)(uint32_t flags, const char *title, const char *message, SDL_Window *window) = nullptr;
61+
char* (*g_SDL_GetPrefPath)(const char* org, const char* app) = nullptr;
62+
char* (*g_SDL_GetBasePath)() = nullptr;
6163

6264
bool DFSDL::init(color_ostream &out) {
6365
for (auto &lib_str : SDL_LIBS) {
@@ -101,6 +103,8 @@ bool DFSDL::init(color_ostream &out) {
101103
bind(g_sdl_handle, SDL_AllocFormat);
102104
bind(g_sdl_handle, SDL_CreateRGBSurfaceWithFormat);
103105
bind(g_sdl_handle, SDL_ShowSimpleMessageBox);
106+
bind(g_sdl_handle, SDL_GetPrefPath);
107+
bind(g_sdl_handle, SDL_GetBasePath);
104108
#undef bind
105109

106110
DEBUG(dfsdl,out).print("sdl successfully loaded\n");
@@ -175,6 +179,16 @@ SDL_Surface* DFSDL::DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width,
175179
return g_SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format);
176180
}
177181

182+
char* DFSDL::DFSDL_GetPrefPath(const char* org, const char* app)
183+
{
184+
return g_SDL_GetPrefPath(org, app);
185+
}
186+
187+
char* DFSDL::DFSDL_GetBasePath()
188+
{
189+
return g_SDL_GetBasePath();
190+
}
191+
178192
int DFSDL::DFSDL_ShowSimpleMessageBox(uint32_t flags, const char *title, const char *message, SDL_Window *window) {
179193
if (!g_SDL_ShowSimpleMessageBox)
180194
return -1;

library/modules/Persistence.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ distribution.
2929
#include "LuaTools.h"
3030
#include "MemAccess.h"
3131

32+
#include "modules/DFSDL.h"
3233
#include "modules/Filesystem.h"
3334
#include "modules/Gui.h"
3435
#include "modules/Persistence.h"
3536
#include "modules/World.h"
3637

3738
#include "df/world.h"
39+
#include "df/init.h"
3840

3941
#include <json/json.h>
4042

@@ -184,7 +186,14 @@ static std::string filterSaveFileName(std::string s) {
184186
}
185187

186188
static std::filesystem::path getSavePath(const std::string &world) {
187-
return std::filesystem::path{} / "save" / world;
189+
auto getsavebase = []() {
190+
if (df::global::init->media.flag.is_set(df::enums::init_media_flags::PORTABLE_MODE))
191+
return DFSDL::DFSDL_GetPrefPath("Bay 12 Games", "Dwarf Fortress");
192+
else
193+
return DFSDL::DFSDL_GetBasePath();
194+
};
195+
std::filesystem::path base{ getsavebase() };
196+
return base / "save" / world;
188197
}
189198

190199
static std::filesystem::path getSaveFilePath(const std::string &world, const std::string &name) {

0 commit comments

Comments
 (0)