forked from Tr0n/client
Used shared pointers and string streams to read data from the CEterPackManager
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
#include "StdAfx.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <io.h>
|
||||
#include <assert.h>
|
||||
|
||||
@ -14,18 +16,6 @@
|
||||
#define PATH_ABSOLUTE_YMIRWORK1 "d:/ymir work/"
|
||||
#define PATH_ABSOLUTE_YMIRWORK2 "d:\\ymir work\\"
|
||||
|
||||
|
||||
void CEterPackManager::SetCacheMode()
|
||||
{
|
||||
m_isCacheMode=true;
|
||||
}
|
||||
|
||||
void CEterPackManager::SetRelativePathMode()
|
||||
{
|
||||
m_bTryRelativePath = true;
|
||||
}
|
||||
|
||||
|
||||
// StringPath std::string <20><><EFBFBD><EFBFBD>
|
||||
std::string CEterPackManager::ConvertFileName(std::string fileName)
|
||||
{
|
||||
@ -58,50 +48,6 @@ bool CEterPackManager::CompareName(const char * c_szDirectoryName, DWORD /*dwLen
|
||||
return true;
|
||||
}
|
||||
|
||||
void CEterPackManager::LoadStaticCache(const char* c_szFileName)
|
||||
{
|
||||
if (!m_isCacheMode)
|
||||
return;
|
||||
|
||||
std::string strFileName = ConvertFileName(c_szFileName);
|
||||
|
||||
DWORD dwFileNameHash = GetCRC32(strFileName.c_str(), strFileName.length());
|
||||
|
||||
std::unordered_map<DWORD, SCache>::iterator f = m_kMap_dwNameKey_kCache.find(dwFileNameHash);
|
||||
if (m_kMap_dwNameKey_kCache.end() != f)
|
||||
return;
|
||||
|
||||
CMappedFile kMapFile;
|
||||
const void* c_pvData;
|
||||
if (!Get(kMapFile, c_szFileName, &c_pvData))
|
||||
return;
|
||||
|
||||
SCache kNewCache;
|
||||
kNewCache.m_dwBufSize = kMapFile.Size();
|
||||
kNewCache.m_abBufData = new BYTE[kNewCache.m_dwBufSize];
|
||||
memcpy(kNewCache.m_abBufData, c_pvData, kNewCache.m_dwBufSize);
|
||||
m_kMap_dwNameKey_kCache.insert(std::unordered_map<DWORD, SCache>::value_type(dwFileNameHash, kNewCache));
|
||||
}
|
||||
|
||||
CEterPackManager::SCache* CEterPackManager::__FindCache(DWORD dwFileNameHash)
|
||||
{
|
||||
std::unordered_map<DWORD, SCache>::iterator f=m_kMap_dwNameKey_kCache.find(dwFileNameHash);
|
||||
if (m_kMap_dwNameKey_kCache.end()==f)
|
||||
return NULL;
|
||||
|
||||
return &f->second;
|
||||
}
|
||||
|
||||
void CEterPackManager::__ClearCacheMap()
|
||||
{
|
||||
std::unordered_map<DWORD, SCache>::iterator i;
|
||||
|
||||
for (i = m_kMap_dwNameKey_kCache.begin(); i != m_kMap_dwNameKey_kCache.end(); ++i)
|
||||
delete [] i->second.m_abBufData;
|
||||
|
||||
m_kMap_dwNameKey_kCache.clear();
|
||||
}
|
||||
|
||||
struct TimeChecker
|
||||
{
|
||||
TimeChecker(const char* name) : name(name)
|
||||
@ -117,27 +63,37 @@ struct TimeChecker
|
||||
DWORD baseTime;
|
||||
};
|
||||
|
||||
bool CEterPackManager::Get(CMappedFile & rMappedFile, const char * c_szFileName, LPCVOID * pData)
|
||||
bool CEterPackManager::Get(const std::string& fileName, TPackDataPtr& dataPtr)
|
||||
{
|
||||
//TimeChecker timeChecker(c_szFileName);
|
||||
//Logf(1, "Load %s\n", c_szFileName);
|
||||
|
||||
if (m_iSearchMode == SEARCH_FILE_FIRST)
|
||||
{
|
||||
if (GetFromFile(rMappedFile, c_szFileName, pData))
|
||||
{
|
||||
if (GetFromFile(fileName, dataPtr))
|
||||
return true;
|
||||
}
|
||||
|
||||
return GetFromPack(rMappedFile, c_szFileName, pData);
|
||||
return GetFromPack(fileName, dataPtr);
|
||||
}
|
||||
|
||||
if (GetFromPack(rMappedFile, c_szFileName, pData))
|
||||
{
|
||||
if (GetFromPack(fileName, dataPtr))
|
||||
return true;
|
||||
}
|
||||
|
||||
return GetFromFile(rMappedFile, c_szFileName, pData);
|
||||
return GetFromFile(fileName, dataPtr);
|
||||
}
|
||||
|
||||
|
||||
bool CEterPackManager::Get(const std::string& fileName, std::stringstream& dataStream)
|
||||
{
|
||||
CEterPackManager::TPackDataPtr data;
|
||||
if (!Get(fileName, data))
|
||||
return false;
|
||||
|
||||
// Copy the data from the pack into the file sstream
|
||||
dataStream.str("");
|
||||
std::copy(data->begin(), data->end(), std::ostream_iterator<char>(dataStream));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct FinderLock
|
||||
@ -155,24 +111,15 @@ struct FinderLock
|
||||
CRITICAL_SECTION* p_cs;
|
||||
};
|
||||
|
||||
bool CEterPackManager::GetFromPack(CMappedFile & rMappedFile, const char * c_szFileName, LPCVOID * pData)
|
||||
bool CEterPackManager::GetFromPack(const std::string& fileName, TPackDataPtr& dataPtr)
|
||||
{
|
||||
FinderLock lock(m_csFinder);
|
||||
|
||||
std::string strFileName = ConvertFileName(c_szFileName);
|
||||
std::string strFileName = ConvertFileName(fileName);
|
||||
|
||||
DWORD dwFileNameHash = GetCRC32(strFileName.c_str(), strFileName.length());
|
||||
SCache* pkCache = __FindCache(dwFileNameHash);
|
||||
auto pkFileItem = m_FileMap.find(strFileName);
|
||||
|
||||
if (pkCache)
|
||||
{
|
||||
rMappedFile.Link(pkCache->m_dwBufSize, pkCache->m_abBufData);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto pkFileItem = m_FileDict.find(dwFileNameHash);
|
||||
|
||||
if (pkFileItem == m_FileDict.end()) {
|
||||
if (pkFileItem == m_FileMap.end()) {
|
||||
#ifdef _DEBUG
|
||||
TraceError("CANNOT_FIND_PACK_FILE [%s]", strFileName.c_str());
|
||||
#endif
|
||||
@ -181,56 +128,43 @@ bool CEterPackManager::GetFromPack(CMappedFile & rMappedFile, const char * c_szF
|
||||
}
|
||||
|
||||
auto data = std::make_shared<std::vector<char>>();
|
||||
bool r = pkFileItem->second->getFile(strFileName, data);
|
||||
|
||||
// Keep the file loaded by always forcing a reference in the smart pointer (temporary hack)
|
||||
keepDataReferencedArray.push_back(data);
|
||||
if (!pkFileItem->second->getFile(strFileName, data))
|
||||
return false;
|
||||
|
||||
rMappedFile.Link(data->size(), data->data());
|
||||
*pData = (LPCVOID *) data->data();
|
||||
return r;
|
||||
// Set dataPtr to the retreived data pointer
|
||||
dataPtr = data;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const time_t g_tCachingInterval = 10; // 10<31><30>
|
||||
|
||||
bool CEterPackManager::GetFromFile(CMappedFile & rMappedFile, const char * c_szFileName, LPCVOID * pData)
|
||||
bool CEterPackManager::GetFromFile(const std::string& fileName, TPackDataPtr& dataPtr)
|
||||
{
|
||||
#ifndef _DEBUG
|
||||
//const char *pcExt = strchr(c_szFileName, '.');
|
||||
//if (pcExt &&
|
||||
// _strnicmp(pcExt, ".py", 3) == 0 && // python <20><>ũ<EFBFBD><C5A9>Ʈ <20><>
|
||||
// stricmp(c_szFileName, "logininfo.py") != 0 && // <20>α<EFBFBD><CEB1><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ƴϰ<C6B4>
|
||||
// strnicmp(c_szFileName, "locale", 6) != 0
|
||||
// )
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
#endif
|
||||
// Try to open the file
|
||||
std::ifstream file(fileName, std::ios::binary);
|
||||
if (!file.is_open())
|
||||
return false;
|
||||
|
||||
//if(m_bTryRelativePath) {
|
||||
// if (strnicmp(c_szFileName, PATH_ABSOLUTE_YMIRWORK1, strlen(PATH_ABSOLUTE_YMIRWORK1)) == 0 || strnicmp(c_szFileName, PATH_ABSOLUTE_YMIRWORK2, strlen(PATH_ABSOLUTE_YMIRWORK2)) == 0) {
|
||||
// if(rMappedFile.Create(c_szFileName+strlen(PATH_ABSOLUTE_YMIRWORK1), pData, 0, 0))
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
return rMappedFile.Create(c_szFileName, pData, 0, 0) ? true : false;
|
||||
// Read the file's contents
|
||||
dataPtr = std::make_shared<std::vector<char>>(
|
||||
std::istreambuf_iterator<char>(file),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CEterPackManager::isExistInPack(const char * c_szFileName)
|
||||
{
|
||||
std::string strFileName = ConvertFileName(c_szFileName);
|
||||
|
||||
DWORD dwFileNameHash = GetCRC32(strFileName.c_str(), strFileName.length());
|
||||
auto pkFileItem = m_FileDict.find(dwFileNameHash);
|
||||
auto pkFileItem = m_FileMap.find(strFileName);
|
||||
|
||||
if (pkFileItem == m_FileDict.end())
|
||||
if (pkFileItem == m_FileMap.end())
|
||||
return false;
|
||||
|
||||
if (pkFileItem->second)
|
||||
return pkFileItem->second->fileExists(strFileName.c_str());
|
||||
if (!pkFileItem->second)
|
||||
return false;
|
||||
|
||||
return pkFileItem->second->fileExists(strFileName.c_str());
|
||||
}
|
||||
|
||||
bool CEterPackManager::isExist(const char * c_szFileName)
|
||||
@ -240,16 +174,9 @@ bool CEterPackManager::isExist(const char * c_szFileName)
|
||||
if (isExistInPack(c_szFileName))
|
||||
return true;
|
||||
|
||||
return _access(c_szFileName, 0) == 0 ? true : false;
|
||||
return _access(c_szFileName, 0) == 0;
|
||||
}
|
||||
|
||||
//if(m_bTryRelativePath) {
|
||||
// if (strnicmp(c_szFileName, PATH_ABSOLUTE_YMIRWORK1, strlen(PATH_ABSOLUTE_YMIRWORK1)) == 0 || strnicmp(c_szFileName, PATH_ABSOLUTE_YMIRWORK2, strlen(PATH_ABSOLUTE_YMIRWORK2)) == 0) {
|
||||
// if(access(c_szFileName+strlen(PATH_ABSOLUTE_YMIRWORK1), 0) == 0)
|
||||
// return true;
|
||||
// }
|
||||
//}
|
||||
|
||||
if (_access(c_szFileName, 0) == 0)
|
||||
return true;
|
||||
|
||||
@ -276,18 +203,20 @@ bool CEterPackManager::RegisterPack(const char * c_szName, const char * c_szDire
|
||||
|
||||
auto packFiles = pack->listFiles();
|
||||
|
||||
for (auto const& fileName : packFiles) {
|
||||
DWORD dwFileNameHash = GetCRC32(fileName.c_str(), fileName.length());
|
||||
m_FileDict.insert({ dwFileNameHash, pack });
|
||||
}
|
||||
for (auto const& fileName : packFiles)
|
||||
m_FileMap.insert({ fileName, pack });
|
||||
|
||||
m_PackMap.insert(TEterPackMap::value_type(c_szName, pack));
|
||||
m_PackMap.insert({ c_szName, pack });
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (...)
|
||||
catch (std::exception& e)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
Tracef("The eterpack doesn't exist [%s]\n", c_szName);
|
||||
Tracef("Unable to load file provider '%s': %s\n", c_szName, e.what());
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -301,14 +230,16 @@ int CEterPackManager::GetSearchMode()
|
||||
return m_iSearchMode;
|
||||
}
|
||||
|
||||
CEterPackManager::CEterPackManager() : m_bTryRelativePath(false), m_iSearchMode(SEARCH_FILE_FIRST), m_isCacheMode(false)
|
||||
CEterPackManager::CEterPackManager() : m_iSearchMode(SEARCH_FILE_FIRST)
|
||||
{
|
||||
InitializeCriticalSection(&m_csFinder);
|
||||
}
|
||||
|
||||
CEterPackManager::~CEterPackManager()
|
||||
{
|
||||
__ClearCacheMap();
|
||||
|
||||
DeleteCriticalSection(&m_csFinder);
|
||||
}
|
||||
|
||||
const CEterPackManager::TFileMap& CEterPackManager::GetFileMap() {
|
||||
return this->m_FileMap;
|
||||
}
|
||||
|
Reference in New Issue
Block a user