mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-08-20 09:57:10 +00:00
refactor: consolidate file I/O into IPlatformFileIO, delete PortableFileIO and PathHelper
This commit is contained in:
parent
e4d871cd2f
commit
98e23cfd4d
|
|
@ -24,7 +24,7 @@
|
||||||
#include "app/include/NetTypes.h"
|
#include "app/include/NetTypes.h"
|
||||||
#include "SkinBox.h"
|
#include "SkinBox.h"
|
||||||
#include "XboxStubs.h"
|
#include "XboxStubs.h"
|
||||||
#include "console_helpers/PathHelper.h"
|
#include "platform/PlatformServices.h"
|
||||||
#include "java/Class.h"
|
#include "java/Class.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
#include "java/Random.h"
|
#include "java/Random.h"
|
||||||
|
|
@ -3653,7 +3653,7 @@ void CMinecraftApp::loadMediaArchive() {
|
||||||
if (!mediapath.empty()) {
|
if (!mediapath.empty()) {
|
||||||
// boom headshot
|
// boom headshot
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
std::wstring exeDirW = PathHelper::GetExecutableDirW();
|
std::wstring exeDirW = PlatformFileIO.getBasePath().wstring();
|
||||||
std::wstring candidate = exeDirW + File::pathSeparator + mediapath;
|
std::wstring candidate = exeDirW + File::pathSeparator + mediapath;
|
||||||
if (File(candidate).exists()) {
|
if (File(candidate).exists()) {
|
||||||
m_mediaArchive = new ArchiveFile(File(candidate));
|
m_mediaArchive = new ArchiveFile(File(candidate));
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <filesystem>
|
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -17,7 +16,7 @@
|
||||||
#include "app/linux/Iggy/include/rrCore.h"
|
#include "app/linux/Iggy/include/rrCore.h"
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "console_helpers/C4JThread.h"
|
#include "console_helpers/C4JThread.h"
|
||||||
#include "console_helpers/PathHelper.h"
|
#include "platform/PlatformServices.h"
|
||||||
#include "java/Random.h"
|
#include "java/Random.h"
|
||||||
#include "minecraft/client/Minecraft.h"
|
#include "minecraft/client/Minecraft.h"
|
||||||
#include "minecraft/client/multiplayer/MultiPlayerLocalPlayer.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++)
|
for (int i = 0; szId[i]; i++)
|
||||||
if (szId[i] == '.') szId[i] = '/';
|
if (szId[i] == '.') szId[i] = '/';
|
||||||
|
|
||||||
std::string base = PathHelper::GetExecutableDirA() + "/";
|
std::string base = PlatformFileIO.getBasePath().string() + "/";
|
||||||
const char* roots[] = {
|
const char* roots[] = {
|
||||||
"Sound/Minecraft/", "app/common/Sound/Minecraft/",
|
"Sound/Minecraft/", "app/common/Sound/Minecraft/",
|
||||||
"app/common/res/TitleUpdate/res/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++) {
|
for (int i = 1; i <= 16; i++) {
|
||||||
char tryP[512];
|
char tryP[512];
|
||||||
snprintf(tryP, 512, "%s%s%d%s", fullRoot.c_str(), szId, i, ext);
|
snprintf(tryP, 512, "%s%s%d%s", fullRoot.c_str(), szId, i, ext);
|
||||||
if (std::filesystem::exists(tryP))
|
if (PlatformFileIO.exists(tryP))
|
||||||
count = i;
|
count = i;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
|
|
@ -211,7 +210,7 @@ void SoundEngine::play(int iSound, float x, float y, float z, float volume,
|
||||||
}
|
}
|
||||||
char tryP[512];
|
char tryP[512];
|
||||||
snprintf(tryP, 512, "%s%s%s", fullRoot.c_str(), szId, ext);
|
snprintf(tryP, 512, "%s%s%s", fullRoot.c_str(), szId, ext);
|
||||||
if (std::filesystem::exists(tryP)) {
|
if (PlatformFileIO.exists(tryP)) {
|
||||||
strncpy(finalPath, tryP, 511);
|
strncpy(finalPath, tryP, 511);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -251,7 +250,7 @@ void SoundEngine::playUI(int iSound, float volume, float pitch) {
|
||||||
wcstombs(szIdentifier, wchUISoundNames[iSound], 255);
|
wcstombs(szIdentifier, wchUISoundNames[iSound], 255);
|
||||||
for (int i = 0; szIdentifier[i]; i++)
|
for (int i = 0; szIdentifier[i]; i++)
|
||||||
if (szIdentifier[i] == '.') szIdentifier[i] = '/';
|
if (szIdentifier[i] == '.') szIdentifier[i] = '/';
|
||||||
std::string base = PathHelper::GetExecutableDirA() + "/";
|
std::string base = PlatformFileIO.getBasePath().string() + "/";
|
||||||
const char* roots[] = {
|
const char* roots[] = {
|
||||||
"Sound/Minecraft/UI/",
|
"Sound/Minecraft/UI/",
|
||||||
"Sound/Minecraft/",
|
"Sound/Minecraft/",
|
||||||
|
|
@ -266,7 +265,7 @@ void SoundEngine::playUI(int iSound, float volume, float pitch) {
|
||||||
char tryP[512];
|
char tryP[512];
|
||||||
snprintf(tryP, 512, "%s%s%s%s", base.c_str(), root, szIdentifier,
|
snprintf(tryP, 512, "%s%s%s%s", base.c_str(), root, szIdentifier,
|
||||||
ext);
|
ext);
|
||||||
if (std::filesystem::exists(tryP)) {
|
if (PlatformFileIO.exists(tryP)) {
|
||||||
strncpy(finalPath, tryP, 511);
|
strncpy(finalPath, tryP, 511);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -453,7 +452,7 @@ void SoundEngine::playMusicTick() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_musicID != -1) {
|
if (m_musicID != -1) {
|
||||||
std::string base = PathHelper::GetExecutableDirA() + "/";
|
std::string base = PlatformFileIO.getBasePath().string() + "/";
|
||||||
bool isCD = (m_musicID >= m_iStream_CD_1);
|
bool isCD = (m_musicID >= m_iStream_CD_1);
|
||||||
const char* folder = isCD ? "cds/" : "music/";
|
const char* folder = isCD ? "cds/" : "music/";
|
||||||
const char* track = m_szStreamFileA[m_musicID];
|
const char* track = m_szStreamFileA[m_musicID];
|
||||||
|
|
@ -469,14 +468,14 @@ void SoundEngine::playMusicTick() {
|
||||||
// try with folder prefix (music/ or cds/)
|
// try with folder prefix (music/ or cds/)
|
||||||
snprintf(c, 512, "%s%s%s%s%s", base.c_str(), r, folder,
|
snprintf(c, 512, "%s%s%s%s%s", base.c_str(), r, folder,
|
||||||
track, e);
|
track, e);
|
||||||
if (std::filesystem::exists(c)) {
|
if (PlatformFileIO.exists(c)) {
|
||||||
strncpy(m_szStreamName, c, 511);
|
strncpy(m_szStreamName, c, 511);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// try without folder prefix
|
// try without folder prefix
|
||||||
snprintf(c, 512, "%s%s%s%s", base.c_str(), r, track, e);
|
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);
|
strncpy(m_szStreamName, c, 511);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
#include "app/common/src/GameRules/GameRuleManager.h"
|
#include "app/common/src/GameRules/GameRuleManager.h"
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "app/linux/Linux_UIController.h"
|
#include "app/linux/Linux_UIController.h"
|
||||||
#include "console_helpers/PortableFileIO.h"
|
#include "platform/PlatformServices.h"
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include "console_helpers/StringHelpers.h"
|
||||||
#include "minecraft/client/Minecraft.h"
|
#include "minecraft/client/Minecraft.h"
|
||||||
#include "minecraft/client/skins/TexturePackRepository.h"
|
#include "minecraft/client/skins/TexturePackRepository.h"
|
||||||
|
|
@ -105,40 +105,20 @@ bool readOwnedDlcFile(const std::string& path, std::uint8_t** ppData,
|
||||||
*pBytesRead = 0;
|
*pBytesRead = 0;
|
||||||
|
|
||||||
const std::wstring readPath = getMountedDlcReadPath(path);
|
const std::wstring readPath = getMountedDlcReadPath(path);
|
||||||
std::FILE* file = PortableFileIO::OpenBinaryFileForRead(readPath);
|
const std::size_t fSize = PlatformFileIO.fileSize(readPath);
|
||||||
if (file == nullptr) {
|
if (fSize == 0 || fSize > std::numeric_limits<unsigned int>::max()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!PortableFileIO::Seek(file, 0, SEEK_END)) {
|
std::uint8_t* data = new std::uint8_t[fSize];
|
||||||
std::fclose(file);
|
auto result = PlatformFileIO.readFile(readPath, data, fSize);
|
||||||
return false;
|
if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
delete[] data;
|
delete[] data;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ppData = data;
|
*ppData = data;
|
||||||
*pBytesRead = static_cast<unsigned int>(bytesRead);
|
*pBytesRead = static_cast<unsigned int>(result.bytesRead);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
@ -398,7 +378,8 @@ unsigned int DLCManager::checkForCorruptDLCAndAlert(
|
||||||
bool DLCManager::readDLCDataFile(unsigned int& dwFilesProcessed,
|
bool DLCManager::readDLCDataFile(unsigned int& dwFilesProcessed,
|
||||||
const std::wstring& path, DLCPack* pack,
|
const std::wstring& path, DLCPack* pack,
|
||||||
bool fromArchive) {
|
bool fromArchive) {
|
||||||
return readDLCDataFile(dwFilesProcessed, wstringtofilename(path), pack,
|
return readDLCDataFile(dwFilesProcessed,
|
||||||
|
std::filesystem::path(path).string(), pack,
|
||||||
fromArchive);
|
fromArchive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
|
@ -25,6 +23,7 @@
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include "console_helpers/StringHelpers.h"
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
#include "java/InputOutputStream/ByteArrayInputStream.h"
|
#include "java/InputOutputStream/ByteArrayInputStream.h"
|
||||||
#include "java/InputOutputStream/DataInputStream.h"
|
#include "java/InputOutputStream/DataInputStream.h"
|
||||||
|
|
@ -521,16 +520,13 @@ int LevelGenerationOptions::packMounted(void* pParam, int iPad, uint32_t dwErr,
|
||||||
dlcFile->getGrfPath(), true,
|
dlcFile->getGrfPath(), true,
|
||||||
L"WPACK:"));
|
L"WPACK:"));
|
||||||
if (grf.exists()) {
|
if (grf.exists()) {
|
||||||
std::filesystem::path grfPath = grf.getPath();
|
uint32_t dwFileSize = grf.length();
|
||||||
std::ifstream fileHandle(grfPath, std::ios::binary);
|
if (dwFileSize > 0) {
|
||||||
|
|
||||||
if (fileHandle) {
|
|
||||||
uint32_t dwFileSize = grf.length();
|
|
||||||
uint8_t* pbData = (uint8_t*)new uint8_t[dwFileSize];
|
uint8_t* pbData = (uint8_t*)new uint8_t[dwFileSize];
|
||||||
fileHandle.read(
|
auto readResult = PlatformFileIO.readFile(
|
||||||
reinterpret_cast<char*>(pbData),
|
grf.getPath(), pbData, dwFileSize);
|
||||||
static_cast<std::streamsize>(dwFileSize));
|
if (readResult.status !=
|
||||||
if (!fileHandle) {
|
IPlatformFileIO::ReadStatus::Ok) {
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -550,15 +546,14 @@ int LevelGenerationOptions::packMounted(void* pParam, int iPad, uint32_t dwErr,
|
||||||
File save(app.getFilePath(lgo->m_parentDLCPack->GetPackID(),
|
File save(app.getFilePath(lgo->m_parentDLCPack->GetPackID(),
|
||||||
lgo->getBaseSavePath(), true, L"WPACK:"));
|
lgo->getBaseSavePath(), true, L"WPACK:"));
|
||||||
if (save.exists()) {
|
if (save.exists()) {
|
||||||
std::filesystem::path savePath = save.getPath();
|
std::size_t dwFileSize =
|
||||||
std::ifstream saveHandle(savePath, std::ios::binary);
|
PlatformFileIO.fileSize(save.getPath());
|
||||||
|
if (dwFileSize > 0) {
|
||||||
if (saveHandle) {
|
|
||||||
auto dwFileSize = std::filesystem::file_size(savePath);
|
|
||||||
uint8_t* pbData = (uint8_t*)new uint8_t[dwFileSize];
|
uint8_t* pbData = (uint8_t*)new uint8_t[dwFileSize];
|
||||||
saveHandle.read(reinterpret_cast<char*>(pbData),
|
auto readResult = PlatformFileIO.readFile(
|
||||||
static_cast<std::streamsize>(dwFileSize));
|
save.getPath(), pbData, dwFileSize);
|
||||||
if (!saveHandle) {
|
if (readResult.status !=
|
||||||
|
IPlatformFileIO::ReadStatus::Ok) {
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <compare>
|
#include <compare>
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -30,6 +28,7 @@
|
||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
#include "XboxStubs.h"
|
#include "XboxStubs.h"
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include "console_helpers/StringHelpers.h"
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
#include "console_helpers/ThreadName.h"
|
#include "console_helpers/ThreadName.h"
|
||||||
#include "console_helpers/compression.h"
|
#include "console_helpers/compression.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
|
|
@ -161,18 +160,15 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft* minecraft,
|
||||||
#endif
|
#endif
|
||||||
File grf(fileRoot);
|
File grf(fileRoot);
|
||||||
if (grf.exists()) {
|
if (grf.exists()) {
|
||||||
std::filesystem::path grfPath = grf.getPath();
|
std::size_t dwFileSize =
|
||||||
std::ifstream fileHandle(grfPath, std::ios::binary);
|
PlatformFileIO.fileSize(grf.getPath());
|
||||||
|
if (dwFileSize > 0) {
|
||||||
if (fileHandle) {
|
|
||||||
auto dwFileSize =
|
|
||||||
std::filesystem::file_size(grfPath);
|
|
||||||
uint8_t* pbData =
|
uint8_t* pbData =
|
||||||
(uint8_t*)new uint8_t[dwFileSize];
|
(uint8_t*)new uint8_t[dwFileSize];
|
||||||
fileHandle.read(
|
auto readResult = PlatformFileIO.readFile(
|
||||||
reinterpret_cast<char*>(pbData),
|
grf.getPath(), pbData, dwFileSize);
|
||||||
static_cast<std::streamsize>(dwFileSize));
|
if (readResult.status !=
|
||||||
if (!fileHandle) {
|
IPlatformFileIO::ReadStatus::Ok) {
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
#include "console_helpers/PortableFileIO.h"
|
#include "platform/PlatformServices.h"
|
||||||
#include "console_helpers/compression.h"
|
#include "console_helpers/compression.h"
|
||||||
#include "java/InputOutputStream/ByteArrayInputStream.h"
|
#include "java/InputOutputStream/ByteArrayInputStream.h"
|
||||||
#include "java/InputOutputStream/DataInputStream.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);
|
const unsigned int fileSize = static_cast<unsigned int>(data->filesize);
|
||||||
std::uint8_t* pbData = new std::uint8_t[fileSize == 0 ? 1 : fileSize];
|
std::uint8_t* pbData = new std::uint8_t[fileSize == 0 ? 1 : fileSize];
|
||||||
out = std::vector<uint8_t>(pbData, pbData + fileSize);
|
out = std::vector<uint8_t>(pbData, pbData + fileSize);
|
||||||
const PortableFileIO::BinaryReadResult readResult =
|
auto readResult =
|
||||||
PortableFileIO::ReadBinaryFileSegment(
|
PlatformFileIO.readFileSegment(
|
||||||
m_sourcefile.getPath(), static_cast<std::size_t>(data->ptr),
|
m_sourcefile.getPath(), static_cast<std::size_t>(data->ptr),
|
||||||
out.data(), static_cast<std::size_t>(data->filesize));
|
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.DebugPrintf("Failed to read archive file segment\n");
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,14 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
|
|
||||||
#include "app/linux/Iggy/include/iggy.h"
|
#include "app/linux/Iggy/include/iggy.h"
|
||||||
#ifndef _ENABLEIGGY
|
#ifndef _ENABLEIGGY
|
||||||
#include "app/linux/Stubs/iggy_stubs.h"
|
#include "app/linux/Stubs/iggy_stubs.h"
|
||||||
#endif
|
#endif
|
||||||
#include "app/linux/Iggy/include/rrCore.h"
|
#include "app/linux/Iggy/include/rrCore.h"
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "console_helpers/PortableFileIO.h"
|
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include "console_helpers/StringHelpers.h"
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
|
|
||||||
UITTFFont::UITTFFont(const std::string& name, const std::string& path,
|
UITTFFont::UITTFFont(const std::string& name, const std::string& path,
|
||||||
S32 fallbackCharacter)
|
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());
|
app.DebugPrintf("UITTFFont opening %s\n", path.c_str());
|
||||||
pbData = nullptr;
|
pbData = nullptr;
|
||||||
|
|
||||||
std::wstring wPath = convStringToWstring(path);
|
const std::size_t fileSize = PlatformFileIO.fileSize(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);
|
|
||||||
if (fileSize != 0) {
|
if (fileSize != 0) {
|
||||||
if (!PortableFileIO::Seek(file, 0, SEEK_SET)) {
|
|
||||||
std::fclose(file);
|
|
||||||
app.FatalLoadError();
|
|
||||||
}
|
|
||||||
|
|
||||||
pbData = new std::uint8_t[fileSize];
|
pbData = new std::uint8_t[fileSize];
|
||||||
const std::size_t bytesRead = std::fread(pbData, 1, fileSize, file);
|
auto result = PlatformFileIO.readFile(path, pbData, fileSize);
|
||||||
const bool failed = std::ferror(file) != 0 || bytesRead != fileSize;
|
if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||||
std::fclose(file);
|
app.DebugPrintf("Failed to open TTF file\n");
|
||||||
if (failed) {
|
delete[] pbData;
|
||||||
|
pbData = nullptr;
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,7 +41,8 @@ UITTFFont::UITTFFont(const std::string& name, const std::string& path,
|
||||||
IggyFontInstallTruetypeUTF8((void*)pbData, IGGY_TTC_INDEX_none, "Arial",
|
IggyFontInstallTruetypeUTF8((void*)pbData, IGGY_TTC_INDEX_none, "Arial",
|
||||||
-1, IGGY_FONTFLAG_none);
|
-1, IGGY_FONTFLAG_none);
|
||||||
} else {
|
} else {
|
||||||
std::fclose(file);
|
app.DebugPrintf("Failed to open TTF file\n");
|
||||||
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <filesystem>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
@ -14,8 +13,8 @@
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
#include "PlatformTypes.h"
|
#include "PlatformTypes.h"
|
||||||
#include "console_helpers/PathHelper.h"
|
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include "console_helpers/StringHelpers.h"
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
|
|
||||||
BufferedImage::BufferedImage(int width, int height, int type) {
|
BufferedImage::BufferedImage(int width, int height, int type) {
|
||||||
data[0] = new int[width * height];
|
data[0] = new int[width * height];
|
||||||
|
|
@ -62,7 +61,7 @@ BufferedImage::BufferedImage(const std::wstring& File,
|
||||||
baseName = baseName.substr(1);
|
baseName = baseName.substr(1);
|
||||||
if (baseName.find(L"res/") == 0) baseName = baseName.substr(4);
|
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++) {
|
for (int l = 0; l < 10; l++) {
|
||||||
std::wstring mipSuffix =
|
std::wstring mipSuffix =
|
||||||
|
|
@ -83,7 +82,7 @@ BufferedImage::BufferedImage(const std::wstring& File,
|
||||||
size_t p;
|
size_t p;
|
||||||
while ((p = attempt.find(L"//")) != std::wstring::npos)
|
while ((p = attempt.find(L"//")) != std::wstring::npos)
|
||||||
attempt.replace(p, 2, L"/");
|
attempt.replace(p, 2, L"/");
|
||||||
if (std::filesystem::exists(wstringtofilename(attempt))) {
|
if (PlatformFileIO.exists(attempt)) {
|
||||||
finalPath = attempt;
|
finalPath = attempt;
|
||||||
foundOnDisk = true;
|
foundOnDisk = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -94,7 +93,8 @@ BufferedImage::BufferedImage(const std::wstring& File,
|
||||||
memset(&ImageInfo, 0, sizeof(D3DXIMAGE_INFO));
|
memset(&ImageInfo, 0, sizeof(D3DXIMAGE_INFO));
|
||||||
|
|
||||||
if (foundOnDisk) {
|
if (foundOnDisk) {
|
||||||
hr = RenderManager.LoadTextureData(wstringtofilename(finalPath),
|
std::string nativePath = std::filesystem::path(finalPath).string();
|
||||||
|
hr = RenderManager.LoadTextureData(nativePath.c_str(),
|
||||||
&ImageInfo, &data[l]);
|
&ImageInfo, &data[l]);
|
||||||
} else {
|
} else {
|
||||||
std::wstring archiveKey = L"res/" + fileName;
|
std::wstring archiveKey = L"res/" + fileName;
|
||||||
|
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
#pragma once
|
|
||||||
#include <string>
|
|
||||||
#if defined(__linux__)
|
|
||||||
#include <limits.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace PathHelper {
|
|
||||||
inline std::wstring GetExecutableDirW() {
|
|
||||||
#if defined(__linux__)
|
|
||||||
char buffer[4096];
|
|
||||||
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
|
||||||
if (len != -1) {
|
|
||||||
buffer[len] = '\0';
|
|
||||||
std::string path(buffer);
|
|
||||||
size_t lastSlash = path.find_last_of('/');
|
|
||||||
if (lastSlash != std::string::npos)
|
|
||||||
return std::wstring(path.begin(), path.begin() + lastSlash);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return L".";
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::string GetExecutableDirA() {
|
|
||||||
#if defined(__linux__)
|
|
||||||
char buffer[4096];
|
|
||||||
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
|
|
||||||
if (len != -1) {
|
|
||||||
buffer[len] = '\0';
|
|
||||||
std::string path(buffer);
|
|
||||||
size_t lastSlash = path.find_last_of('/');
|
|
||||||
if (lastSlash != std::string::npos) return path.substr(0, lastSlash);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return ".";
|
|
||||||
}
|
|
||||||
} // namespace PathHelper
|
|
||||||
|
|
@ -1,146 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "console_helpers/StringHelpers.h"
|
|
||||||
|
|
||||||
namespace PortableFileIO {
|
|
||||||
enum class BinaryReadStatus {
|
|
||||||
ok,
|
|
||||||
not_found,
|
|
||||||
too_large,
|
|
||||||
read_error,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BinaryReadResult {
|
|
||||||
BinaryReadStatus status;
|
|
||||||
std::size_t bytesRead;
|
|
||||||
std::size_t fileSize;
|
|
||||||
};
|
|
||||||
|
|
||||||
inline std::FILE* OpenBinaryFileForRead(const std::wstring& path) {
|
|
||||||
#if defined(_WIN32)
|
|
||||||
return _wfopen(path.c_str(), L"rb");
|
|
||||||
#else
|
|
||||||
const std::string nativePath = wstringtofilename(path);
|
|
||||||
return std::fopen(nativePath.c_str(), "rb");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Seek(std::FILE* file, std::size_t offset, int origin) {
|
|
||||||
#if defined(_WIN32)
|
|
||||||
return _fseeki64(file, static_cast<int64_t>(offset), origin) == 0;
|
|
||||||
#else
|
|
||||||
return fseeko(file, static_cast<off_t>(offset), origin) == 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int64_t Tell(std::FILE* file) {
|
|
||||||
#if defined(_WIN32)
|
|
||||||
return _ftelli64(file);
|
|
||||||
#else
|
|
||||||
return static_cast<int64_t>(ftello(file));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
inline BinaryReadResult ReadBinaryFile(const std::wstring& path, void* buffer,
|
|
||||||
std::size_t capacity) {
|
|
||||||
std::FILE* stream = OpenBinaryFileForRead(path);
|
|
||||||
if (stream == nullptr) {
|
|
||||||
return {BinaryReadStatus::not_found, 0, 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Seek(stream, 0, SEEK_END)) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::read_error, 0, 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
const int64_t endPosition = Tell(stream);
|
|
||||||
if (endPosition < 0) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::read_error, 0, 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t fileSize = static_cast<std::size_t>(endPosition);
|
|
||||||
if (fileSize > capacity) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::too_large, 0, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Seek(stream, 0, SEEK_SET)) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::read_error, 0, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t bytesRead = std::fread(buffer, 1, fileSize, stream);
|
|
||||||
const bool failed = std::ferror(stream) != 0;
|
|
||||||
std::fclose(stream);
|
|
||||||
if (failed || bytesRead != fileSize) {
|
|
||||||
return {BinaryReadStatus::read_error, bytesRead, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {BinaryReadStatus::ok, bytesRead, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline BinaryReadResult ReadBinaryFileSegment(const std::wstring& path,
|
|
||||||
std::size_t offset, void* buffer,
|
|
||||||
std::size_t bytesToRead) {
|
|
||||||
std::FILE* stream = OpenBinaryFileForRead(path);
|
|
||||||
if (stream == nullptr) {
|
|
||||||
return {BinaryReadStatus::not_found, 0, 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Seek(stream, 0, SEEK_END)) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::read_error, 0, 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
const int64_t endPosition = Tell(stream);
|
|
||||||
if (endPosition < 0) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::read_error, 0, 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t fileSize = static_cast<std::size_t>(endPosition);
|
|
||||||
if ((offset > fileSize) || (bytesToRead > (fileSize - offset))) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::too_large, 0, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Seek(stream, offset, SEEK_SET)) {
|
|
||||||
std::fclose(stream);
|
|
||||||
return {BinaryReadStatus::read_error, 0, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t bytesRead = std::fread(buffer, 1, bytesToRead, stream);
|
|
||||||
const bool failed = std::ferror(stream) != 0;
|
|
||||||
std::fclose(stream);
|
|
||||||
if (failed || bytesRead != bytesToRead) {
|
|
||||||
return {BinaryReadStatus::read_error, bytesRead, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {BinaryReadStatus::ok, bytesRead, fileSize};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool WriteBinaryFile(const std::wstring& path, const void* buffer,
|
|
||||||
std::size_t bytesToWrite) {
|
|
||||||
#if defined(_WIN32)
|
|
||||||
std::FILE* stream = _wfopen(path.c_str(), L"wb");
|
|
||||||
#else
|
|
||||||
const std::string nativePath = wstringtofilename(path);
|
|
||||||
std::FILE* stream = std::fopen(nativePath.c_str(), "wb");
|
|
||||||
#endif
|
|
||||||
if (stream == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::size_t bytesWritten =
|
|
||||||
std::fwrite(buffer, 1, bytesToWrite, stream);
|
|
||||||
const bool failed =
|
|
||||||
std::ferror(stream) != 0 || bytesWritten != bytesToWrite;
|
|
||||||
const bool closeFailed = std::fclose(stream) != 0;
|
|
||||||
return !failed && !closeFailed;
|
|
||||||
}
|
|
||||||
} // namespace PortableFileIO
|
|
||||||
|
|
@ -26,7 +26,7 @@ lib_java = static_library('java',
|
||||||
java_sources,
|
java_sources,
|
||||||
dependencies: [console_helpers_dep],
|
dependencies: [console_helpers_dep],
|
||||||
# TODO: remove the .. path once arrayWithLength are evaporated
|
# TODO: remove the .. path once arrayWithLength are evaporated
|
||||||
include_directories: include_directories('include'),
|
include_directories: include_directories('include', '..'),
|
||||||
cpp_args : global_cpp_args + global_cpp_defs,
|
cpp_args : global_cpp_args + global_cpp_defs,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "console_helpers/PathHelper.h" // 4jcraft TODO
|
|
||||||
#include "console_helpers/StringHelpers.h" // 4jcraft TODO
|
#include "console_helpers/StringHelpers.h" // 4jcraft TODO
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
#include "java/FileFilter.h"
|
#include "java/FileFilter.h"
|
||||||
|
|
||||||
const wchar_t File::pathSeparator = L'/';
|
const wchar_t File::pathSeparator = L'/';
|
||||||
|
|
@ -21,8 +21,7 @@ namespace {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
fs::path ToFilesystemPath(const std::wstring& path) {
|
fs::path ToFilesystemPath(const std::wstring& path) {
|
||||||
const std::string nativePath = wstringtofilename(path);
|
return fs::path(path);
|
||||||
return fs::path(nativePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring ToFilename(const fs::path& path) {
|
std::wstring ToFilename(const fs::path& path) {
|
||||||
|
|
@ -65,11 +64,11 @@ File::File(const std::wstring& pathname) {
|
||||||
m_abstractPathName = fixedPath;
|
m_abstractPathName = fixedPath;
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
std::string request = wstringtofilename(m_abstractPathName);
|
std::string request = std::filesystem::path(m_abstractPathName).string();
|
||||||
while (!request.empty() && request[0] == '/') request.erase(0, 1);
|
while (!request.empty() && request[0] == '/') request.erase(0, 1);
|
||||||
if (request.find("res/") == 0) request.erase(0, 4);
|
if (request.find("res/") == 0) request.erase(0, 4);
|
||||||
|
|
||||||
std::string exeDir = PathHelper::GetExecutableDirA();
|
std::string exeDir = PlatformFileIO.getBasePath().string();
|
||||||
std::string fileName = request;
|
std::string fileName = request;
|
||||||
size_t lastSlash = fileName.find_last_of('/');
|
size_t lastSlash = fileName.find_last_of('/');
|
||||||
if (lastSlash != std::string::npos)
|
if (lastSlash != std::string::npos)
|
||||||
|
|
@ -85,11 +84,11 @@ File::File(const std::wstring& pathname) {
|
||||||
for (const char* base : bases) {
|
for (const char* base : bases) {
|
||||||
std::string tryFull = exeDir + base + request;
|
std::string tryFull = exeDir + base + request;
|
||||||
std::string tryFile = exeDir + base + fileName;
|
std::string tryFile = exeDir + base + fileName;
|
||||||
if (std::filesystem::exists(tryFull)) {
|
if (PlatformFileIO.exists(tryFull)) {
|
||||||
m_abstractPathName = convStringToWstring(tryFull);
|
m_abstractPathName = convStringToWstring(tryFull);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (std::filesystem::exists(tryFile)) {
|
if (PlatformFileIO.exists(tryFile)) {
|
||||||
m_abstractPathName = convStringToWstring(tryFile);
|
m_abstractPathName = convStringToWstring(tryFile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +96,7 @@ File::File(const std::wstring& pathname) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WINDOWS64
|
#ifdef _WINDOWS64
|
||||||
std::string path = wstringtofilename(m_abstractPathName);
|
std::string path = std::filesystem::path(m_abstractPathName).string();
|
||||||
std::string finalPath = StorageManager.GetMountedPath(path.c_str());
|
std::string finalPath = StorageManager.GetMountedPath(path.c_str());
|
||||||
if (finalPath.size() == 0) finalPath = path;
|
if (finalPath.size() == 0) finalPath = path;
|
||||||
m_abstractPathName = convStringToWstring(finalPath);
|
m_abstractPathName = convStringToWstring(finalPath);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include <filesystem>
|
||||||
|
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
@ -52,7 +53,7 @@ FileInputStream::FileInputStream(const File& file) : m_fileHandle(nullptr) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
m_fileHandle = _wfopen(file.getPath().c_str(), L"rb");
|
m_fileHandle = _wfopen(file.getPath().c_str(), L"rb");
|
||||||
#else
|
#else
|
||||||
const std::string nativePath = wstringtofilename(file.getPath());
|
const std::string nativePath = std::filesystem::path(file.getPath()).string();
|
||||||
m_fileHandle = std::fopen(nativePath.c_str(), "rb");
|
m_fileHandle = std::fopen(nativePath.c_str(), "rb");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "console_helpers/StringHelpers.h" // 4jcraft TODO
|
#include <filesystem>
|
||||||
|
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
|
|
||||||
// Creates a file output stream to write to the file represented by the
|
// Creates a file output stream to write to the file represented by the
|
||||||
|
|
@ -29,7 +30,7 @@ FileOutputStream::FileOutputStream(const File& file) : m_fileHandle(nullptr) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
m_fileHandle = _wfopen(file.getPath().c_str(), L"wb");
|
m_fileHandle = _wfopen(file.getPath().c_str(), L"wb");
|
||||||
#else
|
#else
|
||||||
const std::string nativePath = wstringtofilename(file.getPath());
|
const std::string nativePath = std::filesystem::path(file.getPath()).string();
|
||||||
m_fileHandle = std::fopen(nativePath.c_str(), "wb");
|
m_fileHandle = std::fopen(nativePath.c_str(), "wb");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
#include "app/linux/Linux_UIController.h"
|
#include "app/linux/Linux_UIController.h"
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
#include "app/include/BufferedImage.h"
|
#include "app/include/BufferedImage.h"
|
||||||
#include "console_helpers/PortableFileIO.h"
|
#include "platform/PlatformServices.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
#include "minecraft/client/Minecraft.h"
|
#include "minecraft/client/Minecraft.h"
|
||||||
#include "minecraft/client/skins/AbstractTexturePack.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);
|
const std::size_t capacity = static_cast<std::size_t>(fileLength);
|
||||||
std::uint8_t* buffer = new std::uint8_t[capacity == 0 ? 1 : capacity];
|
std::uint8_t* buffer = new std::uint8_t[capacity == 0 ? 1 : capacity];
|
||||||
const PortableFileIO::BinaryReadResult readResult =
|
auto readResult =
|
||||||
PortableFileIO::ReadBinaryFile(file.getPath(), buffer, capacity);
|
PlatformFileIO.readFile(file.getPath(), buffer, capacity);
|
||||||
if (readResult.status != PortableFileIO::BinaryReadStatus::ok ||
|
if (readResult.status != IPlatformFileIO::ReadStatus::Ok ||
|
||||||
readResult.fileSize > std::numeric_limits<unsigned int>::max()) {
|
readResult.fileSize > std::numeric_limits<unsigned int>::max()) {
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
data = nullptr;
|
data = nullptr;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#include "NbtSlotFile.h"
|
#include "NbtSlotFile.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
@ -10,7 +12,7 @@ std::FILE* OpenBinaryFileForReadWrite(const File& file) {
|
||||||
stream = _wfopen(file.getPath().c_str(), L"w+b");
|
stream = _wfopen(file.getPath().c_str(), L"w+b");
|
||||||
}
|
}
|
||||||
#else
|
#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");
|
std::FILE* stream = std::fopen(nativePath.c_str(), "r+b");
|
||||||
if (stream == nullptr) {
|
if (stream == nullptr) {
|
||||||
stream = std::fopen(nativePath.c_str(), "w+b");
|
stream = std::fopen(nativePath.c_str(), "w+b");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#include "ZoneFile.h"
|
#include "ZoneFile.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include "java/ByteBuffer.h"
|
#include "java/ByteBuffer.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
|
|
||||||
|
|
@ -11,7 +13,7 @@ std::FILE* OpenBinaryFileForReadWrite(const File& file) {
|
||||||
stream = _wfopen(file.getPath().c_str(), L"w+b");
|
stream = _wfopen(file.getPath().c_str(), L"w+b");
|
||||||
}
|
}
|
||||||
#else
|
#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");
|
std::FILE* stream = std::fopen(nativePath.c_str(), "r+b");
|
||||||
if (stream == nullptr) {
|
if (stream == nullptr) {
|
||||||
stream = std::fopen(nativePath.c_str(), "w+b");
|
stream = std::fopen(nativePath.c_str(), "w+b");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "ZonedChunkStorage.h"
|
#include "ZonedChunkStorage.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
#include "ZoneFile.h"
|
#include "ZoneFile.h"
|
||||||
|
|
@ -66,7 +67,7 @@ ZoneFile* ZonedChunkStorage::getZoneFile(int x, int z, bool create) {
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
if (!create) return nullptr;
|
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,
|
GENERIC_READ | GENERIC_WRITE, 0, nullptr,
|
||||||
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||||
CloseHandle(ch);
|
CloseHandle(ch);
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,9 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
|
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
#include "minecraft/world/level/biome/Biome.h"
|
#include "minecraft/world/level/biome/Biome.h"
|
||||||
#include "minecraft/world/level/chunk/ChunkSource.h"
|
#include "minecraft/world/level/chunk/ChunkSource.h"
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
|
|
@ -45,45 +43,34 @@ CustomLevelSource::CustomLevelSource(Level* level, int64_t seed,
|
||||||
m_heightmapOverride =
|
m_heightmapOverride =
|
||||||
std::vector<uint8_t>((m_XZSize * 16) * (m_XZSize * 16));
|
std::vector<uint8_t>((m_XZSize * 16) * (m_XZSize * 16));
|
||||||
|
|
||||||
std::filesystem::path path = "GameRules/heightmap.bin";
|
{
|
||||||
std::ifstream file(path, std::ios::binary);
|
const char* path = "GameRules/heightmap.bin";
|
||||||
if (!file) {
|
auto result = PlatformFileIO.readFile(
|
||||||
app.FatalLoadError();
|
path, m_heightmapOverride.data(), m_heightmapOverride.size());
|
||||||
assert(false);
|
if (result.status == IPlatformFileIO::ReadStatus::TooLarge) {
|
||||||
} else {
|
|
||||||
auto fileSize = std::filesystem::file_size(path);
|
|
||||||
if (fileSize > m_heightmapOverride.size()) {
|
|
||||||
app.DebugPrintf("Heightmap binary is too large!!\n");
|
app.DebugPrintf("Heightmap binary is too large!!\n");
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
}
|
} else if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||||
file.read(reinterpret_cast<char*>(m_heightmapOverride.data()),
|
|
||||||
static_cast<std::streamsize>(fileSize));
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_waterheightOverride =
|
m_waterheightOverride =
|
||||||
std::vector<uint8_t>((m_XZSize * 16) * (m_XZSize * 16));
|
std::vector<uint8_t>((m_XZSize * 16) * (m_XZSize * 16));
|
||||||
|
|
||||||
std::filesystem::path waterHeightPath = "GameRules/waterheight.bin";
|
{
|
||||||
std::ifstream waterHeightFile(waterHeightPath, std::ios::binary);
|
const char* waterHeightPath = "GameRules/waterheight.bin";
|
||||||
if (!waterHeightFile) {
|
auto result = PlatformFileIO.readFile(
|
||||||
// assert(false);
|
waterHeightPath, m_waterheightOverride.data(),
|
||||||
memset(m_waterheightOverride.data(), level->seaLevel,
|
m_waterheightOverride.size());
|
||||||
m_waterheightOverride.size());
|
if (result.status == IPlatformFileIO::ReadStatus::NotFound) {
|
||||||
} else {
|
memset(m_waterheightOverride.data(), level->seaLevel,
|
||||||
auto waterFileSize = std::filesystem::file_size(waterHeightPath);
|
m_waterheightOverride.size());
|
||||||
if (waterFileSize > m_waterheightOverride.size()) {
|
} else if (result.status == IPlatformFileIO::ReadStatus::TooLarge) {
|
||||||
app.DebugPrintf("waterheight binary is too large!!\n");
|
app.DebugPrintf("waterheight binary is too large!!\n");
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
}
|
} else if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||||
waterHeightFile.read(
|
|
||||||
reinterpret_cast<char*>(m_waterheightOverride.data()),
|
|
||||||
static_cast<std::streamsize>(waterFileSize));
|
|
||||||
|
|
||||||
if (!waterHeightFile) {
|
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,8 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
|
#include "platform/PlatformServices.h"
|
||||||
#include "minecraft/world/level/newbiome/layer/Layer.h"
|
#include "minecraft/world/level/newbiome/layer/Layer.h"
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
|
|
@ -15,24 +13,18 @@
|
||||||
BiomeOverrideLayer::BiomeOverrideLayer(int seedMixup) : Layer(seedMixup) {
|
BiomeOverrideLayer::BiomeOverrideLayer(int seedMixup) : Layer(seedMixup) {
|
||||||
m_biomeOverride = std::vector<uint8_t>(width * height);
|
m_biomeOverride = std::vector<uint8_t>(width * height);
|
||||||
|
|
||||||
std::filesystem::path path = "GameRules/biomemap.bin";
|
{
|
||||||
std::ifstream file(path, std::ios::binary);
|
const char* path = "GameRules/biomemap.bin";
|
||||||
if (!file) {
|
auto result = PlatformFileIO.readFile(
|
||||||
// assert(false);
|
path, m_biomeOverride.data(), m_biomeOverride.size());
|
||||||
app.DebugPrintf("Biome override not found, using plains as default\n");
|
if (result.status == IPlatformFileIO::ReadStatus::NotFound) {
|
||||||
|
app.DebugPrintf("Biome override not found, using plains as default\n");
|
||||||
memset(m_biomeOverride.data(), Biome::plains->id,
|
memset(m_biomeOverride.data(), Biome::plains->id,
|
||||||
m_biomeOverride.size());
|
m_biomeOverride.size());
|
||||||
} else {
|
} else if (result.status == IPlatformFileIO::ReadStatus::TooLarge) {
|
||||||
auto fileSize = std::filesystem::file_size(path);
|
|
||||||
if (fileSize > m_biomeOverride.size()) {
|
|
||||||
app.DebugPrintf("Biomemap binary is too large!!\n");
|
app.DebugPrintf("Biomemap binary is too large!!\n");
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
}
|
} else if (result.status != IPlatformFileIO::ReadStatus::Ok) {
|
||||||
file.read(reinterpret_cast<char*>(m_biomeOverride.data()),
|
|
||||||
static_cast<std::streamsize>(fileSize));
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
app.FatalLoadError();
|
app.FatalLoadError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@
|
||||||
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
|
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
#include "console_helpers/PortableFileIO.h"
|
|
||||||
#include "console_helpers/compression.h"
|
#include "console_helpers/compression.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
#include "java/InputOutputStream/DataInputStream.h"
|
#include "java/InputOutputStream/DataInputStream.h"
|
||||||
|
|
@ -744,13 +743,13 @@ void ConsoleSaveFileOriginal::DebugFlushToFile(
|
||||||
bool writeSucceeded = false;
|
bool writeSucceeded = false;
|
||||||
|
|
||||||
if (compressedData != nullptr && compressedDataSize > 0) {
|
if (compressedData != nullptr && compressedDataSize > 0) {
|
||||||
writeSucceeded = PortableFileIO::WriteBinaryFile(
|
writeSucceeded = PlatformFileIO.writeFile(
|
||||||
outputPath, compressedData, compressedDataSize);
|
outputPath, compressedData, compressedDataSize);
|
||||||
numberOfBytesWritten = writeSucceeded ? compressedDataSize : 0;
|
numberOfBytesWritten = writeSucceeded ? compressedDataSize : 0;
|
||||||
assert(numberOfBytesWritten == compressedDataSize);
|
assert(numberOfBytesWritten == compressedDataSize);
|
||||||
} else {
|
} else {
|
||||||
writeSucceeded =
|
writeSucceeded =
|
||||||
PortableFileIO::WriteBinaryFile(outputPath, pvSaveMem, fileSize);
|
PlatformFileIO.writeFile(outputPath, pvSaveMem, fileSize);
|
||||||
numberOfBytesWritten = writeSucceeded ? fileSize : 0;
|
numberOfBytesWritten = writeSucceeded ? fileSize : 0;
|
||||||
assert(numberOfBytesWritten == fileSize);
|
assert(numberOfBytesWritten == fileSize);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@
|
||||||
#include "app/linux/Linux_App.h"
|
#include "app/linux/Linux_App.h"
|
||||||
#include "app/linux/Stubs/winapi_stubs.h"
|
#include "app/linux/Stubs/winapi_stubs.h"
|
||||||
#include "console_helpers/PlatformTime.h"
|
#include "console_helpers/PlatformTime.h"
|
||||||
#include "console_helpers/PortableFileIO.h"
|
|
||||||
#include "console_helpers/StringHelpers.h"
|
#include "console_helpers/StringHelpers.h"
|
||||||
#include "console_helpers/compression.h"
|
#include "console_helpers/compression.h"
|
||||||
#include "java/File.h"
|
#include "java/File.h"
|
||||||
|
|
@ -1469,13 +1468,13 @@ void ConsoleSaveFileSplit::DebugFlushToFile(
|
||||||
bool writeSucceeded = false;
|
bool writeSucceeded = false;
|
||||||
|
|
||||||
if (compressedData != nullptr && compressedDataSize > 0) {
|
if (compressedData != nullptr && compressedDataSize > 0) {
|
||||||
writeSucceeded = PortableFileIO::WriteBinaryFile(
|
writeSucceeded = PlatformFileIO.writeFile(
|
||||||
outputPath, compressedData, compressedDataSize);
|
outputPath, compressedData, compressedDataSize);
|
||||||
numberOfBytesWritten = writeSucceeded ? compressedDataSize : 0;
|
numberOfBytesWritten = writeSucceeded ? compressedDataSize : 0;
|
||||||
assert(numberOfBytesWritten == compressedDataSize);
|
assert(numberOfBytesWritten == compressedDataSize);
|
||||||
} else {
|
} else {
|
||||||
writeSucceeded =
|
writeSucceeded =
|
||||||
PortableFileIO::WriteBinaryFile(outputPath, pvSaveMem, fileSize);
|
PlatformFileIO.writeFile(outputPath, pvSaveMem, fileSize);
|
||||||
numberOfBytesWritten = writeSucceeded ? fileSize : 0;
|
numberOfBytesWritten = writeSucceeded ? fileSize : 0;
|
||||||
assert(numberOfBytesWritten == fileSize);
|
assert(numberOfBytesWritten == fileSize);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,14 @@
|
||||||
#include "PlatformServices.h"
|
#include "PlatformServices.h"
|
||||||
|
#include "StdFileIO.h"
|
||||||
|
|
||||||
#include "../platform/sdl2/Input.h"
|
#include "sdl2/Input.h"
|
||||||
#include "../platform/sdl2/Profile.h"
|
#include "sdl2/Profile.h"
|
||||||
#include "../platform/sdl2/Render.h"
|
#include "sdl2/Render.h"
|
||||||
#include "../platform/sdl2/Storage.h"
|
#include "sdl2/Storage.h"
|
||||||
|
|
||||||
class IPlatformInput;
|
static StdFileIO s_stdFileIO;
|
||||||
class IPlatformProfile;
|
|
||||||
class IPlatformRenderer;
|
|
||||||
class IPlatformStorage;
|
|
||||||
|
|
||||||
|
IPlatformFileIO& PlatformFileIO = s_stdFileIO;
|
||||||
IPlatformInput& PlatformInput = InputManager;
|
IPlatformInput& PlatformInput = InputManager;
|
||||||
IPlatformProfile& PlatformProfile = ProfileManager;
|
IPlatformProfile& PlatformProfile = ProfileManager;
|
||||||
IPlatformRenderer& PlatformRender = RenderManager;
|
IPlatformRenderer& PlatformRender = RenderManager;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "IPlatformFileIO.h"
|
||||||
#include "IPlatformInput.h"
|
#include "IPlatformInput.h"
|
||||||
#include "IPlatformLeaderboard.h"
|
#include "IPlatformLeaderboard.h"
|
||||||
#include "IPlatformNetwork.h"
|
#include "IPlatformNetwork.h"
|
||||||
|
|
@ -11,6 +12,7 @@
|
||||||
// instead of concrete globals directly. Bindings are established
|
// instead of concrete globals directly. Bindings are established
|
||||||
// by the app layer at startup.
|
// by the app layer at startup.
|
||||||
|
|
||||||
|
extern IPlatformFileIO& PlatformFileIO;
|
||||||
extern IPlatformInput& PlatformInput;
|
extern IPlatformInput& PlatformInput;
|
||||||
extern IPlatformProfile& PlatformProfile;
|
extern IPlatformProfile& PlatformProfile;
|
||||||
extern IPlatformRenderer& PlatformRender;
|
extern IPlatformRenderer& PlatformRender;
|
||||||
|
|
|
||||||
92
targets/platform/StdFileIO.h
Normal file
92
targets/platform/StdFileIO.h
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "IPlatformFileIO.h"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#if defined(__linux__)
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Standard filesystem implementation for desktop platforms.
|
||||||
|
class StdFileIO : public IPlatformFileIO {
|
||||||
|
public:
|
||||||
|
ReadResult readFile(const std::filesystem::path& path, void* buffer,
|
||||||
|
std::size_t capacity) override {
|
||||||
|
std::error_code ec;
|
||||||
|
auto size = std::filesystem::file_size(path, ec);
|
||||||
|
if (ec) return {ReadStatus::NotFound, 0, 0};
|
||||||
|
if (size > capacity) return {ReadStatus::TooLarge, 0, static_cast<std::size_t>(size)};
|
||||||
|
|
||||||
|
std::ifstream f(path, std::ios::binary);
|
||||||
|
if (!f) return {ReadStatus::NotFound, 0, 0};
|
||||||
|
f.read(static_cast<char*>(buffer), static_cast<std::streamsize>(size));
|
||||||
|
auto read = static_cast<std::size_t>(f.gcount());
|
||||||
|
return {f ? ReadStatus::Ok : ReadStatus::ReadError, read, static_cast<std::size_t>(size)};
|
||||||
|
}
|
||||||
|
|
||||||
|
ReadResult readFileSegment(const std::filesystem::path& path,
|
||||||
|
std::size_t offset, void* buffer,
|
||||||
|
std::size_t bytesToRead) override {
|
||||||
|
std::error_code ec;
|
||||||
|
auto size = std::filesystem::file_size(path, ec);
|
||||||
|
if (ec) return {ReadStatus::NotFound, 0, 0};
|
||||||
|
if (offset + bytesToRead > size) return {ReadStatus::TooLarge, 0, static_cast<std::size_t>(size)};
|
||||||
|
|
||||||
|
std::ifstream f(path, std::ios::binary);
|
||||||
|
if (!f) return {ReadStatus::NotFound, 0, 0};
|
||||||
|
f.seekg(static_cast<std::streamoff>(offset));
|
||||||
|
f.read(static_cast<char*>(buffer), static_cast<std::streamsize>(bytesToRead));
|
||||||
|
auto read = static_cast<std::size_t>(f.gcount());
|
||||||
|
return {f ? ReadStatus::Ok : ReadStatus::ReadError, read, static_cast<std::size_t>(size)};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::uint8_t> readFileToVec(
|
||||||
|
const std::filesystem::path& path) override {
|
||||||
|
std::error_code ec;
|
||||||
|
auto size = std::filesystem::file_size(path, ec);
|
||||||
|
if (ec) return {};
|
||||||
|
|
||||||
|
std::vector<std::uint8_t> data(static_cast<std::size_t>(size));
|
||||||
|
std::ifstream f(path, std::ios::binary);
|
||||||
|
if (!f) return {};
|
||||||
|
f.read(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(size));
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool writeFile(const std::filesystem::path& path, const void* buffer,
|
||||||
|
std::size_t bytesToWrite) override {
|
||||||
|
std::ofstream f(path, std::ios::binary);
|
||||||
|
if (!f) return false;
|
||||||
|
f.write(static_cast<const char*>(buffer), static_cast<std::streamsize>(bytesToWrite));
|
||||||
|
return f.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool exists(const std::filesystem::path& path) override {
|
||||||
|
return std::filesystem::exists(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t fileSize(const std::filesystem::path& path) override {
|
||||||
|
std::error_code ec;
|
||||||
|
auto size = std::filesystem::file_size(path, ec);
|
||||||
|
return ec ? 0 : static_cast<std::size_t>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path getBasePath() override {
|
||||||
|
#if defined(__linux__)
|
||||||
|
char buf[4096];
|
||||||
|
ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
|
||||||
|
if (len > 0) {
|
||||||
|
buf[len] = '\0';
|
||||||
|
return std::filesystem::path(buf).parent_path();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return std::filesystem::current_path();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path getUserDataPath() override {
|
||||||
|
return getBasePath();
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue