gitea_ap_art_assets/check_compress_psd.py

260 lines
8.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/python
#encoding:utf-8
import os, sys
curr = os.path.dirname(sys.argv[0])
if curr != "":
os.system("cd " + curr)
from psd_tools import PSDImage
from PIL import Image
import hashlib
import utils
import export_outline
import json
def parse_psd(path):
global scale
global versions
file_name = os.path.split(path)[1]
log = []
log.append("开始检测:{}".format(file_name))
psd = PSDImage.open(path)
try:
image = psd.composite()
except Exception as e:
log.append("文件无法打开" + repr(e))
has_base = False
has_finish = False
lines = {}
patch_stages = {}
auto_stages = {}
#patch的末尾数字
patch_end = []
line_end = []
max_width = 0
max_height = 0
is_activity_main_psd = path.find("activity/main_psd/") != -1
for item in psd.descendants():
if not item.is_group():
nArr = item.name.split('_')
#在活动背景中基础图为base_1
if item.name == "base" or (is_activity_main_psd and item.name == "base_1"):
max_width = item.width
max_height = item.height
break
for item in psd.descendants():
if not item.is_group():
nArr = item.name.split('_')
if item.name.strip() != item.name:
log.append("命名中存在空格:{}".format(item.name))
if not is_activity_main_psd and nArr[0] == 'base':
if has_base:
log.append("存在多个base图层")
has_base = True
elif not is_activity_main_psd and nArr[0] == 'finish':
if has_finish:
log.append("存在多个finish图层")
has_finish = True
elif nArr[0] == 'line':
#以前存在line_1整图轮廓的情况这种情况视为无line
if len(nArr) > 2:
if nArr[1] not in lines:
lines[nArr[1]] = []
if nArr[2] in lines[nArr[1]]:
log.append("存在重复图层:{}".format(item.name))
lines[nArr[1]].append(nArr[2])
else:
pass
line_end.append(item.name.replace("line", ""))
elif nArr[0] == 'patch' or nArr[0] == 'patchrp':
if nArr[1] not in patch_stages:
patch_stages[nArr[1]] = []
patch_stages[nArr[1]].append(nArr[2])
patch_end.append(item.name.replace("patch", ""))
elif nArr[0] == 'auto':
if nArr[1] not in auto_stages:
auto_stages[nArr[1]] = []
auto_stages[nArr[1]].append(nArr[2])
elif nArr[0] == 'thumbnail' or nArr[0] == 'role' or nArr[0] == 'rptmp':
pass
elif is_activity_main_psd and nArr[0] == 'entry':
if len(nArr) != 4:
log.append("活动子点位,命名错误:{}".format(item.name))
elif is_activity_main_psd and nArr[0] == 'base':
if nArr[1] == '1':
has_base = True
else:
log.append("命名错误:{}\n".format(item.name))
#空图层检测
if item.width <= 0 or item.height <= 0:
log.append("存在空图层:{}\n".format(item.name))
#横坐标越界检测
if item.offset[0] < 0 or (item.offset[0] + item.width) > max_width:
log.append("横坐标越界{}\n".format(item.name))
#纵坐标越界检测
if item.offset[1] < 0 or (item.offset[1] + item.height) > max_height:
log.append("纵坐标越界{}\n".format(item.name))
if not has_base:
log.append("未检测到base图层\n")
if not is_activity_main_psd and not has_finish:
log.append("未检测到finish图层\n")
for stage in patch_stages:
tmp = {}
for item in patch_stages[stage]:
if item in tmp:
log.append("存在重复图层patch_{}_{}".format(stage, item))
else:
tmp[item] = 0
for stage in auto_stages:
tmp = {}
for item in auto_stages[stage]:
if item in tmp:
log.append("存在重复图层auto_{}_{}".format(stage, item))
else:
tmp[item] = 0
if not is_activity_main_psd:
if len(lines) > 0 and len(patch_stages) != len(lines):
log.append("阶段与线稿不对应,线稿:{} 关卡:".format(len(lines), len(patch_stages)))
for item in patch_end:
if item not in line_end:
log.append("patch图层找不到描线patch{}".format(item))
for item in line_end:
if item not in patch_end:
log.append("描线找不到patch图层line{}".format(item))
log.append("检测结束")
if len(log) == 2:
return True
else:
for a_log in log:
print(a_log)
return False
def convert_2_zip_file(path, is_activity_psd):
file_info_arr = os.path.split(path)
file_path = file_info_arr[0]
file_name = os.path.split(path)[1]
#检测成功压缩到zip
os.chdir(file_path)
file_name_without_ex = file_name.split(".")[0]
utils.zip_file("{}.psd".format(file_name_without_ex), "{}.zip".format(file_name_without_ex))
os.chdir("../")
if is_activity_psd:
os.chdir("../")
os.remove(path)
###生成version文件
def make_version_file(remake):
version_file = 'version.json';
if remake:
if os.path.exists(version_file):
os.remove(version_file)
if not os.path.exists(version_file):
with open(version_file, 'w') as f:
json.dump({}, f, sort_keys=True, indent=4)
###加载版本文件
def load_version():
global versions
make_version_file(False);
with open('version.json', 'r') as f:
try:
versions = json.load(f)
except:
#删除版本文件并重新生成
make_version_file(True);
def write_version():
with open('version.json', 'w') as f:
json.dump(versions, f, sort_keys=True, indent=4)
if __name__ == '__main__':
load_version()
deal_psdid = []
is_force = sys.argv[1] if len(sys.argv) > 1 else False
for root , dirs, files in os.walk('./'):
for name in files:
src_file = os.path.join(root, name)
if name.endswith('.psd') or name.endswith('.zip'):
file_info_arr = os.path.split(src_file)
file_path = file_info_arr[0]
file_name = file_info_arr[1]
key = file_name.split("_")[0]
try:
i_key = int(key)
except Exception as e:
print("资源命名错误,请查询是否存在特殊字符:{}".format(name))
continue
if key not in deal_psdid:
deal_psdid.append(key)
else:
print("存在重复关卡:{}".format(key))
if name.endswith('.psd') or (name.endswith('.zip') and is_force):
file_info_arr = os.path.split(src_file)
file_path = file_info_arr[0]
file_name = file_info_arr[1]
key = file_name.split("_")[0]
hash_old = ""
if key in versions:
hash_old = versions[key]
if os.path.exists(src_file):
hash_now = ""
if name.endswith('.psd'):
hash_now = utils.calc_hash(src_file)
if hash_now != hash_old or is_force:
psd_file = src_file
if name.endswith('.zip'):
utils.unzip_file(src_file)
psd_file = "{}.psd".format(os.path.splitext(psd_file)[0])
hash_now = utils.calc_hash(psd_file)
try:
ret = parse_psd(psd_file)
is_activity_psd = psd_file.find("activity/main_psd/") != -1 or psd_file.find("activity/level_psd/") != -1
if ret:
#检测成功压缩到zip
convert_2_zip_file(psd_file, is_activity_psd)
versions[key] = hash_now
write_version()
except Exception as e:
print(name + " 解析失败:" + repr(e) + "请检查命名规范比如auto_1_1=>auto_1 或 patch_1_1=>patch_1")
elif hash_now == hash_old:
is_activity_psd = src_file.find("activity/main_psd/") != -1
convert_2_zip_file(src_file, is_activity_psd)