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"
|
|
|
|
|
2014-09-24 11:43:21 +08:00
|
|
|
#define CONFIG_FILE "config.json"
|
|
|
|
#define CONSOLE_PORT 6010
|
|
|
|
#define UPLOAD_PORT 6020
|
|
|
|
#define WIN_WIDTH 960
|
|
|
|
#define WIN_HEIGHT 640
|
|
|
|
|
2014-05-05 21:04:04 +08:00
|
|
|
// ConfigParser
|
|
|
|
ConfigParser *ConfigParser::s_sharedInstance = NULL;
|
|
|
|
ConfigParser *ConfigParser::getInstance(void)
|
|
|
|
{
|
|
|
|
if (!s_sharedInstance)
|
|
|
|
{
|
|
|
|
s_sharedInstance = new ConfigParser();
|
2014-09-16 16:19:05 +08:00
|
|
|
s_sharedInstance->readConfig();
|
2014-05-05 21:04:04 +08:00
|
|
|
}
|
|
|
|
return s_sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigParser::readConfig()
|
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
string fullPathFile = FileUtils::getInstance()->fullPathForFilename(CONFIG_FILE);
|
|
|
|
string fileContent = FileUtils::getInstance()->getStringFromFile(fullPathFile);
|
2014-05-13 17:44:35 +08:00
|
|
|
|
2014-09-24 11:43:21 +08:00
|
|
|
if(fileContent.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_docRootjson.Parse<0>(fileContent.c_str()).HasParseError()) {
|
|
|
|
cocos2d::log("read json file %s failed because of %s", fullPathFile.c_str(), _docRootjson.GetParseError());
|
|
|
|
return;
|
2014-05-13 17:44:35 +08:00
|
|
|
}
|
2014-09-24 11:43:21 +08:00
|
|
|
|
|
|
|
if (_docRootjson.HasMember("init_cfg"))
|
2014-05-05 21:04:04 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
if(_docRootjson["init_cfg"].IsObject())
|
2014-05-05 21:04:04 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
const rapidjson::Value& objectInitView = _docRootjson["init_cfg"];
|
|
|
|
if (objectInitView.HasMember("width") && objectInitView.HasMember("height"))
|
2014-05-05 21:04:04 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
_initViewSize.width = objectInitView["width"].GetUint();
|
|
|
|
_initViewSize.height = objectInitView["height"].GetUint();
|
|
|
|
if (_initViewSize.height>_initViewSize.width)
|
2014-05-14 14:40:27 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
float tmpvalue = _initViewSize.height;
|
|
|
|
_initViewSize.height = _initViewSize.width;
|
|
|
|
_initViewSize.width = tmpvalue;
|
2014-07-16 20:19:19 +08:00
|
|
|
}
|
2014-09-24 11:43:21 +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();
|
|
|
|
}
|
|
|
|
if (objectInitView.HasMember("consolePort"))
|
|
|
|
{
|
|
|
|
_consolePort = objectInitView["consolePort"].GetUint();
|
|
|
|
if(_consolePort <= 0)
|
|
|
|
_consolePort = CONSOLE_PORT;
|
|
|
|
}
|
|
|
|
if (objectInitView.HasMember("uploadPort"))
|
|
|
|
{
|
|
|
|
_uploadPort = objectInitView["uploadPort"].GetUint();
|
|
|
|
if(_uploadPort <= 0)
|
|
|
|
_uploadPort = UPLOAD_PORT;
|
|
|
|
}
|
|
|
|
if (objectInitView.HasMember("isWindowTop") && objectInitView["isWindowTop"].IsBool())
|
|
|
|
{
|
|
|
|
_isWindowTop= objectInitView["isWindowTop"].GetBool();
|
2014-05-09 14:06:20 +08:00
|
|
|
}
|
2014-05-05 21:04:04 +08:00
|
|
|
}
|
2014-09-24 11:43:21 +08:00
|
|
|
}
|
|
|
|
if (_docRootjson.HasMember("simulator_screen_size"))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& ArrayScreenSize = _docRootjson["simulator_screen_size"];
|
|
|
|
if (ArrayScreenSize.IsArray())
|
2014-05-05 21:04:04 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
for (int i = 0; i<ArrayScreenSize.Size(); i++)
|
2014-05-05 21:04:04 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
const rapidjson::Value& objectScreenSize = ArrayScreenSize[i];
|
|
|
|
if (objectScreenSize.HasMember("title") && objectScreenSize.HasMember("width") && objectScreenSize.HasMember("height"))
|
2014-05-05 21:04:04 +08:00
|
|
|
{
|
2014-09-24 11:43:21 +08:00
|
|
|
_screenSizeArray.push_back(SimulatorScreenSize(objectScreenSize["title"].GetString(), objectScreenSize["width"].GetUint(), objectScreenSize["height"].GetUint()));
|
2014-05-05 21:04:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:43:21 +08:00
|
|
|
ConfigParser::ConfigParser(void) :
|
|
|
|
_isLandscape(true),
|
|
|
|
_isWindowTop(false),
|
|
|
|
_consolePort(CONSOLE_PORT),
|
|
|
|
_uploadPort(UPLOAD_PORT),
|
|
|
|
_viewName("HelloLua"),
|
|
|
|
_entryfile("src/main.lua"),
|
|
|
|
_initViewSize(WIN_WIDTH, WIN_HEIGHT)
|
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);
|
|
|
|
}
|