50 lines
1.7 KiB
C++
50 lines
1.7 KiB
C++
#pragma once
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
namespace gateway::peer_armor_numeric {
|
|
struct Slot {
|
|
unsigned index;
|
|
uint32_t source_item, item, appearance;
|
|
};
|
|
|
|
// The capture path is supplied at process startup. Open a fresh file once so
|
|
// another session cannot extend the bound or overwrite an earlier capture.
|
|
inline const char* capture_path() noexcept {
|
|
static const char* const path = std::getenv("GW_PRIVATE_PEER_ARMOR_NUMERIC");
|
|
return path && *path ? path : nullptr;
|
|
}
|
|
|
|
inline void write(bool create, const std::array<Slot, 19>& slots, size_t count) noexcept {
|
|
const char* path = capture_path();
|
|
if (!path || !count || count > slots.size()) return;
|
|
// Gameplay never waits for another recording thread.
|
|
static std::atomic_flag writing = ATOMIC_FLAG_INIT;
|
|
if (writing.test_and_set(std::memory_order_acquire)) return;
|
|
struct Release {
|
|
~Release() { writing.clear(std::memory_order_release); }
|
|
} release;
|
|
static std::FILE* file = std::fopen(path, "wbx");
|
|
static unsigned remaining = 4096;
|
|
if (!file) return;
|
|
for (size_t i = 0; i < count && remaining; ++i) {
|
|
const auto& s = slots[i];
|
|
// 0 empty, 1 hidden by presentation, 2 native/default lookup, 3 explicit IMA.
|
|
unsigned route = !s.source_item ? 0 : !s.item ? 1 : !s.appearance ? 2 : 3;
|
|
if (std::fprintf(file, "%u,%u,%u,%u,%u,%u\n", create ? 1u : 0u,
|
|
s.index, s.source_item, s.item, s.appearance, route) < 0) {
|
|
remaining = 0;
|
|
break;
|
|
}
|
|
--remaining;
|
|
}
|
|
if (!remaining || std::fflush(file) != 0) {
|
|
std::fclose(file);
|
|
file = nullptr;
|
|
}
|
|
}
|
|
}
|