cocos2d-x学习笔记cocos2dx 3.10添加lua LuaFileSystem库遍历文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos2d-x学习笔记cocos2dx 3.10添加lua LuaFileSystem库遍历文件相关的知识,希望对你有一定的参考价值。

在lua中遍历目录文件需要用到lfs库,而所用的cocos2dx 3.10没用lfs,需要自己添加

1、下载lfs.clfs.h

https://github.com/keplerproject/luafilesystem

从github获取源码,在src目录拷贝lfs.clfs.h到cocos2d项目下cocos2d\external\lua\luafilesystem(luafilesystem为自己新建的文件夹)


2、在VS解决方案libluacocos2d项目下添加luafilesystem文件夹筛选器并承载lfs.c和lfs.h


3、修改lua_extensions.c文件

lua_extensions.c文件在cocos2d\cocos\scripting\lua-bindings\manual\network\目录下

新增如下两行代码:

#include "luafilesystem/lfs.h"
和
{"lfs", luaopen_lfs},

代码位置如下:

#include "lua_extensions.h"
#if __cplusplus
extern "C" {
#endif
// socket
#include "luasocket/luasocket.h"
#include "luasocket/luasocket_scripts.h"
#include "luasocket/mime.h"
#include "luafilesystem/lfs.h"
static luaL_Reg luax_exts[] = {
    {"socket.core", luaopen_socket_core},
    {"mime.core", luaopen_mime_core},
        {"lfs", luaopen_lfs},
    {NULL, NULL}
};
...


4、发布到安卓要修改android.mk

在cocos2d\cocos\scripting\lua-bindings\proj.android目录下修改Android.mk

新增一行lfs.c路径

../../../../external/lua/luafilesystem/lfs.c

#network
LOCAL_SRC_FILES += ../manual/network/lua_cocos2dx_network_manual.cpp                    ../manual/network/lua_extensions.c                    ../manual/network/Lua_web_socket.cpp                    ../manual/network/lua_xml_http_request.cpp                    ../../../../external/lua/luasocket/auxiliar.c                    ../../../../external/lua/luasocket/buffer.c                    ../../../../external/lua/luasocket/except.c                    ../../../../external/lua/luasocket/inet.c                    ../../../../external/lua/luasocket/io.c                    ../../../../external/lua/luasocket/luasocket.c                    ../../../../external/lua/luasocket/luasocket_scripts.c                    ../../../../external/lua/luasocket/mime.c                    ../../../../external/lua/luasocket/options.c                    ../../../../external/lua/luasocket/select.c                    ../../../../external/lua/luasocket/serial.c                    ../../../../external/lua/luasocket/tcp.c                    ../../../../external/lua/luasocket/timeout.c                    ../../../../external/lua/luasocket/udp.c                    ../../../../external/lua/luasocket/unix.c                    ../../../../external/lua/luasocket/usocket.c                              ../../../../external/lua/luafilesystem/lfs.c

5、lua遍历文件范例(删除指定目录)

require "lfs"
local function removeDir(path)
    print("os.rmdir:", path)
    if cc.FileUtils:getInstance():isDirectoryExist(path) then
        local function _rmdir(path)
            local iter, dir_obj = lfs.dir(path)
            while true do
                local dir = iter(dir_obj)
                if dir == nil then break end
                if dir ~= "." and dir ~= ".." then
                    local curDir = path..dir
                    local mode = lfs.attributes(curDir, "mode")
                    if mode == "directory" then
                        _rmdir(curDir.."/")
                    elseif mode == "file" then
                        cc.FileUtils:getInstance():removeFile(curDir)
                    end
                end
            end
            local succ, des = cc.FileUtils:getInstance():removeDirectory(path)
            if des then print(des) end
            return succ
        end
        _rmdir(path)
    end
    return true
end




以上是关于cocos2d-x学习笔记cocos2dx 3.10添加lua LuaFileSystem库遍历文件的主要内容,如果未能解决你的问题,请参考以下文章

cocos2dx游戏开发学习笔记2-从helloworld開始

cocos2d-x-3.1 国际化strings.xml解决乱码问题 (coco2d-x 学习笔记四)

cocos2dx基础篇 Cocos2D-X项目创建

Cocos2d-x 3.1.1 学习日志12--一Cocos2dx3.1.1移植到Android平台的方法(最实用最有效的!!)

cocos2dx 3.0以上版本搭建Mac环境(百分百可行)

cocos2dx中Node类getParent和getChildByTag()学习体会