axmol/extensions/cocostudio/CocoLoader.cpp

216 lines
5.3 KiB
C++
Raw Normal View History

#include "CocoLoader.h"
2019-11-23 20:27:39 +08:00
#include "zlib.h"
using namespace std;
using namespace rapidjson;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
char cTypeName[] = {'N', 'F', 'T', 'O', 'A', 'S', 'V'};
const char* kTypeNames[] = {"Null", "False", "True", "Object", "Array", "String", "Number"};
const char* kObjKeyName[] = {"__type", "classname"};
char g_Buff[2048];
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
char* stExpCocoAttribDesc::GetName(CocoLoader* pCoco)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return (pCoco->GetMemoryAddr_String() + m_szName);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
char* stExpCocoObjectDesc::GetName(CocoLoader* pCoco)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return (pCoco->GetMemoryAddr_String() + m_szName);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
int stExpCocoObjectDesc::GetAttribNum()
2019-11-23 20:27:39 +08:00
{
return m_cAttribNum;
}
stExpCocoAttribDesc* stExpCocoObjectDesc::GetAttribDescArray(CocoLoader* pCoco)
{
2021-12-25 10:04:45 +08:00
return (stExpCocoAttribDesc*)(pCoco->GetMemoryAddr_AttribDesc() + m_pAttribDescArray);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
Type stExpCocoNode::GetType(CocoLoader* pCoco)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Type tType = kObjectType;
if (m_ObjIndex >= 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoObjectDesc* tpCocoObjectDesc = pCoco->GetCocoObjectDescArray();
if (m_AttribIndex >= 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoAttribDesc* tpAttribDescArray =
(stExpCocoAttribDesc*)tpCocoObjectDesc[m_ObjIndex].GetAttribDescArray(pCoco);
2019-11-23 20:27:39 +08:00
tType = Type(tpAttribDescArray[m_AttribIndex].m_cTypeName - 'N' + kNullType);
2021-12-25 10:04:45 +08:00
if (kFalseType == tType || kTrueType == tType)
2019-11-23 20:27:39 +08:00
{
char* szValue = (char*)GetValue(pCoco);
2021-12-25 10:04:45 +08:00
if (szValue[0] == '0')
2019-11-23 20:27:39 +08:00
{
return kFalseType;
}
else
{
return kTrueType;
}
}
}
else
{
tType = kObjectType;
}
}
else
{
2021-12-25 10:04:45 +08:00
if (m_AttribIndex >= 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
tType = (Type)m_ChildNum;
if (kFalseType == tType || kTrueType == tType)
2019-11-23 20:27:39 +08:00
{
char* szValue = (char*)GetValue(pCoco);
2021-12-25 10:04:45 +08:00
if (szValue[0] == '0')
2019-11-23 20:27:39 +08:00
{
return kFalseType;
}
else
{
return kTrueType;
}
}
}
else
{
tType = kArrayType;
}
}
return tType;
}
2021-12-25 10:04:45 +08:00
char* stExpCocoNode::GetName(CocoLoader* pCoco)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
char* szName = nullptr;
if (m_ObjIndex >= 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoObjectDesc* tpCocoObjectDesc = pCoco->GetCocoObjectDescArray();
if (m_AttribIndex >= 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoAttribDesc* tpAttribDescArray =
(stExpCocoAttribDesc*)tpCocoObjectDesc[m_ObjIndex].GetAttribDescArray(pCoco);
2019-11-23 20:27:39 +08:00
szName = tpAttribDescArray[m_AttribIndex].GetName(pCoco);
}
else
{
char* szValue = GetValue(pCoco);
2021-12-25 10:04:45 +08:00
if (szValue[0])
2019-11-23 20:27:39 +08:00
{
szName = GetValue(pCoco);
}
else
{
szName = tpCocoObjectDesc[m_ObjIndex].GetName(pCoco);
}
}
}
else
{
2021-12-25 10:04:45 +08:00
if (m_AttribIndex >= 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
char* pStringAddr = (char*)pCoco->GetCocoObjectDescArray() + pCoco->GetFileHeader()->m_lStringMemAddr;
szName = m_ChildArray + pStringAddr;
2019-11-23 20:27:39 +08:00
}
else
{
szName = (char*)GetValue(pCoco);
}
}
2021-12-25 10:04:45 +08:00
return szName;
2019-11-23 20:27:39 +08:00
}
char* stExpCocoNode::GetValue(CocoLoader* pCoco)
{
2021-12-25 10:04:45 +08:00
char* szValue = (pCoco->GetMemoryAddr_String() + m_szValue);
if (0 == strcmp(szValue, "null") && GetType(pCoco) == kStringType)
{
strcpy(szValue, "");
}
return szValue;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
int stExpCocoNode::GetChildNum()
2019-11-23 20:27:39 +08:00
{
return m_ChildNum;
}
2021-12-25 10:04:45 +08:00
stExpCocoNode* stExpCocoNode::GetChildArray(CocoLoader* pCoco)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return (stExpCocoNode*)(pCoco->GetMemoryAddr_CocoNode() + m_ChildArray);
2019-11-23 20:27:39 +08:00
}
CocoLoader::CocoLoader()
{
2021-12-25 10:04:45 +08:00
m_pRootNode = nullptr;
2019-11-23 20:27:39 +08:00
m_pObjectDescArray = nullptr;
2021-12-25 10:04:45 +08:00
m_pMemoryBuff = nullptr;
2019-11-23 20:27:39 +08:00
}
CocoLoader::~CocoLoader()
{
2021-12-25 10:04:45 +08:00
if (m_pMemoryBuff)
2019-11-23 20:27:39 +08:00
{
delete[] m_pMemoryBuff;
m_pMemoryBuff = nullptr;
}
}
2021-12-25 10:04:45 +08:00
bool CocoLoader::ReadCocoBinBuff(char* pBinBuff)
{
if (m_pMemoryBuff)
return true;
char* pTempBuff = pBinBuff;
m_pFileHeader = (stCocoFileHeader*)pTempBuff;
pTempBuff += sizeof(stCocoFileHeader);
char* pStartAddr = m_pMemoryBuff = pTempBuff;
char* pDestBuff = new char[m_pFileHeader->m_nDataSize];
if (m_pFileHeader->m_nCompressSize > 0)
{
uLongf dwSrcSize = m_pFileHeader->m_nCompressSize;
uLongf dwDestSize = m_pFileHeader->m_nDataSize;
uncompress((Bytef*)pDestBuff, &dwDestSize, (Bytef*)m_pMemoryBuff, dwSrcSize);
}
else
{
memcpy(pDestBuff, m_pMemoryBuff, m_pFileHeader->m_nDataSize);
}
pStartAddr = m_pMemoryBuff = pDestBuff;
m_pObjectDescArray = (stExpCocoObjectDesc*)pStartAddr;
char* pCocoMemAddr = pStartAddr + m_pFileHeader->m_CocoNodeMemAddr;
m_pRootNode = (stExpCocoNode*)pCocoMemAddr;
return true;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
char* CocoLoader::GetMemoryAddr_AttribDesc()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return m_pMemoryBuff + m_pFileHeader->m_lAttribMemAddr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
char* CocoLoader::GetMemoryAddr_CocoNode()
2019-11-23 20:27:39 +08:00
{
return m_pMemoryBuff + m_pFileHeader->m_CocoNodeMemAddr;
}
2021-12-25 10:04:45 +08:00
char* CocoLoader::GetMemoryAddr_String()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return m_pMemoryBuff + m_pFileHeader->m_lStringMemAddr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio