在项目开发中,为了提升开发效率每每须要开发一些辅助工具。最近在公司用lua帮拓展了一个资源扫描的工具,这个工具的功能就是从原始demo下指定目标资源文件,对该文件进行读取并筛选过滤一遍而后拷贝到最终demo对应的文件目录下。工具
咱们知道要读取一个文件必须指定对应路径,而咱们在一个大型游戏软件开发中每一个人所提交上去的资源都是在不一样文件目录下的。因此原先的作法就是手动去把路径一个一个贴出来,整合记录到一个文本中,扫资源的时候先去该文本一行一行的拿路径,ui
根据路径获取目标文件并进行筛选拷贝操做等等。若是一个目录下的资源文件很少还好,要是策划批量上传了一堆特效文件或者贴图的话就苦逼了。qc要一个一个的去贴出路径来,这样的话工做效率就低下了,主要是文件特别多还会出现漏交的时候。lua
一旦漏交就至关于隐匿了一个巨大的炸弹随时会爆炸使游戏崩掉。因此为了解决这个问题,使用lua的lfs.dll帮了大忙(咱们的资源扫描工具是用lua弄的)。spa
改进的想法是在记录路径的file_list.txt文件直接贴目标文件夹的路径,而后去获取改文件夹下全部的资源文件包括全部子目录下的全部文件并写回进file_list.txt。code
具体的实现操做以下图:blog
代码很简单具体实现以下:索引
require "lfs" local file_data = {} add_file_data = function (str) table.insert(file_data, str) end output_file_list = function () local file_list = io.open("file_list.txt", "w+") if file_list then for _, file_path in ipairs(file_data) do file_list:write(file_path) file_list:write("\n") end file_list:flush() file_list:close() end end find_file_indir = function(path, r_table) for file in lfs.dir(path) do if file ~= "." and file ~= ".." then local f = path..'\\'..file local attr = lfs.attributes (f) assert(type(attr) == "table") if attr.mode == "directory" then find_file_indir(f, r_table) else table.insert(r_table, f) end end end end -- demo resource路径 SRC_RES_DIR = "L:\\demo\\" --copy资源扫描文件夹 find_include_file = function(folder_path) local target_path = folder_path target_path = string.gsub(target_path, "\\", "/") if string.match(target_path, "(.-)resource/+") then target_path = string.gsub(target_path, "(.-)resource/", SRC_RES_DIR.."resource/") end local input_table = {} find_file_indir(target_path, input_table) local i=1 while input_table[i]~=nil do local input_path = input_table[i] input_path = string.gsub(input_path, SRC_RES_DIR.."(.-)%.(%w+)", function(route, suffix) local _path = route.."."..suffix return _path end) input_path = string.gsub(input_path, "\\", "/") add_file_data(input_path) i=i+1 end end local file = io.open("file_list.txt", "r") if file then local folder_path = file:read("*l") while (folder_path) do find_include_file(folder_path) folder_path = file:read("*l") end file:close() output_file_list() end
注意:lfs.dll要放在lib文件夹下,但若是你想放其余地方的话,就须要加上它的默认索引环境,加上它的索引环境很简单在require的前面加上以下代码:package.cpath = "..\\你的.dll路径"游戏
例如:package.cpath = "..\\res_copy\\bin\\sys_dll\\?.dll"ip