📄 hxresmg.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hxcom.h"
#include "hlxclib/stdio.h"
#include "hlxclib/stdlib.h"
#ifdef _WINDOWS
#include <windows.h>
#endif
#if defined(_MACINTOSH) && !defined(_MAC_MACHO)
#include <stat.h>
#else
#include "hlxclib/sys/stat.h"
#endif
#include "hxxrsmg.h"
#include "hxplugn.h"
#include "hxfiles.h"
#include "hxcomm.h"
#include "hxxres.h"
#include "hxengin.h"
#include "ihxpckts.h"
#include "hxprefs.h"
#include "hxmon.h"
#include "hxslist.h"
#include "hxstrutl.h"
#include "chxpckts.h"
#include "md5.h"
#include "findfile.h"
#include "hxresmg.h"
#include "dllpath.h"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
// IHXPreferences key defines
#define EXT_RES_DIR "ExtResources"
#define EXT_RES_DATA_KEY "ExternalResourcesData"
#define FILE_INFO_KEY "FileInfo"
#define CHECKSUM_KEY "XRSCheckSum"
#define LANGUAGE_KEY "Language"
#define LANG_ID_KEY "LangID"
// for OS_SEPARATOR_CHAR and OS_SEPARATOR_STRING
#include "hxdir.h"
// static initializations
#if !defined(HELIX_CONFIG_NOSTATICS)
HXExternalResourceManager* HXExternalResourceManager::m_pResourceManager = NULL;
#else
#include "globals/hxglobals.h"
const HXExternalResourceManager* const HXExternalResourceManager::m_pResourceManager = NULL;
#endif
// HXExternalResourceManager methods
HXExternalResourceManager::HXExternalResourceManager(IUnknown* pContext):
m_lRefCount(0)
,m_pResourceList(NULL)
,m_pContext(pContext)
,m_pHXXResPlugin(NULL)
,m_ulLanguageID(0x0409)
,m_pExternalResDir(NULL)
,m_pPrefs(NULL)
,m_pRegistry(NULL)
{
if(m_pContext)
{
m_pContext->AddRef();
}
Init();
}
HXExternalResourceManager::~HXExternalResourceManager()
{
delete[] m_pExternalResDir;
if(m_pResourceList)
{
CHXSimpleList::Iterator i = m_pResourceList->Begin();
for (; i != m_pResourceList->End(); ++i)
{
IHXXResFile* pRes = (IHXXResFile*)(*i);
pRes->Close();
pRes->Release();
}
}
HX_DELETE(m_pResourceList);
HX_RELEASE(m_pPrefs);
HX_RELEASE(m_pRegistry);
HX_RELEASE(m_pContext);
HX_RELEASE(m_pHXXResPlugin);
#if defined(HELIX_CONFIG_NOSTATICS)
HXExternalResourceManager*& m_pResourceManager =
(HXExternalResourceManager*&)HXGlobalPtr::Get(
&HXExternalResourceManager::m_pResourceManager);
#endif
m_pResourceManager = NULL;
}
HXExternalResourceManager*
HXExternalResourceManager::Instance(IUnknown* pContext)
{
#if defined(HELIX_CONFIG_NOSTATICS)
HXExternalResourceManager*& m_pResourceManager =
(HXExternalResourceManager*&)HXGlobalPtr::Get(
&HXExternalResourceManager::m_pResourceManager);
#endif
if(!m_pResourceManager)
{
m_pResourceManager = new HXExternalResourceManager(pContext);
}
if (m_pResourceManager)
{
m_pResourceManager->AddRef();
}
return m_pResourceManager;
}
// IUnknown methods
STDMETHODIMP
HXExternalResourceManager::QueryInterface(REFIID riid, void** ppvObj)
{
QInterfaceList qiList[] =
{
{ GET_IIDHANDLE(IID_IHXExternalResourceManager), (IHXExternalResourceManager*)this },
{ GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXExternalResourceManager*)this },
};
return ::QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);
}
STDMETHODIMP_(ULONG32)
HXExternalResourceManager::AddRef()
{
return InterlockedIncrement(&m_lRefCount);
}
STDMETHODIMP_(ULONG32)
HXExternalResourceManager::Release()
{
if (InterlockedDecrement(&m_lRefCount) > 0)
{
return m_lRefCount;
}
delete this;
return 0;
}
// IHXExternalResourceManager methods
STDMETHODIMP
HXExternalResourceManager::Init()
{
HX_RESULT rc = GetHXXResPlugin();
if(HXR_OK == rc)
{
// get language and res directory preferences/reg entries
if (HXR_OK == m_pContext->QueryInterface(IID_IHXPreferences,
(void**)&m_pPrefs) &&
HXR_OK == m_pContext->QueryInterface(IID_IHXRegistry,
(void**)&m_pRegistry))
{
IHXBuffer* pBuffer = NULL;
CHXString strTemp;
strTemp.Format("%s.%s",HXREGISTRY_PREFPROPNAME, "LangID");
if(HXR_OK == m_pRegistry->GetStrByName(strTemp, pBuffer))
{
const char* pActualLangID = (const char*)pBuffer->GetBuffer();
m_ulLanguageID = strtol(pActualLangID, NULL, 10);
HX_RELEASE(pBuffer);
}
const char* pPath = NULL;
// Get the plugin directory from the Dll Access Paths
if (GetDLLAccessPath() &&
(pPath = GetDLLAccessPath()->GetPath(DLLTYPE_PLUGIN)) &&
*pPath)
{
UINT32 ulBufLen = strlen(pPath) + 1 + strlen(EXT_RES_DIR) + 1;
m_pExternalResDir = new char[ulBufLen];
SafeStrCpy(m_pExternalResDir, pPath, ulBufLen);
if (m_pExternalResDir[strlen(m_pExternalResDir)-1] != OS_SEPARATOR_CHAR)
{
SafeStrCat(m_pExternalResDir, OS_SEPARATOR_STRING, ulBufLen);
}
SafeStrCat(m_pExternalResDir, EXT_RES_DIR, ulBufLen);
rc = LoadResourceFiles();
}
}
}
return rc;
}
STDMETHODIMP
HXExternalResourceManager::CreateExternalResourceReader(
const char* pShortName,
REF(IHXExternalResourceReader*) pReader)
{
HX_RESULT rc = HXR_OK;
HXExternalResourceReader* pHXReader = new HXExternalResourceReader(this);
if(!pHXReader)
{
return HXR_OUTOFMEMORY;
}
pHXReader->AddRef();
pHXReader->Init(pShortName, m_pResourceList);
pReader = pHXReader;
return rc;
}
// HXExternalResourceManager private methods
HX_RESULT
HXExternalResourceManager::GetHXXResPlugin()
{
HX_RESULT rc = HXR_FAILED;
IUnknown* pUnk = NULL;
IUnknown* pUnknownInstance = NULL;
IHXCommonClassFactory* pCommonClassFactory = NULL;
IHXPluginGroupEnumerator* pGroupEnumerator = NULL;
UINT32 ulNumPlugins = 0;
// enumerate the plugins
if (HXR_OK != m_pContext->QueryInterface(
IID_IHXCommonClassFactory,
(void**)&pCommonClassFactory))
{
goto cleanup;
}
if (HXR_OK != pCommonClassFactory->CreateInstance(
CLSID_IHXPluginGroupEnumerator,
(void**)&pUnk))
{
goto cleanup;
}
if (HXR_OK != pUnk->QueryInterface(IID_IHXPluginGroupEnumerator,
(void**)&pGroupEnumerator))
{
goto cleanup;
}
if (HXR_OK != pGroupEnumerator->Init(IID_IHXXResFile))
{
goto cleanup;
}
ulNumPlugins = pGroupEnumerator->GetNumOfPlugins();
if(ulNumPlugins > 0)
{
if (HXR_OK == pGroupEnumerator->GetPlugin(0, pUnknownInstance))
{
m_pHXXResPlugin = pUnknownInstance;
m_pHXXResPlugin->AddRef();
rc = HXR_OK;
}
}
cleanup:
HX_RELEASE(pUnk);
HX_RELEASE(pUnknownInstance);
HX_RELEASE(pCommonClassFactory);
HX_RELEASE(pGroupEnumerator);
return rc;
}
BOOL
HXExternalResourceManager::ContainsCurrentLanguage(IHXXResFile* pResFile)
{
BOOL rc = FALSE;
if(!pResFile)
{
return rc;
}
UINT32 ulLangID = 0;
BOOL bFound = FALSE;
rc = pResFile->GetFirstResourceLanguage(ulLangID);
while(HXR_OK == rc)
{
if(m_ulLanguageID == ulLangID)
{
rc = TRUE;
break;
}
rc = pResFile->GetNextResourceLanguage(ulLangID);
};
return rc;
}
IHXXResFile*
HXExternalResourceManager::MakeResFileObject(const char* pPath)
{
IHXXResFile* pResFile = NULL;
if(m_pHXXResPlugin &&
HXR_OK == m_pHXXResPlugin->QueryInterface(IID_IHXXResFile,
(void**)&pResFile))
{
HX_RESULT rc = HXR_OK;
// open res file and see if it contains any resources with the
// current language ID
pResFile->Open(pPath);
if(ContainsCurrentLanguage(pResFile))
{
pResFile->SetLanguage(m_ulLanguageID);
}
else
{
HX_RELEASE(pResFile); // don't want this one
}
}
return pResFile;
}
HX_RESULT
HXExternalResourceManager::Stat(const char* pPath, struct stat* pStatBuffer)
{
HX_RESULT rc = HXR_OK;
memset(pStatBuffer,0,sizeof(*pStatBuffer));
if(stat((const char*)pPath, pStatBuffer) < 0)
{
rc = HXR_FAIL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -