axmol/cocos/editor-support/cocostudio/FlatBuffersSerialize.cpp

1653 lines
52 KiB
C++
Raw Normal View History

2014-11-21 15:15:38 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2014-11-21 15:15:38 +08:00
http://www.cocos2d-x.org
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 "editor-support/cocostudio/FlatBuffersSerialize.h"
2014-11-21 15:15:38 +08:00
#include "base/ObjectFactory.h"
#include "ui/CocosGUI.h"
2016-04-18 15:09:21 +08:00
#include "platform/CCFileUtils.h"
#include "editor-support/cocostudio/CocoStudio.h"
#include "editor-support/cocostudio/CSLanguageDataBinary_generated.h"
#include "editor-support/cocostudio/CSParseBinary_generated.h"
2014-11-21 15:15:38 +08:00
#include "editor-support/cocostudio/WidgetReader/NodeReaderProtocol.h"
#include "editor-support/cocostudio/WidgetReader/NodeReaderDefine.h"
2014-11-21 15:15:38 +08:00
#include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
#include "editor-support/cocostudio/WidgetReader/SingleNodeReader/SingleNodeReader.h"
#include "editor-support/cocostudio/WidgetReader/SpriteReader/SpriteReader.h"
#include "editor-support/cocostudio/WidgetReader/ParticleReader/ParticleReader.h"
#include "editor-support/cocostudio/WidgetReader/GameMapReader/GameMapReader.h"
#include "editor-support/cocostudio/WidgetReader/ComAudioReader/ComAudioReader.h"
#include "editor-support/cocostudio/WidgetReader/ProjectNodeReader/ProjectNodeReader.h"
2014-11-21 15:15:38 +08:00
#include "editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.h"
#include "editor-support/cocostudio/WidgetReader/CheckBoxReader/CheckBoxReader.h"
#include "editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.h"
#include "editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
#include "editor-support/cocostudio/WidgetReader/TextReader/TextReader.h"
#include "editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
#include "editor-support/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
#include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
#include "editor-support/cocostudio/WidgetReader/SliderReader/SliderReader.h"
#include "editor-support/cocostudio/WidgetReader/LayoutReader/LayoutReader.h"
#include "editor-support/cocostudio/WidgetReader/ScrollViewReader/ScrollViewReader.h"
#include "editor-support/cocostudio/WidgetReader/PageViewReader/PageViewReader.h"
#include "editor-support/cocostudio/WidgetReader/ListViewReader/ListViewReader.h"
2014-11-21 15:15:38 +08:00
#include "tinyxml2.h"
2014-11-21 15:15:38 +08:00
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/util.h"
2014-12-10 12:36:14 +08:00
2014-11-21 15:15:38 +08:00
USING_NS_CC;
using namespace cocos2d::ui;
using namespace cocostudio;
using namespace cocostudio::timeline;
using namespace flatbuffers;
namespace cocostudio {
static const char* Property_VisibleForFrame = "VisibleForFrame";
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;
2014-11-21 15:15:38 +08:00
FlatBuffersSerialize::FlatBuffersSerialize()
: _isSimulator(false)
, _builder(nullptr)
, _csparsebinary(nullptr)
{
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);
2014-12-10 12:36:14 +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);
}
FlatBuffersSerialize::~FlatBuffersSerialize()
{
2015-03-30 16:46:33 +08:00
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
{
_instanceFlatBuffersSerialize = new (std::nothrow) FlatBuffersSerialize();
2014-11-21 15:15:38 +08:00
}
return _instanceFlatBuffersSerialize;
}
void FlatBuffersSerialize::purge()
{
CC_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
{
CC_SAFE_DELETE(_instanceFlatBuffersSerialize);
2014-12-10 12:36:14 +08:00
}
void FlatBuffersSerialize::deleteFlatBufferBuilder()
{
if (_builder != nullptr)
{
_builder->Clear();
CC_SAFE_DELETE(_builder);
}
2014-11-21 15:15:38 +08:00
}
std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::string &xmlFileName,
const std::string &flatbuffersFileName)
{
std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
2014-11-21 15:15:38 +08:00
// xml read
if (!FileUtils::getInstance()->isFileExist(inFullpath))
{
2016-03-29 09:52:17 +08:00
return ".csd file does not exist.";
2014-11-21 15:15:38 +08:00
}
std::string content = FileUtils::getInstance()->getStringFromFile(inFullpath);
2014-11-21 15:15:38 +08:00
// xml parse
tinyxml2::XMLDocument* document = new (std::nothrow) tinyxml2::XMLDocument();
2014-11-21 15:15:38 +08:00
document->Parse(content.c_str());
const tinyxml2::XMLElement* rootElement = document->RootElement();// Root
2014-12-10 12:36:14 +08:00
// CCLOG("rootElement name = %s", rootElement->Name());
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLElement* element = rootElement->FirstChildElement();
bool serializeEnabled = false;
std::string rootType = "";
while (element)
{
2014-12-10 12:36:14 +08:00
// CCLOG("entity name = %s", element->Name());
if (strcmp("PropertyGroup", element->Name()) == 0)
{
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
while (attribute && strcmp("Version", attribute->Name()) != 0)
attribute = attribute->Next();
if (attribute)
_csdVersion = attribute->Value();
2015-03-12 16:15:17 +08:00
_csdVersion = "2.1.0.0";
}
2014-11-21 15:15:38 +08:00
if (strcmp("Content", element->Name()) == 0)
{
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
//
if (!attribute)
{
serializeEnabled = true;
rootType = "NodeObjectData";
}
//
//
// while (attribute)
// {
// std::string name = attribute->Name();
// std::string value = attribute->Value();
// CCLOG("attribute name = %s, value = %s", name, value);
// if (name == "")
// {
// serializeEnabled = true;
// rootType = (strcmp("", value) == 0) ? "Node" : value;
// }
//
// if (serializeEnabled)
// {
// break;
// }
//
// attribute = attribute->Next();
// }
//
}
if (serializeEnabled)
{
break;
}
const tinyxml2::XMLElement* child = element->FirstChildElement();
if (child)
{
element = child;
}
else
{
element = element->NextSiblingElement();
}
}
if (serializeEnabled)
{
_builder = new (std::nothrow) FlatBufferBuilder();
2014-11-21 15:15:38 +08:00
Offset<NodeTree> nodeTree;
Offset<NodeAction> aciton;
std::vector<Offset<flatbuffers::AnimationInfo>> animationInfos;
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLElement* child = element->FirstChildElement();
while (child)
{
std::string name = child->Name();
if (name == "Animation") // action
{
const tinyxml2::XMLElement* animation = child;
aciton = createNodeAction(animation);
2014-11-21 15:15:38 +08:00
}
else if (name == "ObjectData") // nodeTree
{
const tinyxml2::XMLElement* objectData = child;
2015-07-03 17:52:42 +08:00
2015-07-23 11:51:49 +08:00
auto nameElem = objectData->FirstAttribute();
while (nameElem)
2015-07-03 17:52:42 +08:00
{
2015-07-23 11:51:49 +08:00
if (0 == strcmp("ctype", nameElem->Name()))
2015-07-03 17:52:42 +08:00
{
2015-07-23 11:51:49 +08:00
rootType = nameElem->Value();
2015-07-03 17:52:42 +08:00
break;
}
2015-07-23 11:51:49 +08:00
else
nameElem = nameElem->Next();
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
2014-11-21 15:15:38 +08:00
nodeTree = createNodeTree(objectData, rootType);
}
else if (name == "AnimationList") // animation list
{
const tinyxml2::XMLElement* animationinfoElement = child->FirstChildElement();
while (animationinfoElement)
{
auto animationinfo = createAnimationInfo(animationinfoElement);
animationInfos.push_back(animationinfo);
animationinfoElement = animationinfoElement->NextSiblingElement();
}
}
2014-11-21 15:15:38 +08:00
child = child->NextSiblingElement();
}
2014-11-21 15:15:38 +08:00
2014-12-10 12:36:14 +08:00
auto csparsebinary = CreateCSParseBinary(*_builder,
_builder->CreateString(_csdVersion),
2014-12-10 12:36:14 +08:00
_builder->CreateVector(_textures),
_builder->CreateVector(_texturePngs),
2014-11-21 15:15:38 +08:00
nodeTree,
aciton,
_builder->CreateVector(animationInfos));
_builder->Finish(csparsebinary);
2014-12-04 20:38:46 +08:00
_textures.clear();
_texturePngs.clear();
2014-11-21 15:15:38 +08:00
std::string outFullPath = FileUtils::getInstance()->fullPathForFilename(flatbuffersFileName);
size_t pos = outFullPath.find_last_of('.');
std::string convert = outFullPath.substr(0, pos).append(".csb");
auto save = flatbuffers::SaveFile(convert.c_str(),
2014-12-10 12:36:14 +08:00
reinterpret_cast<const char *>(_builder->GetBufferPointer()),
_builder->GetSize(),
2014-11-21 15:15:38 +08:00
true);
if (!save)
{
return "couldn't save files!";
}
2014-12-10 12:36:14 +08:00
deleteFlatBufferBuilder();
2014-11-21 15:15:38 +08:00
}
return "";
}
// NodeTree
Offset<NodeTree> FlatBuffersSerialize::createNodeTree(const tinyxml2::XMLElement *objectData,
std::string classType)
{
std::string classname = classType.substr(0, classType.find("ObjectData"));
2014-12-10 12:36:14 +08:00
// CCLOG("classname = %s", classname.c_str());
2014-11-21 15:15:38 +08:00
std::string name = "";
Offset<Options> options;
std::vector<Offset<NodeTree>> children;
if (classname == "ProjectNode")
{
auto reader = ProjectNodeReader::getInstance();
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
2014-11-21 15:15:38 +08:00
}
else if (classname == "SimpleAudio")
{
auto reader = ComAudioReader::getInstance();
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
2014-11-21 15:15:38 +08:00
}
else
{
std::string readername = getGUIClassName(classname);
readername.append("Reader");
NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
2015-03-12 15:47:15 +08:00
if (reader != nullptr)
{
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
2015-03-12 15:47:15 +08:00
}
2014-11-21 15:15:38 +08:00
}
// children
bool containChildrenElement = false;
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
2014-11-21 15:15:38 +08:00
while (child)
2014-11-21 15:15:38 +08:00
{
2014-12-10 12:36:14 +08:00
// CCLOG("child name = %s", child->Name());
2014-11-21 15:15:38 +08:00
if (strcmp("Children", child->Name()) == 0)
2014-11-21 15:15:38 +08:00
{
containChildrenElement = true;
break;
}
child = child->NextSiblingElement();
2014-11-21 15:15:38 +08:00
}
if (containChildrenElement)
{
child = child->FirstChildElement();
2014-12-10 12:36:14 +08:00
// CCLOG("element name = %s", child->Name());
2014-11-21 15:15:38 +08:00
while (child)
2014-11-21 15:15:38 +08:00
{
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
2014-11-21 15:15:38 +08:00
bool bHasType = false;
while (attribute)
{
std::string attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "ctype")
{
children.push_back(createNodeTree(child, value));
2014-11-21 15:15:38 +08:00
bHasType = true;
break;
}
attribute = attribute->Next();
}
if(!bHasType)
{
children.push_back(createNodeTree(child, "NodeObjectData"));
2014-11-21 15:15:38 +08:00
}
child = child->NextSiblingElement();
2014-11-21 15:15:38 +08:00
}
}
//
std::string customClassName = "";
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "CustomClassName")
{
customClassName = value;
break;
}
attribute = attribute->Next();
}
2014-11-21 15:15:38 +08:00
return CreateNodeTree(*_builder,
_builder->CreateString(classname),
_builder->CreateVector(children),
options,
_builder->CreateString(customClassName));
2014-12-10 12:36:14 +08:00
}
int FlatBuffersSerialize::getResourceType(std::string key)
{
2014-12-10 12:36:14 +08:00
if(key == "Normal" || key == "Default")
{
2014-12-10 12:36:14 +08:00
return 0;
}
2014-12-10 12:36:14 +08:00
if(_isSimulator)
{
2014-12-10 12:36:14 +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;
}
2014-12-10 12:36:14 +08:00
std::string FlatBuffersSerialize::getGUIClassName(const std::string &name)
{
2014-12-10 12:36:14 +08:00
std::string convertedClassName = name;
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";
}
2014-12-10 12:36:14 +08:00
return convertedClassName;
}
std::string FlatBuffersSerialize::getWidgetReaderClassName(Widget* widget)
{
std::string readerName;
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";
}
2014-12-10 12:36:14 +08:00
else if (dynamic_cast<Layout*>(widget))
{
readerName = "LayoutReader";
}
else if (dynamic_cast<Widget*>(widget))
{
readerName = "WidgetReader";
}
2014-12-10 12:36:14 +08:00
return readerName;
}
// NodeAction
Offset<NodeAction> FlatBuffersSerialize::createNodeAction(const tinyxml2::XMLElement *objectData)
{
int duration = 0;
float speed = 0.0f;
std::string currentAnimationName = "";
2014-12-10 12:36:14 +08:00
// CCLOG("animation name = %s", objectData->Name());
// ActionTimeline
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
// attributes
2014-12-10 12:36:14 +08:00
while (attribute)
{
2014-12-10 12:36:14 +08:00
std::string name = attribute->Name();
std::string value = attribute->Value();
2014-12-10 12:36:14 +08:00
if (name == "Duration")
{
2014-12-10 12:36:14 +08:00
duration = atoi(value.c_str());
}
else if (name == "Speed")
{
speed = atof(value.c_str());
}
else if (name == "ActivedAnimationName")
{
currentAnimationName = value.c_str();
}
2014-12-10 12:36:14 +08:00
attribute = attribute->Next();
}
2014-12-10 12:36:14 +08:00
// all Timeline
std::vector<Offset<TimeLine>> timelines;
const tinyxml2::XMLElement* timelineElement = objectData->FirstChildElement();
while (timelineElement)
{
2014-12-10 12:36:14 +08:00
auto timeLine = createTimeLine(timelineElement);
timelines.push_back(timeLine);
2014-11-21 15:15:38 +08:00
timelineElement = timelineElement->NextSiblingElement();
}
return CreateNodeAction(*_builder,
duration,
speed,
_builder->CreateVector(timelines),
_builder->CreateString(currentAnimationName));
2014-11-21 15:15:38 +08:00
}
Offset<flatbuffers::AnimationInfo> FlatBuffersSerialize::createAnimationInfo(const tinyxml2::XMLElement *objectData)
{
std::string infoName = "";
int startIndex = 0;
int endIndex = 0;
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string attriname = attribute->Name();
std::string attrivalue = attribute->Value();
if (attriname == "Name")
{
infoName = attrivalue;
}
else if (attriname == "StartIndex")
{
startIndex = atoi(attrivalue.c_str());
}
else if (attriname == "EndIndex")
{
endIndex = atoi(attrivalue.c_str());
}
attribute = attribute->Next();
}
return CreateAnimationInfo(*_builder, _builder->CreateString(infoName), startIndex, endIndex);
}
2014-11-21 15:15:38 +08:00
Offset<TimeLine> FlatBuffersSerialize::createTimeLine(const tinyxml2::XMLElement *objectData)
{
int actionTag = 0;
std::string property = "";
2014-11-21 15:15:38 +08:00
// TimelineData attributes
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "ActionTag")
{
actionTag = atoi(value.c_str());
}
else if (name == "Property")
2014-11-21 15:15:38 +08:00
{
property = value;
2014-11-21 15:15:38 +08:00
}
attribute = attribute->Next();
}
// all Frame
std::vector<Offset<flatbuffers::Frame>> frames;
const tinyxml2::XMLElement* frameElement = objectData->FirstChildElement();
while (frameElement)
{
Offset<flatbuffers::Frame> frame;
if (property == Property_VisibleForFrame)
2014-11-21 15:15:38 +08:00
{
auto boolFrame = createBoolFrame(frameElement);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +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);
2014-11-21 15:15:38 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
2014-11-21 15:15:38 +08:00
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);
frame = CreateFrame(*_builder,
0, // PointFrame
scaleFrame);
}
else if (property == Property_ZOrder)
2014-11-21 15:15:38 +08:00
{
auto intFrame = createIntFrame(frameElement);
2014-11-21 15:15:38 +08:00
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
2014-11-21 15:15:38 +08:00
0, // EventFrame
intFrame);
}
else if (property == Property_ActionValue)
{
auto innerActionFrame = createInnerActionFrame(frameElement);
frame = CreateFrame(*_builder,
0, // PointFrame
2014-11-21 15:15:38 +08:00
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);
frame = CreateFrame(*_builder,
0, // PointFrame
0, // ScaleFrame
0, // ColorFrame
0, // TextureFrame
0, // EventFrame
0, // IntFrame
0, // BoolFrame
0, //InnerActionFrame
blendFrame);
}
2014-12-10 12:36:14 +08:00
frames.push_back(frame);
2014-11-21 15:15:38 +08:00
frameElement = frameElement->NextSiblingElement();
}
return CreateTimeLine(*_builder,
_builder->CreateString(property),
2014-11-21 15:15:38 +08:00
actionTag,
_builder->CreateVector(frames));
}
Offset<flatbuffers::PointFrame> FlatBuffersSerialize::createPointFrame(const tinyxml2::XMLElement *objectData)
2014-11-21 15:15:38 +08:00
{
int frameIndex = 0;
bool tween = true;
Vec2 position;
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
2014-11-21 15:15:38 +08:00
if (name == "X")
2014-11-21 15:15:38 +08:00
{
position.x = atof(value.c_str());
2014-11-21 15:15:38 +08:00
}
else if (name == "Y")
2014-11-21 15:15:38 +08:00
{
position.y = atof(value.c_str());
2014-11-21 15:15:38 +08:00
}
else if (name == "FrameIndex")
{
frameIndex = atoi(value.c_str());
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
}
attribute = attribute->Next();
}
2015-04-09 16:55:15 +08:00
Position f_position(position.x, position.y);
return CreatePointFrame(*_builder,
frameIndex,
tween,
2015-04-09 16:55:15 +08:00
&f_position,
createEasingData(objectData->FirstChildElement()));
2014-11-21 15:15:38 +08:00
}
Offset<flatbuffers::ScaleFrame> FlatBuffersSerialize::createScaleFrame(const tinyxml2::XMLElement *objectData)
2014-11-21 15:15:38 +08:00
{
int frameIndex = 0;
bool tween = true;
Vec2 scale;
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "X")
{
scale.x = atof(value.c_str());
2014-11-21 15:15:38 +08:00
}
else if (name == "Y")
{
scale.y = atof(value.c_str());
2014-11-21 15:15:38 +08:00
}
else if (name == "FrameIndex")
{
frameIndex = atoi(value.c_str());
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
}
attribute = attribute->Next();
}
Scale f_scale(scale.x, scale.y);
2014-11-21 15:15:38 +08:00
return CreateScaleFrame(*_builder,
frameIndex,
tween,
2015-04-09 16:55:15 +08:00
&f_scale,
createEasingData(objectData->FirstChildElement()));
2014-11-21 15:15:38 +08:00
}
Offset<flatbuffers::ColorFrame> FlatBuffersSerialize::createColorFrame(const tinyxml2::XMLElement *objectData)
2014-11-21 15:15:38 +08:00
{
int frameIndex = 0;
bool tween = true;
Color3B color;
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "FrameIndex")
{
frameIndex = atoi(value.c_str());
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
}
attribute = attribute->Next();
}
// color
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
attribute = child->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "R")
{
color.r = atoi(value.c_str());
}
else if (name == "G")
{
color.g = atoi(value.c_str());
}
else if (name == "B")
{
color.b = atoi(value.c_str());
}
2014-11-21 15:15:38 +08:00
attribute = attribute->Next();
}
child = child->NextSiblingElement();
}
Color f_color(255, color.r, color.g, color.b);
2014-11-21 15:15:38 +08:00
return CreateColorFrame(*_builder,
frameIndex,
tween,
2015-04-09 16:55:15 +08:00
&f_color,
createEasingData(objectData->FirstChildElement()));
2014-11-21 15:15:38 +08:00
}
Offset<flatbuffers::TextureFrame> FlatBuffersSerialize::createTextureFrame(const tinyxml2::XMLElement *objectData)
{
int frameIndex = 0;
bool tween = true;
std::string path = "";
std::string plistFile = "";
int resourceType = 0;
std::string texture = "";
std::string texturePng = "";
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
2014-12-10 12:36:14 +08:00
{
std::string attriname = attribute->Name();
std::string value = attribute->Value();
2014-12-10 12:36:14 +08:00
if (attriname == "FrameIndex")
{
frameIndex = atoi(value.c_str());
}
else if (attriname == "Tween")
{
tween = (value == "True") ? true : false;
}
2014-12-10 12:36:14 +08:00
attribute = attribute->Next();
}
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
attribute = child->FirstAttribute();
2014-12-10 12:36:14 +08:00
while (attribute)
{
std::string attriname = attribute->Name();
std::string value = attribute->Value();
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;
texture = value;
2014-12-10 12:36:14 +08:00
}
attribute = attribute->Next();
}
if (resourceType == 1)
2014-12-10 12:36:14 +08:00
{
_textures.push_back(_builder->CreateString(texture));
}
child = child->NextSiblingElement();
}
return CreateTextureFrame(*_builder,
frameIndex,
tween,
CreateResourceData(*_builder,
_builder->CreateString(path),
_builder->CreateString(plistFile),
2015-04-09 16:55:15 +08:00
resourceType),
createEasingData(objectData->FirstChildElement()));
}
Offset<flatbuffers::EventFrame> FlatBuffersSerialize::createEventFrame(const tinyxml2::XMLElement *objectData)
{
int frameIndex = 0;
bool tween = true;
std::string value = "";
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string attrivalue = attribute->Value();
if (name == "Value") // to be gonna modify
{
value = attrivalue;
}
else if (name == "FrameIndex")
{
frameIndex = atoi(attrivalue.c_str());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
attribute = attribute->Next();
}
return CreateEventFrame(*_builder,
frameIndex,
tween,
2015-04-09 16:55:15 +08:00
_builder->CreateString(value),
createEasingData(objectData->FirstChildElement()));
}
Offset<flatbuffers::IntFrame> FlatBuffersSerialize::createIntFrame(const tinyxml2::XMLElement *objectData)
{
int frameIndex = 0;
bool tween = true;
int value = 0;
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string attrivalue = attribute->Value();
if (name == "Value") // to be gonna modify
{
value = atoi(attrivalue.c_str());
}
else if (name == "FrameIndex")
{
frameIndex = atoi(attrivalue.c_str());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
attribute = attribute->Next();
}
return CreateIntFrame(*_builder,
frameIndex,
tween,
2015-04-09 16:55:15 +08:00
value,
createEasingData(objectData->FirstChildElement()));
}
Offset<flatbuffers::BoolFrame> FlatBuffersSerialize::createBoolFrame(const tinyxml2::XMLElement *objectData)
{
int frameIndex = 0;
bool tween = true;
bool value = true;
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string attrivalue = attribute->Value();
if (name == "Value")
{
value = (attrivalue == "True") ? true : false;
}
else if (name == "FrameIndex")
{
frameIndex = atoi(attrivalue.c_str());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
attribute = attribute->Next();
}
return CreateBoolFrame(*_builder,
2015-04-09 16:55:15 +08:00
frameIndex,
tween,
value,
createEasingData(objectData->FirstChildElement()));
}
2015-04-09 16:55:15 +08:00
Offset<flatbuffers::InnerActionFrame> FlatBuffersSerialize::createInnerActionFrame(const tinyxml2::XMLElement *objectData)
{
int frameIndex = 0;
bool tween = true;
int innerActionType = 0;
std::string currentAniamtionName = "";
int singleFrameIndex = 0;
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
2015-04-09 16:55:15 +08:00
std::string name = attribute->Name();
std::string attrivalue = attribute->Value();
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")
{
singleFrameIndex = atoi(attrivalue.c_str());
}
else if (name == "FrameIndex")
{
frameIndex = atoi(attrivalue.c_str());
}
else if (name == "Tween")
{
tween = (attrivalue == "True") ? true : false;
}
attribute = attribute->Next();
}
return CreateInnerActionFrame(*_builder,
frameIndex,
tween,
innerActionType,
_builder->CreateString(currentAniamtionName),
singleFrameIndex,
createEasingData(objectData->FirstChildElement()));
}
flatbuffers::Offset<flatbuffers::BlendFrame> FlatBuffersSerialize::createBlendFrame(const tinyxml2::XMLElement* objectData)
{
int frameIndex = 0;
bool tween = true;
int32_t src = GL_ONE, dst = GL_ONE_MINUS_SRC_ALPHA;
std::string name = "";
std::string value = "";
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
name = attribute->Name();
value = attribute->Value();
if (name == "FrameIndex")
{
frameIndex = atoi(value.c_str());
}
else if (name == "Tween")
{
tween = (value == "True") ? true : false;
}
else if (name == "Src")
{
src = atoi(value.c_str());
}
else if (name == "Dst")
{
dst = atoi(value.c_str());
}
attribute = attribute->Next();
}
flatbuffers::Offset<flatbuffers::EasingData> easingData;
flatbuffers::BlendFunc blendFunc(src, dst);
return CreateBlendFrame(*_builder,
frameIndex,
tween,
&blendFunc,
easingData);
}
2015-04-09 16:55:15 +08:00
flatbuffers::Offset<flatbuffers::EasingData> FlatBuffersSerialize::createEasingData(const tinyxml2::XMLElement *objectData)
{
if (!objectData)
{
return 0;
}
int type = -1;
std::vector<flatbuffers::Position> points;
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "Type")
{
type = atoi(value.c_str());
break;
}
attribute = attribute->Next();
}
const tinyxml2::XMLElement* Points = objectData->FirstChildElement();
if (Points)
{
const tinyxml2::XMLElement* PointF = Points->FirstChildElement();
while (PointF)
{
Vec2 pointF;
2015-04-09 16:55:15 +08:00
attribute = PointF->FirstAttribute();
while (attribute)
{
2015-04-09 16:55:15 +08:00
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "X")
{
pointF.x = atof(value.c_str());
}
else if (name == "Y")
{
pointF.y = atof(value.c_str());
}
attribute = attribute->Next();
2014-12-10 12:36:14 +08:00
}
2015-04-09 16:55:15 +08:00
flatbuffers::Position f_PointF(pointF.x, pointF.y);
points.push_back(f_PointF);
2014-12-10 12:36:14 +08:00
2015-04-09 16:55:15 +08:00
PointF = PointF->NextSiblingElement();
2014-12-10 12:36:14 +08:00
}
}
2015-04-09 16:55:15 +08:00
return CreateEasingData(*_builder,
type,
_builder->CreateVectorOfStructs(points));
}
2014-12-10 12:36:14 +08:00
/* create flat buffers with XML */
FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulator(const std::string &xmlFileName)
{
2016-05-04 03:18:25 +08:00
std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
2014-12-10 12:36:14 +08:00
// xml read
if (!FileUtils::getInstance()->isFileExist(inFullpath))
{
2016-03-29 09:52:17 +08:00
// CCLOG(".csd file does not exist.");
2014-12-10 12:36:14 +08:00
}
std::string content = FileUtils::getInstance()->getStringFromFile(inFullpath);
2014-12-10 12:36:14 +08:00
// xml parse
tinyxml2::XMLDocument* document = new (std::nothrow) tinyxml2::XMLDocument();
2014-12-10 12:36:14 +08:00
document->Parse(content.c_str());
const tinyxml2::XMLElement* rootElement = document->RootElement();// Root
// CCLOG("rootElement name = %s", rootElement->Name());
const tinyxml2::XMLElement* element = rootElement->FirstChildElement();
bool serializeEnabled = false;
std::string rootType = "";
while (element)
{
// CCLOG("entity name = %s", element->Name());
if (strcmp("PropertyGroup", element->Name()) == 0)
{
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
while (attribute && strcmp("Version", attribute->Name()) != 0)
attribute = attribute->Next();
if (attribute)
_csdVersion = attribute->Value();
}
2014-12-10 12:36:14 +08:00
if (strcmp("Content", element->Name()) == 0)
{
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
//
if (!attribute)
{
serializeEnabled = true;
rootType = "NodeObjectData";
}
}
if (serializeEnabled)
{
break;
}
const tinyxml2::XMLElement* child = element->FirstChildElement();
if (child)
{
element = child;
}
else
{
element = element->NextSiblingElement();
}
}
if (serializeEnabled)
{
_builder = new (std::nothrow) FlatBufferBuilder();
2014-12-10 12:36:14 +08:00
Offset<NodeTree> nodeTree;
Offset<NodeAction> aciton;
std::vector<Offset<flatbuffers::AnimationInfo> > animationInfos;
2014-12-10 12:36:14 +08:00
const tinyxml2::XMLElement* child = element->FirstChildElement();
while (child)
{
std::string name = child->Name();
if (name == "Animation") // action
{
const tinyxml2::XMLElement* animation = child;
aciton = createNodeAction(animation);
2014-12-10 12:36:14 +08:00
}
else if (name == "ObjectData") // nodeTree
{
const tinyxml2::XMLElement* objectData = child;
2015-07-23 11:51:49 +08:00
auto nameElem = objectData->FirstAttribute();
while (nameElem)
2015-07-03 17:52:42 +08:00
{
2015-07-23 11:51:49 +08:00
if (0 == strcmp("ctype", nameElem->Name()))
2015-07-03 17:52:42 +08:00
{
2015-07-23 11:51:49 +08:00
rootType = nameElem->Value();
2015-07-03 17:52:42 +08:00
break;
}
2015-07-23 11:51:49 +08:00
else
nameElem = nameElem->Next();
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);
}
else if (name == "AnimationList") // animation list
{
const tinyxml2::XMLElement* animationinfoElement = child->FirstChildElement();
while (animationinfoElement)
{
auto animationinfo = createAnimationInfo(animationinfoElement);
animationInfos.push_back(animationinfo);
animationinfoElement = animationinfoElement->NextSiblingElement();
}
}
2014-12-10 12:36:14 +08:00
child = child->NextSiblingElement();
}
auto csparsebinary = CreateCSParseBinary(*_builder,
_builder->CreateString(_csdVersion),
_builder->CreateVector(_textures),
_builder->CreateVector(_texturePngs),
2014-12-10 12:36:14 +08:00
nodeTree,
aciton,
_builder->CreateVector(animationInfos));
_builder->Finish(csparsebinary);
2014-12-10 12:36:14 +08:00
_textures.clear();
_texturePngs.clear();
}
return _builder;
}
Offset<NodeTree> FlatBuffersSerialize::createNodeTreeForSimulator(const tinyxml2::XMLElement *objectData,
std::string classType)
2014-11-21 15:15:38 +08:00
{
2014-12-10 12:36:14 +08:00
std::string classname = classType.substr(0, classType.find("ObjectData"));
// CCLOG("classname = %s", classname.c_str());
std::string name = "";
Offset<Options> options;
std::vector<Offset<NodeTree>> children;
if (classname == "ProjectNode")
{
auto projectNodeOptions = createProjectNodeOptionsForSimulator(objectData);
options = CreateOptions(*_builder, *(Offset<Table>*)(&projectNodeOptions));
2014-12-10 12:36:14 +08:00
}
else if (classname == "SimpleAudio")
{
auto reader = ComAudioReader::getInstance();
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
2014-12-10 12:36:14 +08:00
}
else
{
std::string readername = getGUIClassName(classname);
readername.append("Reader");
NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
2015-03-12 15:47:15 +08:00
if (reader != nullptr)
{
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
2015-03-12 15:47:15 +08:00
}
2014-12-10 12:36:14 +08:00
}
// children
bool containChildrenElement = false;
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
// CCLOG("child name = %s", child->Name());
if (strcmp("Children", child->Name()) == 0)
{
containChildrenElement = true;
break;
}
child = child->NextSiblingElement();
}
if (containChildrenElement)
{
child = child->FirstChildElement();
// CCLOG("element name = %s", child->Name());
while (child)
{
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
bool bHasType = false;
while (attribute)
{
std::string attriname = attribute->Name();
std::string value = attribute->Value();
if (attriname == "ctype")
{
children.push_back(createNodeTreeForSimulator(child, value));
bHasType = true;
break;
}
attribute = attribute->Next();
}
if(!bHasType)
{
children.push_back(createNodeTreeForSimulator(child, "NodeObjectData"));
}
child = child->NextSiblingElement();
}
}
//
2014-11-21 15:15:38 +08:00
2014-12-10 12:36:14 +08:00
std::string customClassName = "";
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string attriname = attribute->Name();
std::string value = attribute->Value();
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
}
attribute = attribute->Next();
}
2014-12-10 12:36:14 +08:00
return CreateNodeTree(*_builder,
_builder->CreateString(classname),
_builder->CreateVector(children),
options,
_builder->CreateString(customClassName));
}
Offset<ProjectNodeOptions> FlatBuffersSerialize::createProjectNodeOptionsForSimulator(const tinyxml2::XMLElement *objectData)
{
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, _builder);
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
2014-12-10 12:36:14 +08:00
std::string filename = "";
float innerspeed = 1.0f;
const tinyxml2::XMLAttribute* objattri = objectData->FirstAttribute();
// inneraction speed
while (objattri)
{
std::string name = objattri->Name();
std::string value = objattri->Value();
if (name == "InnerActionSpeed")
{
innerspeed = atof(objattri->Value());
break;
}
objattri = objattri->Next();
}
2014-12-10 12:36:14 +08:00
// FileData
2014-11-21 15:15:38 +08:00
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
2014-12-10 12:36:14 +08:00
std::string name = child->Name();
if (name == "FileData")
2014-11-21 15:15:38 +08:00
{
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
2014-11-21 15:15:38 +08:00
while (attribute)
2014-11-21 15:15:38 +08:00
{
name = attribute->Name();
std::string value = attribute->Value();
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
}
2014-12-10 12:36:14 +08:00
attribute = attribute->Next();
2014-11-21 15:15:38 +08:00
}
}
child = child->NextSiblingElement();
}
2014-12-10 12:36:14 +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. */
std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFileForLanguageData(const std::string& xmlFilePath,
const std::string& flatBuffersFilePath,
const std::string& languageName)
{
//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);
tinyxml2::XMLDocument* document = new (std::nothrow) tinyxml2::XMLDocument();
document->Parse(content.c_str());
const tinyxml2::XMLElement* element = document->RootElement();
element = element->FirstChildElement();
//Create FlatBuffers file using the language data in XML file.
_builder = new (std::nothrow) FlatBufferBuilder();
std::vector<Offset<LanguageItem>> langItemList;
while (element)
{
if (strcmp("language", element->Name()) != 0)
{
element = element->NextSiblingElement();
continue;
}
//Read all of the Key-Values in the XML file.
std::string key = "";
std::string text = "";
bool hasKeyReaded = false;
bool hasTextReaded = false;
const tinyxml2::XMLElement* childElement = element->FirstChildElement();
while (childElement)
{
//Record language key.
if (strcmp("key", childElement->Name()) == 0)
{
key = childElement->GetText();
hasKeyReaded = true;
}
//Record corresponding text.
else if (strcmp(languageName.c_str(), childElement->Name()) == 0)
{
const char* langText = childElement->GetText();
if (langText && langText[0] != '\0')
text = langText;
else
text = key;
hasTextReaded = true;
}
if (hasKeyReaded && hasTextReaded)
break;
childElement = childElement->NextSiblingElement();
}
Offset<flatbuffers::LanguageItem> langItem = CreateLanguageItem(*_builder, _builder->CreateString(key), _builder->CreateString(text));
langItemList.push_back(langItem);
element = element->NextSiblingElement();
}
auto langSet = CreateLanguageSet(*_builder, _builder->CreateVector(langItemList));
_builder->Finish(langSet);
bool isSuccess = flatbuffers::SaveFile(flatBuffersFilePath.c_str(),
reinterpret_cast<const char *>(_builder->GetBufferPointer()),
_builder->GetSize(),
true);
if (isSuccess)
return "";
else
return "Failed to save language .csb file.";
}
2014-11-21 15:15:38 +08:00
}
/**/