皇家守卫军1和2中通过资源lua+png的形式。lua中存储着分割png的数据。
因为lua不能将图片切割,然后导出。所以,需要采用python代码来实现。
接下来是实现过程:
1.将所有lua代码改写成python代码
这是lua的数据,改写成python后
require 'os'
require 'engine'
require 'convert'
function find_lua_files(directory)
local files = {}
local handle = io.popen('dir "' .. directory .. '" /b /s')
local result = handle:read("*a")
handle:close()
for file in result:gmatch("[^\r\n]+") do
if file:match("%.lua$") then
table.insert(files, file)
end
end
return files
end
local directory = "F:/freeGame/hjswj_2_iphonehd"
local lua_files = find_lua_files(directory)
for i, file in ipairs(lua_files) do
local filename = file:match("[^/\\]+$")
local newFilename = string.gsub(filename, "%.lua$", "")
print(file,filename,newFilename)
data = dofile(file)
local of_file = io.open("..\\output2\\"..newFilename..".py", "w")
output_table_json(data, of_file, nil, true, weight_tbl,true,"bullet")
end
以上是lua代码。
2.将py文件放入原有的资源中,使用python代码进行解析。
import os,sys from xml.etree import ElementTree from PIL import Image import glob import json def gen_png_from_plist(key,dict,dir,outPath): # dict = bullet_data.bullet[key] # 获取大图 extension = dict["a_name"].split(".")[-1] if extension == "pkm": return big_image = Image.open(dir+"\\"+dict["a_name"]) result_image = Image.new('RGBA', dict["size"], (0, 0, 0, 0)) # 获得小图 result_box = [] result_box.append( dict["f_quad"][0]) result_box.append( dict["f_quad"][1]) result_box.append( dict["f_quad"][0] + dict["f_quad"][2]) result_box.append( dict["f_quad"][1] + dict["f_quad"][3]) rect_on_big = big_image.crop(result_box) left = dict["size"][0] - dict["f_quad"][2] - dict["trim"][2] upper = dict["size"][1] - dict["f_quad"][3] - dict["trim"][3] right = left + dict["size"][0] lower = upper + dict["size"][1] result_image1 = Image.new('RGBA', dict["size"], (0, 0, 0, 0)) result_image1.paste(rect_on_big) result_image.paste(result_image1, (left, upper, right, lower)) # result_image.paste(result_image1) if not os.path.isdir(outPath): os.mkdir(outPath) outfile = (outPath + '/' + key).replace('gift_', '') # print k if outfile.find('.png') == -1: outfile = outfile + '.png' print(outfile, "generated") result_image.save(outfile) for texturename in dict['alias']: outfile = (outPath + '/' + texturename).replace('gift_', '') # print k if outfile.find('.png') == -1: outfile = outfile + '.png' print(outfile, "generated") result_image.save(outfile) # Press the green button in the gutter to run the script. if __name__ == '__main__': if len(sys.argv) < 2: dirName = input("寻找目录: ") else: dirName = sys.argv[1] if len(sys.argv) < 3: outPath = input("输出目录: ") else: outPath = sys.argv[2] python_files = glob.glob(os.path.join(dirName, '*.py')) # 遍历所有Python文件并进行处理 for file in python_files: with open(file, 'r') as f: # 在这里进行你想要的处理操作 # 例如,打印文件内容 print(">>>>>>>>>>filename>"+file) file_content = f.read() data = eval(file_content) #将字符串转成py语言 for key in data.keys(): print(key) gen_png_from_plist(key, data[key],dirName,outPath) 最终结果: