This adds an action to the Game List context menu that lets users link save data from Eden to Ryujinx, or vice versa. Unfortunately, this isn't so simple to deal with due to the way Ryujinx's saves work. Ryujinx stores its saves in the... config directory... in `bis/user/save`. Unlike Yuzu, however, it doesn't store things by TitleID, instead it's just a bunch of directories from 000...01 to 000...0f and so on. The way it *maps* TitleID to SaveID is via `imkvdb.arc` in `bis/system/save/8000000000000000/0/` and also an identical copy in the `1` directory for... some reason. `imkvdb.arc` is handled by `FlatMapKeyValueStore` in LibHac, which, as the name implies, is a key-value storage system that `imkvdb.arc`, and seemingly `imkvdb.arc` alone, uses. The way this class is written is really weird, almost as if it's designed to accommodate more types of kvdbs... but for now we can safely assume that there aren't gonna be any other `kvdb` implementations added to HorizonNX. Regardless, the file format is ridiculously simple so I didn't actually need to do a deep dive into C# code... of which I can basically only read Avalonia. A simple `xxd` on the `imkvdb.arc` is all that's needed, and here's everything that matters: - The `IMKV` magic header (4 bytes) - 8 bytes that don't really have anything useful to us, except for a size byte (presumably a `u32`) strewn at offset `0x08` from the start of the file, which is useless to us - Then we start the `IMEN` list. I don't know what the `IM` stands for, but `IMEN` is just, well, an ENtry. Offsets shown are relative to the start of the `IMEN` header. * 4-byte `IMEN` magic header at 0x0 * 8 bytes of filler data. It contains two `0x40` bytes, but I'm not really sure what they do * TitleID (u64) at `0xC`, for example `00a0 df10 501f 0001` for Legends: Arceus (the byte order is swapped) * 0x38 bytes of filler starting at offset 0x14 * SaveID (u64) at `0x4C`, for example `0a00 0000 0000 0000` for my Legends: Arceus save * 0x38 bytes of filler starting at offset 0x54 Full example for Legends: Arceus: ``` 000001b0: 494d 454e 4000 0000 4000 0000 00a0 df10 IMEN@...@....... 000001c0: 501f 0001 0100 0000 0000 0000 0000 0000 P............... 000001d0: 0000 0000 0000 0000 0000 0000 0100 0000 ................ 000001e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 000001f0: 0000 0000 0000 0000 0000 0000 0a00 0000 ................ 00000200: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000210: 0000 0000 0100 0000 0000 0000 0000 0000 ................ 00000220: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000230: 0000 0000 0000 0000 0000 0000 494d 454e ............IMEN ``` Ultimately, the size of the `IMEN` sits at 0x8C or 140 bytes. With this knowledge reading all the TitleID -> SaveID pairs is basically free, and outside of validation and stuff is like 15 lines of relevant code. Some interesting caveats, though: - There are two entries for some TitleIDs for... some reason? Ignoring the second one seems to work though. - Within each save directory, there are directories `0` and `1`... and only `0` ever seems used??? It's where Ryujinx points you to for save, so I just chose to use that. Once everything is parsed, the rest of the implementation is extremely trivial: - When the user requests a Ryujinx link, match the current program_id to the corresponding SaveID in `imkvdb` - If it doesn't exist, just error out (save data is probably nonexistent) - If it does though, give the user the option to use Eden's current save data OR Ryujinx's current save data. Old save data is deleted depending on which one you chose. Signed-off-by: crueter <crueter@eden-emu.dev> Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2815 Reviewed-by: Lizzie <lizzie@eden-emu.dev> Reviewed-by: MaranBr <maranbr@eden-emu.dev>
94 lines
2.4 KiB
C++
94 lines
2.4 KiB
C++
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "ryujinx_compat.h"
|
|
#include "common/fs/path_util.h"
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <fmt/ranges.h>
|
|
#include <fstream>
|
|
|
|
namespace Common::FS {
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
fs::path GetKvdbPath()
|
|
{
|
|
return GetLegacyPath(EmuPath::RyujinxDir) / "bis" / "system" / "save" / "8000000000000000" / "0"
|
|
/ "imkvdb.arc";
|
|
}
|
|
|
|
fs::path GetRyuSavePath(const u64 &save_id)
|
|
{
|
|
std::string hex = fmt::format("{:016x}", save_id);
|
|
|
|
// TODO: what's the difference between 0 and 1?
|
|
return GetLegacyPath(EmuPath::RyujinxDir) / "bis" / "user" / "save" / hex / "0";
|
|
}
|
|
|
|
IMENReadResult ReadKvdb(const fs::path &path, std::vector<IMEN> &imens)
|
|
{
|
|
std::ifstream kvdb{path, std::ios::binary | std::ios::ate};
|
|
|
|
if (!kvdb) {
|
|
return IMENReadResult::Nonexistent;
|
|
}
|
|
|
|
size_t file_size = kvdb.tellg();
|
|
|
|
// IMKV header + 8 bytes
|
|
if (file_size < 0xB) {
|
|
return IMENReadResult::NoHeader;
|
|
}
|
|
|
|
// magic (not the wizard kind)
|
|
kvdb.seekg(0, std::ios::beg);
|
|
char header[12];
|
|
kvdb.read(header, 12);
|
|
|
|
if (std::memcmp(header, IMKV_MAGIC, 4) != 0) {
|
|
return IMENReadResult::InvalidMagic;
|
|
}
|
|
|
|
// calculate num. of imens left
|
|
std::size_t remaining = (file_size - 12);
|
|
std::size_t num_imens = remaining / IMEN_SIZE;
|
|
|
|
// File is misaligned and probably corrupt (rip)
|
|
if (remaining % IMEN_SIZE != 0) {
|
|
return IMENReadResult::Misaligned;
|
|
}
|
|
|
|
// if there aren't any IMENs, it's empty and we can safely no-op out of here
|
|
if (num_imens == 0) {
|
|
return IMENReadResult::NoImens;
|
|
}
|
|
|
|
imens.reserve(num_imens);
|
|
|
|
// initially I wanted to do a struct, but imkvdb is 140 bytes
|
|
// while the compiler will murder you if you try to align u64 to 4 bytes
|
|
for (std::size_t i = 0; i < num_imens; ++i) {
|
|
char magic[4];
|
|
u64 title_id = 0;
|
|
u64 save_id = 0;
|
|
|
|
kvdb.read(magic, 4);
|
|
if (std::memcmp(magic, IMEN_MAGIC, 4) != 0) {
|
|
return IMENReadResult::InvalidMagic;
|
|
}
|
|
|
|
kvdb.ignore(0x8);
|
|
kvdb.read(reinterpret_cast<char *>(&title_id), 8);
|
|
kvdb.ignore(0x38);
|
|
kvdb.read(reinterpret_cast<char *>(&save_id), 8);
|
|
kvdb.ignore(0x38);
|
|
|
|
imens.emplace_back(IMEN{title_id, save_id});
|
|
}
|
|
|
|
return IMENReadResult::Success;
|
|
}
|
|
|
|
} // namespace Common::FS
|