bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::create("My Game"); director->setOpenGLView(glview); } glview->setDesignResolutionSize(768, 1024, ResolutionPolicy::EXACT_FIT); FileUtils* futils = FileUtils::getInstance(); auto screensize = glview->getFrameSize(); if(screensize.width > 768) { vector<string> dirs(1, "hd"); futils->setSearchResolutionsOrder(dirs); director->setContentScaleFactor(2); } else { vector<string> dirs(1, "sd"); futils->setSearchResolutionsOrder(dirs); director->setContentScaleFactor(1); } SimpleAudioEngine* augine = SimpleAudioEngine::getInstance(); augine->preloadEffect(futils->fullPathForFilename("hit.wav").c_str()); augine->preloadEffect(futils->fullPathForFilename("score.wav").c_str()); // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = LogoLayer::createScene(); // run director->runWithScene(scene); return true; }
int LuaStack::executeScriptFile(const char* filename) { CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename"); static const std::string BYTECODE_FILE_EXT = ".luac"; static const std::string NOT_BYTECODE_FILE_EXT = ".lua"; std::string buf(filename); // // remove .lua or .luac // size_t pos = buf.rfind(BYTECODE_FILE_EXT); if (pos != std::string::npos) { buf = buf.substr(0, pos); } else { pos = buf.rfind(NOT_BYTECODE_FILE_EXT); if (pos == buf.length() - NOT_BYTECODE_FILE_EXT.length()) { buf = buf.substr(0, pos); } } FileUtils *utils = FileUtils::getInstance(); // // 1. check .lua suffix // 2. check .luac suffix // std::string tmpfilename = buf + NOT_BYTECODE_FILE_EXT; if (utils->isFileExist(tmpfilename)) { buf = tmpfilename; } else { tmpfilename = buf + BYTECODE_FILE_EXT; if (utils->isFileExist(tmpfilename)) { buf = tmpfilename; } } std::string fullPath = utils->fullPathForFilename(buf); Data data = utils->getDataFromFile(fullPath); int rn = 0; if (!data.isNull()) { if (luaLoadBuffer(_state, (const char*)data.getBytes(), (int)data.getSize(), fullPath.c_str()) == 0) { rn = executeFunction(0); } } return rn; }
int LuaStack::executeScriptFile(const char* filename) { CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename"); FileUtils *utils = FileUtils::getInstance(); std::string fullPath = utils->fullPathForFilename(filename); Data data = utils->getDataFromFile(fullPath); int rn = 0; if (!data.isNull()) { if (luaLoadBuffer(_state, (const char*)data.getBytes(), (int)data.getSize(), fullPath.c_str()) == 0) { rn = executeFunction(0); } } return rn; }
int LuaStack::luaLoadChunksFromZIP(lua_State *L) { if (lua_gettop(L) < 1) { CCLOG("luaLoadChunksFromZIP() - invalid arguments"); return 0; } const char *zipFilename = lua_tostring(L, -1); lua_settop(L, 0); FileUtils *utils = FileUtils::getInstance(); std::string zipFilePath = utils->fullPathForFilename(zipFilename); LuaStack *stack = this; do { ssize_t size = 0; void *buffer = nullptr; unsigned char *zipFileData = utils->getFileData(zipFilePath.c_str(), "rb", &size); ZipFile *zip = nullptr; bool isXXTEA = stack && stack->_xxteaEnabled && zipFileData; for (int i = 0; isXXTEA && i < stack->_xxteaSignLen && i < size; ++i) { isXXTEA = zipFileData[i] == stack->_xxteaSign[i]; } if (isXXTEA) { // decrypt XXTEA xxtea_long len = 0; buffer = xxtea_decrypt(zipFileData + stack->_xxteaSignLen, (xxtea_long)size - (xxtea_long)stack->_xxteaSignLen, (unsigned char*)stack->_xxteaKey, (xxtea_long)stack->_xxteaKeyLen, &len); free(zipFileData); zipFileData = nullptr; zip = ZipFile::createWithBuffer(buffer, len); } else { if (zipFileData) { zip = ZipFile::createWithBuffer(zipFileData, size); } } if (zip) { CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : ""); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); int count = 0; std::string filename = zip->getFirstFilename(); while (filename.length()) { ssize_t bufferSize = 0; unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize); if (bufferSize) { // remove extension std::size_t found = filename.rfind(".lua"); if (found != std::string::npos) { filename.erase(found); } // replace path seperator '/' '\' to '.' for (int i=0; i<filename.size(); i++) { if (filename[i] == '/' || filename[i] == '\\') { filename[i] = '.'; } } CCLOG("[luaLoadChunksFromZIP] add %s to preload", filename.c_str()); if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) { lua_setfield(L, -2, filename.c_str()); ++count; } free(zbuffer); } filename = zip->getNextFilename(); } CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count); lua_pop(L, 2); lua_pushboolean(L, 1); delete zip; } else { CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str()); lua_pushboolean(L, 0); } if (zipFileData) { free(zipFileData); } if (buffer) { free(buffer); } } while (0); return 1; }
int LuaStack::luaLoadChunksFromZIP(lua_State *L) { if (lua_gettop(L) < 1) { CCLOG("luaLoadChunksFromZIP() - invalid arguments"); return 0; } const char *zipFilename = lua_tostring(L, -1); lua_settop(L, 0); FileUtils *utils = FileUtils::getInstance(); std::string zipFilePath = utils->fullPathForFilename(zipFilename); LuaStack *stack = this; do { void *buffer = nullptr; ZipFile *zip = nullptr; Data zipFileData(utils->getDataFromFile(zipFilePath)); unsigned char* bytes = zipFileData.getBytes(); ssize_t size = zipFileData.getSize(); bool isXXTEA = stack && stack->_xxteaEnabled && size >= stack->_xxteaSignLen && memcmp(stack->_xxteaSign, bytes, stack->_xxteaSignLen) == 0; if (isXXTEA) { // decrypt XXTEA xxtea_long len = 0; buffer = xxtea_decrypt(bytes + stack->_xxteaSignLen, (xxtea_long)size - (xxtea_long)stack->_xxteaSignLen, (unsigned char*)stack->_xxteaKey, (xxtea_long)stack->_xxteaKeyLen, &len); zip = ZipFile::createWithBuffer(buffer, len); } else { if (size > 0) { zip = ZipFile::createWithBuffer(bytes, (unsigned long)size); } } if (zip) { CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : ""); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); int count = 0; std::string filename = zip->getFirstFilename(); while (filename.length()) { ssize_t bufferSize = 0; unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize); if (bufferSize) { // remove .lua or .luac extension size_t pos = filename.find_last_of('.'); if (pos != std::string::npos) { std::string suffix = filename.substr(pos, filename.length()); if (suffix == NOT_BYTECODE_FILE_EXT || suffix == BYTECODE_FILE_EXT) { filename.erase(pos); } } // replace path separator '/' '\' to '.' for (int i=0; i<filename.size(); i++) { if (filename[i] == '/' || filename[i] == '\\') { filename[i] = '.'; } } CCLOG("[luaLoadChunksFromZIP] add %s to preload", filename.c_str()); if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) { lua_setfield(L, -2, filename.c_str()); ++count; } free(zbuffer); } filename = zip->getNextFilename(); } CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count); lua_pop(L, 2); lua_pushboolean(L, 1); delete zip; } else { CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str()); lua_pushboolean(L, 0); } if (buffer) { free(buffer); } } while (0); return 1; }
bool AppDelegate::applicationDidFinishLaunching() { #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 initRuntime(); #elif (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0) // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE if (_launchMode) { initRuntime(); } #endif initResourcePath(); // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { Size viewSize = ConfigParser::getInstance()->getInitViewSize(); string title = ConfigParser::getInstance()->getInitViewName(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) extern void createSimulator(const char* viewName, float width, float height, bool isLandscape = true, float frameZoomFactor = 1.0f); bool isLanscape = ConfigParser::getInstance()->isLanscape(); createSimulator(title.c_str(),viewSize.width,viewSize.height, isLanscape); #else glview = cocos2d::GLViewImpl::createWithRect(title.c_str(), Rect(0, 0, viewSize.width, viewSize.height)); director->setOpenGLView(glview); #endif director->startAnimation(); } auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); lua_State* L = engine->getLuaStack()->getLuaState(); lua_module_register(L); // use Quick-Cocos2d-X quick_module_register(L); LuaStack* stack = engine->getLuaStack(); #if ANYSDK_DEFINE > 0 lua_getglobal(stack->getLuaState(), "_G"); tolua_anysdk_open(stack->getLuaState()); tolua_anysdk_manual_open(stack->getLuaState()); lua_pop(stack->getLuaState(), 1); #endif stack->setXXTEAKeyAndSign("ilovecocos2dx", strlen("ilovecocos2dx"), "XXTEA", strlen("XXTEA")); stack->addSearchPath("src"); #if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0) // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE if (_launchMode) { startRuntime(); } #endif FileUtils *utils = FileUtils::getInstance(); const char *updateFileName = "code/launcher.zip"; std::string updateFilePath = utils->fullPathForFilename(updateFileName); bool isUpdate = false; if (updateFilePath.compare(updateFileName) != 0) //check if update file exist { printf("%s\n", updateFilePath.c_str()); isUpdate = true; engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str()); } if (!isUpdate) //no update file { const char *zipFilename ="code/game.zip"; std::string zipFilePath = utils->fullPathForFilename(zipFilename); if (zipFilePath.compare(zipFilename) == 0) //no game zip file use default lua file { engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str()); } else { stack->loadChunksFromZIP(zipFilename); stack->executeString("require 'main'"); } } return true; }
int LuaStack::lua_loadChunksFromZIP(lua_State *L) { if (lua_gettop(L) < 1) { CCLOG("lua_loadChunksFromZIP() - invalid arguments"); return 0; } const char *zipFilename = lua_tostring(L, -1); lua_settop(L, 0); FileUtils *utils = FileUtils::getInstance(); std::string zipFilePath = utils->fullPathForFilename(zipFilename); LuaStack *stack = curStack; do { ssize_t size = 0; void *buffer = NULL; unsigned char *zipFileData = utils->getFileData(zipFilePath.c_str(), "rb", &size); ZipFile *zip = NULL; bool isXXTEA = stack && stack->_xxteaEnabled && zipFileData; for (int i = 0; isXXTEA && i < stack->_xxteaSignLen && i < size; ++i) { isXXTEA = zipFileData[i] == stack->_xxteaSign[i]; } if (isXXTEA) { // decrypt XXTEA xxtea_long len = 0; buffer = xxtea_decrypt(zipFileData + stack->_xxteaSignLen, (xxtea_long)size - (xxtea_long)stack->_xxteaSignLen, (unsigned char*)stack->_xxteaKey, (xxtea_long)stack->_xxteaKeyLen, &len); delete []zipFileData; zipFileData = NULL; zip = ZipFile::createWithBuffer(buffer, len); } else { if (zipFileData) { zip = ZipFile::createWithBuffer(zipFileData, size); } } if (zip) { CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : ""); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); int count = 0; std::string filename = zip->getFirstFilename(); while (filename.length()) { ssize_t bufferSize = 0; unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize); if (bufferSize) { if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) { lua_setfield(L, -2, filename.c_str()); ++count; } delete []zbuffer; } filename = zip->getNextFilename(); } CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count); lua_pop(L, 2); lua_pushboolean(L, 1); delete zip; } else { CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str()); lua_pushboolean(L, 0); } if (zipFileData) { delete []zipFileData; } if (buffer) { free(buffer); } } while (0); return 1; }