2014-03-10 19:33:57 +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 "lua_debugger.h"
# include "CCLuaEngine.h"
# include "cocos2d.h"
2014-03-26 14:48:04 +08:00
# include "json/document.h"
# include "json/filestream.h"
# include "json/stringbuffer.h"
# include "json/writer.h"
2014-03-10 19:33:57 +08:00
# ifdef _WIN32
# include <direct.h>
# else
# include <sys/stat.h>
# endif
# include <vector>
# include <string>
using namespace std ;
using namespace cocos2d ;
2014-03-18 18:24:58 +08:00
std : : string g_resourcePath ;
2014-04-11 10:41:35 +08:00
static rapidjson : : Document g_filecfgjson ;
2014-03-26 14:48:04 +08:00
2014-03-10 19:33:57 +08:00
extern string getIPAddress ( ) ;
2014-03-28 11:37:17 +08:00
const char * getRuntimeVersion ( )
2014-03-11 16:52:06 +08:00
{
return " 0.0.1 " ;
}
2014-03-10 19:33:57 +08:00
void startScript ( string strDebugArg )
{
// register lua engine
auto engine = LuaEngine : : getInstance ( ) ;
2014-04-10 15:37:59 +08:00
if ( ! strDebugArg . empty ( ) )
{
engine - > executeString ( strDebugArg . c_str ( ) ) ;
}
cocos2d : : log ( " debug args = %s " , strDebugArg . c_str ( ) ) ;
2014-03-10 19:33:57 +08:00
engine - > executeScriptFile ( " src/main.lua " ) ;
}
2014-03-28 11:37:17 +08:00
bool reloadScript ( const string & modulefile )
2014-03-10 19:33:57 +08:00
{
2014-04-10 15:37:59 +08:00
string strfile = modulefile ;
if ( strfile . empty ( ) )
{
strfile = " src/main.lua " ;
}
2014-03-31 16:49:12 +08:00
2014-04-10 15:37:59 +08:00
auto director = Director : : getInstance ( ) ;
FontFNT : : purgeCachedData ( ) ;
if ( director - > getOpenGLView ( ) )
{
SpriteFrameCache : : getInstance ( ) - > removeSpriteFrames ( ) ;
director - > getTextureCache ( ) - > removeAllTextures ( ) ;
}
FileUtils : : getInstance ( ) - > purgeCachedEntries ( ) ;
2014-03-31 16:49:12 +08:00
2014-04-10 15:37:59 +08:00
director - > getScheduler ( ) - > unscheduleAll ( ) ;
director - > getScheduler ( ) - > scheduleUpdate ( director - > getActionManager ( ) , Scheduler : : PRIORITY_SYSTEM , false ) ;
2014-03-31 16:49:12 +08:00
2014-03-28 11:37:17 +08:00
return ( LuaEngine : : getInstance ( ) - > reload ( strfile . c_str ( ) ) = = 0 ) ;
2014-03-10 19:33:57 +08:00
}
class VisibleRect
{
public :
static Rect getVisibleRect ( ) ;
2014-04-15 18:23:40 +08:00
static Vector2 left ( ) ;
static Vector2 right ( ) ;
static Vector2 top ( ) ;
static Vector2 bottom ( ) ;
static Vector2 center ( ) ;
static Vector2 leftTop ( ) ;
static Vector2 rightTop ( ) ;
static Vector2 leftBottom ( ) ;
static Vector2 rightBottom ( ) ;
2014-03-10 19:33:57 +08:00
private :
static void lazyInit ( ) ;
static Rect s_visibleRect ;
} ;
Rect VisibleRect : : s_visibleRect ;
void VisibleRect : : lazyInit ( )
{
// no lazy init
// Useful if we change the resolution in runtime
s_visibleRect = Director : : getInstance ( ) - > getOpenGLView ( ) - > getVisibleRect ( ) ;
}
Rect VisibleRect : : getVisibleRect ( )
{
lazyInit ( ) ;
return s_visibleRect ;
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : left ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x , s_visibleRect . origin . y + s_visibleRect . size . height / 2 ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : right ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x + s_visibleRect . size . width , s_visibleRect . origin . y + s_visibleRect . size . height / 2 ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : top ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x + s_visibleRect . size . width / 2 , s_visibleRect . origin . y + s_visibleRect . size . height ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : bottom ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x + s_visibleRect . size . width / 2 , s_visibleRect . origin . y ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : center ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x + s_visibleRect . size . width / 2 , s_visibleRect . origin . y + s_visibleRect . size . height / 2 ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : leftTop ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x , s_visibleRect . origin . y + s_visibleRect . size . height ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : rightTop ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x + s_visibleRect . size . width , s_visibleRect . origin . y + s_visibleRect . size . height ) ;
2014-03-10 19:33:57 +08:00
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : leftBottom ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
return s_visibleRect . origin ;
}
2014-04-15 18:23:40 +08:00
Vector2 VisibleRect : : rightBottom ( )
2014-03-10 19:33:57 +08:00
{
lazyInit ( ) ;
2014-04-15 18:23:40 +08:00
return Vector2 ( s_visibleRect . origin . x + s_visibleRect . size . width , s_visibleRect . origin . y ) ;
2014-03-10 19:33:57 +08:00
}
class ConnectWaitLayer : public Layer
{
public :
2014-04-10 15:37:59 +08:00
ConnectWaitLayer ( )
{
2014-03-10 19:33:57 +08:00
string strip = getIPAddress ( ) ;
char szIPAddress [ 512 ] = { 0 } ;
sprintf ( szIPAddress , " LocalIP: %s " , strip . c_str ( ) ) ;
auto label = LabelTTF : : create ( szIPAddress , " Arial " , 24 ) ;
addChild ( label , 9999 ) ;
2014-04-15 18:23:40 +08:00
label - > setPosition ( Vector2 ( VisibleRect : : center ( ) . x , VisibleRect : : top ( ) . y - 30 ) ) ;
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
string strShowMsg = " " ;
if ( CC_PLATFORM_WIN32 = = CC_TARGET_PLATFORM | | CC_PLATFORM_MAC = = CC_TARGET_PLATFORM )
{
strShowMsg = " waiting for debugger to connect ... " ;
} else
{
strShowMsg = " waiting for file transfer ... " ;
}
2014-03-21 11:56:21 +08:00
auto labelwait = LabelTTF : : create ( strShowMsg . c_str ( ) , " Arial " , 22 ) ;
2014-03-10 19:33:57 +08:00
addChild ( labelwait , 10000 ) ;
2014-04-15 18:23:40 +08:00
labelwait - > setPosition ( Vector2 ( VisibleRect : : center ( ) . x , VisibleRect : : center ( ) . y ) ) ;
2014-03-10 19:33:57 +08:00
auto labelPlay = LabelTTF : : create ( " play " , " Arial " , 20 ) ;
auto menuItem = MenuItemLabel : : create ( labelPlay , CC_CALLBACK_1 ( ConnectWaitLayer : : playerCallback , this ) ) ;
auto menu = Menu : : create ( menuItem , NULL ) ;
2014-04-15 18:23:40 +08:00
menu - > setPosition ( Vector2 : : ZERO ) ;
menuItem - > setPosition ( Vector2 ( VisibleRect : : right ( ) . x - 50 , VisibleRect : : bottom ( ) . y + 25 ) ) ;
2014-03-10 19:33:57 +08:00
addChild ( menu , 1 ) ;
2014-04-10 15:37:59 +08:00
}
2014-03-10 19:33:57 +08:00
void playerCallback ( Object * sender )
{
startScript ( " " ) ;
}
} ;
# if defined(_MSC_VER) || defined(__MINGW32__)
# include <io.h>
# include <WS2tcpip.h>
# define bzero(a, b) memset(a, 0, b);
# else
# include <netdb.h>
# include <unistd.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <sys/socket.h>
# include <sys/un.h>
# endif
class FileServer
{
public :
FileServer ( )
{
_listenfd = - 1 ;
_running = false ;
_endThread = false ;
}
2014-04-10 15:37:59 +08:00
bool listenOnTCP ( int port ) ;
2014-03-10 19:33:57 +08:00
void stop ( ) ;
private :
2014-04-10 18:15:07 +08:00
bool receiveFile ( int fd ) ;
2014-03-10 19:33:57 +08:00
void addClient ( ) ;
void loop ( ) ;
// file descriptor: socket, console, etc.
int _listenfd ;
int _maxfd ;
std : : vector < int > _fds ;
std : : thread _thread ;
fd_set _read_set ;
bool _running ;
bool _endThread ;
} ;
bool FileServer : : listenOnTCP ( int port )
{
2014-04-10 15:37:59 +08:00
int listenfd , n ;
const int on = 1 ;
struct addrinfo hints , * res , * ressave ;
char serv [ 30 ] ;
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
snprintf ( serv , sizeof ( serv ) - 1 , " %d " , port ) ;
serv [ sizeof ( serv ) - 1 ] = 0 ;
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
bzero ( & hints , sizeof ( struct addrinfo ) ) ;
hints . ai_flags = AI_PASSIVE ;
hints . ai_family = AF_INET ; // AF_UNSPEC: Do we need IPv6 ?
hints . ai_socktype = SOCK_STREAM ;
2014-03-10 19:33:57 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-04-10 15:37:59 +08:00
WSADATA wsaData ;
n = WSAStartup ( MAKEWORD ( 2 , 2 ) , & wsaData ) ;
2014-03-10 19:33:57 +08:00
# endif
2014-04-10 15:37:59 +08:00
if ( ( n = getaddrinfo ( NULL , serv , & hints , & res ) ) ! = 0 ) {
fprintf ( stderr , " net_listen error for %s: %s " , serv , gai_strerror ( n ) ) ;
return false ;
}
ressave = res ;
do {
listenfd = socket ( res - > ai_family , res - > ai_socktype , res - > ai_protocol ) ;
if ( listenfd < 0 )
continue ; /* error, try next one */
setsockopt ( listenfd , SOL_SOCKET , SO_REUSEADDR , ( const char * ) & on , sizeof ( on ) ) ;
if ( : : bind ( listenfd , res - > ai_addr , res - > ai_addrlen ) = = 0 )
break ; /* success */
close ( listenfd ) ; /* bind error, close and try next one */
} while ( ( res = res - > ai_next ) ! = NULL ) ;
if ( res = = NULL ) {
perror ( " net_listen: " ) ;
freeaddrinfo ( ressave ) ;
return false ;
}
listen ( listenfd , 1 ) ;
if ( res - > ai_family = = AF_INET )
{
char buf [ INET_ADDRSTRLEN ] = " " ;
struct sockaddr_in * sin = ( struct sockaddr_in * ) res - > ai_addr ;
if ( inet_ntop ( res - > ai_family , & sin - > sin_addr , buf , sizeof ( buf ) ) ! = NULL )
cocos2d : : log ( " Console: listening on %s : %d " , buf , ntohs ( sin - > sin_port ) ) ;
else
perror ( " inet_ntop " ) ;
} else if ( res - > ai_family = = AF_INET6 )
{
char buf [ INET6_ADDRSTRLEN ] = " " ;
struct sockaddr_in6 * sin = ( struct sockaddr_in6 * ) res - > ai_addr ;
if ( inet_ntop ( res - > ai_family , & sin - > sin6_addr , buf , sizeof ( buf ) ) ! = NULL )
cocos2d : : log ( " Console: listening on %s : %d " , buf , ntohs ( sin - > sin6_port ) ) ;
else
perror ( " inet_ntop " ) ;
}
freeaddrinfo ( ressave ) ;
_listenfd = listenfd ;
_thread = std : : thread ( std : : bind ( & FileServer : : loop , this ) ) ;
return true ;
2014-03-10 19:33:57 +08:00
}
2014-04-10 15:37:59 +08:00
2014-03-10 19:33:57 +08:00
void FileServer : : stop ( )
{
2014-04-10 15:37:59 +08:00
if ( _running ) {
_endThread = true ;
_thread . join ( ) ;
}
2014-03-10 19:33:57 +08:00
}
2014-03-18 18:24:58 +08:00
string & replaceAll ( string & str , const string & old_value , const string & new_value )
2014-03-10 19:33:57 +08:00
{
2014-04-10 15:37:59 +08:00
while ( true )
{
int pos = 0 ;
if ( ( pos = str . find ( old_value , 0 ) ) ! = string : : npos )
str . replace ( pos , old_value . length ( ) , new_value ) ;
else break ;
}
return str ;
2014-03-10 19:33:57 +08:00
}
bool CreateDir ( const char * sPathName )
{
2014-04-10 15:37:59 +08:00
char DirName [ 256 ] = { 0 } ;
strcpy ( DirName , sPathName ) ;
int i , len = strlen ( DirName ) ;
if ( DirName [ len - 1 ] ! = ' / ' )
strcat ( DirName , " / " ) ;
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
len = strlen ( DirName ) ;
for ( i = 1 ; i < len ; i + + )
{
if ( DirName [ i ] = = ' / ' )
{
DirName [ i ] = 0 ;
if ( access ( DirName , NULL ) ! = 0 )
{
2014-03-10 19:33:57 +08:00
# ifdef _WIN32
2014-04-10 15:37:59 +08:00
if ( mkdir ( DirName /*, 0755*/ ) = = - 1 )
2014-03-10 19:33:57 +08:00
# else
if ( mkdir ( DirName , 0755 ) = = - 1 )
# endif
{
perror ( " mkdir error " ) ;
return false ;
}
2014-04-10 15:37:59 +08:00
}
DirName [ i ] = ' / ' ;
}
}
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
return true ;
2014-03-10 19:33:57 +08:00
}
2014-04-10 15:37:59 +08:00
static bool updateResFileInfo ( )
2014-03-26 14:48:04 +08:00
{
2014-04-10 15:37:59 +08:00
rapidjson : : StringBuffer buffer ;
rapidjson : : Writer < rapidjson : : StringBuffer > writer ( buffer ) ;
g_filecfgjson . Accept ( writer ) ;
const char * str = buffer . GetString ( ) ;
2014-03-26 14:48:04 +08:00
2014-04-10 15:37:59 +08:00
string filecfg = g_resourcePath ;
filecfg . append ( " / " ) ;
filecfg . append ( " fileinfo_debug.json " ) ;
FILE * pFile = fopen ( filecfg . c_str ( ) , " w " ) ;
if ( ! pFile )
return false ;
fwrite ( str , sizeof ( char ) , strlen ( str ) , pFile ) ;
fclose ( pFile ) ;
2014-03-31 11:18:41 +08:00
2014-04-10 15:37:59 +08:00
return true ;
2014-03-26 14:48:04 +08:00
}
2014-04-10 15:37:59 +08:00
static void readResFileFinfo ( )
{
string filecfg = g_resourcePath ;
filecfg . append ( " / " ) ;
filecfg . append ( " fileinfo_debug.json " ) ;
FILE * pFile = fopen ( filecfg . c_str ( ) , " r " ) ;
if ( pFile )
{
rapidjson : : FileStream inputStream ( pFile ) ;
g_filecfgjson . ParseStream < 0 > ( inputStream ) ;
fclose ( pFile ) ;
}
if ( ! g_filecfgjson . IsObject ( ) )
{
g_filecfgjson . SetObject ( ) ;
}
2014-03-26 14:48:04 +08:00
}
2014-04-10 18:15:07 +08:00
bool FileServer : : receiveFile ( int fd )
2014-03-10 19:33:57 +08:00
{
2014-03-26 14:48:04 +08:00
char headlen [ 5 ] = { 0 } ;
2014-04-10 15:37:59 +08:00
if ( recv ( fd , headlen , 4 , 0 ) < = 0 ) {
return false ;
}
char * headSeg = new char [ atoi ( headlen ) + 1 ] ;
if ( ! headSeg )
{
return false ;
}
memset ( headSeg , 0 , atoi ( headlen ) + 1 ) ;
if ( recv ( fd , headSeg , atoi ( headlen ) , 0 ) < = 0 ) {
return false ;
}
2014-03-31 11:18:41 +08:00
rapidjson : : Document headjson ;
2014-04-10 15:37:59 +08:00
headjson . Parse < 0 > ( headSeg ) ;
if ( headjson . HasMember ( " filename " ) )
{
string filename = headjson [ " filename " ] . GetString ( ) ;
char fullfilename [ 1024 ] = { 0 } ;
sprintf ( fullfilename , " %s%s " , g_resourcePath . c_str ( ) , filename . c_str ( ) ) ;
string file ( fullfilename ) ;
file = replaceAll ( file , " \\ " , " / " ) ;
sprintf ( fullfilename , " %s " , file . c_str ( ) ) ;
cocos2d : : log ( " recv fullfilename = %s " , fullfilename ) ;
CreateDir ( file . substr ( 0 , file . find_last_of ( " / " ) ) . c_str ( ) ) ;
FILE * fp = fopen ( fullfilename , " wb " ) ;
int length = 0 ;
while ( ( length = recv ( fd , fullfilename , sizeof ( fullfilename ) , 0 ) ) > 0 ) {
fwrite ( fullfilename , sizeof ( char ) , length , fp ) ;
}
fclose ( fp ) ;
if ( headjson . HasMember ( " lastmodifytime " ) )
{
string filemodifytime = headjson [ " lastmodifytime " ] . GetString ( ) ;
if ( g_filecfgjson . HasMember ( filename . c_str ( ) ) )
{
g_filecfgjson . RemoveMember ( filename . c_str ( ) ) ;
}
rapidjson : : Value filetimeValue ( rapidjson : : kStringType ) ;
filetimeValue . SetString ( filemodifytime . c_str ( ) , g_filecfgjson . GetAllocator ( ) ) ;
rapidjson : : Value filenameValue ( rapidjson : : kStringType ) ;
filenameValue . SetString ( filename . c_str ( ) , g_filecfgjson . GetAllocator ( ) ) ;
g_filecfgjson . AddMember ( filenameValue . GetString ( ) , filetimeValue , g_filecfgjson . GetAllocator ( ) ) ;
updateResFileInfo ( ) ;
}
}
if ( headSeg )
{
delete [ ] headSeg ;
headSeg = nullptr ;
}
2014-03-16 17:32:08 +08:00
string finish ( " finish \n " ) ;
send ( fd , finish . c_str ( ) , finish . size ( ) , 0 ) ;
2014-04-10 15:37:59 +08:00
return true ;
2014-03-10 19:33:57 +08:00
}
void FileServer : : addClient ( )
{
2014-04-10 15:37:59 +08:00
struct sockaddr client ;
socklen_t client_len ;
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
/* new client */
client_len = sizeof ( client ) ;
int fd = accept ( _listenfd , ( struct sockaddr * ) & client , & client_len ) ;
2014-03-10 19:33:57 +08:00
2014-04-10 15:37:59 +08:00
// add fd to list of FD
if ( fd ! = - 1 ) {
FD_SET ( fd , & _read_set ) ;
_fds . push_back ( fd ) ;
_maxfd = std : : max ( _maxfd , fd ) ;
}
2014-03-10 19:33:57 +08:00
}
void FileServer : : loop ( )
{
2014-04-10 15:37:59 +08:00
fd_set copy_set ;
struct timeval timeout , timeout_copy ;
_running = true ;
FD_ZERO ( & _read_set ) ;
FD_SET ( _listenfd , & _read_set ) ;
_maxfd = _listenfd ;
timeout . tv_sec = 0 ;
/* 0.016 seconds. Wake up once per frame at 60PFS */
timeout . tv_usec = 16000 ;
while ( ! _endThread ) {
copy_set = _read_set ;
timeout_copy = timeout ;
int nready = select ( _maxfd + 1 , & copy_set , NULL , NULL , & timeout_copy ) ;
if ( nready = = - 1 )
{
/* error */
if ( errno ! = EINTR )
log ( " Abnormal error in select() \n " ) ;
continue ;
}
else if ( nready = = 0 )
{
/* timeout. do somethig ? */
}
else
{
/* new client */
if ( FD_ISSET ( _listenfd , & copy_set ) ) {
addClient ( ) ;
if ( - - nready < = 0 )
continue ;
}
/* data from client */
std : : vector < int > to_remove ;
for ( const auto & fd : _fds ) {
if ( FD_ISSET ( fd , & copy_set ) ) {
2014-04-10 18:15:07 +08:00
if ( ! receiveFile ( fd ) ) {
2014-04-10 15:37:59 +08:00
to_remove . push_back ( fd ) ;
}
if ( - - nready < = 0 )
break ;
}
}
/* remove closed conections */
for ( int fd : to_remove ) {
FD_CLR ( fd , & _read_set ) ;
_fds . erase ( std : : remove ( _fds . begin ( ) , _fds . end ( ) , fd ) , _fds . end ( ) ) ;
}
}
}
// clean up: ignore stdin, stdout and stderr
for ( const auto & fd : _fds )
{
2014-03-11 16:52:06 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-04-10 15:37:59 +08:00
closesocket ( fd ) ;
2014-03-11 16:52:06 +08:00
# else
2014-04-10 15:37:59 +08:00
close ( fd ) ;
2014-03-11 16:52:06 +08:00
# endif
2014-04-10 15:37:59 +08:00
}
2014-03-10 19:33:57 +08:00
2014-03-11 16:52:06 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-04-10 15:37:59 +08:00
closesocket ( _listenfd ) ;
WSACleanup ( ) ;
2014-03-11 16:52:06 +08:00
# else
2014-04-10 15:37:59 +08:00
close ( _listenfd ) ;
2014-03-11 16:52:06 +08:00
# endif
2014-04-10 15:37:59 +08:00
_running = false ;
2014-03-10 19:33:57 +08:00
}
class ConsoleCustomCommand
{
public :
ConsoleCustomCommand ( ) : _fileserver ( nullptr )
{
cocos2d : : Console * _console = Director : : getInstance ( ) - > getConsole ( ) ;
static struct Console : : Command commands [ ] = {
2014-04-10 15:37:59 +08:00
{ " sendrequest " , " send command to runtime.Args[json format] " , std : : bind ( & ConsoleCustomCommand : : onSendCommand , this , std : : placeholders : : _1 , std : : placeholders : : _2 ) } ,
} ;
2014-03-10 19:33:57 +08:00
for ( int i = 0 ; i < sizeof ( commands ) / sizeof ( Console : : Command ) ; i + + ) {
_console - > addCommand ( commands [ i ] ) ;
}
2014-03-12 11:04:56 +08:00
_console - > listenOnTCP ( 6010 ) ;
2014-03-10 19:33:57 +08:00
_fileserver = new FileServer ( ) ;
2014-03-12 11:04:56 +08:00
_fileserver - > listenOnTCP ( 6020 ) ;
2014-03-10 19:33:57 +08:00
}
~ ConsoleCustomCommand ( )
{
2014-04-10 15:37:59 +08:00
Director : : getInstance ( ) - > getConsole ( ) - > stop ( ) ;
2014-03-10 19:33:57 +08:00
_fileserver - > stop ( ) ;
if ( _fileserver ) {
delete _fileserver ;
_fileserver = nullptr ;
}
}
2014-04-10 15:37:59 +08:00
void onSendCommand ( int fd , const std : : string & args )
{
Director : : getInstance ( ) - > getScheduler ( ) - > performFunctionInCocosThread ( [ = ] ( ) {
rapidjson : : Document dArgParse ;
dArgParse . Parse < 0 > ( args . c_str ( ) ) ;
if ( dArgParse . HasMember ( " cmd " ) )
{
2014-03-28 11:37:17 +08:00
string strcmd = dArgParse [ " cmd " ] . GetString ( ) ;
rapidjson : : Document dReplyParse ;
2014-04-10 15:37:59 +08:00
dReplyParse . SetObject ( ) ;
dReplyParse . AddMember ( " cmd " , strcmd . c_str ( ) , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
if ( dArgParse . HasMember ( " seq " ) ) {
2014-04-10 15:37:59 +08:00
dReplyParse . AddMember ( " seq " , dArgParse [ " seq " ] , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
}
2014-04-10 15:37:59 +08:00
if ( strcmp ( strcmd . c_str ( ) , " start-logic " ) = = 0 )
{
char szDebugArg [ 1024 ] = { 0 } ;
sprintf ( szDebugArg , " require('debugger')(%s,'%s') " , dArgParse [ " debugcfg " ] . GetString ( ) , g_resourcePath . c_str ( ) ) ;
startScript ( szDebugArg ) ;
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
} else if ( strcmp ( strcmd . c_str ( ) , " reload " ) = = 0 )
{
if ( dArgParse . HasMember ( " modulefiles " ) )
{
2014-03-28 11:37:17 +08:00
rapidjson : : Value bodyvalue ( rapidjson : : kObjectType ) ;
2014-04-10 15:37:59 +08:00
const rapidjson : : Value & objectfiles = dArgParse [ " modulefiles " ] ;
for ( rapidjson : : SizeType i = 0 ; i < objectfiles . Size ( ) ; i + + )
{
if ( ! reloadScript ( objectfiles [ i ] . GetString ( ) ) ) {
bodyvalue . AddMember ( objectfiles [ i ] . GetString ( ) , 1 , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
}
2014-04-10 15:37:59 +08:00
}
if ( 0 = = objectfiles . Size ( ) )
{
reloadScript ( " " ) ;
}
dReplyParse . AddMember ( " body " , bodyvalue , dReplyParse . GetAllocator ( ) ) ;
}
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
} else if ( strcmp ( strcmd . c_str ( ) , " getversion " ) = = 0 )
{
2014-03-28 11:37:17 +08:00
rapidjson : : Value bodyvalue ( rapidjson : : kObjectType ) ;
2014-04-10 15:37:59 +08:00
bodyvalue . AddMember ( " version " , getRuntimeVersion ( ) , dReplyParse . GetAllocator ( ) ) ;
dReplyParse . AddMember ( " body " , bodyvalue , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
2014-04-10 15:37:59 +08:00
} else if ( strcmp ( strcmd . c_str ( ) , " getfileinfo " ) = = 0 )
{
2014-03-28 11:37:17 +08:00
rapidjson : : Value bodyvalue ( rapidjson : : kObjectType ) ;
2014-04-10 15:37:59 +08:00
for ( auto it = g_filecfgjson . MemberonBegin ( ) ; it ! = g_filecfgjson . MemberonEnd ( ) ; + + it )
{
bodyvalue . AddMember ( it - > name . GetString ( ) , it - > value . GetString ( ) , dReplyParse . GetAllocator ( ) ) ;
}
dReplyParse . AddMember ( " body " , bodyvalue , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
2014-04-10 15:37:59 +08:00
} else if ( strcmp ( strcmd . c_str ( ) , " getIP " ) = = 0 )
{
2014-03-28 11:37:17 +08:00
rapidjson : : Value bodyvalue ( rapidjson : : kObjectType ) ;
2014-04-10 15:37:59 +08:00
rapidjson : : Value IPValue ( rapidjson : : kStringType ) ;
IPValue . SetString ( getIPAddress ( ) . c_str ( ) , dReplyParse . GetAllocator ( ) ) ;
bodyvalue . AddMember ( " IP " , IPValue , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
dReplyParse . AddMember ( " body " , bodyvalue , dReplyParse . GetAllocator ( ) ) ;
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
2014-04-10 15:37:59 +08:00
} else if ( strcmp ( strcmd . c_str ( ) , " updatefileinfo " ) = = 0 )
{
if ( updateResFileInfo ( ) )
{
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
} else
{
dReplyParse . AddMember ( " code " , 1 , dReplyParse . GetAllocator ( ) ) ;
}
} else if ( strcmp ( strcmd . c_str ( ) , " remove " ) = = 0 )
{
if ( dArgParse . HasMember ( " files " ) )
{
2014-03-28 11:37:17 +08:00
rapidjson : : Value bodyvalue ( rapidjson : : kObjectType ) ;
2014-04-10 15:37:59 +08:00
const rapidjson : : Value & objectfiles = dArgParse [ " files " ] ;
for ( rapidjson : : SizeType i = 0 ; i < objectfiles . Size ( ) ; i + + )
{
string filename ( g_resourcePath ) ;
filename . append ( " / " ) ;
filename . append ( objectfiles [ i ] . GetString ( ) ) ;
if ( FileUtils : : getInstance ( ) - > isFileExist ( filename ) )
{
2014-03-31 11:18:41 +08:00
if ( remove ( filename . c_str ( ) ) = = 0 )
2014-04-10 15:37:59 +08:00
{
if ( g_filecfgjson . HasMember ( objectfiles [ i ] . GetString ( ) ) ) {
g_filecfgjson . RemoveMember ( objectfiles [ i ] . GetString ( ) ) ;
}
}
else
{
bodyvalue . AddMember ( objectfiles [ i ] . GetString ( ) , 2 , dReplyParse . GetAllocator ( ) ) ;
}
2014-03-28 11:37:17 +08:00
} else
{
2014-04-10 15:37:59 +08:00
bodyvalue . AddMember ( objectfiles [ i ] . GetString ( ) , 1 , dReplyParse . GetAllocator ( ) ) ;
2014-03-28 11:37:17 +08:00
}
2014-04-10 15:37:59 +08:00
}
dReplyParse . AddMember ( " body " , bodyvalue , dReplyParse . GetAllocator ( ) ) ;
updateResFileInfo ( ) ;
}
2014-03-28 11:37:17 +08:00
dReplyParse . AddMember ( " code " , 0 , dReplyParse . GetAllocator ( ) ) ;
2014-03-26 14:48:04 +08:00
2014-04-10 15:37:59 +08:00
} else if ( strcmp ( strcmd . c_str ( ) , " shutdownapp " ) = = 0 )
{
2014-03-17 20:46:49 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-04-10 15:37:59 +08:00
extern void shutDownApp ( ) ;
shutDownApp ( ) ;
2014-03-17 20:46:49 +08:00
# else
2014-04-10 15:37:59 +08:00
exit ( 0 ) ;
2014-03-17 20:46:49 +08:00
# endif
2014-04-10 15:37:59 +08:00
}
2014-03-28 11:37:17 +08:00
rapidjson : : StringBuffer buffer ;
rapidjson : : Writer < rapidjson : : StringBuffer > writer ( buffer ) ;
dReplyParse . Accept ( writer ) ;
const char * str = buffer . GetString ( ) ;
char msgSize [ 64 ] = { 0x1 , 0 } ;
2014-04-10 15:37:59 +08:00
sprintf ( msgSize + 1 , " %d: " , strlen ( str ) ) ;
2014-03-28 11:37:17 +08:00
string replymsg ( msgSize ) ;
replymsg . append ( str ) ;
2014-04-10 15:37:59 +08:00
send ( fd , replymsg . c_str ( ) , replymsg . size ( ) , 0 ) ;
}
} ) ;
}
2014-03-10 19:33:57 +08:00
private :
FileServer * _fileserver ;
} ;
2014-04-09 19:17:13 +08:00
bool startRuntime ( )
2014-03-10 19:33:57 +08:00
{
2014-04-09 19:17:13 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
# ifndef _DEBUG
2014-04-10 15:37:59 +08:00
return false ;
2014-04-09 19:17:13 +08:00
# endif
# elif(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
# ifdef NDEBUG
2014-04-10 15:37:59 +08:00
return false ;
2014-04-09 19:17:13 +08:00
# endif
# elif(CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2014-04-10 10:35:53 +08:00
# ifndef COCOS2D_DEBUG
2014-04-10 15:37:59 +08:00
return false ;
2014-04-09 19:17:13 +08:00
# endif
# endif
2014-04-10 15:37:59 +08:00
vector < string > searchPathArray ;
2014-03-19 17:23:06 +08:00
searchPathArray = FileUtils : : getInstance ( ) - > getSearchPaths ( ) ;
2014-03-18 18:24:58 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2014-04-10 15:37:59 +08:00
if ( g_resourcePath . empty ( ) )
{
extern std : : string getCurAppPath ( ) ;
string resourcePath = getCurAppPath ( ) ;
2014-03-28 11:37:17 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2014-04-10 15:37:59 +08:00
resourcePath . append ( " /../../ " ) ;
2014-03-28 11:37:17 +08:00
# elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
resourcePath . append ( " /../../../ " ) ;
# endif
2014-04-10 15:37:59 +08:00
resourcePath = replaceAll ( resourcePath , " \\ " , " / " ) ;
g_resourcePath = resourcePath ;
}
2014-03-18 18:24:58 +08:00
# else
2014-04-10 15:37:59 +08:00
g_resourcePath = FileUtils : : getInstance ( ) - > getWritablePath ( ) ;
2014-03-18 18:24:58 +08:00
# endif
2014-03-19 17:23:06 +08:00
2014-03-18 18:24:58 +08:00
g_resourcePath = replaceAll ( g_resourcePath , " \\ " , " / " ) ;
2014-04-10 15:37:59 +08:00
searchPathArray . insert ( searchPathArray . begin ( ) , g_resourcePath ) ;
2014-03-19 17:23:06 +08:00
FileUtils : : getInstance ( ) - > setSearchPaths ( searchPathArray ) ;
2014-04-10 15:37:59 +08:00
static ConsoleCustomCommand s_customCommand ;
auto engine = LuaEngine : : getInstance ( ) ;
ScriptEngineManager : : getInstance ( ) - > setScriptEngine ( engine ) ;
luaopen_debugger ( engine - > getLuaStack ( ) - > getLuaState ( ) ) ;
2014-03-31 11:18:41 +08:00
readResFileFinfo ( ) ;
2014-03-10 19:33:57 +08:00
auto scene = Scene : : create ( ) ;
auto layer = new ConnectWaitLayer ( ) ;
layer - > autorelease ( ) ;
auto director = Director : : getInstance ( ) ;
scene - > addChild ( layer ) ;
director - > runWithScene ( scene ) ;
2014-04-10 15:37:59 +08:00
return true ;
2014-03-10 19:33:57 +08:00
}
// SimulatorConfig
SimulatorConfig * SimulatorConfig : : s_sharedInstance = NULL ;
SimulatorConfig * SimulatorConfig : : getInstance ( void )
{
2014-04-10 15:37:59 +08:00
if ( ! s_sharedInstance )
{
s_sharedInstance = new SimulatorConfig ( ) ;
}
return s_sharedInstance ;
2014-03-10 19:33:57 +08:00
}
SimulatorConfig : : SimulatorConfig ( void )
{
2014-04-10 15:37:59 +08:00
m_screenSizeArray . push_back ( SimulatorScreenSize ( " iPhone 3Gs (480x320) " , 480 , 320 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " iPhone 4 (960x640) " , 960 , 640 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " iPhone 5 (1136x640) " , 1136 , 640 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " iPad (1024x768) " , 1024 , 768 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " iPad Retina (2048x1536) " , 2048 , 1536 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (800x480) " , 800 , 480 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (854x480) " , 854 , 480 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (960x540) " , 960 , 540 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (1024x600) " , 1024 , 600 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (1280x720) " , 1280 , 720 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (1280x800) " , 1280 , 800 ) ) ;
m_screenSizeArray . push_back ( SimulatorScreenSize ( " Android (1920x1080) " , 1920 , 1080 ) ) ;
2014-03-10 19:33:57 +08:00
}
int SimulatorConfig : : getScreenSizeCount ( void )
{
2014-04-10 15:37:59 +08:00
return ( int ) m_screenSizeArray . size ( ) ;
2014-03-10 19:33:57 +08:00
}
const SimulatorScreenSize SimulatorConfig : : getScreenSize ( int index )
{
2014-04-10 15:37:59 +08:00
return m_screenSizeArray . at ( index ) ;
2014-03-10 19:33:57 +08:00
}