首页 > 代码库 > lua学习笔记13:查找并替换文件中关键字
lua学习笔记13:查找并替换文件中关键字
写了一个小工具,拿出分享分享。
先说一下背景吧。
项目中为了重复利用图片资源,把json和图片都放在Resource/ui目录,但有一些图片,比如说道具,是在Resource/images/item下面,
所以,在CocosStudio中编辑完UI之后,需要把资源的相对路径改到item目录,因此就写了这个脚本。
-------------------------------------------------------------------- -- add by 寒风 on 2014/12/26 -- UI中用到了一个资源,路径不在ui下面 -- 此脚本用于矫正资源路径 -------------------------------------------------------------------- local function getFile(file_name) local f,err = io.open(file_name, 'r') if not f then print(err) return nil end local string = f:read("*all") f:close() return string end local function replace(file_name, content, src, dst) content, count = string.gsub(content, src, dst) print(file_name, src, count) return content end local function writeFile(file_name,string) local f = assert(io.open(file_name, 'w+')) f:write(string) f:close() end local function main() local configs = { {"\"path\": \"1jijyd.png\"", "\"path\": \"../images/item/1jijyd.png\""}, {"\"path\": \"chujishengshi.png\"", "\"path\": \"../images/ship/chujishengshi.png\""}, {"\"path\": \"fanpaidazuozhan_0.png\"", "\"path\": \"../images/ui/tiaozhan/fanpaidazuozhan_0.png\""}, {"\"path\": \"guishouzhaofan_0.png\"", "\"path\": \"../images/ui/tiaozhan/guishouzhaofan_0.png\""}, {"\"path\": \"Icon_chongwujinghua.png\"", "\"path\": \"../images/item/Icon_chongwujinghua.png\""}, {"\"path\": \"mofjt.png\"", "\"path\": \"../images/item/mofjt.png\""}, {"\"path\": \"Icon_skill_ATK.png\"", "\"path\": \"../images/ui/feichuanjineng/Icon_skill_ATK.png\""}, {"\"path\": \"Icon_skill_crystal2.png\"", "\"path\": \"../images/ui/feichuanjineng/Icon_skill_crystal2.png\""}, {"\"path\": \"Icon_skill_fire.png\"", "\"path\": \"../images/ui/feichuanjineng/Icon_skill_fire.png\""}, {"\"path\": \"juedizhizhan_0.png\"", "\"path\": \"../images/ui/tiaozhan/juedizhizhan_0.png\""}, {"\"path\": \"miaomiaoweisi1.png\"", "\"path\": \"../images/ui/touxiang/miaomiaoweisi1.png\""}, {"\"path\": \"miaomiaoweisi2.png\"", "\"path\": \"../images/ui/touxiang/miaomiaoweisi2.png\""}, {"\"path\": \"miaomiaoweisi3.png\"", "\"path\": \"../images/ui/touxiang/miaomiaoweisi3.png\""}, {"\"path\": \"miaomiaozhanjian1.png\"", "\"path\": \"../images/ui/touxiang/miaomiaozhanjian1.png\""}, {"\"path\": \"monengshizhu1.png\"", "\"path\": \"../images/ui/touxiang/monengshizhu1.png\""}, {"\"path\": \"shandiandun1.png\"", "\"path\": \"../images/ui/touxiang/shandiandun1.png\""}, } local file_names = { "ui/bangzhu_UI_1.json", "ui/bangzhu_shangdian_UI_1.json", "ui/bangzhu_mojing_UI_1.json", "ui/bangzhu_meiritiaozhan_UI_1.json", "ui/bangzhu_emota_UI_1.json", "ui/bangzhu_chongwupeiyang_UI_1.json", "ui/bangzhu_chongwu_UI_1.json", } for k, file_name in pairs(file_names) do local content = getFile(file_name) if content then for k, configItem in pairs(configs) do content = replace(file_name, content, configItem[1], configItem[2]) end writeFile(file_name, content) end end end main()
file_names中保存的是需要修改路径的json文件;
configs保存的是需要修改资源的原始路径和目标路径。
lua学习笔记13:查找并替换文件中关键字
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。