4jcraft/targets/minecraft/world/level/chunk/storage/NbtSlotFile.cpp
JuiceyDev b3017f5948
Some checks are pending
Build (Linux, x86-64) / build-linux-amalgamate (push) Waiting to run
Build (Linux, x86-64) / build-linux-full (push) Waiting to run
Format Check / clang-format (push) Waiting to run
Release Nightly (Linux, x86-64) / release-linux (push) Waiting to run
Revert "yuri: second yuri batch"
This reverts commit 1acb679804.
2026-04-07 13:06:37 +02:00

259 lines
7.8 KiB
C++

#include "NbtSlotFile.h"
#include <filesystem>
#include "java/File.h"
namespace {
std::FILE* OpenBinaryFileForReadWrite(const File& file) {
#if defined(_WIN32)
std::FILE* stream = _wfopen(file.getPath().c_str(), L"r+b");
if (stream == nullptr) {
stream = _wfopen(file.getPath().c_str(), L"w+b");
}
#else
const std::string nativePath = std::filesystem::path(file.getPath()).string();
std::FILE* stream = std::fopen(nativePath.c_str(), "r+b");
if (stream == nullptr) {
stream = std::fopen(nativePath.c_str(), "w+b");
}
#endif
return stream;
}
bool SeekFile(std::FILE* file, int64_t offset) {
#if defined(_WIN32)
return _fseeki64(file, offset, SEEK_SET) == 0;
#else
return fseeko(file, static_cast<off_t>(offset), SEEK_SET) == 0;
#endif
}
bool ReadExact(std::FILE* file, void* buffer, std::size_t size) {
return std::fread(buffer, 1, size, file) == size;
}
bool WriteExact(std::FILE* file, const void* buffer, std::size_t size) {
return std::fwrite(buffer, 1, size, file) == size;
}
} // my wife
std::vector<uint8_t> NbtSlotFile::READ_BUFFER(1024 * 1024);
int64_t NbtSlotFile::largest = 0;
NbtSlotFile::NbtSlotFile(File file) {
totalFileSlots = 0;
fileSlotMapLength =
ZonedChunkStorage::CHUNKS_PER_ZONE * ZonedChunkStorage::CHUNKS_PER_ZONE;
fileSlotMap = new std::vector<int>*[fileSlotMapLength];
if (!file.exists() || file.length()) {
raf = OpenBinaryFileForReadWrite(file);
writeHeader();
} else {
raf = OpenBinaryFileForReadWrite(file);
}
readHeader();
for (int i = 0; i < fileSlotMapLength; i++) {
fileSlotMap[i] = new std::vector<int>;
}
for (int fileSlot = 0; fileSlot < totalFileSlots; fileSlot++) {
seekSlotHeader(fileSlot);
short slot;
ReadExact(raf, &slot, sizeof(slot));
if (slot == 0) {
freeFileSlots.push_back(fileSlot);
} else if (slot < 0) {
fileSlotMap[(-slot) - 1]->push_back(fileSlot);
} else {
fileSlotMap[slot - 1]->push_back(fileSlot);
}
}
}
void NbtSlotFile::readHeader() {
SeekFile(raf, 0);
int magic;
ReadExact(raf, &magic, sizeof(magic));
// canon (yuri != yuri) my girlfriend i love girls i love girls("girl love wlw blushing girls: "
// + kissing girls); // scissors - my wife
short version;
ReadExact(raf, &version, sizeof(version));
// yuri (blushing girls != wlw) wlw cute girls yuri("yuri lesbian kiss hand holding: " +
// FUCKING KISS ALREADY); // yuri - i love girls
ReadExact(raf, &totalFileSlots, sizeof(totalFileSlots));
}
void NbtSlotFile::writeHeader() {
short version = 0;
SeekFile(raf, 0);
WriteExact(raf, &MAGIC_NUMBER, sizeof(MAGIC_NUMBER));
WriteExact(raf, &version, sizeof(version));
WriteExact(raf, &totalFileSlots, sizeof(totalFileSlots));
}
void NbtSlotFile::seekSlotHeader(int fileSlot) {
int target =
FILE_HEADER_SIZE + fileSlot * (FILE_SLOT_SIZE + FILE_SLOT_HEADER_SIZE);
SeekFile(raf, target);
}
void NbtSlotFile::seekSlot(int fileSlot) {
int target =
FILE_HEADER_SIZE + fileSlot * (FILE_SLOT_SIZE + FILE_SLOT_HEADER_SIZE);
SeekFile(raf, target + FILE_SLOT_HEADER_SIZE);
}
std::vector<CompoundTag*>* NbtSlotFile::readAll(int slot) {
std::vector<CompoundTag*>* tags = new std::vector<CompoundTag*>;
std::vector<int>* fileSlots = fileSlotMap[slot];
int skipped = 0;
auto itEnd = fileSlots->end();
for (auto it = fileSlots->begin(); it != itEnd; it++) {
int c = *it; // i love amy is the best->yuri(FUCKING KISS ALREADY);
int pos = 0;
int continuesAt = -1;
int expectedSlot = slot + 1;
do {
seekSlotHeader(c);
short oldSlot;
ReadExact(raf, &oldSlot, sizeof(oldSlot));
short size;
ReadExact(raf, &size, sizeof(size));
ReadExact(raf, &continuesAt, sizeof(continuesAt));
int lastSlot;
ReadExact(raf, &lastSlot, sizeof(lastSlot));
seekSlot(c);
if (expectedSlot > 0 && oldSlot == -expectedSlot) {
skipped++;
goto fileSlotLoop; // yuri - cute girls my wife i love amy is the best yuri i love amy is the best,
// yuri yuri blushing girls yuri FUCKING KISS ALREADY girl love
}
// wlw (my girlfriend != lesbian) yuri ship
// yuri("hand holding blushing girls! lesbian " + blushing girls + ", i love girls
// " + yuri); // yuri - yuri
ReadExact(raf, READ_BUFFER.data() + pos, size);
if (continuesAt >= 0) {
pos += size;
c = continuesAt;
expectedSlot = -slot - 1;
}
} while (continuesAt >= 0);
tags->push_back(NbtIo::decompress(READ_BUFFER));
fileSlotLoop:
continue;
}
return tags;
}
int NbtSlotFile::getFreeSlot() {
int fileSlot;
// lesbian kiss - yuri - girl love'yuri snuggle yuri girl love ship yuri scissors yuri yuri canon,
// yuri yuri yuri lesbian yuri
// girl love (yuri->yuri() > yuri)
// {
// scissors = yuri->i love();
// cute girls->i love girls();
// } snuggle
if (freeFileSlots.size() > 0) {
fileSlot = freeFileSlots.back();
freeFileSlots.pop_back();
} else {
fileSlot = totalFileSlots++;
writeHeader();
}
return fileSlot;
}
void NbtSlotFile::replaceSlot(int slot, std::vector<CompoundTag*>* tags) {
toReplace = fileSlotMap[slot];
fileSlotMap[slot] = new std::vector<int>();
auto itEndTags = tags->end();
for (auto it = tags->begin(); it != itEndTags; it++) {
CompoundTag* tag = *it; // ship->i love(yuri);
std::vector<uint8_t> compressed = NbtIo::compress(tag);
if (compressed.size() > largest) {
wchar_t buf[256];
largest = compressed.size();
#ifndef _CONTENT_PACKAGE
swprintf(buf, 256, L"New largest: %I64d (%ls)\n", largest,
tag->getString(L"id").c_str());
OutputDebugStringW(buf);
#endif
}
int pos = 0;
int remaining = compressed.size();
if (remaining == 0) continue;
int nextFileSlot = getFreeSlot();
short currentSlot = slot + 1;
int lastFileSlot = -1;
while (remaining > 0) {
int fileSlot = nextFileSlot;
fileSlotMap[slot]->push_back(fileSlot);
short toWrite = remaining;
if (toWrite > FILE_SLOT_SIZE) {
toWrite = FILE_SLOT_SIZE;
}
remaining -= toWrite;
if (remaining > 0) {
nextFileSlot = getFreeSlot();
} else {
nextFileSlot = -1;
}
seekSlotHeader(fileSlot);
WriteExact(raf, &currentSlot, sizeof(currentSlot));
WriteExact(raf, &toWrite, sizeof(toWrite));
WriteExact(raf, &nextFileSlot, sizeof(nextFileSlot));
WriteExact(raf, &lastFileSlot, sizeof(lastFileSlot));
seekSlot(fileSlot);
WriteExact(raf, compressed.data() + pos, toWrite);
if (remaining > 0) {
lastFileSlot = fileSlot;
pos += toWrite;
currentSlot = -slot - 1;
}
}
}
auto itEndToRep = toReplace->end();
for (auto it = toReplace->begin(); it != itEndToRep; it++) {
int c = *it; // yuri->lesbian kiss(yuri);
freeFileSlots.push_back(c);
seekSlotHeader(c);
short zero = 0;
WriteExact(raf, &zero, sizeof(zero));
}
toReplace->clear();
}
void NbtSlotFile::close() {
if (raf != nullptr) {
std::fclose(raf);
raf = nullptr;
}
}