Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
powerof3 committed Mar 5, 2024
1 parent dd860c6 commit 49fd4e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 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.1 CACHE STRING "")
set(VERSION 1.9.2 CACHE STRING "")
set(AE_VERSION 1)
set(VR_VERSION 2)

Expand Down
88 changes: 41 additions & 47 deletions src/Fixes/ValidateScreenshotFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

namespace Fixes::ValidateScreenshotFolder
{
RE::Setting* set_ini_string(RE::Setting* a_setting, const char* a_str)
{
using func_t = decltype(&set_ini_string);
REL::Relocation<func_t> func{ RELOCATION_ID(73882, 75619) };
return func(a_setting,a_str);
}

bool has_root_directory(const std::filesystem::path& a_path)
{
auto path = a_path.string();
Expand All @@ -21,63 +28,50 @@ namespace Fixes::ValidateScreenshotFolder
void Install()
{
if (auto setting = RE::GetINISetting("sScreenShotBaseName:Display")) {
try {
std::filesystem::path gameDirectory = std::filesystem::current_path();
gameDirectory.make_preferred();
std::filesystem::path gameDirectory = std::filesystem::current_path();
gameDirectory.make_preferred();

std::filesystem::path screenshotFolder{};
std::string newBaseName{};
std::filesystem::path screenshotFolder{};
std::string newBaseName{};

auto folder = setting->GetString();
bool emptyPath = string::is_empty(folder);
auto folder = setting->GetString();
bool emptyPath = string::is_empty(folder);

if (emptyPath) {
screenshotFolder = gameDirectory;
newBaseName = "Screenshot";
} else {
screenshotFolder = folder;
screenshotFolder.make_preferred();
if (emptyPath) {
screenshotFolder = gameDirectory;
newBaseName = "Screenshot";
} else {
screenshotFolder = folder;
screenshotFolder.make_preferred();

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();
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();
}
}

if (!newBaseName.empty()) {
//to-do: port this to clib
if (setting->data.s) {
RE::free(setting->data.s);
}

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());
}
if (!newBaseName.empty()) {
set_ini_string(setting, newBaseName.c_str());

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 ({}) validated successfully\n", screenshotFolder.string()).c_str());
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());
}
} 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);
} 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());
}
}
}
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.1",
"version-string": "1.9.2",
"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 49fd4e3

Please sign in to comment.