在項目開發中,為了提高開發效率往往需要開發一些輔助工具。最近在公司用lua幫拓展了一個資源掃描的工具,這個工具的功能就是從原始demo下指定目標資源文件,對該文件進行讀取並篩選過濾一遍然后拷貝到最終demo對應的文件目錄下。
我們知道要讀取一個文件必須指定對應路徑,而我們在一個大型游戲軟件開發中每個人所提交上去的資源都是在不同文件目錄下的。所以原先的做法就是手動去把路徑一個一個貼出來,整合記錄到一個文本中,掃資源的時候先去該文本一行一行的拿路徑,
根據路徑獲取目標文件並進行篩選拷貝操作等等。如果一個目錄下的資源文件不多還好,要是策划批量上傳了一堆特效文件或者貼圖的話就苦逼了。qc要一個一個的去貼出路徑來,這樣的話工作效率就低下了,主要是文件特別多還會出現漏交的時候。
一旦漏交就相當於隱匿了一個巨大的炸彈隨時會爆炸使游戲崩掉。所以為了解決這個問題,使用lua的lfs.dll幫了大忙(我們的資源掃描工具是用lua弄的)。
改進的想法是在記錄路徑的file_list.txt文件直接貼目標文件夾的路徑,然后去獲取改文件夾下所有的資源文件包括所有子目錄下的所有文件並寫回進file_list.txt。
具體的實現操作如下圖:



代碼很簡單具體實現如下:
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"
