cocos2dx-lua 热更新,生成资源md5列表文件

1. cocos2dx-lua 热更新,生成资源md5列表文件

-- file name: make_file_list.lua

-- author : joe
-- version : v1.0
require "lfs"
require "io"

OUT_FILE_NAME = './fileList.lua'

---写入文件
function write2_file(file_name,string)
local f = assert(io.open(file_name, 'w'))
f:write(string)
f:close()
end

--运行DOS命令,查找文件md5  
--------------------
-- fpath 为文件的绝对路径
-----------------------
function get_file_md5( fpath)

    local cmd = "certutil -hashfile "..fpath.." MD5"

    local t = io.popen(cmd)
    local i = 0
    local str_md5
    for line in t:lines() do
    
        i = i+1 
        if i == 2 then
           str_md5 = line
        end
    end


    return str_md5
end

--字符串去前后空格
function trim(s)   
    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))  
end

----------------------------------------------------------------------------------

-- 生成更新文件内容
function list_file_info(param)
-- body
local res = "local files = {}; \n"
local total_size = 0


function list_dir(dir)
for file in lfs.dir(dir) do
local cur_dir = dir
-- print("loop file : " .. file)
if file ~= "." and file ~= ".." then 
-- print("is file exist : " .. cur_dir ..file)
local attr = lfs.attributes(cur_dir ..file)
if attr.mode=="directory" then
cur_dir = cur_dir .. file .. "\\";
list_dir(cur_dir)
elseif attr.mode == "file" then
print("file : " .. cur_dir .. file .. " size : " .. tostring(attr.size / 1024.0 / 1024.0))
print(cur_dir .. file .. " MD5 : " ..  string.gsub(trim( get_file_md5(cur_dir .. file) ), " ", "") .. "\n")
--res = res .. cur_dir .. file .. "\t" .. tostring(   os.date("%Y-%m-%d-%X", attr.modification) ) .. "\t" .. string.gsub(trim( get_file_md5(cur_dir .. file) ), " ", "") .. "\t" .. string.format("%.05f", attr.size / 1024.0 / 1024.0) .. "\n"
local item = '{ ' .. 'name=' .. '\"' ..cur_dir .. file .. '\"'.. '; ' .. 'md5=' .. "\"" .. string.gsub(trim( get_file_md5(cur_dir .. file) ), ' ', '') .. "\"" .. ';' .. 'size=' .. "\"".. tostring(attr.size / 1024.0 / 1024.0) .. "\"" .. "}" .. "\n"
res = res .. "files" .. '[\"' ..cur_dir .. file .. '\"] = ' .. item
-- 需要加载文件大小
total_size = total_size + attr.size
else

end
end
end
end

list_dir(param)
print("==> total_size : " .. total_size)
return res, total_size
end

local res, total_size  = list_file_info("Home\\")
res = res .. '\nreturn files, ' .. total_size .. '\n'
write2_file(OUT_FILE_NAME, res)


2.  测试

>lua

>fs, sz = loadfile("./fileList.lua")

>=fs["文件名"]

>=sz















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值