ProjectConfig.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #include <sstream>
  2. #include "ProjectConfig/ProjectConfig.h"
  3. #include "ProjectConfig/SimulatorConfig.h"
  4. #include "cocostudio/LocalizationManager.h"
  5. #ifdef _MSC_VER
  6. #define strcasecmp _stricmp
  7. #endif
  8. #if defined(_WINDOWS)
  9. #define DIRECTORY_SEPARATOR "\\"
  10. #define DIRECTORY_SEPARATOR_CHAR '\\'
  11. #else
  12. #define DIRECTORY_SEPARATOR "/"
  13. #define DIRECTORY_SEPARATOR_CHAR '/'
  14. #endif
  15. static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
  16. std::stringstream ss(s);
  17. std::string item;
  18. while (std::getline(ss, item, delim)) {
  19. elems.push_back(item);
  20. }
  21. return elems;
  22. }
  23. static std::vector<std::string> split(const std::string &s, char delim) {
  24. std::vector<std::string> elems;
  25. split(s, delim, elems);
  26. return elems;
  27. }
  28. ProjectConfig::ProjectConfig()
  29. : _scriptFile("")
  30. , _writablePath("")
  31. , _packagePath("")
  32. , _frameSize(960, 640)
  33. , _frameScale(1.0f)
  34. , _showConsole(true)
  35. , _loadPrecompiledFramework(false)
  36. , _writeDebugLogToFile(false)
  37. , _windowOffset(0, 0)
  38. , _debuggerType(kCCRuntimeDebuggerNone)
  39. , _isAppMenu(true)
  40. , _isResizeWindow(false)
  41. , _isRetinaDisplay(false)
  42. , _debugLogFile("debug.log")
  43. , _consolePort(kProjectConfigConsolePort)
  44. , _fileUploadPort(kProjectConfigUploadPort)
  45. , _bindAddress("")
  46. {
  47. normalize();
  48. }
  49. string ProjectConfig::getProjectDir() const
  50. {
  51. return _projectDir;
  52. }
  53. void ProjectConfig::setProjectDir(const string &projectDir)
  54. {
  55. _projectDir = projectDir;
  56. normalize();
  57. }
  58. string ProjectConfig::getScriptFile() const
  59. {
  60. return _scriptFile;
  61. }
  62. string ProjectConfig::getScriptFileRealPath() const
  63. {
  64. return replaceProjectDirToFullPath(_scriptFile);
  65. }
  66. void ProjectConfig::setScriptFile(const string &scriptFile)
  67. {
  68. _scriptFile = scriptFile;
  69. normalize();
  70. }
  71. string ProjectConfig::getWritablePath() const
  72. {
  73. return _writablePath;
  74. }
  75. string ProjectConfig::getWritableRealPath() const
  76. {
  77. return replaceProjectDirToFullPath(_writablePath);
  78. }
  79. void ProjectConfig::setWritablePath(const string &writablePath)
  80. {
  81. _writablePath = writablePath;
  82. normalize();
  83. }
  84. string ProjectConfig::getPackagePath() const
  85. {
  86. return _packagePath;
  87. }
  88. string ProjectConfig::getNormalizedPackagePath() const
  89. {
  90. // replace $(PROJDIR)
  91. auto path = _packagePath;
  92. auto pos = string::npos;
  93. while ((pos = path.find("$(PROJDIR)")) != string::npos)
  94. {
  95. path = path.substr(0, pos) + _projectDir + path.substr(pos + 10);
  96. }
  97. auto len = path.length();
  98. if (len && path[len - 1] != ';')
  99. {
  100. path.append(";");
  101. }
  102. path.append(";");
  103. SimulatorConfig::makeNormalizePath(&path, "/");
  104. return path;
  105. }
  106. void ProjectConfig::setPackagePath(const string &packagePath)
  107. {
  108. _packagePath = packagePath;
  109. }
  110. void ProjectConfig::addPackagePath(const string &packagePath)
  111. {
  112. if (packagePath.length())
  113. {
  114. if (_packagePath.length())
  115. {
  116. _packagePath.append(";");
  117. }
  118. _packagePath.append(packagePath);
  119. normalize();
  120. }
  121. }
  122. vector<string> ProjectConfig::getPackagePathArray() const
  123. {
  124. vector<string> arr;
  125. size_t pos = string::npos;
  126. size_t prev = 0;
  127. while ((pos = _packagePath.find_first_of(";", pos + 1)) != string::npos)
  128. {
  129. auto path = _packagePath.substr(prev, pos - prev);
  130. if (path.length() > 0) arr.push_back(path);
  131. prev = pos + 1;
  132. }
  133. auto path = _packagePath.substr(prev);
  134. if (path.length() > 0) arr.push_back(path);
  135. return arr;
  136. }
  137. cocos2d::Size ProjectConfig::getFrameSize() const
  138. {
  139. return _frameSize;
  140. }
  141. void ProjectConfig::setFrameSize(const cocos2d::Size &frameSize)
  142. {
  143. if (frameSize.width > 0 && frameSize.height > 0)
  144. {
  145. _frameSize = frameSize;
  146. }
  147. }
  148. bool ProjectConfig::isLandscapeFrame() const
  149. {
  150. return _frameSize.width > _frameSize.height;
  151. }
  152. bool ProjectConfig::isPortraitFrame() const
  153. {
  154. return _frameSize.width < _frameSize.height;
  155. }
  156. void ProjectConfig::changeFrameOrientation()
  157. {
  158. float w = _frameSize.width;
  159. _frameSize.width = _frameSize.height;
  160. _frameSize.height = w;
  161. }
  162. void ProjectConfig::changeFrameOrientationToPortait()
  163. {
  164. if (isLandscapeFrame()) changeFrameOrientation();
  165. }
  166. void ProjectConfig::changeFrameOrientationToLandscape()
  167. {
  168. if (!isLandscapeFrame()) changeFrameOrientation();
  169. }
  170. float ProjectConfig::getFrameScale() const
  171. {
  172. return _frameScale;
  173. }
  174. void ProjectConfig::setFrameScale(float frameScale)
  175. {
  176. if (frameScale > 0)
  177. {
  178. _frameScale = frameScale;
  179. }
  180. }
  181. bool ProjectConfig::isShowConsole() const
  182. {
  183. return _showConsole;
  184. }
  185. void ProjectConfig::setShowConsole(bool showConsole)
  186. {
  187. _showConsole = showConsole;
  188. }
  189. bool ProjectConfig::isLoadPrecompiledFramework() const
  190. {
  191. return _loadPrecompiledFramework;
  192. }
  193. void ProjectConfig::setLoadPrecompiledFramework(bool load)
  194. {
  195. _loadPrecompiledFramework = load;
  196. }
  197. bool ProjectConfig::isWriteDebugLogToFile() const
  198. {
  199. return _writeDebugLogToFile;
  200. }
  201. void ProjectConfig::setWriteDebugLogToFile(bool writeDebugLogToFile)
  202. {
  203. _writeDebugLogToFile = writeDebugLogToFile;
  204. }
  205. void ProjectConfig::setDebugLogFilePath(const std::string &logFile)
  206. {
  207. _debugLogFile = logFile;
  208. }
  209. string ProjectConfig::getDebugLogFilePath() const
  210. {
  211. if (isAbsolutePath(_debugLogFile)) return _debugLogFile;
  212. auto path(getProjectDir());
  213. path.append(_debugLogFile);
  214. return path;
  215. }
  216. cocos2d::Vec2 ProjectConfig::getWindowOffset() const
  217. {
  218. return _windowOffset;
  219. }
  220. void ProjectConfig::setWindowOffset(const cocos2d::Vec2 &windowOffset)
  221. {
  222. _windowOffset = windowOffset;
  223. }
  224. int ProjectConfig::getDebuggerType() const
  225. {
  226. return _debuggerType;
  227. }
  228. void ProjectConfig::setDebuggerType(int debuggerType)
  229. {
  230. _debuggerType = debuggerType;
  231. }
  232. void ProjectConfig::parseCommandLine(const vector<string> &args)
  233. {
  234. auto it = args.begin();
  235. while (it != args.end())
  236. {
  237. string arg = *it;
  238. if (arg.compare("-workdir") == 0)
  239. {
  240. ++it;
  241. if (it == args.end()) break;
  242. setProjectDir(*it);
  243. if (_writablePath.length() == 0) setWritablePath(*it);
  244. }
  245. else if (arg.compare("-writable-path") == 0)
  246. {
  247. ++it;
  248. if (it == args.end()) break;
  249. setWritablePath(*it);
  250. }
  251. else if (arg.compare("-entry") == 0)
  252. {
  253. ++it;
  254. if (it == args.end()) break;
  255. setScriptFile(*it);
  256. }
  257. else if (arg.compare("-landscape") == 0)
  258. {
  259. setFrameSize(cocos2d::Size(DEFAULT_HEIGHT, DEFAULT_WIDTH));
  260. }
  261. else if (arg.compare("-portrait") == 0)
  262. {
  263. setFrameSize(cocos2d::Size(DEFAULT_WIDTH, DEFAULT_HEIGHT));
  264. }
  265. else if (arg.compare("-resolution") == 0)
  266. {
  267. ++it;
  268. if (it == args.end()) break;
  269. const string& sizeStr(*it);
  270. size_t pos = sizeStr.find('x');
  271. int width = 0;
  272. int height = 0;
  273. if (pos != sizeStr.npos && pos > 0)
  274. {
  275. string widthStr, heightStr;
  276. widthStr.assign(sizeStr, 0, pos);
  277. heightStr.assign(sizeStr, pos + 1, sizeStr.length() - pos);
  278. width = atoi(widthStr.c_str());
  279. height = atoi(heightStr.c_str());
  280. setFrameSize(cocos2d::Size(width, height));
  281. }
  282. }
  283. else if (arg.compare("-scale") == 0)
  284. {
  285. ++it;
  286. if (it == args.end()) break;
  287. float scale = atof((*it).c_str());
  288. setFrameScale(scale);
  289. }
  290. else if (arg.compare("-write-debug-log") == 0)
  291. {
  292. ++it;
  293. if (it == args.end()) break;
  294. setDebugLogFilePath((*it));
  295. setWriteDebugLogToFile(true);
  296. }
  297. else if (arg.compare("-console") == 0)
  298. {
  299. ++it;
  300. if (it == args.end()) break;
  301. if ((*it).compare("enable") == 0)
  302. {
  303. setShowConsole(true);
  304. }
  305. else
  306. {
  307. setShowConsole(false);
  308. }
  309. }
  310. else if (arg.compare("-position") == 0)
  311. {
  312. ++it;
  313. if (it == args.end()) break;
  314. const string& posStr(*it);
  315. size_t pos = posStr.find(',');
  316. int x = 0;
  317. int y = 0;
  318. if (pos != posStr.npos && pos > 0)
  319. {
  320. string xStr, yStr;
  321. xStr.assign(posStr, 0, pos);
  322. yStr.assign(posStr, pos + 1, posStr.length() - pos);
  323. x = atoi(xStr.c_str());
  324. y = atoi(yStr.c_str());
  325. setWindowOffset(cocos2d::Vec2(x, y));
  326. }
  327. }
  328. else if (arg.compare("-debugger") == 0)
  329. {
  330. ++it;
  331. if (it == args.end()) break;
  332. if ((*it).compare("codeide") == 0)
  333. {
  334. setDebuggerType(kCCRuntimeDebuggerCodeIDE);
  335. }
  336. else if ((*it).compare("studio") == 0)
  337. {
  338. setDebuggerType(kCCRuntimeDebuggerStudio);
  339. }
  340. }
  341. else if (arg.compare("-app-menu") == 0)
  342. {
  343. _isAppMenu = true;
  344. }
  345. else if (arg.compare("-resize-window") == 0)
  346. {
  347. _isResizeWindow = true;
  348. }
  349. else if (arg.compare("-retina-display") == 0)
  350. {
  351. _isRetinaDisplay = true;
  352. }
  353. else if (arg.compare("-port") == 0)
  354. {
  355. CCLOG("TODO:");
  356. }
  357. else if (arg.compare("-listen") == 0)
  358. {
  359. ++it;
  360. setBindAddress((*it));
  361. }
  362. else if (arg.compare("-search-path") == 0)
  363. {
  364. ++it;
  365. vector<string> pathes = split((*it), ';');
  366. setSearchPath(pathes);
  367. }
  368. else if (arg.compare("-first-search-path") == 0)
  369. {
  370. ++it;
  371. vector<string> pathes = split((*it), ';');
  372. setFirstSearchPath(pathes);
  373. }
  374. else if (arg.compare("-language-data-path") == 0)
  375. {
  376. ++it;
  377. if (it == args.end()) break;
  378. setLanguageDataPath(*it);
  379. }
  380. ++it;
  381. }
  382. }
  383. string ProjectConfig::makeCommandLine(unsigned int mask /* = kProjectConfigAll */) const
  384. {
  385. stringstream buff;
  386. vector<string> commands = makeCommandLineVector(mask);
  387. for (auto &cmd : commands)
  388. {
  389. buff << " " << cmd;
  390. }
  391. string result = buff.str();
  392. while (result.at(0) == ' ')
  393. {
  394. result = result.assign(result, 1, result.length());
  395. }
  396. return result;
  397. }
  398. vector<string> ProjectConfig::makeCommandLineVector(unsigned int mask /* = kProjectConfigAll */) const
  399. {
  400. vector<string> ret;
  401. stringstream buff;
  402. if (mask & kProjectConfigProjectDir)
  403. {
  404. auto path = getProjectDir();
  405. if (path.length())
  406. {
  407. ret.push_back("-workdir");
  408. ret.push_back(dealWithSpaceWithPath(path));
  409. }
  410. }
  411. if (mask & kProjectConfigScriptFile)
  412. {
  413. auto path = getScriptFileRealPath();
  414. if (path.length())
  415. {
  416. ret.push_back("-entry");
  417. ret.push_back(dealWithSpaceWithPath(path));
  418. }
  419. }
  420. if (mask & kProjectConfigWritablePath)
  421. {
  422. auto path = getWritableRealPath();
  423. if (path.length())
  424. {
  425. ret.push_back("-writable-path");
  426. ret.push_back(dealWithSpaceWithPath(path));
  427. }
  428. }
  429. if (mask & kProjectConfigFrameSize)
  430. {
  431. buff.str("");
  432. buff << (int)getFrameSize().width;
  433. buff << "x";
  434. buff << (int)getFrameSize().height;
  435. ret.push_back("-resolution");
  436. ret.push_back(buff.str());
  437. }
  438. if (mask & kProjectConfigFrameScale)
  439. {
  440. if (getFrameScale() < 1.0f)
  441. {
  442. buff.str("");
  443. buff.precision(2);
  444. buff << getFrameScale();
  445. ret.push_back("-scale");
  446. ret.push_back(buff.str());
  447. }
  448. }
  449. if (mask & kProjectConfigWriteDebugLogToFile)
  450. {
  451. if (isWriteDebugLogToFile())
  452. {
  453. ret.push_back("-write-debug-log");
  454. ret.push_back(getDebugLogFilePath());
  455. }
  456. }
  457. if (mask & kProjectConfigShowConsole)
  458. {
  459. if (isShowConsole())
  460. {
  461. ret.push_back("-console");
  462. ret.push_back("enable");
  463. }
  464. else
  465. {
  466. ret.push_back("-console");
  467. ret.push_back("disable");
  468. }
  469. }
  470. if (mask & kProjectConfigWindowOffset)
  471. {
  472. if (_windowOffset.x != 0 && _windowOffset.y != 0)
  473. {
  474. buff.str("");
  475. buff << (int)_windowOffset.x;
  476. buff << ",";
  477. buff << (int)_windowOffset.y;
  478. buff << "";
  479. ret.push_back("-position");
  480. ret.push_back(buff.str());
  481. }
  482. }
  483. if (mask & kProjectConfigDebugger)
  484. {
  485. switch (getDebuggerType())
  486. {
  487. case kCCRuntimeDebuggerCodeIDE:
  488. ret.push_back("-debugger");
  489. ret.push_back("codeide");
  490. break;
  491. case kCCRuntimeDebuggerStudio:
  492. ret.push_back("-debugger");
  493. ret.push_back("studio");
  494. break;
  495. }
  496. }
  497. if (mask & kProjectConfigListen)
  498. {
  499. if (!_bindAddress.empty())
  500. {
  501. ret.push_back("-listen");
  502. ret.push_back(_bindAddress);
  503. }
  504. }
  505. if (mask & kProjectConfigSearchPath)
  506. {
  507. if (_searchPath.size() > 0)
  508. {
  509. stringstream pathbuff;
  510. for (auto &path : _searchPath)
  511. {
  512. pathbuff << dealWithSpaceWithPath(path) << ";";
  513. }
  514. string pathArgs = pathbuff.str();
  515. pathArgs[pathArgs.length()-1] = '\0';
  516. ret.push_back("-search-path");
  517. ret.push_back(pathArgs);
  518. }
  519. }
  520. if (mask & kProjectConfigFirstSearchPath)
  521. {
  522. if (_searchPath.size() > 0)
  523. {
  524. stringstream pathbuff;
  525. for (auto &path : _searchPath)
  526. {
  527. pathbuff << dealWithSpaceWithPath(path) << ";";
  528. }
  529. string pathArgs = pathbuff.str();
  530. pathArgs[pathArgs.length() - 1] = '\0';
  531. ret.push_back("-first-search-path");
  532. ret.push_back(pathArgs);
  533. }
  534. }
  535. return ret;
  536. }
  537. void ProjectConfig::setConsolePort(int port)
  538. {
  539. _consolePort = port;
  540. }
  541. int ProjectConfig::getConsolePort()
  542. {
  543. return _consolePort;
  544. }
  545. void ProjectConfig::setFileUploadPort(int port)
  546. {
  547. _fileUploadPort = port;
  548. }
  549. int ProjectConfig::getFileUploadPort()
  550. {
  551. return _fileUploadPort;
  552. }
  553. void ProjectConfig::setBindAddress(const std::string &address)
  554. {
  555. _bindAddress = address;
  556. }
  557. const std::string &ProjectConfig::getBindAddress() const
  558. {
  559. return _bindAddress;
  560. }
  561. void ProjectConfig::setSearchPath(const vector<string> &args)
  562. {
  563. _searchPath = args;
  564. }
  565. const vector<string> &ProjectConfig::getSearchPath() const
  566. {
  567. return _searchPath;
  568. }
  569. void ProjectConfig::setFirstSearchPath(const vector<string> &args)
  570. {
  571. _firstSearchPath = args;
  572. }
  573. const vector<string> &ProjectConfig::getFirstSearchPath() const
  574. {
  575. return _firstSearchPath;
  576. }
  577. void ProjectConfig::setLanguageDataPath(const std::string &filePath)
  578. {
  579. bool isBinary = true;
  580. string jsonExtension = ".json";
  581. int exLength = jsonExtension.length();
  582. if (filePath.length() >= exLength &&
  583. (0 == filePath.compare(filePath.length() - exLength, exLength, jsonExtension)))
  584. {
  585. isBinary = false;
  586. }
  587. cocostudio::ILocalizationManager* lm;
  588. if (isBinary)
  589. lm = cocostudio::BinLocalizationManager::getInstance();
  590. else
  591. lm = cocostudio::JsonLocalizationManager::getInstance();
  592. lm->initLanguageData(filePath);
  593. cocostudio::LocalizationHelper::setCurrentManager(lm, isBinary);
  594. }
  595. bool ProjectConfig::isAppMenu() const
  596. {
  597. return _isAppMenu;
  598. }
  599. bool ProjectConfig::isResizeWindow() const
  600. {
  601. return _isResizeWindow;
  602. }
  603. bool ProjectConfig::isRetinaDisplay() const
  604. {
  605. return _isRetinaDisplay;
  606. }
  607. bool ProjectConfig::validate() const
  608. {
  609. auto utils = cocos2d::FileUtils::getInstance();
  610. if (!utils->isDirectoryExist(_projectDir)) return false;
  611. if (!utils->isDirectoryExist(getWritableRealPath())) return false;
  612. if (!utils->isFileExist(getScriptFileRealPath())) return false;
  613. return true;
  614. }
  615. void ProjectConfig::dump()
  616. {
  617. CCLOG("Project Config:");
  618. CCLOG(" project dir: %s", _projectDir.c_str());
  619. CCLOG(" writable path: %s", _writablePath.length() ? _writablePath.c_str() : "-");
  620. CCLOG(" script file: %s", _scriptFile.c_str());
  621. CCLOG(" frame size: %0.0f x %0.0f", _frameSize.width, _frameSize.height);
  622. CCLOG(" frame scale: %0.2f", _frameScale);
  623. CCLOG(" show console: %s", _showConsole ? "YES" : "NO");
  624. CCLOG(" write debug log: %s (%s)", _writeDebugLogToFile ? getDebugLogFilePath().c_str() : "NO",
  625. _writeDebugLogToFile ? getDebugLogFilePath().c_str() : "");
  626. CCLOG(" listen: %s", _bindAddress.c_str());
  627. if (_debuggerType == kCCRuntimeDebuggerLDT)
  628. {
  629. CCLOG(" debugger: Eclipse LDT");
  630. }
  631. else if (_debuggerType == kCCRuntimeDebuggerCodeIDE)
  632. {
  633. CCLOG(" debugger: Cocos Code IDE");
  634. }
  635. else if (_debuggerType == kCCRuntimeDebuggerStudio)
  636. {
  637. CCLOG(" debugger: Cocos Studio");
  638. }
  639. else
  640. {
  641. CCLOG(" debugger: none");
  642. }
  643. CCLOG(" add searching path:");
  644. for (auto &path : _searchPath)
  645. {
  646. CCLOG(" %s", path.c_str());
  647. }
  648. CCLOG("\n\n");
  649. }
  650. void ProjectConfig::normalize()
  651. {
  652. SimulatorConfig::makeNormalizePath(&_projectDir);
  653. SimulatorConfig::makeNormalizePath(&_scriptFile);
  654. SimulatorConfig::makeNormalizePath(&_writablePath);
  655. SimulatorConfig::makeNormalizePath(&_packagePath);
  656. // projectDir
  657. size_t len = _projectDir.length();
  658. if (len > 0 && _projectDir[len - 1] != DIRECTORY_SEPARATOR_CHAR)
  659. {
  660. _projectDir.append(DIRECTORY_SEPARATOR);
  661. len++;
  662. }
  663. // writablePath
  664. if (len > 0 && _writablePath.length() == 0)
  665. {
  666. _writablePath = _projectDir;
  667. }
  668. len = _writablePath.length();
  669. if (len > 0 && _writablePath[len - 1] != DIRECTORY_SEPARATOR_CHAR)
  670. {
  671. _writablePath.append(DIRECTORY_SEPARATOR);
  672. }
  673. _writablePath = replaceProjectDirToMacro(_writablePath);
  674. // scriptFile
  675. _scriptFile = replaceProjectDirToMacro(_scriptFile);
  676. // package.path
  677. vector<string> arr = getPackagePathArray();
  678. _packagePath = string("");
  679. for (auto it = arr.begin(); it != arr.end(); ++it)
  680. {
  681. string path = replaceProjectDirToMacro(*it);
  682. _packagePath.append(path);
  683. _packagePath.append(";");
  684. }
  685. if (_packagePath.length() > 0 && _packagePath[_packagePath.length() - 1] == ';')
  686. {
  687. _packagePath = _packagePath.substr(0, _packagePath.length() - 1);
  688. }
  689. }
  690. string ProjectConfig::replaceProjectDirToMacro(const string &path) const
  691. {
  692. if (!isAbsolutePath(path))
  693. {
  694. if (path.compare(0, 10, "$(PROJDIR)") == 0) return path;
  695. string result("$(PROJDIR)");
  696. result.append(DIRECTORY_SEPARATOR);
  697. result.append(path);
  698. return result;
  699. }
  700. string result = path;
  701. size_t len = _projectDir.length();
  702. if (len > 0 && result.compare(0, len, _projectDir) == 0)
  703. {
  704. result = "$(PROJDIR)";
  705. result.append(DIRECTORY_SEPARATOR);
  706. result.append(path.substr(len));
  707. }
  708. return result;
  709. }
  710. string ProjectConfig::replaceProjectDirToFullPath(const string &path) const
  711. {
  712. if (isAbsolutePath(path)) return path;
  713. if (path.length() == 0) return _projectDir;
  714. string result = path;
  715. if (path.compare(0, 10, "$(PROJDIR)") == 0)
  716. {
  717. result = _projectDir;
  718. string suffix = path.substr(10);
  719. if (suffix[0] == DIRECTORY_SEPARATOR_CHAR)
  720. {
  721. suffix = suffix.substr(1);
  722. }
  723. result.append(suffix);
  724. }
  725. return result;
  726. }
  727. bool ProjectConfig::isAbsolutePath(const string &path) const
  728. {
  729. #if defined(_WINDOWS)
  730. return path.length() > 2 && path[1] == ':';
  731. #else
  732. return path.length() > 0 && path[0] == '/';
  733. #endif
  734. }
  735. string ProjectConfig::dealWithSpaceWithPath(const string &path) const
  736. {
  737. #if defined(_WINDOWS)
  738. string ret("\"");
  739. ret += path;
  740. if (path[path.length() - 1] == '\\')
  741. {
  742. ret += "\\";
  743. }
  744. ret += "\"";
  745. return ret;
  746. #else
  747. return path;
  748. #endif
  749. }