mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/4jcraft.git
synced 2026-09-23 11:18:07 +00:00
refactor: consolidate file I/O into IPlatformFileIO, delete PortableFileIO and PathHelper
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
#include "app/linux/Linux_UIController.h"
|
||||
#include "app/linux/Stubs/winapi_stubs.h"
|
||||
#include "app/include/BufferedImage.h"
|
||||
#include "console_helpers/PortableFileIO.h"
|
||||
#include "platform/PlatformServices.h"
|
||||
#include "java/File.h"
|
||||
#include "minecraft/client/Minecraft.h"
|
||||
#include "minecraft/client/skins/AbstractTexturePack.h"
|
||||
@@ -52,9 +52,9 @@ bool ReadPortableBinaryFile(File& file, std::uint8_t*& data,
|
||||
|
||||
const std::size_t capacity = static_cast<std::size_t>(fileLength);
|
||||
std::uint8_t* buffer = new std::uint8_t[capacity == 0 ? 1 : capacity];
|
||||
const PortableFileIO::BinaryReadResult readResult =
|
||||
PortableFileIO::ReadBinaryFile(file.getPath(), buffer, capacity);
|
||||
if (readResult.status != PortableFileIO::BinaryReadStatus::ok ||
|
||||
auto readResult =
|
||||
PlatformFileIO.readFile(file.getPath(), buffer, capacity);
|
||||
if (readResult.status != IPlatformFileIO::ReadStatus::Ok ||
|
||||
readResult.fileSize > std::numeric_limits<unsigned int>::max()) {
|
||||
delete[] buffer;
|
||||
data = nullptr;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "NbtSlotFile.h"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include "java/File.h"
|
||||
|
||||
namespace {
|
||||
@@ -10,7 +12,7 @@ std::FILE* OpenBinaryFileForReadWrite(const File& file) {
|
||||
stream = _wfopen(file.getPath().c_str(), L"w+b");
|
||||
}
|
||||
#else
|
||||
const std::string nativePath = wstringtofilename(file.getPath());
|
||||
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");
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "ZoneFile.h"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include "java/ByteBuffer.h"
|
||||
#include "java/File.h"
|
||||
|
||||
@@ -11,7 +13,7 @@ std::FILE* OpenBinaryFileForReadWrite(const File& file) {
|
||||
stream = _wfopen(file.getPath().c_str(), L"w+b");
|
||||
}
|
||||
#else
|
||||
const std::string nativePath = wstringtofilename(file.getPath());
|
||||
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");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ZonedChunkStorage.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
|
||||
#include "ZoneFile.h"
|
||||
@@ -66,7 +67,7 @@ ZoneFile* ZonedChunkStorage::getZoneFile(int x, int z, bool create) {
|
||||
|
||||
if (!file.exists()) {
|
||||
if (!create) return nullptr;
|
||||
void* ch = CreateFile(wstringtofilename(file.getPath()),
|
||||
void* ch = CreateFile(std::filesystem::path(file.getPath()).string().c_str(),
|
||||
GENERIC_READ | GENERIC_WRITE, 0, nullptr,
|
||||
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
CloseHandle(ch);
|
||||
|
||||
@@ -4,11 +4,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
|
||||
#include "app/linux/Linux_App.h"
|
||||
#include "platform/PlatformServices.h"
|
||||
#include "minecraft/world/level/biome/Biome.h"
|
||||
#include "minecraft/world/level/chunk/ChunkSource.h"
|
||||
#if defined(__linux__)
|
||||
@@ -45,45 +43,34 @@ CustomLevelSource::CustomLevelSource(Level* level, int64_t seed,
|
||||
m_heightmapOverride =
|
||||
std::vector<uint8_t>((m_XZSize * 16) * (m_XZSize * 16));
|
||||
|
||||
std::filesystem::path path = "GameRules/heightmap.bin";
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if (!file) {
|
||||
app.FatalLoadError();
|
||||
assert(false);
|
||||
} else {
|
||||
auto fileSize = std::filesystem::file_size(path);
|
||||
if (fileSize > m_heightmapOverride.size()) {
|
||||
{
|
||||
const char* path = "GameRules/heightmap.bin";
|
||||
auto result = PlatformFileIO.readFile(
|
||||
path, m_heightmapOverride.data(), m_heightmapOverride.size());
|
||||
if (result.status == IPlatformFileIO::ReadStatus::TooLarge) {
|
||||
app.DebugPrintf("Heightmap binary is too large!!\n");
|
||||
__debugbreak();
|
||||
}
|
||||
file.read(reinterpret_cast<char*>(m_heightmapOverride.data()),
|
||||
static_cast<std::streamsize>(fileSize));
|
||||
|
||||
if (!file) {
|
||||
} else if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||
app.FatalLoadError();
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
m_waterheightOverride =
|
||||
std::vector<uint8_t>((m_XZSize * 16) * (m_XZSize * 16));
|
||||
|
||||
std::filesystem::path waterHeightPath = "GameRules/waterheight.bin";
|
||||
std::ifstream waterHeightFile(waterHeightPath, std::ios::binary);
|
||||
if (!waterHeightFile) {
|
||||
// assert(false);
|
||||
memset(m_waterheightOverride.data(), level->seaLevel,
|
||||
m_waterheightOverride.size());
|
||||
} else {
|
||||
auto waterFileSize = std::filesystem::file_size(waterHeightPath);
|
||||
if (waterFileSize > m_waterheightOverride.size()) {
|
||||
{
|
||||
const char* waterHeightPath = "GameRules/waterheight.bin";
|
||||
auto result = PlatformFileIO.readFile(
|
||||
waterHeightPath, m_waterheightOverride.data(),
|
||||
m_waterheightOverride.size());
|
||||
if (result.status == IPlatformFileIO::ReadStatus::NotFound) {
|
||||
memset(m_waterheightOverride.data(), level->seaLevel,
|
||||
m_waterheightOverride.size());
|
||||
} else if (result.status == IPlatformFileIO::ReadStatus::TooLarge) {
|
||||
app.DebugPrintf("waterheight binary is too large!!\n");
|
||||
__debugbreak();
|
||||
}
|
||||
waterHeightFile.read(
|
||||
reinterpret_cast<char*>(m_waterheightOverride.data()),
|
||||
static_cast<std::streamsize>(waterFileSize));
|
||||
|
||||
if (!waterHeightFile) {
|
||||
} else if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||
app.FatalLoadError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "app/linux/Linux_App.h"
|
||||
#include "platform/PlatformServices.h"
|
||||
#include "minecraft/world/level/newbiome/layer/Layer.h"
|
||||
#if defined(__linux__)
|
||||
#include "app/linux/Stubs/winapi_stubs.h"
|
||||
@@ -15,24 +13,18 @@
|
||||
BiomeOverrideLayer::BiomeOverrideLayer(int seedMixup) : Layer(seedMixup) {
|
||||
m_biomeOverride = std::vector<uint8_t>(width * height);
|
||||
|
||||
std::filesystem::path path = "GameRules/biomemap.bin";
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if (!file) {
|
||||
// assert(false);
|
||||
app.DebugPrintf("Biome override not found, using plains as default\n");
|
||||
|
||||
memset(m_biomeOverride.data(), Biome::plains->id,
|
||||
m_biomeOverride.size());
|
||||
} else {
|
||||
auto fileSize = std::filesystem::file_size(path);
|
||||
if (fileSize > m_biomeOverride.size()) {
|
||||
{
|
||||
const char* path = "GameRules/biomemap.bin";
|
||||
auto result = PlatformFileIO.readFile(
|
||||
path, m_biomeOverride.data(), m_biomeOverride.size());
|
||||
if (result.status == IPlatformFileIO::ReadStatus::NotFound) {
|
||||
app.DebugPrintf("Biome override not found, using plains as default\n");
|
||||
memset(m_biomeOverride.data(), Biome::plains->id,
|
||||
m_biomeOverride.size());
|
||||
} else if (result.status == IPlatformFileIO::ReadStatus::TooLarge) {
|
||||
app.DebugPrintf("Biomemap binary is too large!!\n");
|
||||
__debugbreak();
|
||||
}
|
||||
file.read(reinterpret_cast<char*>(m_biomeOverride.data()),
|
||||
static_cast<std::streamsize>(fileSize));
|
||||
|
||||
if (!file) {
|
||||
} else if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||
app.FatalLoadError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
|
||||
#include "app/linux/Linux_App.h"
|
||||
#include "app/linux/Stubs/winapi_stubs.h"
|
||||
#include "console_helpers/PortableFileIO.h"
|
||||
#include "console_helpers/compression.h"
|
||||
#include "java/File.h"
|
||||
#include "java/InputOutputStream/DataInputStream.h"
|
||||
@@ -744,13 +743,13 @@ void ConsoleSaveFileOriginal::DebugFlushToFile(
|
||||
bool writeSucceeded = false;
|
||||
|
||||
if (compressedData != nullptr && compressedDataSize > 0) {
|
||||
writeSucceeded = PortableFileIO::WriteBinaryFile(
|
||||
writeSucceeded = PlatformFileIO.writeFile(
|
||||
outputPath, compressedData, compressedDataSize);
|
||||
numberOfBytesWritten = writeSucceeded ? compressedDataSize : 0;
|
||||
assert(numberOfBytesWritten == compressedDataSize);
|
||||
} else {
|
||||
writeSucceeded =
|
||||
PortableFileIO::WriteBinaryFile(outputPath, pvSaveMem, fileSize);
|
||||
PlatformFileIO.writeFile(outputPath, pvSaveMem, fileSize);
|
||||
numberOfBytesWritten = writeSucceeded ? fileSize : 0;
|
||||
assert(numberOfBytesWritten == fileSize);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include "app/linux/Linux_App.h"
|
||||
#include "app/linux/Stubs/winapi_stubs.h"
|
||||
#include "console_helpers/PlatformTime.h"
|
||||
#include "console_helpers/PortableFileIO.h"
|
||||
#include "console_helpers/StringHelpers.h"
|
||||
#include "console_helpers/compression.h"
|
||||
#include "java/File.h"
|
||||
@@ -1469,13 +1468,13 @@ void ConsoleSaveFileSplit::DebugFlushToFile(
|
||||
bool writeSucceeded = false;
|
||||
|
||||
if (compressedData != nullptr && compressedDataSize > 0) {
|
||||
writeSucceeded = PortableFileIO::WriteBinaryFile(
|
||||
writeSucceeded = PlatformFileIO.writeFile(
|
||||
outputPath, compressedData, compressedDataSize);
|
||||
numberOfBytesWritten = writeSucceeded ? compressedDataSize : 0;
|
||||
assert(numberOfBytesWritten == compressedDataSize);
|
||||
} else {
|
||||
writeSucceeded =
|
||||
PortableFileIO::WriteBinaryFile(outputPath, pvSaveMem, fileSize);
|
||||
PlatformFileIO.writeFile(outputPath, pvSaveMem, fileSize);
|
||||
numberOfBytesWritten = writeSucceeded ? fileSize : 0;
|
||||
assert(numberOfBytesWritten == fileSize);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user