axmol/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.cpp

163 lines
5.0 KiB
C++
Raw Normal View History

2014-05-05 21:04:04 +08:00
#include "json/document.h"
#include "json/filestream.h"
#include "json/stringbuffer.h"
#include "json/writer.h"
#include "ConfigParser.h"
// ConfigParser
ConfigParser *ConfigParser::s_sharedInstance = NULL;
ConfigParser *ConfigParser::getInstance(void)
{
if (!s_sharedInstance)
{
s_sharedInstance = new ConfigParser();
}
return s_sharedInstance;
}
2014-05-07 20:50:29 +08:00
bool ConfigParser::isInit()
{
return _isInit;
}
2014-05-05 21:04:04 +08:00
void ConfigParser::readConfig()
{
2014-05-07 20:50:29 +08:00
_isInit = true;
2014-07-24 10:30:39 +08:00
_isWindowTop = false;
2014-07-16 20:19:19 +08:00
_consolePort = 6010;
2014-08-29 15:05:55 +08:00
_uploadPort = 6020;
2014-05-14 14:40:27 +08:00
string filecfg = "config.json";
string fileContent;
2014-05-13 17:44:35 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID && !defined(NDEBUG)) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS && defined(COCOS2D_DEBUG))
string fullPathFile = FileUtils::getInstance()->getWritablePath();
2014-05-14 14:58:36 +08:00
fullPathFile.append("debugruntime/");
2014-05-13 17:44:35 +08:00
fullPathFile.append(filecfg.c_str());
2014-05-14 14:40:27 +08:00
fileContent=FileUtils::getInstance()->getStringFromFile(fullPathFile.c_str());
2014-05-13 17:44:35 +08:00
#endif
2014-05-14 14:40:27 +08:00
if (fileContent.empty()) {
filecfg=FileUtils::getInstance()->fullPathForFilename(filecfg.c_str());
fileContent=FileUtils::getInstance()->getStringFromFile(filecfg.c_str());
2014-05-13 17:44:35 +08:00
}
2014-05-14 14:40:27 +08:00
if(!fileContent.empty())
2014-05-05 21:04:04 +08:00
{
2014-05-14 14:40:27 +08:00
_docRootjson.Parse<0>(fileContent.c_str());
if (_docRootjson.HasMember("init_cfg"))
2014-05-05 21:04:04 +08:00
{
2014-05-14 14:40:27 +08:00
if(_docRootjson["init_cfg"].IsObject())
2014-05-05 21:04:04 +08:00
{
2014-05-14 14:40:27 +08:00
const rapidjson::Value& objectInitView = _docRootjson["init_cfg"];
if (objectInitView.HasMember("width") && objectInitView.HasMember("height"))
{
_initViewSize.width = objectInitView["width"].GetUint();
_initViewSize.height = objectInitView["height"].GetUint();
2014-05-22 11:51:56 +08:00
if (_initViewSize.height>_initViewSize.width)
{
float tmpvalue =_initViewSize.height;
_initViewSize.height = _initViewSize.width;
_initViewSize.width = tmpvalue;
}
2014-05-14 14:40:27 +08:00
}
if (objectInitView.HasMember("name") && objectInitView["name"].IsString())
{
_viewName = objectInitView["name"].GetString();
}
if (objectInitView.HasMember("isLandscape") && objectInitView["isLandscape"].IsBool()) {
_isLandscape = objectInitView["isLandscape"].GetBool();
}
if (objectInitView.HasMember("entry") && objectInitView["entry"].IsString()) {
_entryfile = objectInitView["entry"].GetString();
}
2014-07-16 20:19:19 +08:00
if (objectInitView.HasMember("consolePort")){
_consolePort = objectInitView["consolePort"].GetUint();
2014-08-29 15:05:55 +08:00
if(_consolePort<=0)
2014-07-24 10:30:39 +08:00
_consolePort = 6010;
}
2014-08-29 15:05:55 +08:00
if (objectInitView.HasMember("uploadPort")){
_uploadPort = objectInitView["uploadPort"].GetUint();
if(_uploadPort<=0)
_uploadPort = 6020;
}
2014-07-24 10:30:39 +08:00
if (objectInitView.HasMember("isWindowTop") && objectInitView["isWindowTop"].IsBool()){
_isWindowTop= objectInitView["isWindowTop"].GetBool();
2014-07-16 20:19:19 +08:00
}
2014-05-09 14:06:20 +08:00
}
2014-05-05 21:04:04 +08:00
}
2014-05-07 20:50:29 +08:00
if (_docRootjson.HasMember("simulator_screen_size"))
2014-05-05 21:04:04 +08:00
{
2014-05-07 20:50:29 +08:00
const rapidjson::Value& ArrayScreenSize = _docRootjson["simulator_screen_size"];
2014-05-05 21:04:04 +08:00
if (ArrayScreenSize.IsArray())
{
for (int i=0; i<ArrayScreenSize.Size(); i++)
{
const rapidjson::Value& objectScreenSize = ArrayScreenSize[i];
if (objectScreenSize.HasMember("title") && objectScreenSize.HasMember("width") && objectScreenSize.HasMember("height"))
{
_screenSizeArray.push_back(SimulatorScreenSize(objectScreenSize["title"].GetString(), objectScreenSize["width"].GetUint(), objectScreenSize["height"].GetUint()));
}
}
}
}
2014-05-07 20:50:29 +08:00
2014-05-05 21:04:04 +08:00
}
}
2014-05-07 20:50:29 +08:00
ConfigParser::ConfigParser(void):_isInit(false),_isLandscape(true)
2014-05-05 21:04:04 +08:00
{
2014-05-09 14:06:20 +08:00
_initViewSize.setSize(960,640);
_viewName = "HelloLua";
_entryfile = "src/main.lua";
2014-05-05 21:04:04 +08:00
}
2014-05-07 20:50:29 +08:00
rapidjson::Document& ConfigParser::getConfigJsonRoot()
{
return _docRootjson;
}
2014-05-09 14:06:20 +08:00
2014-05-05 21:04:04 +08:00
string ConfigParser::getInitViewName()
{
return _viewName;
}
2014-05-09 14:06:20 +08:00
string ConfigParser::getEntryFile()
{
return _entryfile;
}
2014-05-05 21:04:04 +08:00
Size ConfigParser::getInitViewSize()
{
return _initViewSize;
}
2014-05-07 20:50:29 +08:00
bool ConfigParser::isLanscape()
{
return _isLandscape;
}
2014-07-24 10:30:39 +08:00
bool ConfigParser::isWindowTop()
{
return _isWindowTop;
}
2014-07-16 20:19:19 +08:00
int ConfigParser::getConsolePort()
{
return _consolePort;
}
2014-08-29 15:05:55 +08:00
int ConfigParser::getUploadPort()
{
return _uploadPort;
}
2014-05-05 21:04:04 +08:00
int ConfigParser::getScreenSizeCount(void)
{
return (int)_screenSizeArray.size();
}
const SimulatorScreenSize ConfigParser::getScreenSize(int index)
{
return _screenSizeArray.at(index);
}