在cocos2d-x quick使用cocosstudo导出的json格式文件配置时候, 由于存在json转lua table这个过程导致加载界面非常的慢 于是想直接使用lua格式的配置效率不是要高很多嘛。于是写了个对应的脚本。
#!/usr/bin/python
#coding=utf-8
#by leaveMeAlone
import json
import os
import os.path
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
rootdir = os.getcwd()
def getSpace(indent):
space = ""
for i in range(0,indent):
space = space + " "
return space
def mwrite(file ,v,indent,k):
mt = type(v)
if k == 0 :
k = ""
else :
if k.find("@xml") !=-1 : #过滤掉xml格式转的json文件的xml描述信息
return
k = str(k)+" = "
# print("type",mt,k,v)
if mt == int or mt ==float :
mstr = getSpace(indent)+k+str(v)+",\n"
file.write(mstr)
elif mt == bool :
mstr = getSpace(indent)+k+str(v).lower()+",\n"
file.write(mstr)
elif mt == unicode:
v=v.replace('"',"'")
mstr = getSpace(indent)+k+'"'+str(v)+'"'+",\n"
file.write(mstr)
elif mt == dict :
mstr = getSpace(indent)+k+"{\n"
file.write(mstr)
for (k,v) in v.items():
mwrite(file,v,indent+1,k)
if indent == 1 :
mstr= getSpace(indent)+"}\n"
else :
mstr= getSpace(indent)+"},\n"
file.write(mstr)
elif mt == list :
mstr = getSpace(indent)+k+"{\n"
file.write(mstr)
for i in v:
mwrite(file,i,indent+1,0)
if indent == 1 :
mstr= getSpace(indent)+"}\n"
else :
mstr= getSpace(indent)+"},\n"
file.write(mstr)
else:
print("else type",mt)
def changefile(infile,outfile):
print(infile)
f = file(infile)
jsonobj = json.load(f)
luaData = open(outfile, 'w')
luaData.write("return\n")
mwrite(luaData,jsonobj,1,0)
f.close
luaData.close
for parent,dirnames,filenames in os.walk(rootdir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for name in filenames:
if name.endswith(".json"):
newname = os.path.splitext(name)[0]
title = "outlua"
new_path = os.path.join(rootdir, title)
if not os.path.isdir(new_path):
os.makedirs(new_path)
output =new_path+'/'+newname+".lua"
changefile(name,output)