Beispiel #1
0
void CDataManager::LoadPlayerData()
{
	FileUtils* Utils = FileUtils::sharedFileUtils();
	string fString = FileUtils::sharedFileUtils()->getWritablePath() + "player_data.json";
	string szData;

	ssize_t fileSize;
	const char* stream = (char*)Utils->getFileData(fString, "rb", &fileSize);

	if (stream == NULL)
	{
		m_PlayerData.m_szName = "Guest";
		m_PlayerData.m_nStage1Count = 0;
		m_PlayerData.m_nStage2Count = 0;
		m_PlayerData.m_nStage3Count = 0;
	}
	else
	{
		string data = string((const char*)stream, fileSize);
		CC_SAFE_DELETE_ARRAY(stream);

		//Document doc;

		/*if (doc.Parse<0>(data.c_str()).HasParseError())
			CCLOG(doc.GetParseError());*/
		/*else
		{
			m_PlayerData.m_szName = doc["PlayerName"].GetString();
			m_PlayerData.m_nStage1Count = doc["Stage1"].GetInt();
			m_PlayerData.m_nStage2Count = doc["Stage2"].GetInt();
			m_PlayerData.m_nStage3Count = doc["Stage3"].GetInt();
		}*/
	}
}
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;
}
Beispiel #3
0
    int cocos2dx_lua_loader(lua_State *L)
    {
        static const std::string BYTECODE_FILE_EXT    = ".luac";
        static const std::string NOT_BYTECODE_FILE_EXT = ".lua";
        
        std::string filename(luaL_checkstring(L, 1));
        size_t pos = filename.rfind(BYTECODE_FILE_EXT);
        if (pos != std::string::npos)
        {
            filename = filename.substr(0, pos);
        }
        else
        {
            pos = filename.rfind(NOT_BYTECODE_FILE_EXT);
            if (pos == filename.length() - NOT_BYTECODE_FILE_EXT.length())
            {
                filename = filename.substr(0, pos);
            }
        }
        
        pos = filename.find_first_of(".");
        while (pos != std::string::npos)
        {
            filename.replace(pos, 1, "/");
            pos = filename.find_first_of(".");
        }
        
        // search file in package.path
        unsigned char* chunk = nullptr;
        ssize_t chunkSize = 0;
        std::string chunkName;
        FileUtils* utils = FileUtils::getInstance();
        
        lua_getglobal(L, "package");
        lua_getfield(L, -1, "path");
        std::string searchpath(lua_tostring(L, -1));
        lua_pop(L, 1);
        size_t begin = 0;
        size_t next = searchpath.find_first_of(";", 0);
        
        do
        {
            if (next == std::string::npos)
                next = searchpath.length();
            std::string prefix = searchpath.substr(begin, next);
            if (prefix[0] == '.' && prefix[1] == '/')
            {
                prefix = prefix.substr(2);
            }

            /// LZU
            /// Change loading to support path like './?/init.lua'
            /*
            pos = prefix.find("?.lua");
            chunkName = prefix.substr(0, pos) + filename + BYTECODE_FILE_EXT;
            */
            pos = prefix.find("?");
            prefix = prefix.substr(0,pos) + filename + prefix.substr(pos+1);

            pos = prefix.find(".lua");
            chunkName = prefix.substr(0,pos) + BYTECODE_FILE_EXT;
            /// END LZU

            if (utils->isFileExist(chunkName))
            {
                chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize);
                break;
            }
            else
            {
                /// LZU
                chunkName = prefix.substr(0,pos) + NOT_BYTECODE_FILE_EXT;
                // chunkName = prefix.substr(0, pos) + filename + NOT_BYTECODE_FILE_EXT;
                /// END LZU
                if (utils->isFileExist(chunkName))
                {
                    chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize);
                    break;
                }
            }
            
            begin = next + 1;
            next = searchpath.find_first_of(";", begin);
        } while (begin < (int)searchpath.length());
        
        if (chunk)
        {
            LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
            stack->luaLoadBuffer(L, (char*)chunk, (int)chunkSize, chunkName.c_str());
            free(chunk);
        }
        else
        {
            CCLOG("can not get file data of %s", chunkName.c_str());
            return 0;
        }
        
        return 1;
    }
Beispiel #4
0
void CDataManager::LoadMapData(string filename)
{
	string fString = filename;
	string szData;
	FileUtils* Utils = FileUtils::sharedFileUtils();

	ssize_t fileSize;
	char* stream = (char*)Utils->getFileData(filename, "r", &fileSize);
	szData = string(stream);

	int nCharIndex = 0;

	for (int i = 0; i < szData.size(); i++)
	{
		if (szData[i] == '\n')
		{
			string temp = szData.substr(i - nCharIndex, nCharIndex);
			if (temp.find("BOX") != std::string::npos)
			{
				m_arrBoxData.push_back(_GetBoxData(temp));
			}

			if (temp.find("RAIL") != std::string::npos)
			{
				m_arrRailData.push_back(_GetRailData(temp));
			}

			if (temp.find("PULLEY") != std::string::npos)
			{
				m_arrPulleyData.push_back(_GetPulleyData(temp));
			}

			if (temp.find("GROUND") != std::string::npos)
			{
				m_arrGroundData.push_back(_GetGroundData(temp));
			}

			if (temp.find("BG") != std::string::npos)
			{
				m_szBackgroundTextureName = _GetBackgroundTextureName(temp);
			}

			if (temp.find("NEXTSTAGE") != string::npos)
			{
				m_szNextStageFileName = _GetNextSceneFileName(temp);
			}

			if (temp.find("DESTINATION") != string::npos)
			{
				m_DestinationData = _GetDestinationData(temp);
			}

			if (temp.find("MIRAGE") != string::npos)
			{
				m_arrMirageData.push_back(_GetMirageData(temp));
			}

			if (temp.find("WATER") != string::npos)
			{
				m_arrWaterData.push_back(_GetWaterData(temp));
			}

			if (temp.find("SAND") != string::npos)
			{
				m_arrSandData.push_back(_GetSandData(temp));
			}

			if (temp.find("WIND") != string::npos)
			{
				m_arrWindBoxData.push_back(_GetWindBoxData(temp));
			}
			nCharIndex = 0;
		}
		nCharIndex++;
	}

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