不直接使用getContents,调整加密做准备。

This commit is contained in:
u0u0
2020-01-17 14:42:54 +08:00
parent bf2b780c57
commit f3cb054de3
6 changed files with 17 additions and 168 deletions

View File

@@ -159,7 +159,6 @@ void captureScreen(const std::function<void(bool, const std::string&)>& afterCap
director->getRenderer()->addCommand(&s_captureScreenCommand);
director->getRenderer()->render();
});
}
static std::unordered_map<Node*, EventListenerCustom*> 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);
}

View File

@@ -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;
};

View File

@@ -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);

View File

@@ -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)

View File

@@ -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

View File

@@ -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
}