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

208 lines
6.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"
2014-11-28 15:40:12 +08:00
#include "FileServer.h"
2014-05-05 21:04:04 +08:00
// ConfigParser
2014-11-26 14:58:23 +08:00
ConfigParser *ConfigParser::s_sharedConfigParserInstance = NULL;
2014-05-05 21:04:04 +08:00
ConfigParser *ConfigParser::getInstance(void)
{
2014-11-26 14:58:23 +08:00
if (!s_sharedConfigParserInstance)
2014-05-05 21:04:04 +08:00
{
2014-11-26 14:58:23 +08:00
s_sharedConfigParserInstance = new ConfigParser();
s_sharedConfigParserInstance->readConfig();
2014-05-05 21:04:04 +08:00
}
2014-11-26 14:58:23 +08:00
return s_sharedConfigParserInstance;
}
void ConfigParser::purge()
{
CC_SAFE_DELETE(s_sharedConfigParserInstance);
2014-05-05 21:04:04 +08:00
}
2014-12-27 02:21:55 +08:00
void ConfigParser::readConfig(const string &filepath)
2014-05-05 21:04:04 +08:00
{
2014-12-27 02:21:55 +08:00
string fullPathFile = filepath;
2014-11-28 15:40:12 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// add writable path to search path temporarily for reading config file
vector<std::string> searchPathArray = FileUtils::getInstance()->getSearchPaths();
searchPathArray.insert(searchPathArray.begin(), FileServer::getShareInstance()->getWritePath());
FileUtils::getInstance()->setSearchPaths(searchPathArray);
#endif
// read config file
2014-12-27 02:21:55 +08:00
if (fullPathFile.empty())
{
fullPathFile = FileUtils::getInstance()->fullPathForFilename(CONFIG_FILE);
2014-12-27 02:21:55 +08:00
}
string fileContent = FileUtils::getInstance()->getStringFromFile(fullPathFile);
2014-11-28 15:40:12 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// revert search path
searchPathArray.erase(searchPathArray.end() - 1);
FileUtils::getInstance()->setSearchPaths(searchPathArray);
#endif
2014-05-13 17:44:35 +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
}
if (_docRootjson.HasMember("init_cfg"))
2014-05-05 21:04:04 +08:00
{
if(_docRootjson["init_cfg"].IsObject())
2014-05-05 21:04:04 +08:00
{
const rapidjson::Value& objectInitView = _docRootjson["init_cfg"];
if (objectInitView.HasMember("width") && objectInitView.HasMember("height"))
2014-05-05 21:04:04 +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
{
float tmpvalue = _initViewSize.height;
_initViewSize.height = _initViewSize.width;
_initViewSize.width = tmpvalue;
2014-07-16 20:19:19 +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())
{
2014-12-27 02:21:55 +08:00
setEntryFile(objectInitView["entry"].GetString());
}
if (objectInitView.HasMember("consolePort"))
{
_consolePort = objectInitView["consolePort"].GetUint();
if(_consolePort <= 0)
2014-12-27 02:21:55 +08:00
_consolePort = kProjectConfigConsolePort;
}
if (objectInitView.HasMember("uploadPort"))
{
_uploadPort = objectInitView["uploadPort"].GetUint();
if(_uploadPort <= 0)
2014-12-27 02:21:55 +08:00
_uploadPort = kProjectConfigUploadPort;
}
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
}
}
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
{
for (int i = 0; i < ArrayScreenSize.Size(); i++)
2014-05-05 21:04:04 +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
{
_screenSizeArray.push_back(SimulatorScreenSize(objectScreenSize["title"].GetString(), objectScreenSize["width"].GetUint(), objectScreenSize["height"].GetUint()));
2014-05-05 21:04:04 +08:00
}
}
}
}
}
ConfigParser::ConfigParser(void) :
_isLandscape(true),
_isWindowTop(false),
2014-12-27 02:21:55 +08:00
_consolePort(kProjectConfigConsolePort),
_uploadPort(kProjectConfigUploadPort),
_viewName("simulator"),
_entryfile("src/main.lua"),
2014-12-27 02:21:55 +08:00
_initViewSize(ProjectConfig::DEFAULT_HEIGHT, ProjectConfig::DEFAULT_WIDTH),
_bindAddress("")
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-12-27 02:21:55 +08:00
void ConfigParser::setConsolePort(int port)
{
_consolePort = port;
}
void ConfigParser::setUploadPort(int port)
{
_uploadPort = port;
}
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);
}
2014-12-27 02:21:55 +08:00
void ConfigParser::setEntryFile(const std::string &file)
{
_entryfile = file;
}
void ConfigParser::setInitViewSize(const cocos2d::Size &size)
{
_initViewSize = size;
}
void ConfigParser::setBindAddress(const std::string &address)
{
_bindAddress = address;
}
const std::string &ConfigParser::getBindAddress()
{
return _bindAddress;
}