void ActorManager::LoadResource(const vector<Name>& resNameList) { //1.打开文件 FileUtils* fin = FileUtils::getInstance(); Data data = fin->getDataFromFile("data/Actor.json"); CCASSERT(!data.isNull(), "[Actor.json] Lost!"); //2.载入json string str = string((char*)data.getBytes(), data.getSize()); rapidjson::Document root; root.Parse<0>(str.c_str()); CCASSERT(root.IsObject() && root.HasMember("actordata"), "illegal [Actor.json]"); //3.读取json数据 int Size = root["actordata"].Size(); for (int i = 0; i < Size; i++) { string name = root["actordata"][i]["name"].GetString(); if (std::find(resNameList.begin(), resNameList.end(), name) != resNameList.end()) { ActorData d; d.defaultanimate=root["actordata"][i]["defaultanimate"].GetString(); int size = root["actordata"][i]["actioncount"].GetInt(); CCASSERT(size != 0, "framecount must NOT equal 0"); for (int j = 0; j < size; j++) { d.maps[root["actordata"][i]["actionName"][j].GetString()] = root["actordata"][i]["animateName"][j].GetString(); } m_actordata[name] = d; } } }
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 { 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; }