Skip to content
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

fix: relative file paths for uvfs #892

Merged
merged 2 commits into from
Oct 23, 2023
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
21 changes: 13 additions & 8 deletions src/scripting/LuaSandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void LuaSandbox::InitializeIOForSandbox(Sandbox& aSandbox, const sol::state& acp
for (const auto& entry : std::filesystem::directory_iterator(path))
{
sol::table item(stateView, sol::create);
item["name"] = UTF16ToUTF8(relative(entry.path(), path).native());
item["name"] = UTF16ToUTF8(Relative(entry.path(), path).native());
item["type"] = entry.is_directory() ? "directory" : "file";
res[index++] = item;
}
Expand Down Expand Up @@ -673,6 +673,15 @@ std::filesystem::path LuaSandbox::GetLuaPath(const std::string& acFilePath, cons
return GetLuaPath(UTF8ToUTF16(acFilePath), acRootPath, acAllowNonExisting);
}

std::filesystem::path LuaSandbox::Relative(const std::filesystem::path& acFilePath, const std::filesystem::path& acRootPath) const
{
if (m_isLaunchedThroughMO2)
{
return acFilePath.lexically_relative(acRootPath);
}
return relative(acFilePath, acRootPath);
}

std::filesystem::path LuaSandbox::GetLuaPath(std::filesystem::path aFilePath, const std::filesystem::path& acRootPath, const bool acAllowNonExisting) const
{
assert(!aFilePath.empty());
Expand All @@ -690,15 +699,11 @@ std::filesystem::path LuaSandbox::GetLuaPath(std::filesystem::path aFilePath, co
if (aFilePath.empty())
return {};

const auto relativeFilePathToRoot = relative(aFilePath, acRootPath);
const auto relativeFilePathToRoot = Relative(aFilePath, acRootPath);
if (relativeFilePathToRoot.native().starts_with(L"..\\") || relativeFilePathToRoot.native().find(L"\\..\\") != std::wstring::npos)
{
// make an exception if path outside of sandbox originates from MO2
if (m_isLaunchedThroughMO2)
{
return aFilePath.lexically_relative(std::filesystem::current_path());
}
return {};
}

return relative(aFilePath, std::filesystem::current_path());
return Relative(aFilePath, std::filesystem::current_path());
}
2 changes: 2 additions & 0 deletions src/scripting/LuaSandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct LuaSandbox
sol::table& GetGlobals();

const bool GetIsLaunchedThroughMO2() const { return m_isLaunchedThroughMO2; }
[[nodiscard]] std::filesystem::path
Relative(const std::filesystem::path& acFilePath, const std::filesystem::path& acRootPath) const;
[[nodiscard]] std::filesystem::path
GetLuaPath(const std::string& acFilePath, const std::filesystem::path& acRootPath, const bool acAllowNonExisting) const;
[[nodiscard]] std::filesystem::path
Expand Down
Loading