Skip to content

Commit

Permalink
ValidateScreenshotLocation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
powerof3 committed Mar 4, 2024
1 parent c26f37c commit dd860c6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
set(NAME "po3_Tweaks" CACHE STRING "")
set(VERSION 1.9.0 CACHE STRING "")
set(VERSION 1.9.1 CACHE STRING "")
set(AE_VERSION 1)
set(VR_VERSION 2)

Expand Down
89 changes: 52 additions & 37 deletions src/Fixes/ValidateScreenshotFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,72 @@ namespace Fixes::ValidateScreenshotFolder
return path.contains(":\\") || path.contains(":/");
}

bool is_subpath(const std::filesystem::path& a_path, const std::filesystem::path& a_base)
{
const auto mismatch_pair = std::mismatch(a_path.begin(), a_path.end(), a_base.begin(), a_base.end());
return mismatch_pair.second == a_base.end();
}

void Install()
{
if (auto setting = RE::GetINISetting("sScreenShotBaseName:Display")) {
std::filesystem::path screenshotFolder;
std::string newBaseName;
try {
std::filesystem::path gameDirectory = std::filesystem::current_path();
gameDirectory.make_preferred();

auto folder = setting->GetString();
bool emptyPath = string::is_empty(folder);
std::filesystem::path screenshotFolder{};
std::string newBaseName{};

if (emptyPath) {
screenshotFolder = std::filesystem::current_path();
newBaseName = "Screenshot";
} else {
screenshotFolder = folder;
screenshotFolder.make_preferred();
auto folder = setting->GetString();
bool emptyPath = string::is_empty(folder);

if (has_root_directory(screenshotFolder)) {
if (!std::filesystem::exists(screenshotFolder)) {
newBaseName = "Screenshot";
}
if (emptyPath) {
screenshotFolder = gameDirectory;
newBaseName = "Screenshot";
} else {
// folder will be generated in skyrim root
screenshotFolder = std::filesystem::current_path() /= screenshotFolder;
screenshotFolder.remove_filename();
}
}
screenshotFolder = folder;
screenshotFolder.make_preferred();

if (!newBaseName.empty()) {
//to-do: port this to clib
if (setting->data.s) {
RE::free(setting->data.s);
if (has_root_directory(screenshotFolder)) {
if (!is_subpath(screenshotFolder, gameDirectory) && !std::filesystem::exists(screenshotFolder)) {
newBaseName = "Screenshot";
}
} else {
// folder will be generated in skyrim root
screenshotFolder = gameDirectory /= screenshotFolder;
screenshotFolder.remove_filename();
}
}

std::size_t strLen = newBaseName.length() + 1;
setting->data.s = RE::malloc<char>(strLen);
std::memcpy(setting->data.s, newBaseName.c_str(), sizeof(char) * strLen);
if (!newBaseName.empty()) {
//to-do: port this to clib
if (setting->data.s) {
RE::free(setting->data.s);
}

if (emptyPath) {
RE::ConsoleLog::GetSingleton()->Print("[po3 Tweaks] sScreenShotBaseName:Display ini setting is empty");
std::size_t strLen = newBaseName.length() + 1;
setting->data.s = RE::malloc<char>(strLen);
std::memcpy(setting->data.s, newBaseName.c_str(), sizeof(char) * strLen);

if (emptyPath) {
RE::ConsoleLog::GetSingleton()->Print("[po3 Tweaks] sScreenShotBaseName:Display ini setting is empty");
} else {
RE::ConsoleLog::GetSingleton()->Print(std::format("[po3 Tweaks] Screenshot folder ({}) does not exist", screenshotFolder.string()).c_str());
}
RE::ConsoleLog::GetSingleton()->Print(std::format("[po3 Tweaks] Defaulting to {} folder\n", gameDirectory.string()).c_str());
if (emptyPath) {
logger::info("\t\tValidated screenshot location ({})"sv, screenshotFolder.string());
} else {
logger::info("\t\tValidated screenshot location ({} -> {})"sv, screenshotFolder.string(), gameDirectory.string());
}
} else {
RE::ConsoleLog::GetSingleton()->Print(std::format("[po3 Tweaks] Screenshot folder ({}) does not exist", screenshotFolder.string()).c_str());
}
RE::ConsoleLog::GetSingleton()->Print(std::format("[po3 Tweaks] Defaulting to {} folder\n", std::filesystem::current_path().string()).c_str());
if (emptyPath) {
RE::ConsoleLog::GetSingleton()->Print(std::format("[po3 Tweaks] Screenshot folder ({}) validated successfully\n", screenshotFolder.string()).c_str());
logger::info("\t\tValidated screenshot location ({})"sv, screenshotFolder.string());
} else {
logger::info("\t\tValidated screenshot location ({} -> {})"sv, screenshotFolder.string(), std::filesystem::current_path().string());
}
} else {
RE::ConsoleLog::GetSingleton()->Print(std::format("[po3 Tweaks] Screenshot folder ({}) validated successfully\n", screenshotFolder.string()).c_str());
logger::info("\t\tValidated screenshot location ({})"sv, screenshotFolder.string());
} catch (std::exception& e) {
logger::info("\t\tUnable to validate screenshot path (error: {})"sv, e.what());
} catch (...) {
logger::info("\t\tUnable to validate screenshot path (unknown error)"sv);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "po3tweaks",
"version-string": "1.9.0",
"version-string": "1.9.1",
"description": "Collection of bug fixes and tweaks for Skyrim SE/AE/VR",
"homepage": "https://github.com/powerof3/po3-Tweaks/",
"license": "MIT",
Expand Down

0 comments on commit dd860c6

Please sign in to comment.