溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

cocos2d-x學(xué)習(xí)筆記(十二)cocos2dx 3.10添加lua LuaFileSystem庫遍歷文件

發(fā)布時間:2020-07-23 15:08:13 來源:網(wǎng)絡(luò) 閱讀:2355 作者:wty530 欄目:游戲開發(fā)

在lua中遍歷目錄文件需要用到lfs庫,而所用的cocos2dx 3.10沒用lfs,需要自己添加

1、下載lfs.c和lfs.h

https://github.com/keplerproject/luafilesystem

從github獲取源碼,在src目錄拷貝lfs.c和lfs.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、發(fā)布到安卓要修改Android.mk

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

新增一行l(wèi)fs.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




向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI