The depth parsing cocostudio is how to handle the JSON file that the Action editor leads (1) (Turn)

Source: Internet
Author: User

Turn from: blog.csdn.net/eduwc/article/details/9858723


Before you start parsing, let's talk about how to read this article.

1. Due to some classes of jumps, so in the presence (jump xx) place, you can use search search to the corresponding destination to see if not in the series of depth parsing cocostudio is how to handle the action editor derived from the JSON file (1), Please go to the depth resolution Cocostudio to find out how to handle the JSON file (2) that is derived from the action editor

2. This article is the first code, and then to do a specific analysis (some of them are not very important to write directly behind the code)

Let's take a look at Cocostudio how to parse the animation

The first is to load the animation file
Cs::armaturedatamanager::sharedarmaturedatamanager ()->addarmaturefileinfo ("NewProject", "", "NewProject0.png" , "Newproject0.plist", "Newproject.json");

Armaturedatamanager This is the housekeeper who manages the animated data.
Sharedarmaturedatamanager This is a single example that is used to create a Armaturedatamanager
Let's take a closer look at how Addarmaturefileinfo can load JSON's animated information in and parse

First on the source code, and then to gradually analyze
void Armaturedatamanager::addarmaturefileinfo (const char *armaturename, const char *useexistfileinfo, const char * ImagePath, const char *plistpath, const char *configfilepath)
{
Armaturefileinfo *fileinfo = (armaturefileinfo*) m_parmaturefileinfodic->objectforkey (armatureName);

if (!fileinfo)
{
FileInfo = Armaturefileinfo::create ();
Fileinfo->armaturename = Armaturename;
Fileinfo->configfilepath = ConfigFilePath;
Fileinfo->useexistfileinfo = Useexistfileinfo;

if (Fileinfo->useexistfileinfo.compare ("")!= 0)
{
FileInfo = (armaturefileinfo*) m_parmaturefileinfodic->objectforkey (fileinfo->useexistfileinfo);
}


M_parmaturefileinfodic->setobject (FileInfo, fileinfo->armaturename);
}

Datareaderhelper::adddatafromfile (ConfigFilePath);

for (Std::vector<imageinfo>::iterator it = Fileinfo->imageinfovector.begin (); it!= fileinfo-> Imageinfovector.end (); it++)
{
if (It->imagepath.compare (imagepath) = = 0)
{
Return
}
}

Imageinfo Imageinfo;

Imageinfo.imagepath = ImagePath;
Imageinfo.plistpath = Plistpath;

Addspriteframefromfile (Plistpath, ImagePath);

Fileinfo->imageinfovector.push_back (Imageinfo);


}

Analysis

1.//The author considers very thoughtful, will first determine if you have loaded the JSON file you need, if loaded directly according to the name to fetch
Armaturefileinfo *fileinfo = (armaturefileinfo*) m_parmaturefileinfodic->objectforkey (armatureName);

2.

This is the point. This code starts parsing JSON and formats your data for later use (Hop 1)
Datareaderhelper::adddatafromfile (ConfigFilePath);

--------------------------------------------------------------Gorgeous split Line----------------------------------------------------- -------------------

(jump 1) Let's see the Adddatafromfile.
Datareaderhelper::adddatafromfile (ConfigFilePath);

Let's open the Adddatafromfile and see.
void Datareaderhelper::adddatafromfile (const char *filepath)
{
/*
* Check If file is already added to Armaturedatamanager, if then return.
*/
for (unsigned int i = 0; I<m_arrconfigfilelist.size (); i++)
{
if (M_arrconfigfilelist[i].compare (filePath) = = 0)
{
Return
}
}
M_arrconfigfilelist.push_back (FilePath);


std::string filepathstr = FilePath;
size_t startpos = filepathstr.find_last_of (".");
std::string str = &filePathStr[startPos];

if (Str.compare (". xml") = = 0)
{

#if Cs_tool_platform
if (Game::sharedgame ()->isusepackage ())
{
Datareaderhelper::adddatafromxmlpak (Filepathstr.c_str ());
}
Else
{
Datareaderhelper::adddatafromxml (Filepathstr.c_str ());
}
#else
Datareaderhelper::adddatafromxml (Filepathstr.c_str ());
#endif
}
else if (Str.compare (". JSON") = = 0 | | str.compare (". Exportjson ") = = 0)
{
Datareaderhelper::adddatafromjson (Filepathstr.c_str ());
}
}

It starts with a detection program that detects whether to add
for (unsigned int i = 0; I<m_arrconfigfilelist.size (); i++)
{
if (M_arrconfigfilelist[i].compare (filePath) = = 0)
{
Return
}
}
Analytical

1. See whether XML files or JSON files do different processing according to different files (XML files and cocostudio that support dragonbones export

Action Editor export JSON file)
std::string filepathstr = FilePath;
size_t startpos = filepathstr.find_last_of (".");
std::string str = &filePathStr[startPos];

2. Depending on the results above, different processing is done according to XML and JSON respectively, because the action editor uses JSON format, so we start with the main
Look at the JSON format parsing (to use jsoncpp. Not very understanding of students can be viewed online)

if (Str.compare (". xml") = = 0)
{

#if Cs_tool_platform
if (Game::sharedgame ()->isusepackage ())
{
Datareaderhelper::adddatafromxmlpak (Filepathstr.c_str ());
}
Else
{
Datareaderhelper::adddatafromxml (Filepathstr.c_str ());
}
#else
Datareaderhelper::adddatafromxml (Filepathstr.c_str ());
#endif
}
else if (Str.compare (". JSON") = = 0 | | str.compare (". Exportjson ") = = 0)
{

Let's focus on how Datareaderhelper::adddatafromjson (Filepathstr.c_str ()) parses JSON (Hop 2)
Datareaderhelper::adddatafromjson (Filepathstr.c_str ());
}

--------------------------------------------------------------Gorgeous split Line----------------------------------------------------- -------------------

(Jump 2)

This side does not need too much parsing, the main is to read the file

void Datareaderhelper::adddatafromjson (const char *filepath)
{
unsigned long size;
std::string fullpath = Ccfileutils::sharedfileutils ()->fullpathforfilename (FilePath);
const char *pfilecontent = (char*) ccfileutils::sharedfileutils ()->getfiledata (Fullpath.c_str (), "R", &size);

Add the processed data to the cache

Adddatafromjsoncache (pfilecontent);
}

Let's watch Adddatafromjsoncache.

void Datareaderhelper::adddatafromjsoncache (const char *filecontent)
{
Csjsondictionary JSON;
Json.initwithdescription (filecontent)//Here is where to parse JSON (Hop 3)

Decode armatures
int length = Json.getarrayitemcount (Armature_data);

(Jump 4)
for (int i = 0; i<length; i++)
{
Csjsondictionary *armaturedic = Json.getsubitemfromarray (Armature_data, i);
Armaturedata *armaturedata = decodearmature (*armaturedic);
Armaturedatamanager::sharedarmaturedatamanager ()->addarmaturedata (Armaturedata->name.c_str (), Armaturedata);
}

Decode Animations
Length = Json.getarrayitemcount (Animation_data);
for (int i = 0; i<length; i++)
{
Csjsondictionary *animationdic = Json.getsubitemfromarray (Animation_data, i);
Animationdata *animationdata = decodeanimation (*animationdic);
Armaturedatamanager::sharedarmaturedatamanager ()->addanimationdata (Animationdata->name.c_str (), Animationdata);
}

Decode Textures
Length = Json.getarrayitemcount (Texture_data);
for (int i = 0; i<length; i++)
{
Csjsondictionary *texturedic = Json.getsubitemfromarray (Texture_data, i);
Texturedata *texturedata = decodetexture (*texturedic);
Armaturedatamanager::sharedarmaturedatamanager ()->addtexturedata (Texturedata->name.c_str (), textureData);
}


}

(Jump 3)

void csjsondictionary::initwithdescription (const char *pszdescription)
{
Json::reader CReader;
M_cvalue.clear ();
if (pszdescription && *pszdescription)
{
std::string strvalue = pszdescription;

Start calling Jsoncpp to parse JSON
Creader.parse (strvalue, M_cvalue, false);
}
}

Parse Creader.parse (strvalue, M_cvalue, false);

strvalue this is the string to parse
M_cvalue the Json::value type of variable used to hold the result
False This parameter is used to decide whether to add a note to the
According to the official explanation,
* \param collectcomments \c true to collect comment and allow writing them back during
* serialization, \c false to discard comments.
* This parameter is ignored if Features::allowcomments_
* is \c false.
Below the translation (English is not good people do not take offense AH)
If you set it to ture, you can add the annotation to it when you serialize it.
If set to False, the comment is not added
This parameter is ignored if the Features::allowcomments_ is set to False

We then looked at the parse and nested another layer ... The water is deep.
bool
Reader::p Arse (const std::string &document,
Value &root,
BOOL collectcomments)
{
Document_ = document;
const char *begin = DOCUMENT_.C_STR ();//Get JSON content
const char *end = begin + Document_.length ()//or Last character "" "
Return parse (begin, end, Root, collectcomments);
}

Then look back parse (begin, end, Root, collectcomments);

bool
Reader::p Arse (const char *begindoc, const char *enddoc,
Value &root,
BOOL collectcomments)
{
if (!FEATURES_.ALLOWCOMMENTS_)
{
Collectcomments = false;
}

Begin_ = Begindoc;
End_ = EndDoc;
Collectcomments_ = collectcomments;
Current_ = Begin_;
Lastvalueend_ = 0;
Lastvalue_ = 0;
Commentsbefore_ = "";
Errors_.clear ();
while (!nodes_.empty ())
Nodes_.pop ();
Nodes_.push (&root);

BOOL successful = ReadValue ();
Token Token;
Skipcommenttokens (token);
if (Collectcomments_ &&!commentsbefore_.empty ())
Root.setcomment (Commentsbefore_, Commentafter);
if (features_.strictroot_)
{
if (!root.isarray () &&!root.isobject ())
{
Set error location to start of Doc, ideally should is a-token found in doc
Token.type_ = Tokenerror;
Token.start_ = Begindoc;
Token.end_ = EndDoc;
Adderror ("A valid JSON document must be either" ","
token);
return false;
}
}
return successful;
}

We'll continue in the next section.

--------------------------------------------------------------Gorgeous split Line----------------------------------------------------- -------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.