Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Template for new versions:
## New Features

## Fixes
- several fixes related to changes in file system handling in DF 52.01

## Misc Improvements
- `autoclothing`: added a ``clear`` option to unset previously set orders
Expand Down
10 changes: 10 additions & 0 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,16 @@ unless otherwise noted.
following it for each entry. Set ``include_prefix`` to false if you don't
want the ``path`` string prepended to the returned filenames.

* ``dfhack.filesystem.getBaseDir()``

Returns a directory to which DF (and thus DFHack) can save files. This will either
be DF's install directory, or the path returned by ``SDLGetPrefDir``, depending on whether
DF is in "portable mode" or not.

* ``dfhack.filesystem.getInstallDir()``

Returns the the directory in which DF is installed.

Console API
-----------

Expand Down
2 changes: 2 additions & 0 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,8 @@ static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = {
WRAPM(Filesystem, isdir),
WRAPM(Filesystem, mtime),
WRAPM(Filesystem, canonicalize),
WRAPM(Filesystem, getInstallDir),
WRAPM(Filesystem, getBaseDir),
{NULL, NULL}
};

Expand Down
3 changes: 2 additions & 1 deletion library/LuaTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ distribution.

#include "PluginManager.h"
#include "MiscUtils.h"
#include "modules/Filesystem.h"

#include <lua.h>
#include <lauxlib.h>
Expand Down Expand Up @@ -191,7 +192,7 @@ void df::stl_string_identity::lua_write(lua_State *state, int fname_idx, void *p
void df::path_identity::lua_read(lua_State* state, int fname_idx, void* ptr) const
{
auto ppath = (std::filesystem::path*)ptr;
auto str = ppath->u8string();
auto str = DFHack::Filesystem::as_string(*ppath);
lua_pushlstring(state, (char*)str.data(), str.size());
}

Expand Down
5 changes: 4 additions & 1 deletion library/include/modules/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace DFHack {
DFHACK_EXPORT int listdir_recursive(std::filesystem::path dir, std::map<std::filesystem::path, bool>& files,
int depth = 10, bool include_prefix = true) noexcept;
DFHACK_EXPORT std::filesystem::path canonicalize(std::filesystem::path p) noexcept;
inline std::string as_string(std::filesystem::path path) noexcept
inline std::string as_string(const std::filesystem::path path) noexcept
{
auto pStr = path.string();
if constexpr (std::filesystem::path::preferred_separator != '/')
Expand All @@ -84,5 +84,8 @@ namespace DFHack {
}
return pStr;
}
DFHACK_EXPORT std::filesystem::path getInstallDir() noexcept;
DFHACK_EXPORT std::filesystem::path getBaseDir() noexcept;

}
}
6 changes: 3 additions & 3 deletions library/lua/script-manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ end
-- this perhaps could/should be queried from the Steam API
-- are there any installation configurations where this will be wrong, though?
local WORKSHOP_MODS_PATH = '../../workshop/content/975370/'
local MODS_PATH = 'mods/'
local INSTALLED_MODS_PATH = 'data/installed_mods/'
local MODS_PATH = dfhack.filesystem.getBaseDir() .. 'mods/'
local INSTALLED_MODS_PATH = dfhack.filesystem.getBaseDir() .. 'data/installed_mods/'

-- last instance of the same version of the same mod wins, so read them in this
-- order (in increasing order of liklihood that players may have made custom
Expand Down Expand Up @@ -214,7 +214,7 @@ function get_active_mods()
local ol = df.global.world.object_loader

for idx=0,#ol.object_load_order_id-1 do
local path = ol.object_load_order_src_dir[idx].value
local path = ol.object_load_order_src_dir[idx]
table.insert(mods, {
id=ol.object_load_order_id[idx].value,
name=ol.object_load_order_name[idx].value,
Expand Down
19 changes: 19 additions & 0 deletions library/modules/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ SOFTWARE.
#include <chrono>
#include <iostream>

#include "modules/DFSDL.h"
#include "modules/Filesystem.h"

#include "df/init.h"

using namespace DFHack;

static bool initialized = false;
Expand Down Expand Up @@ -226,3 +229,19 @@ std::filesystem::path Filesystem::canonicalize(std::filesystem::path p) noexcept
return p;
}
}

std::filesystem::path Filesystem::getInstallDir() noexcept
{
return std::filesystem::path{ DFSDL::DFSDL_GetBasePath() };
}

std::filesystem::path Filesystem::getBaseDir() noexcept
{
auto getsavebase = []() {
if (df::global::init->media.flag.is_set(df::enums::init_media_flags::PORTABLE_MODE))
return DFSDL::DFSDL_GetBasePath();
else
return DFSDL::DFSDL_GetPrefPath("Bay 12 Games", "Dwarf Fortress");
};
return std::filesystem::path{ getsavebase() };
}
8 changes: 1 addition & 7 deletions library/modules/Persistence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,7 @@ static std::string filterSaveFileName(std::string s) {
}

static std::filesystem::path getSavePath(const std::string &world) {
auto getsavebase = []() {
if (df::global::init->media.flag.is_set(df::enums::init_media_flags::PORTABLE_MODE))
return DFSDL::DFSDL_GetPrefPath("Bay 12 Games", "Dwarf Fortress");
else
return DFSDL::DFSDL_GetBasePath();
};
std::filesystem::path base{ getsavebase() };
auto base{ Filesystem::getBaseDir() };
return base / "save" / world;
}

Expand Down