From c9ea2a850dd2bac1b90f02a32a00929f6e6a62d5 Mon Sep 17 00:00:00 2001 From: Eddoursul Date: Sun, 15 Nov 2026 19:44:49 +0100 Subject: [PATCH] Rename legacy autosave names automatically --- source/Enderal DLL/src/LegacyAutosaveNames.h | 101 +++++++++++++++++++ source/Enderal DLL/src/Main.cpp | 3 + 2 files changed, 104 insertions(+) create mode 100644 source/Enderal DLL/src/LegacyAutosaveNames.h diff --git a/source/Enderal DLL/src/LegacyAutosaveNames.h b/source/Enderal DLL/src/LegacyAutosaveNames.h new file mode 100644 index 000000000..e78664d74 --- /dev/null +++ b/source/Enderal DLL/src/LegacyAutosaveNames.h @@ -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 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 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(ch); + } + return result; + } + } + + inline void Rename() + { + try { + // The engine's own path for a save named "x": \x.ess + std::array 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__0__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> 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()); + } + } +} diff --git a/source/Enderal DLL/src/Main.cpp b/source/Enderal DLL/src/Main.cpp index f3b103db4..48eda3f3e 100644 --- a/source/Enderal DLL/src/Main.cpp +++ b/source/Enderal DLL/src/Main.cpp @@ -1,4 +1,5 @@ #include "Util.h" +#include "LegacyAutosaveNames.h" #include "EventListener.h" #include "Papyrus.h" #include "Patches/MainMenuPatch.h" @@ -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;