Skip to content

Expose ACP versions of the base/script/user paths #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion engine/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ IndexedUTF32String IndexUTF8ToUTF32(std::string_view input)
byteIdx += 4;
}
else {
codepoints.push_back(0xFFFDu);
codepoint = 0xFFFDu;
byteIdx += 1;
}
codepoints.push_back(codepoint);
Expand Down
1 change: 1 addition & 0 deletions engine/system/sys_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class sys_IMain {
int processorCount = 0;
std::filesystem::path basePath;
std::optional<std::filesystem::path> userPath;
std::optional<std::string> userPathReason;

virtual int GetTime() = 0;
virtual void Sleep(int msec) = 0;
Expand Down
38 changes: 19 additions & 19 deletions engine/system/win/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ std::filesystem::path FindBasePath()
progPath = basePath.data();
#elif __linux__
char basePath[PATH_MAX];
ssize_t len = ::readlink("/proc/self/exe", basePath, sizeof(basePath));
if (len == -1 || len == sizeof(basePath))
len = 0;
ssize_t len = ::readlink("/proc/self/exe", basePath, sizeof(basePath));
if (len == -1 || len == sizeof(basePath))
len = 0;
basePath[len] = '\0';
progPath = basePath;
#elif __APPLE__ && __MACH__
Expand All @@ -575,34 +575,34 @@ std::filesystem::path FindBasePath()
proc_pidpath(pid, basePath, sizeof(basePath));
progPath = basePath;
#endif
progPath = canonical(progPath);
progPath = weakly_canonical(progPath);
return progPath.parent_path();
}

std::optional<std::filesystem::path> FindUserPath()
std::tuple<std::optional<std::filesystem::path>, std::optional<std::string>> FindUserPath()
{
#ifdef _WIN32
PWSTR osPath{};
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &osPath);
PWSTR osPath{};
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &osPath);
if (FAILED(hr)) {
// The path may be inaccessible due to malfunctioning cloud providers.
CoTaskMemFree(osPath);
return {};
return { {}, "Could not obtain Documents path from Windows" };
}
std::wstring pathStr = osPath;
CoTaskMemFree(osPath);
std::filesystem::path path(pathStr);
return canonical(path);
return { weakly_canonical(path), {} };
#else
if (char const* data_home_path = getenv("XDG_DATA_HOME")) {
return data_home_path;
}
if (char const* home_path = getenv("HOME")) {
return std::filesystem::path(home_path) / ".local/share";
}
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
return std::filesystem::path(pw->pw_dir) / ".local/share";
if (char const* data_home_path = getenv("XDG_DATA_HOME")) {
return { data_home_path, {} };
}
if (char const* home_path = getenv("HOME")) {
return { std::filesystem::path(home_path) / ".local/share", {} };
}
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
return { std::filesystem::path(pw->pw_dir) / ".local/share", {} };
#endif
}

Expand All @@ -628,7 +628,7 @@ sys_main_c::sys_main_c()

// Set the local system information
basePath = FindBasePath();
userPath = FindUserPath();
std::tie(userPath, userPathReason) = FindUserPath();
}

bool sys_main_c::Run(int argc, char** argv)
Expand Down
56 changes: 47 additions & 9 deletions ui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
** compressed = Deflate(uncompressed)
** uncompressed = Inflate(compressed)
** msec = GetTime()
** path = GetScriptPath()
** path = GetRuntimePath()
** path = GetUserPath() -- may return nil if the user path could not be determined
** path[, pathACP[, err]] = GetScriptPath()
** path[, pathACP[, err]] = GetRuntimePath()
** path[, pathACP[, err]] = GetUserPath() -- may return nil if the user path could not be determined
** SetWorkDir("<path>")
** path = GetWorkDir()
** ssID = LaunchSubScript("<scriptText>", "<funcList>", "<subList>"[, ...])
Expand Down Expand Up @@ -1544,25 +1544,63 @@ static int l_GetScriptPath(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
lua_pushstring(L, ui->scriptPath.generic_u8string().c_str());
return 1;
try
{
lua_pushstring(L, ui->scriptPath.generic_string().c_str());
return 2;
}
catch (std::exception& e)
{
lua_pushnil(L);
lua_pushstring(L, e.what());
return 3;
}
}

static int l_GetRuntimePath(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
lua_pushstring(L, ui->sys->basePath.generic_u8string().c_str());
return 1;
try
{
lua_pushstring(L, ui->sys->basePath.generic_string().c_str());
return 2;
}
catch (std::exception& e)
{
lua_pushnil(L);
lua_pushstring(L, e.what());
return 3;
}
}

static int l_GetUserPath(lua_State* L)
{
ui_main_c* ui = GetUIPtr(L);
auto& userPath = ui->sys->userPath;
if (userPath) {
lua_pushstring(L, userPath->generic_u8string().c_str());
return 1;
if (!userPath) {
lua_pushnil(L);
lua_pushnil(L);
if (auto& reason = ui->sys->userPathReason)
{
lua_pushstring(L, reason->c_str());
return 3;
}
return 2;
}

lua_pushstring(L, userPath->generic_u8string().c_str());
try
{
lua_pushstring(L, userPath->generic_string().c_str());
return 2;
}
catch (std::exception& e)
{
lua_pushnil(L);
lua_pushstring(L, e.what());
return 3;
}
return 0;
}

static int l_MakeDir(lua_State* L)
Expand Down
Loading