Compare commits

..

4 Commits

8 changed files with 117 additions and 37 deletions

View File

@ -149,7 +149,7 @@ message(STATUS "Fetching CommonLibSSE-GG...")
FetchContent_Declare(
CommonLibSSE
GIT_REPOSITORY https://github.com/eddoursul/CommonLibSSE-GG
GIT_TAG 3aa704ee2de9fdba3a2e77cc08e3d3ed8ea6e00f
GIT_TAG 5507da62fef3853b15e6285f9a758b17c58f95b7
)
FetchContent_MakeAvailable(CommonLibSSE)

View File

@ -0,0 +1,101 @@
#pragma once
// Older versions of GetAutosaveName hex-encoded the player name with sign extension, so every byte
// above 0x7F (any non-ASCII UTF-8 character) became FFFFFFxx instead of xx. The load menu's
// character list names a character after the first save file the engine enumerates for it
// (FindFirstFileA order, kept for the whole session), decoding the name out of the file name.
// Timed autosaves (Autosave00_...) sort first, so the list showed the bytes FF FF FF xx.
//
// Rename gives those files the name GetAutosaveName writes now, so they rotate as usual. It must run
// at kInputLoaded: the engine has set the save directory and read sLocalSavePath by then, and it
// builds its first save list only afterwards. A file whose new name is already taken stays as is.
namespace LegacyAutosaveNames
{
namespace detail
{
// The two digit form of a sign extended hex name, or nothing if a_hex is not sign extended
inline std::optional<std::string> FixHex(std::string_view a_hex)
{
if (a_hex.size() % 2) {
return std::nullopt;
}
std::string result;
bool signExtended = false;
for (std::size_t i = 0; i < a_hex.size();) {
if (a_hex.substr(i, 2) != "FF") {
result += a_hex.substr(i, 2);
i += 2;
continue;
}
// No ASCII byte encodes as FF, so FF starts FFFFFFxx, with xx 80 or above
if (a_hex.substr(i, 6) != "FFFFFF" || i + 8 > a_hex.size() || a_hex[i + 6] < '8') {
return std::nullopt;
}
result += a_hex.substr(i + 6, 2);
signExtended = true;
i += 8;
}
return signExtended ? std::optional(result) : std::nullopt;
}
// Save file names are ASCII; anything else in the folder is not ours
inline std::optional<std::string> AsciiFileName(const std::filesystem::path& a_path)
{
std::string result;
for (const auto ch : a_path.filename().native()) {
if (ch > 0x7F) {
return std::nullopt;
}
result += static_cast<char>(ch);
}
return result;
}
}
inline void Rename()
{
try {
// The engine's own path for a save named "x": <save directory>\x.ess
std::array<char, MAX_PATH> buf{};
if (RE::BSWin32SaveDataSystemUtility::GetSingleton()->PrepareFileSavePath("x", buf.data(), false, false) != 0) {
logger::warn("Legacy autosave names: failed to get the save directory");
return;
}
const auto dir = std::filesystem::path(buf.data()).parent_path();
// GetAutosaveName: Autosave0<index>_<character ID>_0_<hex name>_Vyn_000000_00000000000000_1_1,
// followed by .ess, .skse, .ess.bak and so on
static const std::regex pattern(R"((Autosave0\d+_[0-9A-F]{8}_0_)([0-9A-F]+)(_Vyn_000000_00000000000000_1_1\..+))");
std::vector<std::pair<std::string, std::string>> renames;
for (const auto& entry : std::filesystem::directory_iterator(dir)) {
const auto name = detail::AsciiFileName(entry.path());
std::smatch match;
if (!name || !std::regex_match(*name, match, pattern)) {
continue;
}
if (const auto hex = detail::FixHex(match.str(2)); hex) {
renames.emplace_back(*name, match.str(1) + *hex + match.str(3));
}
}
for (const auto& [from, to] : renames) {
std::error_code ec;
if (std::filesystem::exists(dir / to, ec)) {
logger::info("Legacy autosave name kept, {} already exists: {}", to, from);
continue;
}
std::filesystem::rename(dir / from, dir / to, ec);
if (ec) {
logger::warn("Legacy autosave name: failed to rename {}: {}", from, ec.message());
} else {
logger::info("Legacy autosave renamed to {}", to);
}
}
} catch (const std::exception& e) {
logger::error("Legacy autosave names: {}", e.what());
}
}
}

View File

@ -1,4 +1,5 @@
#include "Util.h"
#include "LegacyAutosaveNames.h"
#include "EventListener.h"
#include "Papyrus.h"
#include "Patches/MainMenuPatch.h"
@ -59,7 +60,7 @@ namespace {
path /= "SKSE"sv;
path /= "EnderalSE.log"sv;
auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(path.string(), true);
auto sink = logger::file_sink(path);
auto log = std::make_shared<spdlog::logger>("global log"s, std::move(sink));
log->set_level(spdlog::level::info);
@ -102,6 +103,8 @@ namespace {
MapMarkerPlacement::Install();
}
}
} else if (message->type == MessagingInterface::kInputLoaded) {
LegacyAutosaveNames::Rename();
} else if (message->type == MessagingInterface::kDataLoaded) {
if (REL::Module::get().version() > REL::Version(1, 5, 97, 0)) {
RE::INIPrefSettingCollection::GetSingleton()->GetSetting("bFreebiesSeen:General")->data.b = true;

View File

@ -9,7 +9,6 @@
#include <ShlObj_core.h>
#undef cdecl // Workaround for Clang 14 CMake configure error.
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>
#include <xbyak/xbyak.h>

View File

@ -330,15 +330,15 @@ inline RE::BSFixedString GetPlayerHash()
return buf;
}
inline RE::BSFixedString StringToHex(RE::BSFixedString a_string)
// Two digits per byte, as the engine writes save names ("%.2X"). char is signed, so a byte
// above 0x7F (any UTF-8 non-ASCII character) must be widened as unsigned, or it prints as FFFFFFxx.
inline std::string StringToHex(std::string_view a_string)
{
std::string_view str(a_string);
std::stringstream sstream;
for (auto ch : str) {
sstream << std::uppercase << std::hex << static_cast<int>(ch);
std::string result;
result.reserve(a_string.size() * 2);
for (const auto ch : a_string) {
result += std::format("{:02X}", static_cast<std::uint8_t>(ch));
}
return sstream.str();
return result;
}

View File

@ -157,7 +157,7 @@ message(STATUS "Fetching CommonLibSSE-GG...")
FetchContent_Declare(
CommonLibSSE
GIT_REPOSITORY https://github.com/eddoursul/CommonLibSSE-GG
GIT_TAG 3aa704ee2de9fdba3a2e77cc08e3d3ed8ea6e00f
GIT_TAG 5507da62fef3853b15e6285f9a758b17c58f95b7
)
FetchContent_MakeAvailable(CommonLibSSE)

View File

@ -5,40 +5,18 @@
using namespace SKSE;
namespace {
void InitializeLogging() {
auto path = logger::log_directory();
if (!path) {
SKSE::stl::report_and_fail("Failed to find standard logging directory"sv);
}
*path /= "EnderalSteam.log"sv;
auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(path->string(), true);
auto log = std::make_shared<spdlog::logger>("global log"s, std::move(sink));
log->set_level(spdlog::level::info);
log->flush_on(spdlog::level::info);
spdlog::set_default_logger(std::move(log));
spdlog::set_pattern("[%l] %v"s);
}
}
SKSEPluginLoad(const LoadInterface* skse) {
GetLoadInterface(skse);
InitializeLogging();
// Also opens EnderalSteam.log, through SKSE::log::init()
Init(skse);
auto* plugin = PluginDeclaration::GetSingleton();
auto version = plugin->GetVersion();
logger::info("{} {} is loading...", plugin->GetName(), version);
logger::info("Built against Steamworks SDK {}", ENDERAL_STEAM_SDK);
Init(skse);
// SDK 1.55: has to happen here, not from a game event: the game opens its own Steam session
// from BSWin32SystemUtility during WinMain, and whichever session is opened first
// decides which AppID the process stays registered as.

View File

@ -104,7 +104,6 @@
#include <Psapi.h>
#undef cdecl // Workaround for Clang 14 CMake configure error.
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>
// Compatible declarations with other sample projects.