From f3cb054de3eb875845b211323da68bbbcbb87d59 Mon Sep 17 00:00:00 2001 From: u0u0 Date: Fri, 17 Jan 2020 14:42:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8ge?= =?UTF-8?q?tContents=EF=BC=8C=E8=B0=83=E6=95=B4=E5=8A=A0=E5=AF=86=E5=81=9A?= =?UTF-8?q?=E5=87=86=E5=A4=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/base/ccUtils.cpp | 5 +- .../cocostudio/CCDataReaderHelper.cpp | 8 +- .../dragonBones/cocos2dx/CCFactory.cpp | 6 - .../lua-bindings/manual/CCLuaStack.cpp | 113 ++---------------- .../lua-bindings/manual/CCLuaStack.h | 26 ---- .../runtime-src/Classes/AppDelegate.cpp | 27 ----- 6 files changed, 17 insertions(+), 168 deletions(-) diff --git a/cocos/base/ccUtils.cpp b/cocos/base/ccUtils.cpp index 9a91569..584b430 100644 --- a/cocos/base/ccUtils.cpp +++ b/cocos/base/ccUtils.cpp @@ -159,7 +159,6 @@ void captureScreen(const std::function& afterCap director->getRenderer()->addCommand(&s_captureScreenCommand); director->getRenderer()->render(); }); - } static std::unordered_map s_captureNodeListener; @@ -409,9 +408,7 @@ Node* findChild(Node* levelRoot, int tag) std::string getFileMD5Hash(const std::string &filename) { - Data data; - FileUtils::getInstance()->getContents(filename, &data); - + Data data = FileUtils::getInstance()->getDataFromFile(filename); return getDataMD5Hash(data); } diff --git a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp index 64f3f8c..f84985b 100644 --- a/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp +++ b/cocos/editor-support/cocostudio/CCDataReaderHelper.cpp @@ -145,10 +145,12 @@ static const char *CONTENT_SCALE = "content_scale"; static std::string readFileContent(const std::string& filename, bool binary) { auto fs = FileUtils::getInstance(); std::string s; - if (binary) - fs->getContents(filename, &s); - else + if (binary) { + auto data = fs->getDataFromFile(filename); + s = std::string((char *)data.getBytes(), data.getSize()); + } else { s = fs->getStringFromFile(filename); + } return s; }; diff --git a/cocos/editor-support/dragonBones/cocos2dx/CCFactory.cpp b/cocos/editor-support/dragonBones/cocos2dx/CCFactory.cpp index a71a1db..33af7a8 100755 --- a/cocos/editor-support/dragonBones/cocos2dx/CCFactory.cpp +++ b/cocos/editor-support/dragonBones/cocos2dx/CCFactory.cpp @@ -136,17 +136,11 @@ DragonBonesData* CCFactory::loadDragonBonesData(const std::string& filePath, con if (pos != std::string::npos) { const auto data = cocos2d::FileUtils::getInstance()->getStringFromFile(filePath); - return parseDragonBonesData(data.c_str(), name, scale); } else { -#if COCOS2D_VERSION >= 0x00031200 - cocos2d::Data cocos2dData; - cocos2d::FileUtils::getInstance()->getContents(fullpath, &cocos2dData); -#else const auto cocos2dData = cocos2d::FileUtils::getInstance()->getDataFromFile(fullpath); -#endif const auto binary = (unsigned char*)malloc(sizeof(unsigned char)* cocos2dData.getSize()); memcpy(binary, cocos2dData.getBytes(), cocos2dData.getSize()); const auto data = parseDragonBonesData((char*)binary, name, scale); diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp index 3dab3cf..9bd9df6 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -609,44 +609,6 @@ int LuaStack::reload(const char* moduleFileName) return executeString(require.c_str()); } -void LuaStack::setXXTEAKeyAndSign(const char *key, int keyLen, const char *sign, int signLen) -{ - cleanupXXTEAKeyAndSign(); - - if (key && keyLen && sign && signLen) - { - _xxteaKey = (char*)malloc(keyLen); - memcpy(_xxteaKey, key, keyLen); - _xxteaKeyLen = keyLen; - - _xxteaSign = (char*)malloc(signLen); - memcpy(_xxteaSign, sign, signLen); - _xxteaSignLen = signLen; - - _xxteaEnabled = true; - } - else - { - _xxteaEnabled = false; - } -} - -void LuaStack::cleanupXXTEAKeyAndSign() -{ - if (_xxteaKey) - { - free(_xxteaKey); - _xxteaKey = nullptr; - _xxteaKeyLen = 0; - } - if (_xxteaSign) - { - free(_xxteaSign); - _xxteaSign = nullptr; - _xxteaSignLen = 0; - } -} - int LuaStack::loadChunksFromZIP(const char *zipFilePath) { pushString(zipFilePath); @@ -668,37 +630,21 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) 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 (size > 0) { + zip = ZipFile::createWithBuffer(bytes, (unsigned long)size); } if (zip) { - CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : ""); + CCLOG("lua_loadChunksFromZIP() - load zip file: %s", zipFilePath.c_str()); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); + lua_getfield(L, -2, "loaded"); int count = 0; std::string filename = zip->getFirstFilename(); @@ -706,23 +652,10 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) 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 (auto & character : filename) { - if (character == '/' || character == '\\') { - character = '.'; - } - } - CCLOG("[luaLoadChunksFromZIP] add %s to preload", filename.c_str()); - if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) { + if (luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) { + lua_setfield(L, -3, filename.c_str()); + // clear loaded, make the next require run the new module. + lua_pushnil(L); lua_setfield(L, -2, filename.c_str()); ++count; } @@ -731,7 +664,7 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) filename = zip->getNextFilename(); } CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count); - lua_pop(L, 2); + lua_pop(L, 3); lua_pushboolean(L, 1); delete zip; @@ -739,11 +672,6 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) 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; @@ -768,27 +696,8 @@ void skipBOM(const char*& chunk, int& chunkSize) int LuaStack::luaLoadBuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName) { int r = 0; - - if (_xxteaEnabled && strncmp(chunk, _xxteaSign, _xxteaSignLen) == 0) - { - // decrypt XXTEA - xxtea_long len = 0; - unsigned char* result = xxtea_decrypt((unsigned char*)chunk + _xxteaSignLen, - (xxtea_long)chunkSize - _xxteaSignLen, - (unsigned char*)_xxteaKey, - (xxtea_long)_xxteaKeyLen, - &len); - unsigned char* content = result; - xxtea_long contentSize = len; - skipBOM((const char*&)content, (int&)contentSize); - r = luaL_loadbuffer(L, (char*)content, contentSize, chunkName); - free(result); - } - else - { - skipBOM(chunk, chunkSize); - r = luaL_loadbuffer(L, chunk, chunkSize, chunkName); - } + skipBOM(chunk, chunkSize); + r = luaL_loadbuffer(L, chunk, chunkSize, chunkName); #if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0 if (r) diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.h b/cocos/scripting/lua-bindings/manual/CCLuaStack.h index e05b606..8a2d032 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.h +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.h @@ -280,24 +280,8 @@ public: */ virtual bool handleAssert(const char *msg); - /** - * Set the key and sign for xxtea encryption algorithm. - * - * @param key a string pointer - * @param keyLen the length of key - * @param sign a string sign - * @param signLen the length of sign - */ - virtual void setXXTEAKeyAndSign(const char *key, int keyLen, const char *sign, int signLen); - - /** - * free the key and sign for xxtea encryption algorithm. - */ - virtual void cleanupXXTEAKeyAndSign(); - /** * Loads a buffer as a Lua chunk.This function uses lua_load to load the Lua chunk in the buffer pointed to by chunk with size chunkSize. - * If it supports xxtea encryption algorithm, the chunk and the chunkSize would be processed by calling xxtea_decrypt to the real buffer and buffer size. * * @param L the current lua_State. * @param chunk the buffer pointer. @@ -327,11 +311,6 @@ protected: LuaStack() : _state(nullptr) , _callFromLua(0) - , _xxteaEnabled(false) - , _xxteaKey(nullptr) - , _xxteaKeyLen(0) - , _xxteaSign(nullptr) - , _xxteaSignLen(0) { } @@ -340,11 +319,6 @@ protected: lua_State *_state; int _callFromLua; - bool _xxteaEnabled; - char* _xxteaKey; - int _xxteaKeyLen; - char* _xxteaSign; - int _xxteaSignLen; }; NS_CC_END diff --git a/templates/lua-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp b/templates/lua-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp index c2ef1a2..b505a69 100644 --- a/templates/lua-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/templates/lua-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -27,13 +27,6 @@ #include "cocos2d.h" #include "scripting/lua-bindings/manual/lua_module_register.h" -// #define USE_AUDIO_ENGINE 1 - -#if USE_AUDIO_ENGINE -#include "audio/include/AudioEngine.h" -using namespace cocos2d::experimental; -#endif - USING_NS_CC; using namespace std; @@ -43,15 +36,6 @@ AppDelegate::AppDelegate() AppDelegate::~AppDelegate() { -#if USE_AUDIO_ENGINE - AudioEngine::end(); -#endif - -#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 - RuntimeEngine::getInstance()->end(); -#endif - } // if you want a different context, modify the value of glContextAttrs @@ -84,9 +68,6 @@ bool AppDelegate::applicationDidFinishLaunching() register_all_packages(); - LuaStack* stack = engine->getLuaStack(); - stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA")); - //register custom function //LuaStack* stack = engine->getLuaStack(); //register_custom_function(stack->getLuaState()); @@ -108,18 +89,10 @@ bool AppDelegate::applicationDidFinishLaunching() void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); - -#if USE_AUDIO_ENGINE - AudioEngine::pauseAll(); -#endif } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); - -#if USE_AUDIO_ENGINE - AudioEngine::resumeAll(); -#endif }