用python将.apk解压出来的.atlas文件转成.plist文件

用python将.apk解压出来的.atlas文件转成.plist文件

本篇文章我会把代码直接免费贡献出来,如果转载请说明出处,毕竟创作不易,谢谢理解与支持~
因为要用到另外一个项目的图片,但是解压出来以后全是.atlas文件,不能直接使用,然后用ide打开文件看了一下这两个文件的区别,首先是.atlas文件
在这里插入图片描述
我们来和.plist文件对比一下就一目了然了
在这里插入图片描述

在这里插入图片描述
明白原理以后,就是字符串和IO操作了,接来下要怎么搞呢,当然是人生苦短,我选python了,因为CocosCreator要配置python2.7.5的环境,所以直接上菜鸟教程.看一下基本语法和文件读写就直接开搞了,老规矩,直接上代码,拷贝直接用( ̄▽ ̄)"

#_*_coding:utf-8_*_ 
import os

# 遍历所有根目录文件
result = []
def get_all(cwd):
    get_dir = os.listdir(cwd)  
    for path in get_dir:          
        sub_dir = os.path.join(cwd,path)  
        if os.path.isdir(sub_dir):     
            get_all(sub_dir)
        else:
            result.append(cwd + '/'+ path)

# 处理单个atlas
def handleAtlas(atlas):
    lines = atlas.split('\n')
    list = []
    repeatName = {}
    for index in range(len(lines)):
        line = lines[index].replace(' ','').replace('\n','')

        if line.find('.png') != -1:
            list.append({'name': line})
            continue

        if line == '':
            continue
        
        if (line.find(':') == -1):
            if repeatName.has_key(line):
                repeatName[line] += 1
            else:
                repeatName[line] = 0

            if repeatName[line] > 0:
                line += '%d'%repeatName[line]
            list.append({'name': line})
            continue
        
        if (len(list) == 0):
            continue

        if (line.find('size:') != -1):
            dict = list[len(list) - 1]
            dict['size'] = line.replace('size:','')
        elif (line.find('rotate:') != -1):
            dict = list[len(list) - 1]
            dict['rotate'] = line.replace('rotate:','')
        elif (line.find('xy:') != -1):
            dict = list[len(list) - 1]
            dict['xy'] = line.replace('xy:','')
        elif (line.find('orig:') != -1):
            dict = list[len(list) - 1]
            dict['orig'] = line.replace('orig:','')
        elif (line.find('offset:') != -1):
            dict = list[len(list) - 1]
            dict['offset'] = line.replace('offset:','')

    data = list[0]
    head = '<key>metadata</key>\n'
    head += '<dict>\n'
    head += '<key>format</key>\n'
    head += '<integer>2</integer>\n'
    head += '<key>realTextureFileName</key>\n'
    head += '<string>{name}</string>\n'
    head += '<key>size</key>\n'
    head += '<string>{{{size}}}</string>\n'
    head += '<key>textureFileName</key>\n'
    head += '<string>{name}</string>\n'
    head += '</dict>\n'
    head = head.format(name=data['name'], size=data['size'])
    name = data['name'].replace('.png','');

    frames = ''
    list.remove(data)
    for data in list:
        frame = '<key>{name}.png</key>\n'
        frame += '<dict>\n'
        frame += '<key>frame</key>\n'
        frame += '<string>{{{{{xy}}},{{{size}}}}}</string>\n'
        frame += '<key>offset</key>\n'
        frame += '<string>{{{offset}}}</string>\n'
        frame += '<key>rotated</key>\n'
        frame += '<{rotate}/>\n'
        frame += '<key>sourceColorRect</key>\n'
        frame += '<string>{{{{{xy}}},{{{size}}}}}</string>\n'
        frame += '<key>sourceSize</key>\n'
        frame += '<string>{{{size}}}</string>\n'
        frame += '</dict>\n'
        frames += frame.format(name=data['name'], xy=data['xy'],size=data['size'],rotate=data['rotate'],offset=data['offset'])

    plist = '<?xml version="1.0" encoding="UTF-8"?>\n'
    plist += '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
    plist += '<plist version="1.0">\n'
    plist += '<dict>\n'

    plist += '<key>frames</key>\n'
    plist += '<dict>\n'
    plist += frames
    plist += '</dict>\n'

    plist += head + '\n'

    plist += '</dict>\n'
    plist += '</plist>\n'

    return {'plist': plist,'name': name}

# 处理单个altas文件
def handle(path):
    f = open(path,"r")

    atlasList = []
    for line in f.readlines():
        if line.find('.png') != -1:
            atlasList.append(line);
        elif len(atlasList) > 0:
            atlasList[len(atlasList) - 1] += line

    for atlas in atlasList:
        data = handleAtlas(atlas)
        fo = open(os.path.join(os.path.dirname(path) , data['name'] + ".plist"), "w")
        fo.write(data['plist']) 

# main
get_all('./')
for path in result:
    if path.lower().endswith('.atlas'):
        handle(path)

使用方法:新建一个parse.py,然后把代码拷进去,把所有的.atlas文件和parse.py放到同一目录下,然后在当前目录输入cmd打开命令窗口,输入:python parse.py运行这个python文件,然后就可以看到所有转换成功的.plist文件了,最后把.plist文件和所对应的.png文件一起拖入项目中就可以直接使用了。
在这里插入图片描述
哪里不会的可以私信我讲解一下

另附上源码下载

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页