cocos2dx 3.x lua 网络加载并且保存资源(unix、linux)

#ifndef __DazzleParkour__TextLoader__

#define __DazzleParkour__TextLoader__

#include <stdio.h>

#include "cocos2d.h"

#include "network/HttpClient.h"

#include "CCLuaValue.h"

using namespace cocos2d::network;

using namespace cocos2d;

using namespace std;

class TextLoader

{

public:

    static TextLoader*create(const char* url,const char* requestData,const char* tag,string textName,string savePath,LUA_FUNCTION callback){

        TextLoader * instance = new TextLoader();

        instance->doHttpRequest(url,requestData,tag,textName,savePath,callback);

        return instance;

    }

    void onHttpRequestImageCompleted(HttpClient *sender, HttpResponse *response);

    void doHttpRequest(const char* url,const char* requestData,const char* tag,string textName,string savePath,LUA_FUNCTION callback);

private:

    string m_textName;

    string m_savePath;

    char m_url[500];

    LUA_FUNCTION m_callback;

};

#endif

//安卓项目需要获取网络权限

#include <stdio.h>

#include "TextLoader.h"

#include "ImageLoader.h"

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <dirent.h>

#include "CCLuaStack.h"

#include "CCLuaEngine.h"

#include "StringUtils.h"

void TextLoader::onHttpRequestImageCompleted(HttpClient *sender, HttpResponse *response)

{

    if (!response)

    {

        return;

    }

    const char* tag  =  response->getHttpRequest()->getTag();

    

    if (0 != strlen(tag))

    {

        log("%s completed", response->getHttpRequest()->getTag());

    }

    

    if (!response->isSucceed())

    {

        log("error buffer: %s", response->getErrorBuffer());

        log("error buffer: %s",this->m_url

            );

        return;

    }

 //检测当前目录下是否存在保存目录,不存在则创建

    DIR *dir = nullptr;

    dir = opendir (this->m_savePath.c_str());

    if (!dir)

    {

        vector<string> v;

        split(this->m_savePath,v,"/");

        string path = FileUtils::getInstance()->getWritablePath();

        for (size_t i = 0; i < v.size(); ++ i)

        {

            path+=v[i];

            path+="/";

            int ret = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);

            if(ret!=0)

            {

                log("fail to createDirectory");

            }

        }

       

    }

    string path = FileUtils::getInstance()->getWritablePath()+this->m_textName;

    FILE*file=fopen(path.c_str(),"w+");

    

    std::vector<char> *buffer = response->getResponseData();

    string data = string(buffer->begin(),buffer->end());

   

    fwrite(data.c_str(), sizeof(char), data.size(), file);

    fclose(file);

    LuaStack* pStack = LuaEngine::getInstance()->getLuaStack();//调用lua回调函数

    //第一个参数是函数的整数句柄,第二个参数是函数参数个数

    pStack->clean();

    

    pStack->executeFunctionByHandler(this->m_callback,0);

}

void TextLoader::doHttpRequest(const char* url,const char* requestData,const char* tag,string textName,string savePath,LUA_FUNCTION callback)

{

    HttpRequest* request = new HttpRequest();

    request->setUrl(url);

    request->setRequestType(HttpRequest::Type::POST);

    

    //这是回调对象和回调函数

    request->setResponseCallback(CC_CALLBACK_2(TextLoader::onHttpRequestImageCompleted, this));

    request->setTag(tag);

    

    //请求的数据

    if(strlen(requestData)!=0){

        request->setRequestData(requestData,strlen(requestData));

    }

    HttpClient::getInstance()->send(request);

    this->m_textName = textName;

    this->m_savePath = savePath;

    this->m_callback = callback;

    strcpy(this->m_url, url);

    //释放内存

    request->release();

}

注意:在lua取栈的回调函数需要使用调用

LUA_FUNCTION handler =  toluafix_ref_function(tolua_S,栈位置,0);

TextLoader* ret = TextLoader::create(arg0, arg1, arg2, arg3, arg4, handler);

自带lua打包工具会使用:

bool ok = luaval_to_int32(tolua_S, 7,(int *)&handler, "TextLoader:create");

笔者试过,不可行。

原文地址:https://www.cnblogs.com/HemJohn/p/4637783.html