2015-01-07 17:50:15 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
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 "lua_debugger.h"
|
|
|
|
|
#include "CCLuaEngine.h"
|
|
|
|
|
#include "LuaBasicConversions.h"
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
#include "RuntimeLuaImpl.h"
|
|
|
|
|
#include "RuntimeCCSImpl.h"
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
#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
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
#include <vector>
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
std::string g_projectPath;
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2015-02-27 14:49:04 +08:00
|
|
|
|
return "2.0";
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
//////////////////////// Loader ////////////////////
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
void resetDesignResolution()
|
|
|
|
|
{
|
|
|
|
|
cocos2d::Size size = ConfigParser::getInstance()->getInitViewSize();
|
|
|
|
|
if (!ConfigParser::getInstance()->isLanscape())
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
std::swap(size.width, size.height);
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
2015-01-09 04:08:41 +08:00
|
|
|
|
Director::getInstance()->getOpenGLView()->setDesignResolutionSize(size.width, size.height, ResolutionPolicy::EXACT_FIT);
|
|
|
|
|
}
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
//
|
|
|
|
|
// RuntimeEngine
|
|
|
|
|
//
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
RuntimeEngine::RuntimeEngine()
|
|
|
|
|
: _runtime(nullptr)
|
2015-01-12 15:18:30 +08:00
|
|
|
|
, _eventTrackingEnable(false)
|
|
|
|
|
, _launchEvent("empty")
|
2015-01-09 04:08:41 +08:00
|
|
|
|
{
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
}
|
2015-01-07 17:50:15 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
RuntimeEngine* RuntimeEngine::getInstance()
|
|
|
|
|
{
|
|
|
|
|
static RuntimeEngine *instance = nullptr;
|
|
|
|
|
if (!instance)
|
|
|
|
|
{
|
|
|
|
|
instance = new RuntimeEngine();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
2015-01-09 04:08:41 +08:00
|
|
|
|
return instance;
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
void RuntimeEngine::setupRuntime()
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
CC_SAFE_DELETE(_runtime);
|
|
|
|
|
//
|
|
|
|
|
// 1. get project type fron config.json
|
|
|
|
|
// 2. init Lua / Js runtime
|
|
|
|
|
//
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 12:52:44 +08:00
|
|
|
|
updateConfigParser();
|
2015-01-09 04:08:41 +08:00
|
|
|
|
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();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
#endif
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
// Lua
|
|
|
|
|
if ((entryFile.rfind(".lua") != std::string::npos) ||
|
|
|
|
|
(entryFile.rfind(".luac") != std::string::npos))
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-12 15:18:30 +08:00
|
|
|
|
_launchEvent = "lua";
|
2015-01-09 04:08:41 +08:00
|
|
|
|
_runtime = RuntimeLuaImpl::create();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
2015-01-09 04:08:41 +08:00
|
|
|
|
// Js
|
|
|
|
|
else if ((entryFile.rfind(".js") != std::string::npos) ||
|
|
|
|
|
(entryFile.rfind(".jsc") != std::string::npos))
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-12 15:18:30 +08:00
|
|
|
|
_launchEvent = "js";
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
2015-01-09 04:08:41 +08:00
|
|
|
|
// csb
|
|
|
|
|
else if ((entryFile.rfind(".csb") != std::string::npos))
|
|
|
|
|
{
|
2015-01-12 15:18:30 +08:00
|
|
|
|
_launchEvent = "ccs";
|
2015-01-09 04:08:41 +08:00
|
|
|
|
_runtime = RuntimeCCSImpl::create();
|
|
|
|
|
}
|
|
|
|
|
// csd
|
|
|
|
|
else if ((entryFile.rfind(".csd") != std::string::npos))
|
|
|
|
|
{
|
2015-01-12 15:18:30 +08:00
|
|
|
|
_launchEvent = "ccs";
|
2015-01-09 04:08:41 +08:00
|
|
|
|
_runtime = RuntimeCCSImpl::create();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-12 12:52:44 +08:00
|
|
|
|
|
|
|
|
|
void RuntimeEngine::setProjectConfig(const ProjectConfig &config)
|
|
|
|
|
{
|
|
|
|
|
_project = config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ProjectConfig &RuntimeEngine::getProjectConfig()
|
|
|
|
|
{
|
|
|
|
|
return _project;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
void RuntimeEngine::setProjectPath(const std::string &workPath)
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
|
vector<std::string> searchPathArray = FileUtils::getInstance()->getSearchPaths();
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-07 17:50:15 +08:00
|
|
|
|
if (workPath.empty())
|
|
|
|
|
{
|
|
|
|
|
extern std::string getCurAppPath();
|
|
|
|
|
std::string appPath = getCurAppPath();
|
2015-01-09 04:08:41 +08:00
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2015-01-07 17:50:15 +08:00
|
|
|
|
appPath.append("/../../");
|
2015-01-09 04:08:41 +08:00
|
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
2015-01-07 17:50:15 +08:00
|
|
|
|
appPath.append("/../../../");
|
2015-01-09 04:08:41 +08:00
|
|
|
|
#endif
|
2015-01-07 17:50:15 +08:00
|
|
|
|
appPath = replaceAll(appPath, "\\", "/");
|
|
|
|
|
g_projectPath = appPath;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_projectPath = workPath;
|
|
|
|
|
}
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-07 17:50:15 +08:00
|
|
|
|
// add project's root directory to search path
|
|
|
|
|
searchPathArray.insert(searchPathArray.begin(), g_projectPath);
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-07 17:50:15 +08:00
|
|
|
|
// add writable path to search path
|
|
|
|
|
searchPathArray.insert(searchPathArray.begin(), FileServer::getShareInstance()->getWritePath());
|
|
|
|
|
FileUtils::getInstance()->setSearchPaths(searchPathArray);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
void RuntimeEngine::startScript(const std::string &args)
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
resetDesignResolution();
|
2015-01-12 15:18:30 +08:00
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
if (_runtime)
|
|
|
|
|
{
|
|
|
|
|
_runtime->startScript(args);
|
|
|
|
|
}
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
trackLaunchEvent();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 12:52:44 +08:00
|
|
|
|
void RuntimeEngine::start()
|
|
|
|
|
{
|
2015-01-12 22:59:46 +08:00
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
|
2015-01-12 17:54:58 +08:00
|
|
|
|
_project.setDebuggerType(kCCRuntimeDebuggerCodeIDE);
|
|
|
|
|
#endif
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 12:52:44 +08:00
|
|
|
|
// 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);
|
|
|
|
|
}
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 12:52:44 +08:00
|
|
|
|
// update search pathes
|
|
|
|
|
FileUtils::getInstance()->addSearchPath(_project.getProjectDir());
|
|
|
|
|
auto &customizedPathes = _project.getSearchPath();
|
|
|
|
|
for (auto &path : customizedPathes)
|
|
|
|
|
{
|
|
|
|
|
FileUtils::getInstance()->addSearchPath(path);
|
|
|
|
|
}
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 12:52:44 +08:00
|
|
|
|
//
|
|
|
|
|
if (_project.getDebuggerType() == kCCRuntimeDebuggerNone)
|
|
|
|
|
{
|
2015-01-12 15:18:30 +08:00
|
|
|
|
setupRuntime();
|
|
|
|
|
startScript(_project.getScriptFileRealPath());
|
2015-01-12 12:52:44 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
startNetwork();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
void RuntimeEngine::end()
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
if (_runtime)
|
|
|
|
|
{
|
|
|
|
|
_runtime->end();
|
|
|
|
|
}
|
|
|
|
|
ConsoleCommand::purge();
|
|
|
|
|
FileServer::getShareInstance()->stop();
|
2015-01-12 12:52:44 +08:00
|
|
|
|
ConfigParser::purge();
|
|
|
|
|
// FileServer::purge();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
void RuntimeEngine::setEventTrackingEnable(bool enable)
|
|
|
|
|
{
|
|
|
|
|
_eventTrackingEnable = enable;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
RuntimeProtocol* RuntimeEngine::getRuntime()
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
return _runtime;
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 04:08:41 +08:00
|
|
|
|
//
|
|
|
|
|
// private
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void RuntimeEngine::showUI()
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
auto scene = Scene::create();
|
|
|
|
|
auto connectLayer = new ConnectWaitLayer();
|
|
|
|
|
connectLayer->autorelease();
|
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
|
scene->addChild(connectLayer);
|
|
|
|
|
director->runWithScene(scene);
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
2015-01-12 12:52:44 +08:00
|
|
|
|
|
|
|
|
|
bool RuntimeEngine::startNetwork()
|
|
|
|
|
{
|
|
|
|
|
ConsoleCommand::getShareInstance()->init();
|
|
|
|
|
showUI();
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 12:52:44 +08:00
|
|
|
|
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());
|
2015-01-12 15:18:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// NOTE: track event on windows / mac platform
|
|
|
|
|
//
|
|
|
|
|
void RuntimeEngine::trackEvent(const std::string &eventName)
|
|
|
|
|
{
|
|
|
|
|
if (!_eventTrackingEnable)
|
|
|
|
|
{
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
#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
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
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");
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
request->addPOSTValue("an", "simulator");
|
|
|
|
|
request->addPOSTValue("av", cocos2dVersion());
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
request->addPOSTValue("ec", platform);
|
|
|
|
|
request->addPOSTValue("ea", eventName.c_str());
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
request->start();
|
2015-01-16 23:22:03 +08:00
|
|
|
|
|
2015-01-12 15:18:30 +08:00
|
|
|
|
#endif // ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RuntimeEngine::trackLaunchEvent()
|
|
|
|
|
{
|
|
|
|
|
trackEvent(_launchEvent);
|
|
|
|
|
}
|