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保存的是需要修改资源的原始路径和目标路径。

时间: 2024-08-15 04:34:22

lua学习笔记13:查找并替换文件中关键字的相关文章

【Node.js学习笔记八】package.json文件中使用的指令

指令 说明 示例 name 包的唯一名字 "name": "camelot" preferGlobal 表示该模块更倾向于在全局范围内安装 "preferGlobal": true version 该模块的版本 "version": 0.0.1 author 项目的作者 "author": "[email protected]" description 模块的文字说明(描述) "

python 3.x 学习笔记13 (socket_ssh and socket_文件传输)

ssh服务端 import socket,os server = socket.socket() server.bind(('localhost',6666)) server.listen() conn, addr = server.accept() while True: print('等待新数据!') cmd = conn.recv(1024) if len(cmd) == 0: break data = os.popen(cmd.decode()).read() cmd_size = le

lua学习笔记13:协程具体解释和举例

一.coroutine.create创建协程 參数是协程的主函数,返回一个thread对象 co = coroutine.create(function() print("coroutine execute!") end) 二.协程状态 协程有三种状态:挂起(suspended).执行(running)和死亡(dead) coroutine.status(co)返回协程当前的状态 协程创建完之后处于挂起状态 print(coroutine.status(co)) 输出: suspend

Linux学习笔记——例说makefile 头文件查找路径

0.前言 从学习C语言开始就慢慢开始接触makefile,查阅了很多的makefile的资料但总感觉没有真正掌握makefile,如果自己动手写一个makefile总觉得非常吃力.所以特意借助博客总结makefile的相关知识,通过例子说明makefile的具体用法. 例说makefile大致分为4个部分 1.只有单个C文件 2.含有多个C文件 3.需要包括头文件路径 4.一个较为复杂的例子 [代码仓库]--makefile-example 代码仓库位于bitbucket,可借助Tortoise

Lua学习笔记9:多文件

一 终端中执行多个文件:-l 加入在文件一中定义了一个变量,在另一文件中输出这个变量,代码如下: --file1.lua num = 100 --file2.lua print(num) 终端输入(注意:不是lua命令行): lua -lfile1 -lfile2 注意:不要加上文件后缀名.lua 二 命令行中加载文件 --lib.lua function norm(x, y) local n2 = x^2 + y^2 return math.sqrt(n2) end function twic

Lua学习笔记8:文件读写

lua中文件读写经常在游戏配置中用到,比如客户端的音效音乐开关等. Lua官方API文档:点这里 I/O库为文件操作提供4个主要函数:io.open(),io.read(),io.write和io.close(). io.open(文件路径,打开方式):以指定方式打开一个文件,打开成功返回一个文件句柄,失败返回nil和错误描述. 可以传入以下六种打开方式: "r":读模式(默认): "w":写模式: "a":附加模式: "r+"

Lua学习笔记(七):迭代器与泛型for

1.迭代器与闭包 迭代器是一种支持指针类型的结构,它可以遍历集合的每一个元素.在Lua中我们常常使用函数来描述迭代器,每次调用该函数就返回集合的下一个元素. 迭代器需要保留上一次成功调用的状态和下一次成功调用的状态,也就是他知道来自于哪里和将要前往哪里.闭包提供的机制可以很容易实现这个任务.记住:闭包是一个内部函数,它可以访问一个或者多个外部函数的外部局部变量.每次闭包的成功调用后这些外部局部变量都保存他们的值(状态).当然如果要创建一个闭包必须要创建其外部局部变量.所以一个典型的闭包的结构包含

Lua学习笔记(八):数据结构

table是Lua中唯一的数据结构,其他语言所提供的数据结构,如:arrays.records.lists.queues.sets等,Lua都是通过table来实现,并且在Lua中table很好的实现了这些数据结构. 1.数组 在Lua中通过整数下标访问table中元素,既是数组,并且数组大小不固定,可动态增长.通常我们初始化数组时,就间接地定义了数组的大小,例如: 1 a = {} -- new array 2 for i=1, 1000 do 3 a[i] = 0 4 end 5 6 --数

python基础教程_学习笔记13:标准库:一些最爱——sys

标准库:一些最爱 sys sys这个模块让你能够访问与python解释器联系紧密的变量和函数. sys模块中一些重要的函数和变量 函数/变量 描述 argv 命令行参数,包括脚本名称 exit([arg]) 退出当前程序,可选参数为给定的返回值或者错误信息 modules 映射模块名字到载入模块的字典 path 查找模块所在目录的目录名列表 platform 类似sunos5或者win32的平台标识符 stdin 标准输入流--一个类文件对象 stdout 标准输出流--一个类文件对象 stde