Skip to content

Commit

Permalink
Merge pull request #892 from rfuzzo/feat/uvfs_support
Browse files Browse the repository at this point in the history
fix: relative file paths for uvfs
  • Loading branch information
WSSDude committed Oct 23, 2023
2 parents fbfe95b + ac08c10 commit caa375a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
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

0 comments on commit caa375a

Please sign in to comment.