client/src/GameLib/PropertyManager.cpp

137 lines
2.8 KiB
C++

#include "StdAfx.h"
#include "../eterPack/EterPackManager.h"
#include "PropertyManager.h"
#include "Property.h"
CPropertyManager::CPropertyManager()
{
}
CPropertyManager::~CPropertyManager()
{
Clear();
}
void CPropertyManager::Initialize()
{
auto fileMap = CEterPackManager::Instance().GetFileMap();
for (auto& itor : fileMap)
{
if (itor.first.rfind("property/reserve", 0) == 0)
{
LoadReservedCRC(itor.first.c_str());
continue;
}
if (itor.first.rfind("property/", 0) == 0) {
if (!Register(itor.first.c_str()))
continue;
}
}
}
bool CPropertyManager::LoadReservedCRC(const char * c_pszFileName)
{
CEterPackManager::TPackDataPtr data;
CMemoryTextFileLoader textFileLoader;
if (!CEterPackManager::Instance().Get(c_pszFileName, data))
return false;
textFileLoader.Bind(data->size(), data->data());
for (DWORD i = 0; i < textFileLoader.GetLineCount(); ++i)
{
const char * pszLine = textFileLoader.GetLineString(i).c_str();
if (!pszLine || !*pszLine)
continue;
ReserveCRC(atoi(pszLine));
}
return true;
}
void CPropertyManager::ReserveCRC(DWORD dwCRC)
{
m_ReservedCRCSet.insert(dwCRC);
}
DWORD CPropertyManager::GetUniqueCRC(const char * c_szSeed)
{
std::string stTmp = c_szSeed;
while (1)
{
DWORD dwCRC = GetCRC32(stTmp.c_str(), stTmp.length());
if (m_ReservedCRCSet.find(dwCRC) == m_ReservedCRCSet.end() &&
m_PropertyByCRCMap.find(dwCRC) == m_PropertyByCRCMap.end())
return dwCRC;
char szAdd[2];
_snprintf(szAdd, sizeof(szAdd), "%d", random() % 10);
stTmp += szAdd;
}
}
bool CPropertyManager::Register(const char * c_pszFileName, CProperty ** ppProperty)
{
CEterPackManager::TPackDataPtr data;
if (!CEterPackManager::Instance().Get(c_pszFileName, data))
return false;
CProperty * pProperty = new CProperty(c_pszFileName);
if (!pProperty->ReadFromMemory(data->data(), data->size(), c_pszFileName))
{
delete pProperty;
return false;
}
DWORD dwCRC = pProperty->GetCRC();
TPropertyCRCMap::iterator itor = m_PropertyByCRCMap.find(dwCRC);
if (m_PropertyByCRCMap.end() != itor)
{
Tracef("Property already registered, replace %s to %s\n",
itor->second->GetFileName(),
c_pszFileName);
delete itor->second;
itor->second = pProperty;
}
else
m_PropertyByCRCMap.insert(TPropertyCRCMap::value_type(dwCRC, pProperty));
if (ppProperty)
*ppProperty = pProperty;
return true;
}
bool CPropertyManager::Get(const char * c_pszFileName, CProperty ** ppProperty)
{
return Register(c_pszFileName, ppProperty);
}
bool CPropertyManager::Get(DWORD dwCRC, CProperty ** ppProperty)
{
TPropertyCRCMap::iterator itor = m_PropertyByCRCMap.find(dwCRC);
if (m_PropertyByCRCMap.end() == itor)
return false;
*ppProperty = itor->second;
return true;
}
void CPropertyManager::Clear()
{
stl_wipe_second(m_PropertyByCRCMap);
}