RetroArch/xdk/xdk_resources.cpp

280 lines
6.9 KiB
C++
Raw Normal View History

/* RetroArch - A frontend for libretro.
2015-01-07 18:24:13 +01:00
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <xgraphics.h>
#include "xdk_resources.h"
2015-03-15 04:52:46 +01:00
#include <retro_inline.h>
#ifdef _XBOX360
struct XPR_HEADER
{
2013-01-09 07:27:05 +01:00
DWORD dwMagic;
DWORD dwHeaderSize;
DWORD dwDataSize;
};
#endif
#define XPR0_MAGIC_VALUE 0x30525058
#define XPR1_MAGIC_VALUE 0x31525058
#define XPR2_MAGIC_VALUE 0x58505232
PackedResource::PackedResource()
{
2013-01-09 07:27:05 +01:00
m_pSysMemData = NULL;
m_dwSysMemDataSize = 0L;
m_pVidMemData = NULL;
m_dwVidMemDataSize = 0L;
m_pResourceTags = NULL;
m_dwNumResourceTags = 0L;
m_bInitialized = FALSE;
}
PackedResource::~PackedResource()
{
2013-01-09 07:27:05 +01:00
Destroy();
}
2015-03-15 04:52:46 +01:00
void *PackedResource::GetData(const char *strName) const
{
2013-03-13 01:51:03 +01:00
if (m_pResourceTags == NULL || strName == NULL)
2013-01-09 07:27:05 +01:00
return NULL;
#if defined(_XBOX1)
2015-08-31 21:17:15 +02:00
for (DWORD i=0; m_pResourceTags[i].strName; i++)
#elif defined(_XBOX360)
2015-08-31 21:17:15 +02:00
for (DWORD i = 0; i < m_dwNumResourceTags; i++)
#endif
2013-01-09 07:27:05 +01:00
{
2015-08-31 21:17:15 +02:00
if (!strcasecmp(strName, m_pResourceTags[i].strName))
return &m_pSysMemData[m_pResourceTags[i].dwOffset];
2013-01-09 07:27:05 +01:00
}
2013-01-09 07:27:05 +01:00
return NULL;
}
2015-03-15 04:52:46 +01:00
static INLINE void* AllocateContiguousMemory(DWORD Size, DWORD Alignment)
{
#if defined(_XBOX1)
2013-01-09 07:27:05 +01:00
return D3D_AllocContiguousMemory(Size, Alignment);
#elif defined(_XBOX360)
2015-08-31 21:17:15 +02:00
return XMemAlloc(Size, MAKE_XALLOC_ATTRIBUTES(0, 0, 0, 0, eXALLOCAllocatorId_GameMax,
Alignment, XALLOC_MEMPROTECT_WRITECOMBINE, 0, XALLOC_MEMTYPE_PHYSICAL));
#endif
}
2015-03-15 04:52:46 +01:00
static INLINE void FreeContiguousMemory(void* pData)
{
#if defined(_XBOX1)
2013-01-09 07:27:05 +01:00
return D3D_FreeContiguousMemory(pData);
#elif defined(_XBOX360)
2015-08-31 21:17:15 +02:00
return XMemFree(pData, MAKE_XALLOC_ATTRIBUTES(0, 0, 0, 0, eXALLOCAllocatorId_GameMax,
0, 0, 0, XALLOC_MEMTYPE_PHYSICAL));
#endif
}
#ifdef _XBOX1
char g_strMediaPath[512] = "D:\\Media\\";
2015-03-15 04:52:46 +01:00
static HRESULT FindMediaFile(char *strPath, const char *strFilename, size_t strPathsize)
{
2015-08-31 21:17:15 +02:00
if(strFilename == NULL || strPath == NULL)
2013-01-09 07:27:05 +01:00
return E_INVALIDARG;
2015-08-31 21:17:15 +02:00
strlcpy(strPath, strFilename, strPathsize);
2013-01-09 07:27:05 +01:00
if(strFilename[1] != ':')
snprintf(strPath, strPathsize, "%s%s", g_strMediaPath, strFilename);
2015-08-31 21:17:15 +02:00
HANDLE hFile = CreateFile(strPath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
2013-01-09 07:27:05 +01:00
2015-08-31 21:17:15 +02:00
if (hFile == INVALID_HANDLE_VALUE)
2013-01-09 07:27:05 +01:00
return 0x82000004;
2015-08-31 21:17:15 +02:00
CloseHandle(hFile);
2013-01-09 07:27:05 +01:00
return S_OK;
}
#endif
#if defined(_XBOX1)
2015-03-15 04:52:46 +01:00
HRESULT PackedResource::Create(const char *strFilename,
2013-01-09 07:27:05 +01:00
DWORD dwNumResourceTags, XBRESOURCE* pResourceTags)
#elif defined(_XBOX360)
2015-03-15 04:52:46 +01:00
HRESULT PackedResource::Create(const char *strFilename)
#endif
{
2015-08-31 21:17:15 +02:00
HANDLE hFile;
DWORD dwNumBytesRead;
XPR_HEADER xprh;
bool retval;
#ifdef _XBOX1
2013-01-09 07:27:05 +01:00
BOOL bHasResourceOffsetsTable = FALSE;
2015-08-31 21:17:15 +02:00
char strResourcePath[512];
2013-01-09 07:27:05 +01:00
2015-08-31 21:17:15 +02:00
if (FAILED(FindMediaFile(strResourcePath, strFilename, sizeof(strResourcePath))))
2013-01-09 07:27:05 +01:00
return E_FAIL;
2015-08-31 21:17:15 +02:00
strFilename = strResourcePath;
#endif
2015-08-31 21:17:15 +02:00
hFile = CreateFile(strFilename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
2013-01-09 07:27:05 +01:00
return E_FAIL;
2015-08-31 21:17:15 +02:00
retval = ReadFile(hFile, &xprh, sizeof(XPR_HEADER), &dwNumBytesRead, NULL);
#if defined(_XBOX1)
2015-08-31 21:17:15 +02:00
if(xprh.dwMagic == XPR0_MAGIC_VALUE)
2013-01-09 07:27:05 +01:00
bHasResourceOffsetsTable = FALSE;
2015-08-31 21:17:15 +02:00
else if(xprh.dwMagic == XPR1_MAGIC_VALUE)
2013-01-09 07:27:05 +01:00
bHasResourceOffsetsTable = TRUE;
else
#elif defined(_XBOX360)
2013-01-09 07:27:05 +01:00
if(!retval)
{
2015-08-31 21:17:15 +02:00
CloseHandle(hFile);
2013-01-09 07:27:05 +01:00
return E_FAIL;
}
2015-08-31 21:17:15 +02:00
if (xprh.dwMagic != XPR2_MAGIC_VALUE)
#endif
2013-01-09 07:27:05 +01:00
{
2015-08-31 21:17:15 +02:00
CloseHandle(hFile);
2013-01-09 07:27:05 +01:00
return E_FAIL;
}
2013-01-09 07:27:05 +01:00
// Compute memory requirements
#if defined(_XBOX1)
2013-01-09 07:27:05 +01:00
m_dwSysMemDataSize = xprh.dwHeaderSize - sizeof(XPR_HEADER);
m_dwVidMemDataSize = xprh.dwTotalSize - xprh.dwHeaderSize;
#elif defined(_XBOX360)
2013-01-09 07:27:05 +01:00
m_dwSysMemDataSize = xprh.dwHeaderSize;
m_dwVidMemDataSize = xprh.dwDataSize;
#endif
2013-01-09 07:27:05 +01:00
// Allocate memory
m_pSysMemData = (BYTE*)malloc(m_dwSysMemDataSize);
2013-03-13 01:51:03 +01:00
if (m_pSysMemData == NULL)
2013-01-09 07:27:05 +01:00
{
m_dwSysMemDataSize = 0;
return E_FAIL;
}
2015-08-31 21:17:15 +02:00
m_pVidMemData = (BYTE*)AllocateContiguousMemory(m_dwVidMemDataSize,
#if defined(_XBOX1)
2013-01-09 07:27:05 +01:00
D3DTEXTURE_ALIGNMENT
#elif defined(_XBOX360)
2013-01-09 07:27:05 +01:00
XALLOC_PHYSICAL_ALIGNMENT_4K
#endif
2015-08-31 21:17:15 +02:00
);
2013-01-09 07:27:05 +01:00
2015-08-31 21:17:15 +02:00
if(m_pVidMemData == NULL)
2013-01-09 07:27:05 +01:00
{
m_dwSysMemDataSize = 0;
m_dwVidMemDataSize = 0;
free(m_pSysMemData);
m_pSysMemData = NULL;
return E_FAIL;
}
// Read in the data from the file
2015-08-31 21:17:15 +02:00
if( !ReadFile( hFile, m_pSysMemData, m_dwSysMemDataSize, &dwNumBytesRead, NULL) ||
!ReadFile( hFile, m_pVidMemData, m_dwVidMemDataSize, &dwNumBytesRead, NULL))
2013-01-09 07:27:05 +01:00
{
2015-08-31 21:17:15 +02:00
CloseHandle( hFile);
2013-01-09 07:27:05 +01:00
return E_FAIL;
}
// Done with the file
2015-08-31 21:17:15 +02:00
CloseHandle( hFile);
#ifdef _XBOX1
2013-01-09 07:27:05 +01:00
if (bHasResourceOffsetsTable)
{
#endif
2013-01-09 07:27:05 +01:00
// Extract resource table from the header data
2015-08-31 21:17:15 +02:00
m_dwNumResourceTags = *(DWORD*)(m_pSysMemData + 0);
m_pResourceTags = (XBRESOURCE*)(m_pSysMemData + 4);
2013-01-09 07:27:05 +01:00
// Patch up the resources
2015-08-31 21:17:15 +02:00
for(DWORD i = 0; i < m_dwNumResourceTags; i++)
2013-01-09 07:27:05 +01:00
{
2015-08-31 21:17:15 +02:00
m_pResourceTags[i].strName = (char*)(m_pSysMemData + (DWORD)m_pResourceTags[i].strName);
#ifdef _XBOX360
2015-08-31 21:17:15 +02:00
if((m_pResourceTags[i].dwType & 0xffff0000) == (RESOURCETYPE_TEXTURE & 0xffff0000))
2013-01-09 07:27:05 +01:00
{
2015-08-31 21:17:15 +02:00
D3DTexture *pTexture = (D3DTexture*)&m_pSysMemData[m_pResourceTags[i].dwOffset];
XGOffsetBaseTextureAddress(pTexture, m_pVidMemData, m_pVidMemData);
2013-01-09 07:27:05 +01:00
}
#endif
2013-01-09 07:27:05 +01:00
}
#ifdef _XBOX1
2013-01-09 07:27:05 +01:00
}
#endif
#ifdef _XBOX1
2013-01-09 07:27:05 +01:00
// Use user-supplied number of resources and the resource tags
2015-08-31 21:17:15 +02:00
if(dwNumResourceTags != 0 || pResourceTags != NULL)
2013-01-09 07:27:05 +01:00
{
m_pResourceTags = pResourceTags;
m_dwNumResourceTags = dwNumResourceTags;
}
#endif
2013-01-09 07:27:05 +01:00
m_bInitialized = TRUE;
2013-01-09 07:27:05 +01:00
return S_OK;
}
#ifdef _XBOX360
2015-03-15 04:52:46 +01:00
void PackedResource::GetResourceTags(DWORD* pdwNumResourceTags,
XBRESOURCE** ppResourceTags)
{
2013-03-13 01:51:03 +01:00
if (pdwNumResourceTags)
(*pdwNumResourceTags) = m_dwNumResourceTags;
2015-08-31 21:17:15 +02:00
if (ppResourceTags)
2013-03-13 01:51:03 +01:00
(*ppResourceTags) = m_pResourceTags;
}
#endif
void PackedResource::Destroy()
{
2013-01-09 07:27:05 +01:00
free(m_pSysMemData);
m_pSysMemData = NULL;
m_dwSysMemDataSize = 0L;
2013-03-13 01:51:03 +01:00
if (m_pVidMemData != NULL)
FreeContiguousMemory(m_pVidMemData);
2013-01-09 07:27:05 +01:00
m_pVidMemData = NULL;
m_dwVidMemDataSize = 0L;
2013-01-09 07:27:05 +01:00
m_pResourceTags = NULL;
m_dwNumResourceTags = 0L;
2013-01-09 07:27:05 +01:00
m_bInitialized = FALSE;
}
BOOL PackedResource::Initialized() const
{
2013-01-09 07:27:05 +01:00
return m_bInitialized;
}