Files
bluflame/aiwaz/Aiwaz/FileSystem/FileSystem.cpp
2026-04-18 22:31:51 +02:00

82 lines
1.8 KiB
C++

#include "stdafx.h"
#include "FileSystem.h"
#include "File.h"
FileSystem::FileSystem(IEngine* ar_Engine_)
: m_Engine(ar_Engine_)
, m_Zip(false)
{
::GetModuleFileName(NULL, m_AppDirectory, MAX_PATH);
::PathRemoveFileSpec(m_AppDirectory);
}
FileSystem::~FileSystem()
{
if (m_Zip)
{
::CloseZip(m_ZipHandle);
}
}
void FileSystem::InitializeFromDirectory(string16 argDirectory)
{
m_Zip = false;
::PathCombine(m_Directory, m_AppDirectory, std::to_string8(argDirectory).c_str());
}
void FileSystem::InitializeFromZipFile(string16 argZipFile)
{
std::cout << "Initializing file system with data file: ";
std::wcout << argZipFile << std::white << std::endl;
m_Zip = true;
char fullPath[MAX_PATH];
::PathCombine(fullPath, m_AppDirectory, std::to_string8(argZipFile).c_str());
m_ZipHandle = ::OpenZip(fullPath, 0);
if (m_ZipHandle == NULL)
{
std::cout << std::red << "FAILED!" << std::white << std::endl;
}
}
IFile* FileSystem::Open(string16 argFileName)
{
if (m_Zip)
{
ZIPENTRY zipEntry;
int index;
::FindZipItem(m_ZipHandle, std::to_string8(argFileName).c_str(), true, &index, &zipEntry);
if (index == -1)
return NULL;
unsigned int bufferSize = zipEntry.unc_size;
char* buffer = new char[bufferSize];
::UnzipItem(m_ZipHandle, index, buffer, bufferSize);
return new File(m_Engine, buffer, bufferSize, argFileName);
}
else
{
char fullPath[MAX_PATH];
::PathCombine(fullPath, m_Directory, std::to_string8(argFileName).c_str());
FILE* file = NULL;
_tfopen_s(&file, fullPath, _T("rb"));
if (file == NULL)
return NULL;
fseek(file, 0, SEEK_END);
unsigned int bufferSize = ftell(file);
rewind(file);
char* buffer = new char[bufferSize];
fread_s(buffer, bufferSize, 1, bufferSize, file);
fclose(file);
return new File(m_Engine, buffer, bufferSize, argFileName);
}
}