feat: Implement resource system Phase 4.5 - ResourceFileSystem (4 files, +305 lines)
This commit is contained in:
97
engine/src/Resources/FileArchive.cpp
Normal file
97
engine/src/Resources/FileArchive.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "Resources/FileArchive.h"
|
||||
#include <cstdio>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
FileArchive::FileArchive() = default;
|
||||
FileArchive::~FileArchive() {
|
||||
Close();
|
||||
}
|
||||
|
||||
bool FileArchive::Open(const Containers::String& path) {
|
||||
m_archivePath = path;
|
||||
m_isValid = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileArchive::Close() {
|
||||
m_archivePath = "";
|
||||
m_isValid = false;
|
||||
}
|
||||
|
||||
bool FileArchive::Read(const Containers::String& fileName, void* buffer, size_t size, size_t offset) const {
|
||||
if (!m_isValid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Containers::String fullPath = m_archivePath;
|
||||
if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) {
|
||||
fullPath += "/";
|
||||
}
|
||||
fullPath += fileName;
|
||||
|
||||
FILE* file = std::fopen(fullPath.CStr(), "rb");
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (offset > 0) {
|
||||
std::fseek(file, static_cast<long>(offset), SEEK_SET);
|
||||
}
|
||||
|
||||
size_t bytesRead = std::fread(buffer, 1, size, file);
|
||||
std::fclose(file);
|
||||
|
||||
return bytesRead == size;
|
||||
}
|
||||
|
||||
size_t FileArchive::GetSize(const Containers::String& fileName) const {
|
||||
if (!m_isValid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Containers::String fullPath = m_archivePath;
|
||||
if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) {
|
||||
fullPath += "/";
|
||||
}
|
||||
fullPath += fileName;
|
||||
|
||||
FILE* file = std::fopen(fullPath.CStr(), "rb");
|
||||
if (!file) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::fseek(file, 0, SEEK_END);
|
||||
size_t size = static_cast<size_t>(std::ftell(file));
|
||||
std::fclose(file);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
bool FileArchive::Exists(const Containers::String& fileName) const {
|
||||
if (!m_isValid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Containers::String fullPath = m_archivePath;
|
||||
if (!fullPath.EndsWith("/") && !fullPath.EndsWith("\\")) {
|
||||
fullPath += "/";
|
||||
}
|
||||
fullPath += fileName;
|
||||
|
||||
FILE* file = std::fopen(fullPath.CStr(), "rb");
|
||||
if (file) {
|
||||
std::fclose(file);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void FileArchive::Enumerate(const Containers::String& pattern, Containers::Array<Containers::String>& outFiles) const {
|
||||
outFiles.Clear();
|
||||
// TODO: Implement pattern-based enumeration
|
||||
}
|
||||
|
||||
} // namespace Resources
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user