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-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()
|
|
|
|
|
{
|
|
|
|
|
return "1.8";
|
|
|
|
|
}
|
|
|
|
|
|
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-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
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
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-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-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-09 04:08:41 +08:00
|
|
|
|
// csb
|
|
|
|
|
else if ((entryFile.rfind(".csb") != std::string::npos))
|
|
|
|
|
{
|
|
|
|
|
_runtime = RuntimeCCSImpl::create();
|
|
|
|
|
}
|
|
|
|
|
// csd
|
|
|
|
|
else if ((entryFile.rfind(".csd") != std::string::npos))
|
|
|
|
|
{
|
|
|
|
|
_runtime = RuntimeCCSImpl::create();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
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-09 04:08:41 +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-09 04:08:41 +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-09 04:08:41 +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
|
|
|
|
bool RuntimeEngine::startNetwork()
|
2015-01-07 17:50:15 +08:00
|
|
|
|
{
|
2015-01-09 04:08:41 +08:00
|
|
|
|
ConsoleCommand::getShareInstance()->init();
|
|
|
|
|
showUI();
|
|
|
|
|
|
|
|
|
|
return true;
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
if (_runtime)
|
|
|
|
|
{
|
|
|
|
|
_runtime->startScript(args);
|
|
|
|
|
}
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
//FileServer::purge();
|
2015-01-07 17:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|