refactor: consolidate file I/O into IPlatformFileIO, delete PortableFileIO and PathHelper

This commit is contained in:
MatthewBeshay
2026-04-03 18:57:29 +11:00
parent e4d871cd2f
commit 98e23cfd4d
25 changed files with 220 additions and 379 deletions
+2 -2
View File
@@ -24,7 +24,7 @@
#include "app/include/NetTypes.h"
#include "SkinBox.h"
#include "XboxStubs.h"
#include "console_helpers/PathHelper.h"
#include "platform/PlatformServices.h"
#include "java/Class.h"
#include "java/File.h"
#include "java/Random.h"
@@ -3653,7 +3653,7 @@ void CMinecraftApp::loadMediaArchive() {
if (!mediapath.empty()) {
// boom headshot
#if defined(__linux__)
std::wstring exeDirW = PathHelper::GetExecutableDirW();
std::wstring exeDirW = PlatformFileIO.getBasePath().wstring();
std::wstring candidate = exeDirW + File::pathSeparator + mediapath;
if (File(candidate).exists()) {
m_mediaArchive = new ArchiveFile(File(candidate));
+9 -10
View File
@@ -6,7 +6,6 @@
#include <cmath>
#include <cstdlib>
#include <filesystem>
#include <initializer_list>
#include <memory>
#include <vector>
@@ -17,7 +16,7 @@
#include "app/linux/Iggy/include/rrCore.h"
#include "app/linux/Linux_App.h"
#include "console_helpers/C4JThread.h"
#include "console_helpers/PathHelper.h"
#include "platform/PlatformServices.h"
#include "java/Random.h"
#include "minecraft/client/Minecraft.h"
#include "minecraft/client/multiplayer/MultiPlayerLocalPlayer.h"
@@ -184,7 +183,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume,
for (int i = 0; szId[i]; i++)
if (szId[i] == '.') szId[i] = '/';
std::string base = PathHelper::GetExecutableDirA() + "/";
std::string base = PlatformFileIO.getBasePath().string() + "/";
const char* roots[] = {
"Sound/Minecraft/", "app/common/Sound/Minecraft/",
"app/common/res/TitleUpdate/res/Sound/Minecraft/"};
@@ -198,7 +197,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume,
for (int i = 1; i <= 16; i++) {
char tryP[512];
snprintf(tryP, 512, "%s%s%d%s", fullRoot.c_str(), szId, i, ext);
if (std::filesystem::exists(tryP))
if (PlatformFileIO.exists(tryP))
count = i;
else
break;
@@ -211,7 +210,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume,
}
char tryP[512];
snprintf(tryP, 512, "%s%s%s", fullRoot.c_str(), szId, ext);
if (std::filesystem::exists(tryP)) {
if (PlatformFileIO.exists(tryP)) {
strncpy(finalPath, tryP, 511);
found = true;
break;
@@ -251,7 +250,7 @@ void SoundEngine::playUI(int iSound, float volume, float pitch) {
wcstombs(szIdentifier, wchUISoundNames[iSound], 255);
for (int i = 0; szIdentifier[i]; i++)
if (szIdentifier[i] == '.') szIdentifier[i] = '/';
std::string base = PathHelper::GetExecutableDirA() + "/";
std::string base = PlatformFileIO.getBasePath().string() + "/";
const char* roots[] = {
"Sound/Minecraft/UI/",
"Sound/Minecraft/",
@@ -266,7 +265,7 @@ void SoundEngine::playUI(int iSound, float volume, float pitch) {
char tryP[512];
snprintf(tryP, 512, "%s%s%s%s", base.c_str(), root, szIdentifier,
ext);
if (std::filesystem::exists(tryP)) {
if (PlatformFileIO.exists(tryP)) {
strncpy(finalPath, tryP, 511);
found = true;
break;
@@ -453,7 +452,7 @@ void SoundEngine::playMusicTick() {
return;
}
if (m_musicID != -1) {
std::string base = PathHelper::GetExecutableDirA() + "/";
std::string base = PlatformFileIO.getBasePath().string() + "/";
bool isCD = (m_musicID >= m_iStream_CD_1);
const char* folder = isCD ? "cds/" : "music/";
const char* track = m_szStreamFileA[m_musicID];
@@ -469,14 +468,14 @@ void SoundEngine::playMusicTick() {
// try with folder prefix (music/ or cds/)
snprintf(c, 512, "%s%s%s%s%s", base.c_str(), r, folder,
track, e);
if (std::filesystem::exists(c)) {
if (PlatformFileIO.exists(c)) {
strncpy(m_szStreamName, c, 511);
found = true;
break;
}
// try without folder prefix
snprintf(c, 512, "%s%s%s%s", base.c_str(), r, track, e);
if (std::filesystem::exists(c)) {
if (PlatformFileIO.exists(c)) {
strncpy(m_szStreamName, c, 511);
found = true;
break;
+9 -28
View File
@@ -20,7 +20,7 @@
#include "app/common/src/GameRules/GameRuleManager.h"
#include "app/linux/Linux_App.h"
#include "app/linux/Linux_UIController.h"
#include "console_helpers/PortableFileIO.h"
#include "platform/PlatformServices.h"
#include "console_helpers/StringHelpers.h"
#include "minecraft/client/Minecraft.h"
#include "minecraft/client/skins/TexturePackRepository.h"
@@ -105,40 +105,20 @@ bool readOwnedDlcFile(const std::string& path, std::uint8_t** ppData,
*pBytesRead = 0;
const std::wstring readPath = getMountedDlcReadPath(path);
std::FILE* file = PortableFileIO::OpenBinaryFileForRead(readPath);
if (file == nullptr) {
const std::size_t fSize = PlatformFileIO.fileSize(readPath);
if (fSize == 0 || fSize > std::numeric_limits<unsigned int>::max()) {
return false;
}
if (!PortableFileIO::Seek(file, 0, SEEK_END)) {
std::fclose(file);
return false;
}
const int64_t endPosition = PortableFileIO::Tell(file);
if (endPosition <= 0 ||
endPosition > std::numeric_limits<unsigned int>::max()) {
std::fclose(file);
return false;
}
const unsigned int fileSize = static_cast<unsigned int>(endPosition);
if (!PortableFileIO::Seek(file, 0, SEEK_SET)) {
std::fclose(file);
return false;
}
std::uint8_t* data = new std::uint8_t[fileSize];
const std::size_t bytesRead = std::fread(data, 1, fileSize, file);
const bool failed = std::ferror(file) != 0 || bytesRead != fileSize;
std::fclose(file);
if (failed) {
std::uint8_t* data = new std::uint8_t[fSize];
auto result = PlatformFileIO.readFile(readPath, data, fSize);
if (result.status != IPlatformFileIO::ReadStatus::Ok) {
delete[] data;
return false;
}
*ppData = data;
*pBytesRead = static_cast<unsigned int>(bytesRead);
*pBytesRead = static_cast<unsigned int>(result.bytesRead);
return true;
}
} // namespace
@@ -398,7 +378,8 @@ unsigned int DLCManager::checkForCorruptDLCAndAlert(
bool DLCManager::readDLCDataFile(unsigned int& dwFilesProcessed,
const std::wstring& path, DLCPack* pack,
bool fromArchive) {
return readDLCDataFile(dwFilesProcessed, wstringtofilename(path), pack,
return readDLCDataFile(dwFilesProcessed,
std::filesystem::path(path).string(), pack,
fromArchive);
}
@@ -3,8 +3,6 @@
#include <limits.h>
#include <wchar.h>
#include <filesystem>
#include <fstream>
#include <unordered_set>
#include <utility>
@@ -25,6 +23,7 @@
#include "app/linux/Linux_App.h"
#include "app/linux/Stubs/winapi_stubs.h"
#include "console_helpers/StringHelpers.h"
#include "platform/PlatformServices.h"
#include "java/File.h"
#include "java/InputOutputStream/ByteArrayInputStream.h"
#include "java/InputOutputStream/DataInputStream.h"
@@ -521,16 +520,13 @@ int LevelGenerationOptions::packMounted(void* pParam, int iPad, uint32_t dwErr,
dlcFile->getGrfPath(), true,
L"WPACK:"));
if (grf.exists()) {
std::filesystem::path grfPath = grf.getPath();
std::ifstream fileHandle(grfPath, std::ios::binary);
if (fileHandle) {
uint32_t dwFileSize = grf.length();
uint32_t dwFileSize = grf.length();
if (dwFileSize > 0) {
uint8_t* pbData = (uint8_t*)new uint8_t[dwFileSize];
fileHandle.read(
reinterpret_cast<char*>(pbData),
static_cast<std::streamsize>(dwFileSize));
if (!fileHandle) {
auto readResult = PlatformFileIO.readFile(
grf.getPath(), pbData, dwFileSize);
if (readResult.status !=
IPlatformFileIO::ReadStatus::Ok) {
app.FatalLoadError();
}
@@ -550,15 +546,14 @@ int LevelGenerationOptions::packMounted(void* pParam, int iPad, uint32_t dwErr,
File save(app.getFilePath(lgo->m_parentDLCPack->GetPackID(),
lgo->getBaseSavePath(), true, L"WPACK:"));
if (save.exists()) {
std::filesystem::path savePath = save.getPath();
std::ifstream saveHandle(savePath, std::ios::binary);
if (saveHandle) {
auto dwFileSize = std::filesystem::file_size(savePath);
std::size_t dwFileSize =
PlatformFileIO.fileSize(save.getPath());
if (dwFileSize > 0) {
uint8_t* pbData = (uint8_t*)new uint8_t[dwFileSize];
saveHandle.read(reinterpret_cast<char*>(pbData),
static_cast<std::streamsize>(dwFileSize));
if (!saveHandle) {
auto readResult = PlatformFileIO.readFile(
save.getPath(), pbData, dwFileSize);
if (readResult.status !=
IPlatformFileIO::ReadStatus::Ok) {
app.FatalLoadError();
}
@@ -5,8 +5,6 @@
#include <algorithm>
#include <chrono>
#include <compare>
#include <filesystem>
#include <fstream>
#include <memory>
#include <thread>
#include <vector>
@@ -30,6 +28,7 @@
#include "Socket.h"
#include "XboxStubs.h"
#include "console_helpers/StringHelpers.h"
#include "platform/PlatformServices.h"
#include "console_helpers/ThreadName.h"
#include "console_helpers/compression.h"
#include "java/File.h"
@@ -161,18 +160,15 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft* minecraft,
#endif
File grf(fileRoot);
if (grf.exists()) {
std::filesystem::path grfPath = grf.getPath();
std::ifstream fileHandle(grfPath, std::ios::binary);
if (fileHandle) {
auto dwFileSize =
std::filesystem::file_size(grfPath);
std::size_t dwFileSize =
PlatformFileIO.fileSize(grf.getPath());
if (dwFileSize > 0) {
uint8_t* pbData =
(uint8_t*)new uint8_t[dwFileSize];
fileHandle.read(
reinterpret_cast<char*>(pbData),
static_cast<std::streamsize>(dwFileSize));
if (!fileHandle) {
auto readResult = PlatformFileIO.readFile(
grf.getPath(), pbData, dwFileSize);
if (readResult.status !=
IPlatformFileIO::ReadStatus::Ok) {
app.FatalLoadError();
}
@@ -7,7 +7,7 @@
#include "app/linux/Linux_App.h"
#include "app/linux/Stubs/winapi_stubs.h"
#include "console_helpers/PortableFileIO.h"
#include "platform/PlatformServices.h"
#include "console_helpers/compression.h"
#include "java/InputOutputStream/ByteArrayInputStream.h"
#include "java/InputOutputStream/DataInputStream.h"
@@ -116,12 +116,12 @@ std::vector<uint8_t> ArchiveFile::getFile(const std::wstring& filename) {
const unsigned int fileSize = static_cast<unsigned int>(data->filesize);
std::uint8_t* pbData = new std::uint8_t[fileSize == 0 ? 1 : fileSize];
out = std::vector<uint8_t>(pbData, pbData + fileSize);
const PortableFileIO::BinaryReadResult readResult =
PortableFileIO::ReadBinaryFileSegment(
auto readResult =
PlatformFileIO.readFileSegment(
m_sourcefile.getPath(), static_cast<std::size_t>(data->ptr),
out.data(), static_cast<std::size_t>(data->filesize));
if (readResult.status != PortableFileIO::BinaryReadStatus::ok) {
if (readResult.status != IPlatformFileIO::ReadStatus::Ok) {
app.DebugPrintf("Failed to read archive file segment\n");
app.FatalLoadError();
}
+9 -32
View File
@@ -2,16 +2,14 @@
#include <assert.h>
#include <cstdio>
#include "app/linux/Iggy/include/iggy.h"
#ifndef _ENABLEIGGY
#include "app/linux/Stubs/iggy_stubs.h"
#endif
#include "app/linux/Iggy/include/rrCore.h"
#include "app/linux/Linux_App.h"
#include "console_helpers/PortableFileIO.h"
#include "console_helpers/StringHelpers.h"
#include "platform/PlatformServices.h"
UITTFFont::UITTFFont(const std::string& name, const std::string& path,
S32 fallbackCharacter)
@@ -19,36 +17,14 @@ UITTFFont::UITTFFont(const std::string& name, const std::string& path,
app.DebugPrintf("UITTFFont opening %s\n", path.c_str());
pbData = nullptr;
std::wstring wPath = convStringToWstring(path);
std::FILE* file = PortableFileIO::OpenBinaryFileForRead(wPath);
if (file == nullptr) {
app.DebugPrintf("Failed to open TTF file\n");
assert(false);
}
if (!PortableFileIO::Seek(file, 0, SEEK_END)) {
std::fclose(file);
app.FatalLoadError();
}
const int64_t endPosition = PortableFileIO::Tell(file);
if (endPosition < 0) {
std::fclose(file);
app.FatalLoadError();
}
const std::size_t fileSize = static_cast<std::size_t>(endPosition);
const std::size_t fileSize = PlatformFileIO.fileSize(path);
if (fileSize != 0) {
if (!PortableFileIO::Seek(file, 0, SEEK_SET)) {
std::fclose(file);
app.FatalLoadError();
}
pbData = new std::uint8_t[fileSize];
const std::size_t bytesRead = std::fread(pbData, 1, fileSize, file);
const bool failed = std::ferror(file) != 0 || bytesRead != fileSize;
std::fclose(file);
if (failed) {
auto result = PlatformFileIO.readFile(path, pbData, fileSize);
if (result.status != IPlatformFileIO::ReadStatus::Ok) {
app.DebugPrintf("Failed to open TTF file\n");
delete[] pbData;
pbData = nullptr;
app.FatalLoadError();
}
@@ -65,7 +41,8 @@ UITTFFont::UITTFFont(const std::string& name, const std::string& path,
IggyFontInstallTruetypeUTF8((void*)pbData, IGGY_TTC_INDEX_none, "Arial",
-1, IGGY_FONTFLAG_none);
} else {
std::fclose(file);
app.DebugPrintf("Failed to open TTF file\n");
assert(false);
}
}
+5 -5
View File
@@ -3,7 +3,6 @@
#include <string.h>
#include <cstdint>
#include <filesystem>
#include <string>
#include <vector>
@@ -14,8 +13,8 @@
#include "app/linux/Linux_App.h"
#include "app/linux/Stubs/winapi_stubs.h"
#include "PlatformTypes.h"
#include "console_helpers/PathHelper.h"
#include "console_helpers/StringHelpers.h"
#include "platform/PlatformServices.h"
BufferedImage::BufferedImage(int width, int height, int type) {
data[0] = new int[width * height];
@@ -62,7 +61,7 @@ BufferedImage::BufferedImage(const std::wstring& File,
baseName = baseName.substr(1);
if (baseName.find(L"res/") == 0) baseName = baseName.substr(4);
std::wstring exeDir = PathHelper::GetExecutableDirW();
std::wstring exeDir = PlatformFileIO.getBasePath().wstring();
for (int l = 0; l < 10; l++) {
std::wstring mipSuffix =
@@ -83,7 +82,7 @@ BufferedImage::BufferedImage(const std::wstring& File,
size_t p;
while ((p = attempt.find(L"//")) != std::wstring::npos)
attempt.replace(p, 2, L"/");
if (std::filesystem::exists(wstringtofilename(attempt))) {
if (PlatformFileIO.exists(attempt)) {
finalPath = attempt;
foundOnDisk = true;
break;
@@ -94,7 +93,8 @@ BufferedImage::BufferedImage(const std::wstring& File,
memset(&ImageInfo, 0, sizeof(D3DXIMAGE_INFO));
if (foundOnDisk) {
hr = RenderManager.LoadTextureData(wstringtofilename(finalPath),
std::string nativePath = std::filesystem::path(finalPath).string();
hr = RenderManager.LoadTextureData(nativePath.c_str(),
&ImageInfo, &data[l]);
} else {
std::wstring archiveKey = L"res/" + fileName;