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