📄 vfs.cpp
字号:
// ==========================================================================================================
//
// BREW v2.0+ OPENGLES MICROENGINE
//
// ----------------------------------------
//
// Written by Vander Nunes
//
// ==========================================================================================================
#include "vfs.h"
//
// Virtual file system constructor
//
CVfs::CVfs()
{
m_pMemory = m_pReadMemory = NULL;
m_dwMemorySize = 0;
}
//
// Virtual file system destructor
//
CVfs::~CVfs()
{
if (m_pMemory) API_FREE(m_pMemory);
}
#if !USE_BREW
//
// WIN32 (FOR DEBUG-ONLY) Virtual file system unpack a file to memory block
//
int CVfs::Unpack(char* szPakFile, char *szFile, void* pApiExtra)
{
API_FILE* fp = fopen(szPakFile, "rb");
if (!fp) return 0;
char szName[MAX_PATH];
ZIPEnd End;
ZIPCtrlHeader File;
// read final header
API_FSEEK(fp, -22, API_SEEK_END);
API_FREAD(&End, sizeof(End), 1, fp);
if (End.Signature != ZIPEndSig)
// not a zip file
return 0;
//
// scan zip to find the target file
//
// read the header of each file
API_FSEEK(fp, End.Offset, SEEK_SET);
for (int n = 0; n < End.FilesOnDisk; ++n)
{
API_FREAD(&File, sizeof(File), 1, fp);
if (File.Signature != ZIPCtrlHeaderSig)
// invalid signature
return 0;
// read compressed file name
API_MEMSET(szName, 0, MAX_PATH);
API_FREAD(szName, File.FileNameLength, 1, fp);
// check if its the one we're searching for
if (!stricmp(szName, szFile))
{
// found the file
API_FSEEK(fp, File.Offset, SEEK_SET);
m_pMemory = m_pReadMemory = m_Zip.Unzip(pFile);
m_dwMemorySize = File.UnCompressedSize;
return 1;
}
// next file
API_FSEEK(fp, File.ExtraLength+File.CommentLength, SEEK_CUR);
}
fclose(fp);
return 0;
}
#else
//
// BREW Virtual file system unpack a file to memory block
//
int CVfs::Unpack(char* szPakFile, char *szFile, void* pApiExtra)
{
AEEApplet* pApplet = (AEEApplet*)pApiExtra;
IFileMgr *pFileMgr;
ISHELL_CreateInstance(pApplet->m_pIShell, AEECLSID_FILEMGR, (void**)&pFileMgr);
IFile *pFile = IFILEMGR_OpenFile(pFileMgr, szPakFile, _OFM_READ);
if (!pFile)
{
//DBGPRINTF("Failed to load %s!", szFile);
return 0;
}
char szName[MAX_PATH];
ZIPEnd End;
ZIPCtrlHeader File;
// read final header
API_FSEEK(pFile, -22, API_SEEK_END);
API_FREAD(&End, sizeof(End), 1, pFile);
if (End.Signature != ZIPEndSig)
// not a zip file
return 0;
//
// scan zip to find the target file
//
// read the header of each file
API_FSEEK(pFile, End.Offset, API_SEEK_SET);
for (int n = 0; n < End.FilesOnDisk; ++n)
{
API_FREAD(&File, sizeof(File), 1, pFile);
if (File.Signature != ZIPCtrlHeaderSig)
// invalid signature
return 0;
// read compressed file name
API_MEMSET(szName, 0, MAX_PATH);
API_FREAD(szName, File.FileNameLength, 1, pFile);
// check if its the one we're searching for
if (!API_STRICMP(szName, szFile))
{
// found the file
API_FSEEK(pFile, File.Offset, API_SEEK_SET);
m_pMemory = m_pReadMemory = m_Zip.Unzip(pFile);
m_dwMemorySize = File.UnCompressedSize;
IFILE_Release(pFile);
IFILEMGR_Release(pFileMgr);
return 1; // success
}
// next file
API_FSEEK(pFile, File.ExtraLength+File.CommentLength, API_SEEK_CUR);
}
IFILE_Release(pFile);
IFILEMGR_Release(pFileMgr);
return 0; // fail (file not found in the zip)
}
//
// brew fgetc (non-existant until v2.0)
//
int BREW_fgetc(API_FILE *fp)
{
BYTE c;
IFILE_Read(fp, &c, 1);
return (int)c;
}
//
// brew fread (different from win32)
//
int BREW_fread(void* pBuf, DWORD dwSize, DWORD dwPages, API_FILE* fp)
{
return IFILE_Read(fp, pBuf, dwSize);
}
//
// brew fseek (different from win32)
//
int BREW_fseek(API_FILE* fp, long lOffset, int Type)
{
return IFILE_Seek(fp, (AEEFileSeekType)Type, lOffset);
}
#endif
//
// Return size of the unzipped memory block
//
DWORD CVfs::FileSize(void)
{
return m_dwMemorySize;
}
//
// Read bytes from the unzipped memory block
//
int CVfs::Read(void* pBuf, DWORD dwBytes)
{
API_MEMCPY(pBuf, m_pReadMemory, dwBytes);
m_pReadMemory += dwBytes;
return dwBytes;
}
//
// Change the reading pointer to the specified position
//
void CVfs::SeekTo(DWORD dwBytes)
{
m_pReadMemory = m_pMemory + dwBytes;
}
//
// Release unzipped memory block
//
void CVfs::Finish(void)
{
if (m_pMemory) API_FREE(m_pMemory);
m_pMemory = m_pReadMemory = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -