Merge pull request #8578 from SunLightJuly/Lua_LoadChunksFromZIP

added  LuaStack::loadChunksFromZIP()
This commit is contained in:
minggo 2014-10-10 22:14:55 +08:00
commit 4d084496a3
14 changed files with 208 additions and 3 deletions

View File

@ -1813,6 +1813,10 @@
B60C5BD519AC68B10056FBDE /* CCBillBoard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B60C5BD219AC68B10056FBDE /* CCBillBoard.cpp */; };
B60C5BD619AC68B10056FBDE /* CCBillBoard.h in Headers */ = {isa = PBXBuildFile; fileRef = B60C5BD319AC68B10056FBDE /* CCBillBoard.h */; };
B60C5BD719AC68B10056FBDE /* CCBillBoard.h in Headers */ = {isa = PBXBuildFile; fileRef = B60C5BD319AC68B10056FBDE /* CCBillBoard.h */; };
DA8C62A219E52C6400000516 /* ioapi_mem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DA8C62A019E52C6400000516 /* ioapi_mem.cpp */; };
DA8C62A319E52C6400000516 /* ioapi_mem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DA8C62A019E52C6400000516 /* ioapi_mem.cpp */; };
DA8C62A419E52C6400000516 /* ioapi_mem.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8C62A119E52C6400000516 /* ioapi_mem.h */; };
DA8C62A519E52C6400000516 /* ioapi_mem.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8C62A119E52C6400000516 /* ioapi_mem.h */; };
ED9C6A9418599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; };
ED9C6A9518599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; };
ED9C6A9618599AD8000A5232 /* CCNodeGrid.h in Headers */ = {isa = PBXBuildFile; fileRef = ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */; };
@ -2802,6 +2806,8 @@
B67C624319D4186F00F11FC6 /* ccShader_3D_ColorNormal.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_ColorNormal.frag; sourceTree = "<group>"; };
B67C624419D4186F00F11FC6 /* ccShader_3D_ColorNormalTex.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_ColorNormalTex.frag; sourceTree = "<group>"; };
B67C624519D4186F00F11FC6 /* ccShader_3D_PositionNormalTex.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_PositionNormalTex.vert; sourceTree = "<group>"; };
DA8C62A019E52C6400000516 /* ioapi_mem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ioapi_mem.cpp; sourceTree = "<group>"; };
DA8C62A119E52C6400000516 /* ioapi_mem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ioapi_mem.h; sourceTree = "<group>"; };
ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNodeGrid.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -3350,6 +3356,8 @@
isa = PBXGroup;
children = (
1A570350180BD0B00088DEC7 /* ioapi.cpp */,
DA8C62A019E52C6400000516 /* ioapi_mem.cpp */,
DA8C62A119E52C6400000516 /* ioapi_mem.h */,
1A570351180BD0B00088DEC7 /* ioapi.h */,
1A570352180BD0B00088DEC7 /* unzip.cpp */,
1A570353180BD0B00088DEC7 /* unzip.h */,
@ -4936,6 +4944,7 @@
50ABBED11925AB6F00A911A9 /* TGAlib.h in Headers */,
15AE19D319AAD3A700C27E9E /* Bone.h in Headers */,
1A57019F180BCB590088DEC7 /* CCFont.h in Headers */,
DA8C62A419E52C6400000516 /* ioapi_mem.h in Headers */,
1A5701A3180BCB590088DEC7 /* CCFontAtlas.h in Headers */,
15AE18E919AAD35000C27E9E /* CCActionManagerEx.h in Headers */,
1A01C68618F57BE800EFE3A6 /* CCArray.h in Headers */,
@ -5395,6 +5404,7 @@
15AE1B9619AADA9A00C27E9E /* CocosGUI.h in Headers */,
1A57008C180BC5A10088DEC7 /* CCActionProgressTimer.h in Headers */,
1A570090180BC5A10088DEC7 /* CCActionTiledGrid.h in Headers */,
DA8C62A519E52C6400000516 /* ioapi_mem.h in Headers */,
1A570094180BC5A10088DEC7 /* CCActionTween.h in Headers */,
1A57009B180BC5C10088DEC7 /* CCAtlasNode.h in Headers */,
15AE184919AAD2F700C27E9E /* cocos3d.h in Headers */,
@ -5907,6 +5917,7 @@
15AE18A219AAD33D00C27E9E /* CCParticleSystemQuadLoader.cpp in Sources */,
15AE188C19AAD33D00C27E9E /* CCLabelBMFontLoader.cpp in Sources */,
15AE19E619AAD3A700C27E9E /* Skin.cpp in Sources */,
DA8C62A219E52C6400000516 /* ioapi_mem.cpp in Sources */,
1A570069180BC5A10088DEC7 /* CCActionCatmullRom.cpp in Sources */,
B257B44E1989D5E800D9A687 /* CCPrimitive.cpp in Sources */,
299CF1FB19A434BC00C378C1 /* ccRandom.cpp in Sources */,
@ -6426,6 +6437,7 @@
3E61781D1966A5A300DE83F5 /* CCController.cpp in Sources */,
50ABC00E1926664800A911A9 /* CCFileUtils.cpp in Sources */,
299CF1FC19A434BC00C378C1 /* ccRandom.cpp in Sources */,
DA8C62A319E52C6400000516 /* ioapi_mem.cpp in Sources */,
15AE18C919AAD33D00C27E9E /* CCMenuItemLoader.cpp in Sources */,
50ABBE241925AB6F00A911A9 /* base64.cpp in Sources */,
1A5701A6180BCB590088DEC7 /* CCFontAtlasCache.cpp in Sources */,

View File

@ -288,6 +288,7 @@
<ClCompile Include="..\..\external\ConvertUTF\ConvertUTFWrapper.cpp" />
<ClCompile Include="..\..\external\edtaa3func\edtaa3func.cpp" />
<ClCompile Include="..\..\external\tinyxml2\tinyxml2.cpp" />
<ClCompile Include="..\..\external\unzip\ioapi_mem.cpp" />
<ClCompile Include="..\..\external\unzip\ioapi.cpp" />
<ClCompile Include="..\..\external\unzip\unzip.cpp" />
<ClCompile Include="..\..\external\xxhash\xxhash.c">
@ -506,6 +507,7 @@
<ClInclude Include="..\..\external\ConvertUTF\ConvertUTF.h" />
<ClInclude Include="..\..\external\edtaa3func\edtaa3func.h" />
<ClInclude Include="..\..\external\tinyxml2\tinyxml2.h" />
<ClInclude Include="..\..\external\unzip\ioapi_mem.h" />
<ClInclude Include="..\..\external\unzip\ioapi.h" />
<ClInclude Include="..\..\external\unzip\unzip.h" />
<ClInclude Include="..\..\external\xxhash\xxhash.h" />

View File

@ -455,6 +455,9 @@
<ClCompile Include="..\..\external\tinyxml2\tinyxml2.cpp">
<Filter>external\tinyxml2</Filter>
</ClCompile>
<ClCompile Include="..\..\external\unzip\ioapi_mem.cpp">
<Filter>external\unzip</Filter>
</ClCompile>
<ClCompile Include="..\..\external\unzip\ioapi.cpp">
<Filter>external\unzip</Filter>
</ClCompile>
@ -1071,6 +1074,9 @@
<ClInclude Include="..\..\external\tinyxml2\tinyxml2.h">
<Filter>external\tinyxml2</Filter>
</ClInclude>
<ClInclude Include="..\..\external\unzip\ioapi_mem.h">
<Filter>external\unzip</Filter>
</ClInclude>
<ClInclude Include="..\..\external\unzip\ioapi.h">
<Filter>external\unzip</Filter>
</ClInclude>

View File

@ -203,6 +203,7 @@
<ClCompile Include="..\..\external\ConvertUTF\ConvertUTFWrapper.cpp" />
<ClCompile Include="..\..\external\edtaa3func\edtaa3func.cpp" />
<ClCompile Include="..\..\external\tinyxml2\tinyxml2.cpp" />
<ClCompile Include="..\..\external\unzip\ioapi_mem.cpp" />
<ClCompile Include="..\..\external\unzip\ioapi.cpp" />
<ClCompile Include="..\..\external\unzip\unzip.cpp" />
<ClCompile Include="..\..\external\xxhash\xxhash.c">
@ -444,6 +445,7 @@
<ClInclude Include="..\..\external\ConvertUTF\ConvertUTF.h" />
<ClInclude Include="..\..\external\edtaa3func\edtaa3func.h" />
<ClInclude Include="..\..\external\tinyxml2\tinyxml2.h" />
<ClInclude Include="..\..\external\unzip\ioapi_mem.h" />
<ClInclude Include="..\..\external\unzip\ioapi.h" />
<ClInclude Include="..\..\external\unzip\unzip.h" />
<ClInclude Include="..\..\external\xxhash\xxhash.h" />

View File

@ -382,6 +382,9 @@
<ClCompile Include="..\..\external\tinyxml2\tinyxml2.cpp">
<Filter>external\tinyxml2</Filter>
</ClCompile>
<ClCompile Include="..\..\external\unzip\ioapi_mem.cpp">
<Filter>external\unzip</Filter>
</ClCompile>
<ClCompile Include="..\..\external\unzip\ioapi.cpp">
<Filter>external\unzip</Filter>
</ClCompile>
@ -1012,6 +1015,9 @@
<ClInclude Include="..\..\external\tinyxml2\tinyxml2.h">
<Filter>external\tinyxml2</Filter>
</ClInclude>
<ClInclude Include="..\..\external\unzip\ioapi_mem.h">
<Filter>external\unzip</Filter>
</ClInclude>
<ClInclude Include="..\..\external\unzip\ioapi.h">
<Filter>external\unzip</Filter>
</ClInclude>

View File

@ -204,6 +204,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClCompile Include="..\..\external\ConvertUTF\ConvertUTFWrapper.cpp" />
<ClCompile Include="..\..\external\edtaa3func\edtaa3func.cpp" />
<ClCompile Include="..\..\external\tinyxml2\tinyxml2.cpp" />
<ClCompile Include="..\..\external\unzip\ioapi_mem.cpp" />
<ClCompile Include="..\..\external\unzip\ioapi.cpp" />
<ClCompile Include="..\..\external\unzip\unzip.cpp" />
<ClCompile Include="..\..\external\xxhash\xxhash.c" />
@ -544,6 +545,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClInclude Include="..\..\external\json\stringbuffer.h" />
<ClInclude Include="..\..\external\json\writer.h" />
<ClInclude Include="..\..\external\tinyxml2\tinyxml2.h" />
<ClInclude Include="..\..\external\unzip\ioapi_mem.h" />
<ClInclude Include="..\..\external\unzip\ioapi.h" />
<ClInclude Include="..\..\external\unzip\unzip.h" />
<ClInclude Include="..\..\external\xxhash\xxhash.h" />

View File

@ -679,6 +679,9 @@
<ClCompile Include="..\..\external\tinyxml2\tinyxml2.cpp">
<Filter>external\tinyxml2</Filter>
</ClCompile>
<ClCompile Include="..\..\external\unzip\ioapi_mem.cpp">
<Filter>external\unzip</Filter>
</ClCompile>
<ClCompile Include="..\..\external\unzip\ioapi.cpp">
<Filter>external\unzip</Filter>
</ClCompile>
@ -1729,6 +1732,9 @@
<ClInclude Include="..\..\external\unzip\ioapi.h">
<Filter>external\unzip</Filter>
</ClInclude>
<ClInclude Include="..\..\external\unzip\ioapi_mem.h">
<Filter>external\unzip</Filter>
</ClInclude>
<ClInclude Include="..\..\external\edtaa3func\edtaa3func.h">
<Filter>external\edtaa</Filter>
</ClInclude>

View File

@ -180,6 +180,7 @@ physics/chipmunk/CCPhysicsWorldInfo_chipmunk.cpp \
../external/ConvertUTF/ConvertUTFWrapper.cpp \
../external/ConvertUTF/ConvertUTF.c \
../external/tinyxml2/tinyxml2.cpp \
../external/unzip/ioapi_mem.cpp \
../external/unzip/ioapi.cpp \
../external/unzip/unzip.cpp \
../external/edtaa3func/edtaa3func.cpp \

View File

@ -484,6 +484,8 @@ void ZipUtils::setPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2,
// from unzip.cpp
#define UNZ_MAXFILENAMEINZIP 256
static const std::string emptyFilename("");
struct ZipEntryInfo
{
unz_file_pos pos;
@ -500,6 +502,23 @@ public:
FileListContainer fileList;
};
ZipFile *ZipFile::createWithBuffer(const void* buffer, uLong size)
{
ZipFile *zip = new ZipFile();
if (zip && zip->initWithBuffer(buffer, size)) {
return zip;
} else {
if (zip) delete zip;
return nullptr;
}
}
ZipFile::ZipFile()
: _data(new ZipFilePrivate)
{
_data->zipFile = nullptr;
}
ZipFile::ZipFile(const std::string &zipFile, const std::string &filter)
: _data(new ZipFilePrivate)
{
@ -612,4 +631,45 @@ unsigned char *ZipFile::getFileData(const std::string &fileName, ssize_t *size)
return buffer;
}
std::string ZipFile::getFirstFilename()
{
if (unzGoToFirstFile(_data->zipFile) != UNZ_OK) return emptyFilename;
std::string path;
unz_file_info info;
getCurrentFileInfo(&path, &info);
return path;
}
std::string ZipFile::getNextFilename()
{
if (unzGoToNextFile(_data->zipFile) != UNZ_OK) return emptyFilename;
std::string path;
unz_file_info info;
getCurrentFileInfo(&path, &info);
return path;
}
int ZipFile::getCurrentFileInfo(std::string *filename, unz_file_info *info)
{
char path[FILENAME_MAX + 1];
int ret = unzGetCurrentFileInfo(_data->zipFile, info, path, sizeof(path), nullptr, 0, nullptr, 0);
if (ret != UNZ_OK) {
*filename = emptyFilename;
} else {
filename->assign(path);
}
return ret;
}
bool ZipFile::initWithBuffer(const void *buffer, uLong size)
{
if (!buffer || size == 0) return false;
_data->zipFile = unzOpenBuffer(buffer, size);
if (!_data->zipFile) return false;
setFilter(emptyFilename);
return true;
}
NS_CC_END

View File

@ -214,6 +214,8 @@ namespace cocos2d
// forward declaration
class ZipFilePrivate;
struct unz_file_info_s;
typedef struct unz_file_info_s unz_file_info;
/**
* Zip file - reader helper class.
@ -270,7 +272,18 @@ namespace cocos2d
*/
unsigned char *getFileData(const std::string &fileName, ssize_t *size);
std::string getFirstFilename();
std::string getNextFilename();
static ZipFile *createWithBuffer(const void* buffer, unsigned long size);
private:
/* Only used internal for createWithBuffer() */
ZipFile();
bool initWithBuffer(const void *buffer, unsigned long size);
int getCurrentFileInfo(std::string *filename, unz_file_info *info);
/** Internal data like zip file pointer / file list array and so on */
ZipFilePrivate *_data;
};

View File

@ -177,7 +177,7 @@ bool LuaStack::init(void)
const luaL_reg global_functions [] = {
{"print", lua_print},
{"release_print",lua_release_print},
{NULL, NULL}
{nullptr, nullptr}
};
luaL_register(_state, "_G", global_functions);
@ -585,7 +585,7 @@ int LuaStack::executeFunctionReturnArray(int handler,int numArgs,int numResults,
}else{
resultArray.addObject(static_cast<Ref*>(tolua_tousertype(_state, -1, NULL)));
resultArray.addObject(static_cast<Ref*>(tolua_tousertype(_state, -1, nullptr)));
}
// remove return value from stack
lua_pop(_state, 1); /* L: ... [G] ret1 ret2 ... ret*/
@ -732,6 +732,97 @@ void LuaStack::cleanupXXTEAKeyAndSign()
}
}
int LuaStack::loadChunksFromZIP(const char *zipFilePath)
{
pushString(zipFilePath);
luaLoadChunksFromZIP(_state);
int ret = lua_toboolean(_state, -1);
lua_pop(_state, 1);
return ret;
}
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);
delete []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) {
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;
}
int LuaStack::luaLoadBuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName)
{
int r = 0;

View File

@ -133,6 +133,8 @@ public:
virtual void cleanupXXTEAKeyAndSign();
int luaLoadBuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName);
int loadChunksFromZIP(const char *zipFilePath);
int luaLoadChunksFromZIP(lua_State *L);
protected:
LuaStack(void)

View File

@ -1,5 +1,5 @@
{
"version":"v3-deps-12",
"version":"v3-deps-14",
"zip_file_size":"70810291",
"repo_name":"cocos2d-x-3rd-party-libs-bin",
"repo_parent":"https://github.com/cocos2d/",

View File

@ -2146,6 +2146,8 @@
"external/tinyxml2/tinyxml2.cpp",
"external/tinyxml2/tinyxml2.h",
"external/unzip/CMakeLists.txt",
"external/unzip/ioapi_mem.cpp",
"external/unzip/ioapi_mem.h",
"external/unzip/ioapi.cpp",
"external/unzip/ioapi.h",
"external/unzip/unzip.cpp",