ConfigParser.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "ConfigParser.h"
  2. #include "json/filereadstream.h"
  3. #include "json/stringbuffer.h"
  4. #include "json/writer.h"
  5. #include "FileServer.h"
  6. // ConfigParser
  7. ConfigParser *ConfigParser::s_sharedConfigParserInstance = NULL;
  8. ConfigParser *ConfigParser::getInstance(void)
  9. {
  10. if (!s_sharedConfigParserInstance)
  11. {
  12. s_sharedConfigParserInstance = new ConfigParser();
  13. s_sharedConfigParserInstance->readConfig();
  14. }
  15. return s_sharedConfigParserInstance;
  16. }
  17. void ConfigParser::purge()
  18. {
  19. CC_SAFE_DELETE(s_sharedConfigParserInstance);
  20. }
  21. void ConfigParser::readConfig(const string &filepath)
  22. {
  23. string fullPathFile = filepath;
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  25. // add writable path to search path temporarily for reading config file
  26. vector<std::string> searchPathArray = FileUtils::getInstance()->getSearchPaths();
  27. searchPathArray.insert(searchPathArray.begin(), FileServer::getShareInstance()->getWritePath());
  28. FileUtils::getInstance()->setSearchPaths(searchPathArray);
  29. #endif
  30. // read config file
  31. if (fullPathFile.empty())
  32. {
  33. fullPathFile = FileUtils::getInstance()->fullPathForFilename(CONFIG_FILE);
  34. }
  35. string fileContent = FileUtils::getInstance()->getStringFromFile(fullPathFile);
  36. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  37. // revert search path
  38. searchPathArray.erase(searchPathArray.begin());
  39. FileUtils::getInstance()->setSearchPaths(searchPathArray);
  40. #endif
  41. if(fileContent.empty())
  42. return;
  43. if (_docRootjson.Parse<0>(fileContent.c_str()).HasParseError()) {
  44. cocos2d::log("read json file %s failed because of %d", fullPathFile.c_str(), _docRootjson.GetParseError());
  45. return;
  46. }
  47. if (_docRootjson.HasMember("init_cfg"))
  48. {
  49. if(_docRootjson["init_cfg"].IsObject())
  50. {
  51. const rapidjson::Value& objectInitView = _docRootjson["init_cfg"];
  52. if (objectInitView.HasMember("width") && objectInitView.HasMember("height"))
  53. {
  54. _initViewSize.width = objectInitView["width"].GetUint();
  55. _initViewSize.height = objectInitView["height"].GetUint();
  56. if (_initViewSize.height>_initViewSize.width)
  57. {
  58. float tmpvalue = _initViewSize.height;
  59. _initViewSize.height = _initViewSize.width;
  60. _initViewSize.width = tmpvalue;
  61. }
  62. }
  63. if (objectInitView.HasMember("name") && objectInitView["name"].IsString())
  64. {
  65. _viewName = objectInitView["name"].GetString();
  66. }
  67. if (objectInitView.HasMember("isLandscape") && objectInitView["isLandscape"].IsBool())
  68. {
  69. _isLandscape = objectInitView["isLandscape"].GetBool();
  70. }
  71. if (objectInitView.HasMember("entry") && objectInitView["entry"].IsString())
  72. {
  73. setEntryFile(objectInitView["entry"].GetString());
  74. }
  75. if (objectInitView.HasMember("consolePort"))
  76. {
  77. setConsolePort(objectInitView["consolePort"].GetUint());
  78. }
  79. if (objectInitView.HasMember("debugPort"))
  80. {
  81. setDebugPort(objectInitView["debugPort"].GetUint());
  82. }
  83. if (objectInitView.HasMember("uploadPort"))
  84. {
  85. setUploadPort(objectInitView["uploadPort"].GetUint());
  86. }
  87. if (objectInitView.HasMember("isWindowTop") && objectInitView["isWindowTop"].IsBool())
  88. {
  89. _isWindowTop= objectInitView["isWindowTop"].GetBool();
  90. }
  91. }
  92. }
  93. if (_docRootjson.HasMember("simulator_screen_size"))
  94. {
  95. const rapidjson::Value& ArrayScreenSize = _docRootjson["simulator_screen_size"];
  96. if (ArrayScreenSize.IsArray())
  97. {
  98. for (int i = 0; i < ArrayScreenSize.Size(); i++)
  99. {
  100. const rapidjson::Value& objectScreenSize = ArrayScreenSize[i];
  101. if (objectScreenSize.HasMember("title") && objectScreenSize.HasMember("width") && objectScreenSize.HasMember("height"))
  102. {
  103. _screenSizeArray.push_back(SimulatorScreenSize(objectScreenSize["title"].GetString(), objectScreenSize["width"].GetUint(), objectScreenSize["height"].GetUint()));
  104. }
  105. }
  106. }
  107. }
  108. }
  109. ConfigParser::ConfigParser(void) :
  110. _isLandscape(true),
  111. _isWindowTop(false),
  112. _consolePort(kProjectConfigConsolePort),
  113. _uploadPort(kProjectConfigUploadPort),
  114. _debugPort(kProjectConfigDebugger),
  115. _viewName("simulator"),
  116. _entryfile(""),
  117. _initViewSize(ProjectConfig::DEFAULT_HEIGHT, ProjectConfig::DEFAULT_WIDTH),
  118. _bindAddress("")
  119. {
  120. }
  121. rapidjson::Document& ConfigParser::getConfigJsonRoot()
  122. {
  123. return _docRootjson;
  124. }
  125. string ConfigParser::getInitViewName()
  126. {
  127. return _viewName;
  128. }
  129. string ConfigParser::getEntryFile()
  130. {
  131. return _entryfile;
  132. }
  133. Size ConfigParser::getInitViewSize()
  134. {
  135. return _initViewSize;
  136. }
  137. bool ConfigParser::isLanscape()
  138. {
  139. return _isLandscape;
  140. }
  141. bool ConfigParser::isWindowTop()
  142. {
  143. return _isWindowTop;
  144. }
  145. void ConfigParser::setConsolePort(int port)
  146. {
  147. if (port > 0)
  148. {
  149. _consolePort = port;
  150. }
  151. }
  152. void ConfigParser::setUploadPort(int port)
  153. {
  154. if (port > 0)
  155. {
  156. _uploadPort = port;
  157. }
  158. }
  159. void ConfigParser::setDebugPort(int port)
  160. {
  161. if (port > 0)
  162. {
  163. _debugPort = port;
  164. }
  165. }
  166. int ConfigParser::getConsolePort()
  167. {
  168. return _consolePort;
  169. }
  170. int ConfigParser::getUploadPort()
  171. {
  172. return _uploadPort;
  173. }
  174. int ConfigParser::getDebugPort()
  175. {
  176. return _debugPort;
  177. }
  178. int ConfigParser::getScreenSizeCount(void)
  179. {
  180. return (int)_screenSizeArray.size();
  181. }
  182. const SimulatorScreenSize ConfigParser::getScreenSize(int index)
  183. {
  184. return _screenSizeArray.at(index);
  185. }
  186. void ConfigParser::setEntryFile(const std::string &file)
  187. {
  188. _entryfile = file;
  189. }
  190. void ConfigParser::setInitViewSize(const cocos2d::Size &size)
  191. {
  192. _initViewSize = size;
  193. }
  194. void ConfigParser::setBindAddress(const std::string &address)
  195. {
  196. _bindAddress = address;
  197. }
  198. const std::string &ConfigParser::getBindAddress()
  199. {
  200. return _bindAddress;
  201. }