axmol/extensions/cocostudio/FlatBuffersSerialize.cpp

1596 lines
49 KiB
C++
Raw Normal View History

2014-11-21 15:15:38 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
2019-11-26 17:51:16 +08:00
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-2020 simdsoft.com, @HALX99.
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +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:
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
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 "FlatBuffersSerialize.h"
2014-11-21 15:15:38 +08:00
#include "base/ObjectFactory.h"
2019-11-26 17:51:16 +08:00
#include "base/ccConstants.h"
2014-11-21 15:15:38 +08:00
#include "ui/CocosGUI.h"
2016-04-18 15:09:21 +08:00
#include "platform/CCFileUtils.h"
#include "CSLanguageDataBinary_generated.h"
#include "CSParseBinary_generated.h"
2014-11-21 15:15:38 +08:00
#include "WidgetReader/NodeReaderProtocol.h"
#include "WidgetReader/NodeReaderDefine.h"
2014-11-21 15:15:38 +08:00
#include "WidgetReader/NodeReader/NodeReader.h"
#include "WidgetReader/SingleNodeReader/SingleNodeReader.h"
#include "WidgetReader/SpriteReader/SpriteReader.h"
#include "WidgetReader/ParticleReader/ParticleReader.h"
#include "WidgetReader/GameMapReader/GameMapReader.h"
#include "WidgetReader/ComAudioReader/ComAudioReader.h"
#include "WidgetReader/ProjectNodeReader/ProjectNodeReader.h"
2014-11-21 15:15:38 +08:00
#include "WidgetReader/ButtonReader/ButtonReader.h"
#include "WidgetReader/CheckBoxReader/CheckBoxReader.h"
#include "WidgetReader/ImageViewReader/ImageViewReader.h"
#include "WidgetReader/TextBMFontReader/TextBMFontReader.h"
#include "WidgetReader/TextReader/TextReader.h"
#include "WidgetReader/TextFieldReader/TextFieldReader.h"
#include "WidgetReader/TextAtlasReader/TextAtlasReader.h"
#include "WidgetReader/LoadingBarReader/LoadingBarReader.h"
#include "WidgetReader/SliderReader/SliderReader.h"
#include "WidgetReader/LayoutReader/LayoutReader.h"
#include "WidgetReader/ScrollViewReader/ScrollViewReader.h"
#include "WidgetReader/PageViewReader/PageViewReader.h"
#include "WidgetReader/ListViewReader/ListViewReader.h"
2014-11-21 15:15:38 +08:00
#include "WidgetReader/TextFieldReader/TextFieldExReader.h"
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
#include "flatbuffers/flatbuffers.h"
2014-12-10 12:36:14 +08:00
USING_NS_AX;
2022-08-08 18:02:17 +08:00
using namespace ax::ui;
2014-11-21 15:15:38 +08:00
using namespace cocostudio;
using namespace cocostudio::timeline;
using namespace flatbuffers;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2014-11-21 15:15:38 +08:00
static const char* Property_VisibleForFrame = "VisibleForFrame";
2021-12-25 10:04:45 +08:00
static const char* Property_Position = "Position";
static const char* Property_Scale = "Scale";
static const char* Property_RotationSkew = "RotationSkew";
static const char* Property_CColor = "CColor";
static const char* Property_FileData = "FileData";
static const char* Property_FrameEvent = "FrameEvent";
static const char* Property_Alpha = "Alpha";
static const char* Property_AnchorPoint = "AnchorPoint";
static const char* Property_ZOrder = "ZOrder";
static const char* Property_ActionValue = "ActionValue";
static const char* Property_BlendValue = "BlendFunc";
2014-11-21 15:15:38 +08:00
2014-12-10 12:36:14 +08:00
static FlatBuffersSerialize* _instanceFlatBuffersSerialize = nullptr;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize::FlatBuffersSerialize() : _isSimulator(false), _builder(nullptr), _csparsebinary(nullptr)
2014-11-21 15:15:38 +08:00
{
CREATE_CLASS_NODE_READER_INFO(NodeReader);
CREATE_CLASS_NODE_READER_INFO(SingleNodeReader);
CREATE_CLASS_NODE_READER_INFO(SpriteReader);
CREATE_CLASS_NODE_READER_INFO(ParticleReader);
CREATE_CLASS_NODE_READER_INFO(GameMapReader);
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
CREATE_CLASS_NODE_READER_INFO(ButtonReader);
CREATE_CLASS_NODE_READER_INFO(CheckBoxReader);
CREATE_CLASS_NODE_READER_INFO(ImageViewReader);
CREATE_CLASS_NODE_READER_INFO(TextBMFontReader);
CREATE_CLASS_NODE_READER_INFO(TextReader);
CREATE_CLASS_NODE_READER_INFO(TextFieldReader);
CREATE_CLASS_NODE_READER_INFO(TextAtlasReader);
CREATE_CLASS_NODE_READER_INFO(LoadingBarReader);
CREATE_CLASS_NODE_READER_INFO(SliderReader);
CREATE_CLASS_NODE_READER_INFO(LayoutReader);
CREATE_CLASS_NODE_READER_INFO(ScrollViewReader);
CREATE_CLASS_NODE_READER_INFO(PageViewReader);
CREATE_CLASS_NODE_READER_INFO(ListViewReader);
2019-11-24 23:15:56 +08:00
CREATE_CLASS_NODE_READER_INFO(TextFieldExReader);
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize::~FlatBuffersSerialize() {}
2014-11-21 15:15:38 +08:00
FlatBuffersSerialize* FlatBuffersSerialize::getInstance()
{
2014-12-10 12:36:14 +08:00
if (!_instanceFlatBuffersSerialize)
2014-11-21 15:15:38 +08:00
{
2021-12-08 00:11:53 +08:00
_instanceFlatBuffersSerialize = new FlatBuffersSerialize();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
return _instanceFlatBuffersSerialize;
}
2019-11-24 23:15:56 +08:00
void FlatBuffersSerialize::purge()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_instanceFlatBuffersSerialize);
}
2014-11-21 15:15:38 +08:00
2015-03-30 16:46:33 +08:00
void FlatBuffersSerialize::destroyInstance()
2014-11-21 15:15:38 +08:00
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_instanceFlatBuffersSerialize);
2014-12-10 12:36:14 +08:00
}
void FlatBuffersSerialize::deleteFlatBufferBuilder()
{
if (_builder != nullptr)
{
_builder->Clear();
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_builder);
2014-12-10 12:36:14 +08:00
}
2014-11-21 15:15:38 +08:00
}
2021-12-26 23:26:34 +08:00
std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(std::string_view xmlFileName,
std::string_view flatbuffersFileName)
2014-11-21 15:15:38 +08:00
{
2019-11-24 23:15:56 +08:00
std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName).c_str();
2014-11-21 15:15:38 +08:00
// xml read
if (!FileUtils::getInstance()->isFileExist(inFullpath))
{
2019-11-24 23:15:56 +08:00
return ".csd file doesn't exists!";
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(inFullpath);
return serializeFlatBuffersWithXMLBuffer(xmlBuffer, flatbuffersFileName);
}
std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLBuffer(std::string& xmlBuffer,
2021-12-26 23:26:34 +08:00
std::string_view flatbuffersFileName)
2019-11-24 23:15:56 +08:00
{
2014-11-21 15:15:38 +08:00
// xml parse
2019-11-24 23:15:56 +08:00
pugi::xml_document document;
if (!xmlBuffer.empty())
document.load_buffer_inplace(&xmlBuffer.front(), xmlBuffer.length());
if (document)
return serializeFlatBuffersWithOpaque(&document, flatbuffersFileName);
return "";
}
2021-12-26 23:26:34 +08:00
std::string FlatBuffersSerialize::serializeFlatBuffersWithOpaque(void* opaque, std::string_view flatbuffersFileName)
2019-11-24 23:15:56 +08:00
{
auto thiz = FlatBuffersSerialize::getInstance();
// xml parse
pugi::xml_document& document = *reinterpret_cast<pugi::xml_document*>(opaque);
2021-12-25 10:04:45 +08:00
pugi::xml_node rootElement = document.document_element(); // Root
2022-07-16 10:43:05 +08:00
// AXLOG("rootElement name = %s", rootelement.name());
2019-11-24 23:15:56 +08:00
pugi::xml_node element = rootElement.first_child();
2014-11-21 15:15:38 +08:00
bool serializeEnabled = false;
2020-08-04 10:55:30 +08:00
std::string rootType;
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
while (element)
{
2022-07-16 10:43:05 +08:00
// AXLOG("entity name = %s", element.name());
2021-12-31 15:49:45 +08:00
if ("PropertyGroup"sv == element.name())
{
2021-12-25 10:04:45 +08:00
auto attribute = element.first_attribute();
2021-12-31 15:49:45 +08:00
while (attribute && "Version"sv != attribute.name())
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
if (attribute)
2019-11-24 23:15:56 +08:00
thiz->_csdVersion = attribute.value();
thiz->_csdVersion = "10.0.3000.0";
}
2021-12-31 15:49:45 +08:00
if ("Content"sv == element.name())
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
auto attribute = element.first_attribute();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
//
if (!attribute)
{
serializeEnabled = true;
2021-12-25 10:04:45 +08:00
rootType = "NodeObjectData";
2014-11-21 15:15:38 +08:00
}
//
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
//
// while (attribute)
// {
2019-11-24 23:15:56 +08:00
// std::string name = attribute.name();
2021-12-31 15:49:45 +08:00
// std::string_view value = attribute.value();
2022-07-16 10:43:05 +08:00
// AXLOG("attribute name = %s, value = %s", name, value);
2014-11-21 15:15:38 +08:00
// if (name == "")
// {
// serializeEnabled = true;
// rootType = (strcmp("", value) == 0) ? "Node" : value;
// }
//
// if (serializeEnabled)
// {
// break;
// }
//
2019-11-24 23:15:56 +08:00
// attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
// }
//
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (serializeEnabled)
{
break;
}
2019-11-24 23:15:56 +08:00
auto child = element.first_child();
2014-11-21 15:15:38 +08:00
if (child)
{
element = child;
}
else
{
2019-11-24 23:15:56 +08:00
element = element.next_sibling();
2014-11-21 15:15:38 +08:00
}
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (serializeEnabled)
{
2021-12-08 00:11:53 +08:00
thiz->_builder = new FlatBufferBuilder();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
Offset<NodeTree> nodeTree;
2019-11-24 23:15:56 +08:00
Offset<NodeAction> action;
std::vector<Offset<flatbuffers::AnimationInfo>> animationInfos;
2019-11-24 23:15:56 +08:00
auto child = element.first_child();
2014-11-21 15:15:38 +08:00
while (child)
{
2021-12-31 15:49:45 +08:00
std::string_view name = child.name();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Animation") // action
2014-11-21 15:15:38 +08:00
{
2019-11-24 23:15:56 +08:00
pugi::xml_node animation = child;
2021-12-25 10:04:45 +08:00
action = thiz->createNodeAction(animation);
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "ObjectData") // nodeTree
2014-11-21 15:15:38 +08:00
{
2019-11-24 23:15:56 +08:00
pugi::xml_node objectData = child;
2015-07-03 17:52:42 +08:00
2019-11-24 23:15:56 +08:00
auto nameElem = objectData.first_attribute();
2015-07-23 11:51:49 +08:00
while (nameElem)
2015-07-03 17:52:42 +08:00
{
2021-12-31 15:49:45 +08:00
if ("ctype"sv == nameElem.name())
2015-07-03 17:52:42 +08:00
{
2019-11-24 23:15:56 +08:00
rootType = nameElem.value();
2015-07-03 17:52:42 +08:00
break;
}
2015-07-23 11:51:49 +08:00
else
2019-11-24 23:15:56 +08:00
nameElem = nameElem.next_attribute();
2015-07-03 17:52:42 +08:00
}
if (rootType == "GameNodeObjectData" || rootType == "GameLayerObjectData") // for adaptate old version
2015-07-23 11:51:49 +08:00
rootType = "NodeObjectData";
2015-07-03 17:52:42 +08:00
2019-11-24 23:15:56 +08:00
nodeTree = thiz->createNodeTree(objectData, rootType);
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "AnimationList") // animation list
{
2019-11-24 23:15:56 +08:00
pugi::xml_node animationinfoElement = child.first_child();
while (animationinfoElement)
{
2019-11-24 23:15:56 +08:00
auto animationinfo = thiz->createAnimationInfo(animationinfoElement);
animationInfos.emplace_back(animationinfo);
2019-11-24 23:15:56 +08:00
animationinfoElement = animationinfoElement.next_sibling();
}
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
auto csparsebinary = CreateCSParseBinary(*thiz->_builder, thiz->_builder->CreateString(thiz->_csdVersion),
thiz->_builder->CreateVector(thiz->_textures),
thiz->_builder->CreateVector(thiz->_texturePngs), nodeTree, action,
thiz->_builder->CreateVector(animationInfos));
2019-11-24 23:15:56 +08:00
thiz->_builder->Finish(csparsebinary);
thiz->_textures.clear();
thiz->_texturePngs.clear();
2014-11-21 15:15:38 +08:00
std::string outFullPath = FileUtils::getInstance()->fullPathForFilename(flatbuffersFileName);
2021-12-25 10:04:45 +08:00
size_t pos = outFullPath.find_last_of('.');
std::string convert = outFullPath.substr(0, pos).append(".csb");
auto save =
FileUtils::writeBinaryToFile(thiz->_builder->GetBufferPointer(), thiz->_builder->GetSize(), convert);
2014-11-21 15:15:38 +08:00
if (!save)
{
return "couldn't save files!";
}
2019-11-24 23:15:56 +08:00
thiz->deleteFlatBufferBuilder();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
return "";
}
// NodeTree
2021-12-31 15:49:45 +08:00
Offset<NodeTree> FlatBuffersSerialize::createNodeTree(pugi::xml_node objectData, std::string_view classType)
2014-11-21 15:15:38 +08:00
{
2021-12-31 15:49:45 +08:00
auto classname = classType.substr(0, classType.find("ObjectData"));
2022-07-16 10:43:05 +08:00
// AXLOG("classname = %s", classname.c_str());
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
Offset<Options> options;
std::vector<Offset<NodeTree>> children;
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (classname == "ProjectNode")
{
2021-12-25 10:04:45 +08:00
auto reader = ProjectNodeReader::getInstance();
2019-11-24 23:15:56 +08:00
auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
2021-12-25 10:04:45 +08:00
2019-11-24 23:15:56 +08:00
options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
2014-11-21 15:15:38 +08:00
}
else if (classname == "SimpleAudio")
{
2021-12-25 10:04:45 +08:00
auto reader = ComAudioReader::getInstance();
2019-11-24 23:15:56 +08:00
auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
2021-12-25 10:04:45 +08:00
options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
2014-11-21 15:15:38 +08:00
}
else
{
std::string readername = getGUIClassName(classname);
readername.append("Reader");
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
NodeReaderProtocol* reader =
dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
2015-03-12 15:47:15 +08:00
if (reader != nullptr)
{
2019-11-24 23:15:56 +08:00
auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
2021-12-25 10:04:45 +08:00
options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
2015-03-12 15:47:15 +08:00
}
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
// children
bool containChildrenElement = false;
2021-12-25 10:04:45 +08:00
auto child = objectData.first_child();
2019-11-24 23:15:56 +08:00
while (child)
2014-11-21 15:15:38 +08:00
{
2022-07-16 10:43:05 +08:00
// AXLOG("child name = %s", child.name());
2019-11-24 23:15:56 +08:00
2021-12-31 15:49:45 +08:00
if ("Children" == child.name())
2014-11-21 15:15:38 +08:00
{
containChildrenElement = true;
break;
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (containChildrenElement)
{
2019-11-24 23:15:56 +08:00
child = child.first_child();
2022-07-16 10:43:05 +08:00
// AXLOG("element name = %s", child.name());
2019-11-24 23:15:56 +08:00
while (child)
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
auto attribute = child.first_attribute();
bool bHasType = false;
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view attriname = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (attriname == "ctype")
{
children.emplace_back(createNodeTree(child, value));
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
bHasType = true;
break;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
if (!bHasType)
2014-11-21 15:15:38 +08:00
{
children.emplace_back(createNodeTree(child, "NodeObjectData"));
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
}
//
2019-11-24 23:15:56 +08:00
2020-08-04 10:55:30 +08:00
std::string customClassName;
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view attriname = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
if (attriname == "CustomClassName")
{
customClassName = value;
break;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateNodeTree(*_builder, _builder->CreateString(classname), _builder->CreateVector(children), options,
_builder->CreateString(customClassName));
2014-12-10 12:36:14 +08:00
}
2021-12-31 15:49:45 +08:00
int FlatBuffersSerialize::getResourceType(std::string_view key)
{
2019-11-24 23:15:56 +08:00
if (key == "Normal" || key == "Default")
{
2021-12-25 10:04:45 +08:00
return 0;
}
2019-11-24 23:15:56 +08:00
if (_isSimulator)
{
2019-11-24 23:15:56 +08:00
if (key == "MarkedSubImage")
{
2014-12-10 12:36:14 +08:00
return 0;
}
}
2014-12-10 12:36:14 +08:00
return 1;
}
2021-12-26 23:26:34 +08:00
std::string FlatBuffersSerialize::getGUIClassName(std::string_view name)
{
2021-12-27 13:52:08 +08:00
std::string convertedClassName;
2014-12-10 12:36:14 +08:00
if (name == "Panel")
{
convertedClassName = "Layout";
}
else if (name == "TextArea")
{
convertedClassName = "Text";
}
else if (name == "TextButton")
{
convertedClassName = "Button";
}
else if (name == "Label")
{
convertedClassName = "Text";
}
else if (name == "LabelAtlas")
{
convertedClassName = "TextAtlas";
}
else if (name == "LabelBMFont")
{
convertedClassName = "TextBMFont";
}
2021-12-27 13:52:08 +08:00
else
convertedClassName = name;
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
return convertedClassName;
}
std::string FlatBuffersSerialize::getWidgetReaderClassName(Widget* widget)
{
std::string readerName;
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
// 1st., custom widget parse properties of parent widget with parent widget reader
if (dynamic_cast<Button*>(widget))
{
2014-12-10 12:36:14 +08:00
readerName = "ButtonReader";
}
2014-12-10 12:36:14 +08:00
else if (dynamic_cast<CheckBox*>(widget))
{
2014-12-10 12:36:14 +08:00
readerName = "CheckBoxReader";
}
2014-12-10 12:36:14 +08:00
else if (dynamic_cast<ImageView*>(widget))
{
2014-12-10 12:36:14 +08:00
readerName = "ImageViewReader";
}
else if (dynamic_cast<TextAtlas*>(widget))
{
readerName = "TextAtlasReader";
}
else if (dynamic_cast<TextBMFont*>(widget))
{
readerName = "TextBMFontReader";
}
else if (dynamic_cast<Text*>(widget))
{
readerName = "TextReader";
}
else if (dynamic_cast<LoadingBar*>(widget))
{
readerName = "LoadingBarReader";
}
else if (dynamic_cast<Slider*>(widget))
{
readerName = "SliderReader";
}
else if (dynamic_cast<TextField*>(widget))
{
readerName = "TextFieldReader";
}
else if (dynamic_cast<ListView*>(widget))
{
readerName = "ListViewReader";
}
else if (dynamic_cast<PageView*>(widget))
{
readerName = "PageViewReader";
}
else if (dynamic_cast<ScrollView*>(widget))
{
readerName = "ScrollViewReader";
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
else if (dynamic_cast<Layout*>(widget))
{
readerName = "LayoutReader";
}
else if (dynamic_cast<Widget*>(widget))
{
readerName = "WidgetReader";
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
return readerName;
}
// NodeAction
2019-11-24 23:15:56 +08:00
Offset<NodeAction> FlatBuffersSerialize::createNodeAction(pugi::xml_node objectData)
2014-12-10 12:36:14 +08:00
{
int duration = 0;
2021-12-25 10:04:45 +08:00
float speed = 0.0f;
2020-08-04 10:55:30 +08:00
std::string currentAnimationName;
2019-11-24 23:15:56 +08:00
2022-07-16 10:43:05 +08:00
// AXLOG("animation name = %s", objectData->Name());
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// ActionTimeline
auto attribute = objectData.first_attribute();
2019-11-24 23:15:56 +08:00
// attributes
2014-12-10 12:36:14 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (name == "Duration")
{
2021-12-31 15:49:45 +08:00
duration = atoi(value.data());
2014-12-10 12:36:14 +08:00
}
else if (name == "Speed")
{
2021-12-31 15:49:45 +08:00
speed = atof(value.data());
}
else if (name == "ActivedAnimationName")
{
2021-12-31 15:49:45 +08:00
currentAnimationName = value;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
// all Timeline
std::vector<Offset<TimeLine>> timelines;
2019-11-24 23:15:56 +08:00
pugi::xml_node timelineElement = objectData.first_child();
2014-12-10 12:36:14 +08:00
while (timelineElement)
{
2014-12-10 12:36:14 +08:00
auto timeLine = createTimeLine(timelineElement);
timelines.emplace_back(timeLine);
2019-11-24 23:15:56 +08:00
timelineElement = timelineElement.next_sibling();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateNodeAction(*_builder, duration, speed, _builder->CreateVector(timelines),
_builder->CreateString(currentAnimationName));
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::AnimationInfo> FlatBuffersSerialize::createAnimationInfo(pugi::xml_node objectData)
{
2020-08-04 10:55:30 +08:00
std::string infoName;
2019-11-24 23:15:56 +08:00
int startIndex = 0;
2021-12-25 10:04:45 +08:00
int endIndex = 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2019-11-24 23:15:56 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
auto attriname = attribute.name();
auto attrivalue = attribute.value();
2019-11-24 23:15:56 +08:00
if (attriname == "Name")
{
infoName = attrivalue;
}
else if (attriname == "StartIndex")
{
2021-12-31 15:49:45 +08:00
startIndex = atoi(attrivalue.data());
2019-11-24 23:15:56 +08:00
}
else if (attriname == "EndIndex")
{
2021-12-31 15:49:45 +08:00
endIndex = atoi(attrivalue.data());
2019-11-24 23:15:56 +08:00
}
attribute = attribute.next_attribute();
}
return CreateAnimationInfo(*_builder, _builder->CreateString(infoName), startIndex, endIndex);
}
Offset<TimeLine> FlatBuffersSerialize::createTimeLine(pugi::xml_node objectData)
2014-11-21 15:15:38 +08:00
{
int actionTag = 0;
2020-08-04 10:55:30 +08:00
std::string property;
2019-11-24 23:15:56 +08:00
// TimelineData attributes
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (name == "ActionTag")
{
2021-12-31 15:49:45 +08:00
actionTag = atoi(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "Property")
2014-11-21 15:15:38 +08:00
{
property = value;
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
// all Frame
std::vector<Offset<flatbuffers::Frame>> frames;
2019-11-24 23:15:56 +08:00
pugi::xml_node frameElement = objectData.first_child();
2014-11-21 15:15:38 +08:00
while (frameElement)
{
Offset<flatbuffers::Frame> frame;
2019-11-24 23:15:56 +08:00
if (property == Property_VisibleForFrame)
2014-11-21 15:15:38 +08:00
{
auto boolFrame = createBoolFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
0, // EventFrame
0, // IntFrame
boolFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_Position)
2014-11-21 15:15:38 +08:00
{
auto pointFrame = createPointFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder, pointFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_Scale)
2014-11-21 15:15:38 +08:00
{
auto scaleFrame = createScaleFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
scaleFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_RotationSkew)
2014-11-21 15:15:38 +08:00
{
auto scaleFrame = createScaleFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
scaleFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_CColor)
2014-11-21 15:15:38 +08:00
{
auto colorFrame = createColorFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
colorFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_FileData)
2014-11-21 15:15:38 +08:00
{
auto textureFrame = createTextureFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
textureFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_FrameEvent)
2014-11-21 15:15:38 +08:00
{
auto eventFrame = createEventFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
eventFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_Alpha)
2014-11-21 15:15:38 +08:00
{
auto intFrame = createIntFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
0, // EventFrame
intFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_AnchorPoint)
{
auto scaleFrame = createScaleFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
scaleFrame);
}
else if (property == Property_ZOrder)
2014-11-21 15:15:38 +08:00
{
auto intFrame = createIntFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
0, // EventFrame
intFrame);
}
else if (property == Property_ActionValue)
{
auto innerActionFrame = createInnerActionFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
0, // EventFrame
0, // IntFrame
0, // BoolFrame
innerActionFrame);
2014-11-21 15:15:38 +08:00
}
else if (property == Property_BlendValue)
{
auto blendFrame = createBlendFrame(frameElement);
2021-12-25 10:04:45 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
0, // EventFrame
0, // IntFrame
0, // BoolFrame
0, // InnerActionFrame
blendFrame);
}
frames.emplace_back(frame);
2019-11-24 23:15:56 +08:00
frameElement = frameElement.next_sibling();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateTimeLine(*_builder, _builder->CreateString(property), actionTag, _builder->CreateVector(frames));
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::PointFrame> FlatBuffersSerialize::createPointFrame(pugi::xml_node objectData)
2014-11-21 15:15:38 +08:00
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
Vec2 position;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
if (name == "X")
2014-11-21 15:15:38 +08:00
{
2021-12-31 15:49:45 +08:00
position.x = atof(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "Y")
2014-11-21 15:15:38 +08:00
{
2021-12-31 15:49:45 +08:00
position.y = atof(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
FVec2 f_position(position.x, position.y);
2021-12-25 10:04:45 +08:00
return CreatePointFrame(*_builder, frameIndex, tween, &f_position, createEasingData(objectData.first_child()));
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::ScaleFrame> FlatBuffersSerialize::createScaleFrame(pugi::xml_node objectData)
2014-11-21 15:15:38 +08:00
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
Vec2 scale;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (name == "X")
{
2021-12-31 15:49:45 +08:00
scale.x = atof(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "Y")
{
2021-12-31 15:49:45 +08:00
scale.y = atof(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Scale f_scale(scale.x, scale.y);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateScaleFrame(*_builder, frameIndex, tween, &f_scale, createEasingData(objectData.first_child()));
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::ColorFrame> FlatBuffersSerialize::createColorFrame(pugi::xml_node objectData)
2014-11-21 15:15:38 +08:00
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
Color3B color;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
// color
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
2014-11-21 15:15:38 +08:00
while (child)
{
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-11-21 15:15:38 +08:00
if (name == "R")
{
2021-12-31 15:49:45 +08:00
color.r = atoi(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "G")
{
2021-12-31 15:49:45 +08:00
color.g = atoi(value.data());
2014-11-21 15:15:38 +08:00
}
else if (name == "B")
{
2021-12-31 15:49:45 +08:00
color.b = atoi(value.data());
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Color f_color(255, color.r, color.g, color.b);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateColorFrame(*_builder, frameIndex, tween, &f_color, createEasingData(objectData.first_child()));
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::TextureFrame> FlatBuffersSerialize::createTextureFrame(pugi::xml_node objectData)
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
2019-11-24 23:15:56 +08:00
2020-08-04 10:55:30 +08:00
std::string path;
std::string plistFile;
int resourceType = 0;
2019-11-24 23:15:56 +08:00
2020-08-04 10:55:30 +08:00
std::string texture;
std::string texturePng;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
while (attribute)
2014-12-10 12:36:14 +08:00
{
2021-12-31 15:49:45 +08:00
std::string_view attriname = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
if (attriname == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(value.data());
}
else if (attriname == "Tween")
{
tween = (value == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
while (child)
{
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2014-12-10 12:36:14 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view attriname = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
if (attriname == "Path")
2014-12-10 12:36:14 +08:00
{
path = value;
2014-12-10 12:36:14 +08:00
}
else if (attriname == "Type")
2014-12-10 12:36:14 +08:00
{
resourceType = getResourceType(value);
}
else if (attriname == "Plist")
{
plistFile = value;
2021-12-25 10:04:45 +08:00
texture = value;
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
if (resourceType == 1)
2014-12-10 12:36:14 +08:00
{
_textures.emplace_back(_builder->CreateString(texture));
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateTextureFrame(
*_builder, frameIndex, tween,
CreateResourceData(*_builder, _builder->CreateString(path), _builder->CreateString(plistFile), resourceType),
2019-11-24 23:15:56 +08:00
createEasingData(objectData.first_child()));
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::EventFrame> FlatBuffersSerialize::createEventFrame(pugi::xml_node objectData)
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
2020-08-04 10:55:30 +08:00
std::string value;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-31 15:49:45 +08:00
auto name = attribute.name();
auto attrivalue = attribute.value();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Value") // to be gonna modify
{
value = attrivalue;
}
else if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(attrivalue.data());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateEventFrame(*_builder, frameIndex, tween, _builder->CreateString(value),
createEasingData(objectData.first_child()));
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::IntFrame> FlatBuffersSerialize::createIntFrame(pugi::xml_node objectData)
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
int value = 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-31 15:49:45 +08:00
auto name = attribute.name();
auto attrivalue = attribute.value();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Value") // to be gonna modify
{
2021-12-31 15:49:45 +08:00
value = atoi(attrivalue.data());
}
else if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(attrivalue.data());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateIntFrame(*_builder, frameIndex, tween, value, createEasingData(objectData.first_child()));
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::BoolFrame> FlatBuffersSerialize::createBoolFrame(pugi::xml_node objectData)
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
bool value = true;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-31 15:49:45 +08:00
auto name = attribute.name();
auto attrivalue = attribute.value();
2019-11-24 23:15:56 +08:00
if (name == "Value")
{
value = (attrivalue == "True") ? true : false;
}
else if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(attrivalue.data());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateBoolFrame(*_builder, frameIndex, tween, value, createEasingData(objectData.first_child()));
}
2019-11-24 23:15:56 +08:00
Offset<flatbuffers::InnerActionFrame> FlatBuffersSerialize::createInnerActionFrame(pugi::xml_node objectData)
2015-04-09 16:55:15 +08:00
{
2021-12-25 10:04:45 +08:00
int frameIndex = 0;
bool tween = true;
2015-04-09 16:55:15 +08:00
int innerActionType = 0;
2020-08-04 10:55:30 +08:00
std::string currentAniamtionName;
2015-04-09 16:55:15 +08:00
int singleFrameIndex = 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2015-04-09 16:55:15 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
auto name = attribute.name();
auto attrivalue = attribute.value();
2019-11-24 23:15:56 +08:00
2015-04-09 16:55:15 +08:00
if (name == "InnerActionType")
{
2015-04-09 16:55:15 +08:00
if (attrivalue == "LoopAction")
2014-12-10 12:36:14 +08:00
{
2015-04-09 16:55:15 +08:00
innerActionType = 0;
}
2015-04-09 16:55:15 +08:00
else if (attrivalue == "NoLoopAction")
{
2015-04-09 16:55:15 +08:00
innerActionType = 1;
}
2015-04-09 16:55:15 +08:00
else if (attrivalue == "SingleFrame")
{
2015-04-09 16:55:15 +08:00
innerActionType = 2;
}
2015-04-09 16:55:15 +08:00
}
else if (name == "CurrentAniamtionName")
{
currentAniamtionName = attrivalue;
}
else if (name == "SingleFrameIndex")
{
2021-12-31 15:49:45 +08:00
singleFrameIndex = atoi(attrivalue.data());
2015-04-09 16:55:15 +08:00
}
else if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(attrivalue.data());
2015-04-09 16:55:15 +08:00
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2015-04-09 16:55:15 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateInnerActionFrame(*_builder, frameIndex, tween, innerActionType,
_builder->CreateString(currentAniamtionName), singleFrameIndex,
createEasingData(objectData.first_child()));
2015-04-09 16:55:15 +08:00
}
2019-11-24 23:15:56 +08:00
flatbuffers::Offset<flatbuffers::BlendFrame> FlatBuffersSerialize::createBlendFrame(pugi::xml_node objectData)
{
int frameIndex = 0;
2021-12-25 10:04:45 +08:00
bool tween = true;
2019-11-26 16:37:39 +08:00
int32_t src = GLBlendConst::ONE, dst = GLBlendConst::ONE_MINUS_SRC_ALPHA;
2020-08-04 10:55:30 +08:00
std::string name;
std::string value;
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
2019-11-24 23:15:56 +08:00
value = attribute.value();
if (name == "FrameIndex")
{
2021-12-31 15:49:45 +08:00
frameIndex = atoi(value.data());
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
}
else if (name == "Src")
{
2021-12-31 15:49:45 +08:00
src = atoi(value.data());
}
else if (name == "Dst")
{
2021-12-31 15:49:45 +08:00
dst = atoi(value.data());
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
flatbuffers::Offset<flatbuffers::EasingData> easingData;
flatbuffers::BlendFunc blendFunc(src, dst);
2021-12-25 10:04:45 +08:00
return CreateBlendFrame(*_builder, frameIndex, tween, &blendFunc, easingData);
}
2019-11-24 23:15:56 +08:00
flatbuffers::Offset<flatbuffers::EasingData> FlatBuffersSerialize::createEasingData(pugi::xml_node objectData)
2015-04-09 16:55:15 +08:00
{
if (!objectData)
{
return 0;
}
2019-11-24 23:15:56 +08:00
2015-04-09 16:55:15 +08:00
int type = -1;
2019-11-24 23:15:56 +08:00
std::vector<flatbuffers::FVec2> points;
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2019-11-24 23:15:56 +08:00
2015-04-09 16:55:15 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2015-04-09 16:55:15 +08:00
if (name == "Type")
{
2021-12-31 15:49:45 +08:00
type = atoi(value.data());
2015-04-09 16:55:15 +08:00
break;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2015-04-09 16:55:15 +08:00
}
2019-11-24 23:15:56 +08:00
pugi::xml_node Points = objectData.first_child();
2015-04-09 16:55:15 +08:00
if (Points)
{
2019-11-24 23:15:56 +08:00
pugi::xml_node PointF = Points.first_child();
2015-04-09 16:55:15 +08:00
while (PointF)
{
Vec2 pointF;
2019-11-24 23:15:56 +08:00
attribute = PointF.first_attribute();
2015-04-09 16:55:15 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2015-04-09 16:55:15 +08:00
if (name == "X")
{
2021-12-31 15:49:45 +08:00
pointF.x = atof(value.data());
2015-04-09 16:55:15 +08:00
}
else if (name == "Y")
{
2021-12-31 15:49:45 +08:00
pointF.y = atof(value.data());
2015-04-09 16:55:15 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
flatbuffers::FVec2 f_PointF(pointF.x, pointF.y);
points.emplace_back(f_PointF);
2019-11-24 23:15:56 +08:00
PointF = PointF.next_sibling();
2014-12-10 12:36:14 +08:00
}
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateEasingData(*_builder, type, _builder->CreateVectorOfStructs(points));
2015-04-09 16:55:15 +08:00
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
/* create flat buffers with XML */
2021-12-26 23:26:34 +08:00
FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulator(std::string_view xmlFileName)
2019-11-24 23:15:56 +08:00
{
2016-05-04 03:18:25 +08:00
std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
// xml read
if (!FileUtils::getInstance()->isFileExist(inFullpath))
{
2022-07-16 10:43:05 +08:00
// AXLOG(".csd file does not exist.");
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
std::string content = FileUtils::getInstance()->getStringFromFile(inFullpath);
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
// xml parse
2019-11-24 23:15:56 +08:00
pugi::xml_document document;
2021-12-25 10:04:45 +08:00
if (!content.empty())
2019-11-24 23:15:56 +08:00
document.load_buffer_inplace(&content.front(), content.length());
2021-12-25 10:04:45 +08:00
pugi::xml_node rootElement = document.document_element(); // Root
2022-07-16 10:43:05 +08:00
// AXLOG("rootElement name = %s", rootelement.name());
2019-11-24 23:15:56 +08:00
pugi::xml_node element = rootElement.first_child();
2014-12-10 12:36:14 +08:00
bool serializeEnabled = false;
2020-08-04 10:55:30 +08:00
std::string rootType;
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
while (element)
{
2022-07-16 10:43:05 +08:00
// AXLOG("entity name = %s", element.name());
2021-12-31 15:49:45 +08:00
if ("PropertyGroup"sv == element.name())
{
2021-12-25 10:04:45 +08:00
auto attribute = element.first_attribute();
2021-12-31 15:49:45 +08:00
while (attribute && "Version"sv != attribute.name())
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
if (attribute)
2019-11-24 23:15:56 +08:00
_csdVersion = attribute.value();
}
2019-11-24 23:15:56 +08:00
2021-12-31 15:49:45 +08:00
if ("Content"sv == element.name())
2014-12-10 12:36:14 +08:00
{
2021-12-25 10:04:45 +08:00
auto attribute = element.first_attribute();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
//
if (!attribute)
{
serializeEnabled = true;
2021-12-25 10:04:45 +08:00
rootType = "NodeObjectData";
2014-12-10 12:36:14 +08:00
}
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (serializeEnabled)
{
break;
}
2019-11-24 23:15:56 +08:00
auto child = element.first_child();
2014-12-10 12:36:14 +08:00
if (child)
{
element = child;
}
else
{
2019-11-24 23:15:56 +08:00
element = element.next_sibling();
2014-12-10 12:36:14 +08:00
}
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (serializeEnabled)
{
2021-12-08 00:11:53 +08:00
_builder = new FlatBufferBuilder();
2014-12-10 12:36:14 +08:00
Offset<NodeTree> nodeTree;
2019-11-24 23:15:56 +08:00
Offset<NodeAction> action;
2021-12-25 10:04:45 +08:00
std::vector<Offset<flatbuffers::AnimationInfo>> animationInfos;
2019-11-24 23:15:56 +08:00
auto child = element.first_child();
2014-12-10 12:36:14 +08:00
while (child)
{
2021-12-31 15:49:45 +08:00
std::string_view name = child.name();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Animation") // action
2014-12-10 12:36:14 +08:00
{
2019-11-24 23:15:56 +08:00
pugi::xml_node animation = child;
2021-12-25 10:04:45 +08:00
action = createNodeAction(animation);
2014-12-10 12:36:14 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "ObjectData") // nodeTree
2014-12-10 12:36:14 +08:00
{
2019-11-24 23:15:56 +08:00
pugi::xml_node objectData = child;
2021-12-25 10:04:45 +08:00
auto nameElem = objectData.first_attribute();
2015-07-23 11:51:49 +08:00
while (nameElem)
2015-07-03 17:52:42 +08:00
{
2021-12-31 15:49:45 +08:00
if ("ctype"sv == nameElem.name())
2015-07-03 17:52:42 +08:00
{
2019-11-24 23:15:56 +08:00
rootType = nameElem.value();
2015-07-03 17:52:42 +08:00
break;
}
2015-07-23 11:51:49 +08:00
else
2019-11-24 23:15:56 +08:00
nameElem = nameElem.next_attribute();
2015-07-03 17:52:42 +08:00
}
if (rootType == "GameNodeObjectData" || rootType == "GameLayerObjectData") // for adaptate old version
2015-07-23 11:51:49 +08:00
rootType = "NodeObjectData";
2014-12-10 12:36:14 +08:00
nodeTree = createNodeTreeForSimulator(objectData, rootType);
}
2021-12-25 10:04:45 +08:00
else if (name == "AnimationList") // animation list
{
2019-11-24 23:15:56 +08:00
pugi::xml_node animationinfoElement = child.first_child();
while (animationinfoElement)
{
auto animationinfo = createAnimationInfo(animationinfoElement);
animationInfos.emplace_back(animationinfo);
2019-11-24 23:15:56 +08:00
animationinfoElement = animationinfoElement.next_sibling();
}
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-12-10 12:36:14 +08:00
}
2021-12-25 10:04:45 +08:00
auto csparsebinary = CreateCSParseBinary(
*_builder, _builder->CreateString(_csdVersion), _builder->CreateVector(_textures),
_builder->CreateVector(_texturePngs), nodeTree, action, _builder->CreateVector(animationInfos));
_builder->Finish(csparsebinary);
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
_textures.clear();
_texturePngs.clear();
}
return _builder;
}
2021-12-31 15:49:45 +08:00
Offset<NodeTree> FlatBuffersSerialize::createNodeTreeForSimulator(pugi::xml_node objectData, std::string_view classType)
2014-11-21 15:15:38 +08:00
{
2021-12-31 15:49:45 +08:00
auto classname = classType.substr(0, classType.find("ObjectData"));
2022-07-16 10:43:05 +08:00
// AXLOG("classname = %s", classname.c_str());
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
Offset<Options> options;
std::vector<Offset<NodeTree>> children;
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (classname == "ProjectNode")
{
auto projectNodeOptions = createProjectNodeOptionsForSimulator(objectData);
2021-12-25 10:04:45 +08:00
options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&projectNodeOptions));
2014-12-10 12:36:14 +08:00
}
else if (classname == "SimpleAudio")
{
2021-12-25 10:04:45 +08:00
auto reader = ComAudioReader::getInstance();
2019-11-24 23:15:56 +08:00
auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
2021-12-25 10:04:45 +08:00
2019-11-24 23:15:56 +08:00
options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
2014-12-10 12:36:14 +08:00
}
else
{
std::string readername = getGUIClassName(classname);
readername.append("Reader");
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
NodeReaderProtocol* reader =
dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
2015-03-12 15:47:15 +08:00
if (reader != nullptr)
{
2019-11-24 23:15:56 +08:00
auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
2021-12-25 10:04:45 +08:00
2019-11-24 23:15:56 +08:00
options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
2015-03-12 15:47:15 +08:00
}
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
// children
bool containChildrenElement = false;
2021-12-25 10:04:45 +08:00
auto child = objectData.first_child();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
while (child)
{
2022-07-16 10:43:05 +08:00
// AXLOG("child name = %s", child.name());
2019-11-24 23:15:56 +08:00
2021-12-31 15:49:45 +08:00
if ("Children"sv == child.name())
2014-12-10 12:36:14 +08:00
{
containChildrenElement = true;
break;
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (containChildrenElement)
{
2019-11-24 23:15:56 +08:00
child = child.first_child();
2022-07-16 10:43:05 +08:00
// AXLOG("element name = %s", child.name());
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
while (child)
{
2021-12-25 10:04:45 +08:00
auto attribute = child.first_attribute();
bool bHasType = false;
2014-12-10 12:36:14 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view attriname = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (attriname == "ctype")
{
children.emplace_back(createNodeTreeForSimulator(child, value));
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
bHasType = true;
break;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
if (!bHasType)
2014-12-10 12:36:14 +08:00
{
children.emplace_back(createNodeTreeForSimulator(child, "NodeObjectData"sv));
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-12-10 12:36:14 +08:00
}
}
//
2019-11-24 23:15:56 +08:00
2020-08-04 10:55:30 +08:00
std::string customClassName;
2021-12-25 10:04:45 +08:00
auto attribute = objectData.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view attriname = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (attriname == "CustomClassName")
2014-11-21 15:15:38 +08:00
{
2014-12-10 12:36:14 +08:00
customClassName = value;
break;
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateNodeTree(*_builder, _builder->CreateString(classname), _builder->CreateVector(children), options,
_builder->CreateString(customClassName));
2014-12-10 12:36:14 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<ProjectNodeOptions> FlatBuffersSerialize::createProjectNodeOptionsForSimulator(pugi::xml_node objectData)
2014-12-10 12:36:14 +08:00
{
2021-12-25 10:04:45 +08:00
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, _builder);
2014-12-10 12:36:14 +08:00
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
2020-08-04 10:55:30 +08:00
std::string filename;
float innerspeed = 1.0f;
2019-11-24 23:15:56 +08:00
pugi::xml_attribute objattri = objectData.first_attribute();
// inneraction speed
while (objattri)
{
2021-12-31 15:49:45 +08:00
std::string_view name = objattri.name();
std::string_view value = objattri.value();
if (name == "InnerActionSpeed")
{
2021-12-31 15:49:45 +08:00
innerspeed = atof(value.data());
break;
}
2019-11-24 23:15:56 +08:00
objattri = objattri.next_attribute();
}
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
// FileData
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
2014-11-21 15:15:38 +08:00
while (child)
{
2021-12-31 15:49:45 +08:00
std::string_view name = child.name();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (name == "FileData")
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
auto attribute = child.first_attribute();
2019-11-24 23:15:56 +08:00
while (attribute)
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2014-12-10 12:36:14 +08:00
if (name == "Path")
2014-12-05 19:19:53 +08:00
{
2014-12-10 12:36:14 +08:00
filename = value;
2014-12-05 19:19:53 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return CreateProjectNodeOptions(*_builder, nodeOptions, _builder->CreateString(filename), innerspeed);
2014-11-21 15:15:38 +08:00
}
/* Serialize language XML file to Flat Buffers file. */
2021-12-26 23:26:34 +08:00
std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFileForLanguageData(std::string_view xmlFilePath,
std::string_view flatBuffersFilePath,
std::string_view languageName)
{
2021-12-25 10:04:45 +08:00
// Read and parse XML data file.
if (!FileUtils::getInstance()->isFileExist(xmlFilePath))
2016-03-29 09:52:17 +08:00
return "Language XML file does not exist.";
std::string content = FileUtils::getInstance()->getStringFromFile(xmlFilePath);
2019-11-24 23:15:56 +08:00
pugi::xml_document document;
if (!content.empty())
document.load_buffer_inplace(&content.front(), content.length());
pugi::xml_node element = document.document_element();
2021-12-25 10:04:45 +08:00
element = element.first_child();
2021-12-25 10:04:45 +08:00
// Create FlatBuffers file using the language data in XML file.
2021-12-08 00:11:53 +08:00
_builder = new FlatBufferBuilder();
std::vector<Offset<LanguageItem>> langItemList;
while (element)
{
2021-12-31 15:49:45 +08:00
if ("language"sv != element.name())
{
2019-11-24 23:15:56 +08:00
element = element.next_sibling();
continue;
}
2021-12-25 10:04:45 +08:00
// Read all of the Key-Values in the XML file.
2020-08-04 10:55:30 +08:00
std::string key;
std::string text;
2021-12-25 10:04:45 +08:00
bool hasKeyReaded = false;
bool hasTextReaded = false;
2019-11-24 23:15:56 +08:00
pugi::xml_node childElement = element.first_child();
while (childElement)
{
2021-12-25 10:04:45 +08:00
// Record language key.
2021-12-31 15:49:45 +08:00
if ("key" == childElement.name())
{
2021-12-25 10:04:45 +08:00
key = childElement.text().as_string();
hasKeyReaded = true;
}
2021-12-25 10:04:45 +08:00
// Record corresponding text.
2021-12-27 13:52:08 +08:00
else if (languageName == childElement.name())
{
2021-12-31 15:49:45 +08:00
auto langText = childElement.text().as_string();
if (!langText.empty())
text = langText;
else
text = key;
hasTextReaded = true;
}
if (hasKeyReaded && hasTextReaded)
break;
2019-11-24 23:15:56 +08:00
childElement = childElement.next_sibling();
}
2021-12-25 10:04:45 +08:00
Offset<flatbuffers::LanguageItem> langItem =
CreateLanguageItem(*_builder, _builder->CreateString(key), _builder->CreateString(text));
langItemList.emplace_back(langItem);
2019-11-24 23:15:56 +08:00
element = element.next_sibling();
}
auto langSet = CreateLanguageSet(*_builder, _builder->CreateVector(langItemList));
_builder->Finish(langSet);
2021-12-25 10:04:45 +08:00
bool isSuccess =
FileUtils::writeBinaryToFile(_builder->GetBufferPointer(), _builder->GetSize(), flatBuffersFilePath);
if (isSuccess)
return "";
else
return "Failed to save language .csb file.";
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio
/**/