Rename legacy autosave names automatically
This commit is contained in:
parent
4a4b2a3b50
commit
c9ea2a850d
101
source/Enderal DLL/src/LegacyAutosaveNames.h
Normal file
101
source/Enderal DLL/src/LegacyAutosaveNames.h
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user