axmol/extensions/cocostudio/CCSSceneReader.cpp

570 lines
19 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2021-07-16 11:04:38 +08:00
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2021-07-16 11:04:38 +08:00
https://adxe.org
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CocoStudio.h"
2019-11-23 20:27:39 +08:00
#include "ui/CocosGUI.h"
2020-10-17 16:32:16 +08:00
// #include "audio/include/SimpleAudioEngine.h"
2019-11-23 20:27:39 +08:00
#include "base/ObjectFactory.h"
#include "base/ccUtils.h"
#include "platform/CCFileUtils.h"
using namespace cocos2d;
using namespace ui;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
SceneReader* SceneReader::s_sharedReader = nullptr;
2021-12-25 10:04:45 +08:00
SceneReader::SceneReader() : _fnSelector(nullptr), _node(nullptr), _attachComponent(AttachComponentType::EMPTY_NODE)
2019-11-23 20:27:39 +08:00
{
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAttribute));
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComRender));
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAudio));
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComController));
}
2021-12-25 10:04:45 +08:00
SceneReader::~SceneReader() {}
2019-11-23 20:27:39 +08:00
const char* SceneReader::sceneReaderVersion()
{
return "1.0.0.0";
}
2021-12-25 10:04:45 +08:00
cocos2d::Node* SceneReader::createNodeWithSceneFile(
std::string_view fileName,
2021-12-25 10:04:45 +08:00
AttachComponentType attachComponent /*= AttachComponentType::EMPTY_NODE*/)
2019-11-23 20:27:39 +08:00
{
std::string fileExtension = cocos2d::FileUtils::getInstance()->getFileExtension(fileName);
if (fileExtension == ".json")
{
_node = nullptr;
rapidjson::Document jsonDict;
2021-12-25 10:04:45 +08:00
do
{
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(!readJson(fileName, jsonDict));
_node = createObject(jsonDict, nullptr, attachComponent);
TriggerMng::getInstance()->parse(jsonDict);
} while (0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return _node;
}
2021-12-25 10:04:45 +08:00
else if (fileExtension == ".csb")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
do
{
2019-11-23 20:27:39 +08:00
std::string binaryFilePath = FileUtils::getInstance()->fullPathForFilename(fileName);
2021-12-25 10:04:45 +08:00
auto fileData = FileUtils::getInstance()->getDataFromFile(binaryFilePath);
auto fileDataBytes = fileData.getBytes();
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(fileData.isNull());
CocoLoader tCocoLoader;
if (tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* tpRootCocoNode = tCocoLoader.GetRootCocoNode();
rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
if (rapidjson::kObjectType == tType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(tpRootCocoNode->GetChildNum() == 0);
2021-12-25 10:04:45 +08:00
_node = Node::create();
int nCount = 0;
2019-11-23 20:27:39 +08:00
std::vector<Component*> _vecComs;
2021-12-25 10:04:45 +08:00
ComRender* pRender = nullptr;
std::string key = tpChildArray[15].GetName(&tCocoLoader);
2019-11-23 20:27:39 +08:00
if (key == "components")
{
nCount = tpChildArray[15].GetChildNum();
}
2021-12-25 10:04:45 +08:00
stExpCocoNode* pComponents = tpChildArray[15].GetChildArray(&tCocoLoader);
SerData* data = new SerData();
2019-11-23 20:27:39 +08:00
for (int i = 0; i < nCount; i++)
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* subDict = pComponents[i].GetChildArray(&tCocoLoader);
2019-11-23 20:27:39 +08:00
if (subDict == nullptr)
{
continue;
}
2021-12-25 10:04:45 +08:00
std::string key1 = subDict[1].GetName(&tCocoLoader);
const char* comName = subDict[1].GetValue(&tCocoLoader);
Component* pCom = nullptr;
2019-11-23 20:27:39 +08:00
if (key1 == "classname" && comName != nullptr)
{
pCom = createComponent(comName);
}
if (pCom != nullptr)
{
2021-12-25 10:04:45 +08:00
data->_rData = nullptr;
data->_cocoNode = subDict;
2019-11-23 20:27:39 +08:00
data->_cocoLoader = &tCocoLoader;
if (pCom->serialize(data))
{
2021-12-25 10:04:45 +08:00
ComRender* pTRender = dynamic_cast<ComRender*>(pCom);
2019-11-23 20:27:39 +08:00
if (pTRender != nullptr)
{
pRender = pTRender;
}
else
{
_vecComs.push_back(pCom);
}
}
else
{
CC_SAFE_RELEASE_NULL(pCom);
}
}
2021-12-25 10:04:45 +08:00
if (_fnSelector != nullptr)
2019-11-23 20:27:39 +08:00
{
_fnSelector(pCom, (void*)(data));
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setPropertyFromJsonDict(&tCocoLoader, tpRootCocoNode, _node);
for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
{
_node->addComponent(*iter);
}
2021-12-25 10:04:45 +08:00
stExpCocoNode* pGameObjects = tpChildArray[11].GetChildArray(&tCocoLoader);
int length = tpChildArray[11].GetChildNum();
2019-11-23 20:27:39 +08:00
for (int i = 0; i < length; ++i)
{
createObject(&tCocoLoader, &pGameObjects[i], _node, attachComponent);
}
TriggerMng::getInstance()->parse(&tCocoLoader, tpChildArray);
}
}
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
return _node;
}
else
{
log("read file [%s] error!\n", fileName.data());
2019-11-23 20:27:39 +08:00
}
return nullptr;
}
bool SceneReader::readJson(std::string_view fileName, rapidjson::Document& doc)
2019-11-23 20:27:39 +08:00
{
bool ret = false;
2021-12-25 10:04:45 +08:00
do
{
std::string jsonpath = FileUtils::getInstance()->fullPathForFilename(fileName);
2019-11-23 20:27:39 +08:00
std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
doc.Parse<0>(contentStr.c_str());
CC_BREAK_IF(doc.HasParseError());
ret = true;
} while (0);
return ret;
}
2021-12-25 10:04:45 +08:00
Node* SceneReader::nodeByTag(Node* parent, int tag)
{
2019-11-23 20:27:39 +08:00
if (parent == nullptr)
{
return nullptr;
}
2021-12-25 10:04:45 +08:00
Node* _retNode = nullptr;
Vector<Node*>& Children = parent->getChildren();
2019-11-23 20:27:39 +08:00
Vector<Node*>::iterator iter = Children.begin();
while (iter != Children.end())
{
Node* pNode = *iter;
2021-12-25 10:04:45 +08:00
if (pNode != nullptr && pNode->getTag() == tag)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_retNode = pNode;
2019-11-23 20:27:39 +08:00
break;
}
else
{
_retNode = nodeByTag(pNode, tag);
if (_retNode != nullptr)
{
break;
}
}
++iter;
}
return _retNode;
}
2021-12-25 10:04:45 +08:00
cocos2d::Component* SceneReader::createComponent(std::string_view classname)
2019-11-23 20:27:39 +08:00
{
std::string name = this->getComponentClassName(classname);
2021-12-25 10:04:45 +08:00
Ref* object = ObjectFactory::getInstance()->createObject(name);
2019-11-23 20:27:39 +08:00
return dynamic_cast<Component*>(object);
}
std::string SceneReader::getComponentClassName(std::string_view name)
2019-11-23 20:27:39 +08:00
{
std::string comName;
2021-12-25 10:04:45 +08:00
if (name == "CCSprite" || name == "CCTMXTiledMap" || name == "CCParticleSystemQuad" || name == "CCArmature" ||
name == "GUIComponent")
2019-11-23 20:27:39 +08:00
{
comName = "ComRender";
}
else if (name == ComAudio::COMPONENT_NAME || name == "CCBackgroundAudio")
{
comName = "ComAudio";
}
else if (name == ComController::COMPONENT_NAME)
{
comName = "ComController";
}
else if (name == ComAttribute::COMPONENT_NAME)
{
comName = "ComAttribute";
}
else if (name == "CCScene")
{
comName = "Scene";
}
else
{
CCASSERT(false, "Unregistered Component!");
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return comName;
}
2021-12-25 10:04:45 +08:00
Node* SceneReader::createObject(const rapidjson::Value& dict,
cocos2d::Node* parent,
AttachComponentType attachComponent)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const char* className = DICTOOL->getStringValue_json(dict, "classname");
if (strcmp(className, "CCNode") == 0)
2019-11-23 20:27:39 +08:00
{
Node* gb = nullptr;
2021-12-25 10:04:45 +08:00
if (nullptr == parent)
2019-11-23 20:27:39 +08:00
{
gb = Node::create();
}
std::vector<Component*> vecComs;
2021-12-25 10:04:45 +08:00
ComRender* render = nullptr;
int count = DICTOOL->getArrayCount_json(dict, "components");
2019-11-23 20:27:39 +08:00
for (int i = 0; i < count; i++)
{
2021-12-25 10:04:45 +08:00
const rapidjson::Value& subDict = DICTOOL->getSubDictionary_json(dict, "components", i);
2019-11-23 20:27:39 +08:00
if (!DICTOOL->checkObjectExist_json(subDict))
{
break;
}
2021-12-25 10:04:45 +08:00
const char* comName = DICTOOL->getStringValue_json(subDict, "classname");
Component* com = this->createComponent(comName);
SerData* data = new SerData();
2019-11-23 20:27:39 +08:00
if (com != nullptr)
{
2021-12-25 10:04:45 +08:00
data->_rData = &subDict;
data->_cocoNode = nullptr;
2019-11-23 20:27:39 +08:00
data->_cocoLoader = nullptr;
if (com->serialize(data))
{
2021-12-25 10:04:45 +08:00
ComRender* tRender = dynamic_cast<ComRender*>(com);
2019-11-23 20:27:39 +08:00
if (tRender == nullptr)
{
vecComs.push_back(com);
}
else
{
render = tRender;
}
}
}
CC_SAFE_DELETE(data);
2021-12-25 10:04:45 +08:00
if (_fnSelector != nullptr)
2019-11-23 20:27:39 +08:00
{
_fnSelector(com, data);
}
}
if (parent != nullptr)
{
if (render == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
{
gb = Node::create();
if (render != nullptr)
{
vecComs.push_back(render);
}
}
else
{
gb = render->getNode();
gb->retain();
render->setNode(nullptr);
}
parent->addChild(gb);
}
setPropertyFromJsonDict(dict, gb);
for (std::vector<Component*>::iterator iter = vecComs.begin(); iter != vecComs.end(); ++iter)
{
2021-12-25 10:04:45 +08:00
gb->addComponent(*iter);
2019-11-23 20:27:39 +08:00
}
int length = DICTOOL->getArrayCount_json(dict, "gameobjects");
for (int i = 0; i < length; ++i)
{
2021-12-25 10:04:45 +08:00
const rapidjson::Value& subDict = DICTOOL->getSubDictionary_json(dict, "gameobjects", i);
2019-11-23 20:27:39 +08:00
if (!DICTOOL->checkObjectExist_json(subDict))
{
break;
}
createObject(subDict, gb, attachComponent);
}
2021-12-25 10:04:45 +08:00
if (dict.HasMember("CanvasSize"))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const rapidjson::Value& canvasSizeDict = DICTOOL->getSubDictionary_json(dict, "CanvasSize");
2019-11-23 20:27:39 +08:00
if (DICTOOL->checkObjectExist_json(canvasSizeDict))
{
2021-12-25 10:04:45 +08:00
int width = DICTOOL->getIntValue_json(canvasSizeDict, "_width");
2019-11-23 20:27:39 +08:00
int height = DICTOOL->getIntValue_json(canvasSizeDict, "_height");
gb->setContentSize(Size(width, height));
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return gb;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return nullptr;
}
2021-12-25 10:04:45 +08:00
cocos2d::Node* SceneReader::createObject(CocoLoader* cocoLoader,
stExpCocoNode* cocoNode,
cocos2d::Node* parent,
AttachComponentType attachComponent)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* pNodeArray = cocoNode->GetChildArray(cocoLoader);
std::string Key = pNodeArray[1].GetName(cocoLoader);
const char* className = Key == "classname" ? pNodeArray[1].GetValue(cocoLoader) : "";
if (strcmp(className, "CCNode") == 0)
2019-11-23 20:27:39 +08:00
{
Node* gb = nullptr;
std::vector<Component*> _vecComs;
2021-12-25 10:04:45 +08:00
ComRender* pRender = nullptr;
int count = 0;
std::string key = pNodeArray[13].GetName(cocoLoader);
2019-11-23 20:27:39 +08:00
if (key == "components")
{
count = pNodeArray[13].GetChildNum();
}
2021-12-25 10:04:45 +08:00
stExpCocoNode* pComponents = pNodeArray[13].GetChildArray(cocoLoader);
SerData* data = new SerData();
2019-11-23 20:27:39 +08:00
for (int i = 0; i < count; ++i)
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* subDict = pComponents[i].GetChildArray(cocoLoader);
2019-11-23 20:27:39 +08:00
if (subDict == nullptr)
{
continue;
}
2021-12-25 10:04:45 +08:00
std::string key1 = subDict[1].GetName(cocoLoader);
const char* comName = subDict[1].GetValue(cocoLoader);
Component* pCom = nullptr;
2019-11-23 20:27:39 +08:00
if (key1 == "classname" && comName != nullptr)
{
pCom = createComponent(comName);
}
if (pCom != nullptr)
{
2021-12-25 10:04:45 +08:00
data->_rData = nullptr;
data->_cocoNode = subDict;
2019-11-23 20:27:39 +08:00
data->_cocoLoader = cocoLoader;
if (pCom->serialize(data))
{
2021-12-25 10:04:45 +08:00
ComRender* pTRender = dynamic_cast<ComRender*>(pCom);
2019-11-23 20:27:39 +08:00
if (pTRender != nullptr)
{
pRender = pTRender;
}
else
{
_vecComs.push_back(pCom);
}
}
else
{
CC_SAFE_RELEASE_NULL(pCom);
}
}
2021-12-25 10:04:45 +08:00
if (_fnSelector != nullptr)
2019-11-23 20:27:39 +08:00
{
_fnSelector(pCom, (void*)(data));
}
}
CC_SAFE_DELETE(data);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (parent != nullptr)
{
if (pRender == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
{
gb = Node::create();
if (pRender != nullptr)
{
_vecComs.push_back(pRender);
}
}
else
{
gb = pRender->getNode();
gb->retain();
pRender->setNode(nullptr);
CC_SAFE_RELEASE_NULL(pRender);
}
parent->addChild(gb);
}
setPropertyFromJsonDict(cocoLoader, cocoNode, gb);
for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
{
gb->addComponent(*iter);
}
2021-12-25 10:04:45 +08:00
stExpCocoNode* pGameObjects = pNodeArray[12].GetChildArray(cocoLoader);
2019-11-23 20:27:39 +08:00
if (pGameObjects != nullptr)
{
int length = pNodeArray[12].GetChildNum();
for (int i = 0; i < length; ++i)
{
createObject(cocoLoader, &pGameObjects[i], gb, attachComponent);
}
}
return gb;
}
return nullptr;
}
void SceneReader::setTarget(const std::function<void(cocos2d::Ref* obj, void* doc)>& selector)
{
_fnSelector = selector;
}
Node* SceneReader::getNodeByTag(int nTag)
{
if (_node == nullptr)
{
return nullptr;
}
if (_node->getTag() == nTag)
{
return _node;
}
return nodeByTag(_node, nTag);
}
2021-12-25 10:04:45 +08:00
void SceneReader::setPropertyFromJsonDict(const rapidjson::Value& root, cocos2d::Node* node)
2019-11-23 20:27:39 +08:00
{
float x = DICTOOL->getFloatValue_json(root, "x");
float y = DICTOOL->getFloatValue_json(root, "y");
node->setPosition(x, y);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
const bool bVisible = (DICTOOL->getIntValue_json(root, "visible", 1) != 0);
node->setVisible(bVisible);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int nTag = DICTOOL->getIntValue_json(root, "objecttag", -1);
node->setTag(nTag);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int nZorder = DICTOOL->getIntValue_json(root, "zorder");
node->setLocalZOrder(nZorder);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float fScaleX = DICTOOL->getFloatValue_json(root, "scalex", 1.0);
float fScaleY = DICTOOL->getFloatValue_json(root, "scaley", 1.0);
node->setScaleX(fScaleX);
node->setScaleY(fScaleY);
2021-12-25 10:04:45 +08:00
float fRotationZ = DICTOOL->getFloatValue_json(root, "rotation");
2019-11-23 20:27:39 +08:00
node->setRotation(fRotationZ);
2021-12-25 10:04:45 +08:00
const char* sName = DICTOOL->getStringValue_json(root, "name", "");
2019-11-23 20:27:39 +08:00
node->setName(sName);
}
2021-12-25 10:04:45 +08:00
void SceneReader::setPropertyFromJsonDict(CocoLoader* cocoLoader, stExpCocoNode* cocoNode, cocos2d::Node* node)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* stChildArray = cocoNode->GetChildArray(cocoLoader);
2021-10-11 12:15:41 +08:00
2019-11-23 20:27:39 +08:00
for (int i = 0; i < cocoNode->GetChildNum(); ++i)
{
2021-12-25 10:04:45 +08:00
std::string key = stChildArray[i].GetName(cocoLoader);
2019-11-23 20:27:39 +08:00
std::string value = stChildArray[i].GetValue(cocoLoader);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (key == "x")
{
2021-10-11 12:15:41 +08:00
node->setPositionX(utils::atof(value.c_str()));
2019-11-23 20:27:39 +08:00
}
else if (key == "y")
{
2021-10-11 12:15:41 +08:00
node->setPositionY(utils::atof(value.c_str()));
2019-11-23 20:27:39 +08:00
}
else if (key == "visible")
{
2021-10-11 12:15:41 +08:00
node->setVisible(atoi(value.c_str()) != 0);
2019-11-23 20:27:39 +08:00
}
else if (key == "objecttag")
{
2021-10-11 12:15:41 +08:00
node->setTag(atoi(value.c_str()));
2019-11-23 20:27:39 +08:00
}
else if (key == "zorder")
{
2021-10-11 12:15:41 +08:00
node->setLocalZOrder(atoi(value.c_str()));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == "scalex")
2019-11-23 20:27:39 +08:00
{
2021-10-11 12:15:41 +08:00
node->setScaleX(utils::atof(value.c_str()));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == "scaley")
2019-11-23 20:27:39 +08:00
{
2021-10-11 12:15:41 +08:00
node->setScaleY(atof(value.c_str()));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == "rotation")
2019-11-23 20:27:39 +08:00
{
2021-10-11 12:15:41 +08:00
node->setRotation(utils::atof(value.c_str()));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == "name")
2019-11-23 20:27:39 +08:00
{
2021-10-11 12:15:41 +08:00
node->setName(value);
2019-11-23 20:27:39 +08:00
}
}
}
SceneReader* SceneReader::getInstance()
{
if (s_sharedReader == nullptr)
{
2021-12-08 00:11:53 +08:00
s_sharedReader = new SceneReader();
2019-11-23 20:27:39 +08:00
}
return s_sharedReader;
}
void SceneReader::destroyInstance()
{
DictionaryHelper::destroyInstance();
TriggerMng::destroyInstance();
2019-11-25 01:35:26 +08:00
// CocosDenshion::SimpleAudioEngine::end();
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(s_sharedReader);
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio