123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- /****************************************************************************
- Copyright (c) 2013 cocos2d-x.org
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "Runtime.h"
- #include "FileServer.h"
- #include "ConnectWaitLayer.h"
- #include "ConsoleCommand.h"
- #include "cocos2d.h"
- #include "ConfigParser.h"
- #include "RuntimeCCSImpl.h" // TODO: move to ide-support
- #if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
- #include "DeviceEx.h"
- #include "network/CCHTTPRequest.h"
- #include "xxhash/xxhash.h"
- #endif
- std::string g_projectPath;
- void recvBuf(int fd, char *pbuf, unsigned long bufsize)
- {
- unsigned long leftLength = bufsize;
- while (leftLength != 0)
- {
- size_t recvlen = recv(fd, pbuf + bufsize - leftLength, leftLength ,0);
- if (recvlen <= 0)
- {
- usleep(1);
- continue;
- }
- leftLength -= recvlen;
- }
- }
- void sendBuf(int fd, const char *pbuf, unsigned long bufsize)
- {
- unsigned long leftLength = bufsize;
- while (leftLength != 0)
- {
- size_t sendlen = send(fd, pbuf + bufsize - leftLength, leftLength ,0);
- if (sendlen <= 0)
- {
- usleep(1);
- continue;
- }
- leftLength -= sendlen;
- }
- }
- std::string& replaceAll(std::string& str, const std::string& old_value, const std::string& new_value)
- {
- size_t start = 0;
- while(true)
- {
- size_t pos = 0;
- if((pos = str.find(old_value, start)) != std::string::npos) {
- str.replace(pos, old_value.length(), new_value);
- start = pos + new_value.length();
- }
- else break;
- }
- return str;
- }
- const char* getRuntimeVersion()
- {
- return "2.0";
- }
- //////////////////////// Loader ////////////////////
- void resetDesignResolution()
- {
- cocos2d::Size size = ConfigParser::getInstance()->getInitViewSize();
- if (!ConfigParser::getInstance()->isLanscape())
- {
- if (size.width > size.height)
- std::swap(size.width, size.height);
- }
- else
- {
- if (size.width < size.height)
- std::swap(size.width, size.height);
- }
- Director::getInstance()->getOpenGLView()->setDesignResolutionSize(size.width, size.height, ResolutionPolicy::EXACT_FIT);
- }
- //
- // RuntimeEngine
- //
- RuntimeEngine::RuntimeEngine()
- : _runtime(nullptr)
- , _eventTrackingEnable(false)
- , _launchEvent("empty")
- {
- }
- RuntimeEngine* RuntimeEngine::getInstance()
- {
- static RuntimeEngine *instance = nullptr;
- if (!instance)
- {
- instance = new RuntimeEngine();
- instance->addRuntime(RuntimeCCSImpl::create(), kRuntimeEngineCCS);
- }
- return instance;
- }
- void RuntimeEngine::setupRuntime()
- {
- //
- // 1. get project type fron config.json
- // 2. init Lua / Js runtime
- //
- updateConfigParser();
- auto entryFile = ConfigParser::getInstance()->getEntryFile();
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
- ConfigParser::getInstance()->readConfig();
- entryFile = ConfigParser::getInstance()->getEntryFile();
- #endif
- // Lua
- if ((entryFile.rfind(".lua") != std::string::npos) ||
- (entryFile.rfind(".luac") != std::string::npos))
- {
- _launchEvent = "lua";
- _runtime = _runtimes[kRuntimeEngineLua];
- }
- // Js
- else if ((entryFile.rfind(".js") != std::string::npos) ||
- (entryFile.rfind(".jsc") != std::string::npos))
- {
- _launchEvent = "js";
- _runtime = _runtimes[kRuntimeEngineJs];
- }
- // csb
- else if ((entryFile.rfind(".csb") != std::string::npos))
- {
- _launchEvent = "ccs";
- _runtime = _runtimes[kRuntimeEngineCCS];
- }
- // csd
- else if ((entryFile.rfind(".csd") != std::string::npos))
- {
- _launchEvent = "ccs";
- _runtime = _runtimes[kRuntimeEngineCCS];
- }
- }
- void RuntimeEngine::setProjectConfig(const ProjectConfig &config)
- {
- _project = config;
- setProjectPath(_project.getProjectDir());
- }
- const ProjectConfig &RuntimeEngine::getProjectConfig()
- {
- return _project;
- }
- void RuntimeEngine::setProjectPath(const std::string &workPath)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
- vector<std::string> searchPathArray = FileUtils::getInstance()->getSearchPaths();
- if (workPath.empty())
- {
- std::string appPath = std::string("");
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- TCHAR szAppDir[MAX_PATH] = { 0 };
- if (GetModuleFileName(NULL, szAppDir, MAX_PATH))
- {
- int nEnd = 0;
- for (int i = 0; szAppDir[i]; i++)
- {
- if (szAppDir[i] == '\\')
- nEnd = i;
- }
- szAppDir[nEnd] = 0;
- int iLen = 2 * wcslen(szAppDir);
- char* chRtn = new char[iLen + 1];
- wcstombs(chRtn, szAppDir, iLen + 1);
- std::string strPath = chRtn;
- delete[] chRtn;
- chRtn = NULL;
- char fuldir[MAX_PATH] = { 0 };
- _fullpath(fuldir, strPath.c_str(), MAX_PATH);
- appPath = fuldir;
- }
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
- appPath.append("/../../../");
- #endif
- appPath = replaceAll(appPath, "\\", "/");
- g_projectPath = appPath;
- }
- else
- {
- g_projectPath = workPath;
- }
- // add project's root directory to search path
- searchPathArray.insert(searchPathArray.begin(), g_projectPath);
- if (!_project.getFirstSearchPath().empty())
- {
- searchPathArray.insert(searchPathArray.begin(), _project.getFirstSearchPath().begin(), _project.getFirstSearchPath().end());
- }
- // add writable path to search path
- searchPathArray.insert(searchPathArray.begin(), FileServer::getShareInstance()->getWritePath());
- FileUtils::getInstance()->setSearchPaths(searchPathArray);
- #endif
- }
- void RuntimeEngine::startScript(const std::string &args)
- {
- resetDesignResolution();
- if (_runtime)
- {
- _runtime->startScript(args);
- }
- trackLaunchEvent();
- }
- void RuntimeEngine::start()
- {
- #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
- _project.setDebuggerType(kCCRuntimeDebuggerCodeIDE);
- #endif
- // set search path
- string path = FileUtils::getInstance()->fullPathForFilename(_project.getScriptFileRealPath().c_str());
- size_t pos;
- while ((pos = path.find_first_of("\\")) != std::string::npos)
- {
- path.replace(pos, 1, "/");
- }
- size_t p = path.find_last_of("/");
- string workdir;
- if (p != path.npos)
- {
- workdir = path.substr(0, p);
- FileUtils::getInstance()->addSearchPath(workdir);
- }
- // update search pathes
- FileUtils::getInstance()->addSearchPath(_project.getProjectDir());
- auto &customizedPathes = _project.getSearchPath();
- for (auto &path : customizedPathes)
- {
- FileUtils::getInstance()->addSearchPath(path);
- }
- //
- if (_project.getDebuggerType() == kCCRuntimeDebuggerNone)
- {
- setupRuntime();
- startScript("");
- }
- else
- {
- startNetwork();
- }
- }
- void RuntimeEngine::end()
- {
- if (_runtime)
- {
- _runtime->end();
- }
- // delete all runtimes
- for (auto it = _runtimes.begin(); it != _runtimes.end(); it++)
- {
- CC_SAFE_DELETE(it->second);
- }
- ConsoleCommand::purge();
- FileServer::getShareInstance()->stop();
- ConfigParser::purge();
- // FileServer::purge();
- }
- void RuntimeEngine::setEventTrackingEnable(bool enable)
- {
- _eventTrackingEnable = enable;
- }
- void RuntimeEngine::addRuntime(RuntimeProtocol *runtime, int type)
- {
- if (_runtimes.find(type) == _runtimes.end())
- {
- _runtimes.insert(std::make_pair(type, runtime));
- }
- else
- {
- CCLOG("RuntimeEngine already has Runtime type %d.", type);
- }
- }
- RuntimeProtocol* RuntimeEngine::getRuntime()
- {
- return _runtime;
- }
- //
- // private
- //
- void RuntimeEngine::showUI()
- {
- auto scene = Scene::create();
- auto connectLayer = new ConnectWaitLayer();
- connectLayer->autorelease();
- auto director = Director::getInstance();
- scene->addChild(connectLayer);
- director->runWithScene(scene);
- }
- bool RuntimeEngine::startNetwork()
- {
- ConsoleCommand::getShareInstance()->init();
- showUI();
- return true;
- }
- void RuntimeEngine::updateConfigParser()
- {
- // set entry file
- auto parser = ConfigParser::getInstance();
- string entryFile(_project.getScriptFileRealPath());
- if (entryFile.find(_project.getProjectDir()) != string::npos)
- {
- entryFile.erase(0, _project.getProjectDir().length());
- }
- std::replace(entryFile.begin(), entryFile.end(), '\\', '/');
- parser->setEntryFile(entryFile);
- parser->setBindAddress(_project.getBindAddress());
- }
- //
- // NOTE: track event on windows / mac platform
- //
- void RuntimeEngine::trackEvent(const std::string &eventName)
- {
- if (!_eventTrackingEnable)
- {
- return ;
- }
- #if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- const char *platform = "win";
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
- const char *platform = "mac";
- #else
- const char *platform = "UNKNOWN";
- #endif
- char cidBuf[64] = {0};
- auto guid = player::DeviceEx::getInstance()->getUserGUID();
- snprintf(cidBuf, sizeof(cidBuf), "%x", XXH32(guid.c_str(), (int)guid.length(), 0));
- auto request = extra::HTTPRequest::createWithUrl(NULL,
- "http://www.google-analytics.com/collect",
- kCCHTTPRequestMethodPOST);
- request->addPOSTValue("v", "1");
- request->addPOSTValue("tid", "UA-58200293-1");
- request->addPOSTValue("cid", cidBuf);
- request->addPOSTValue("t", "event");
- request->addPOSTValue("an", "simulator");
- request->addPOSTValue("av", cocos2dVersion());
- request->addPOSTValue("ec", platform);
- request->addPOSTValue("ea", eventName.c_str());
- request->start();
- #endif // ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
- }
- void RuntimeEngine::trackLaunchEvent()
- {
- trackEvent(_launchEvent);
- }
|