Cocos2dx-lua webp

前言

本文仅供学习参考,请勿将其用于任何违法操作。本文中所涉及的技术和方法仅用于解析和理解游戏开发中可能存在的漏洞和调试技巧,并非用于破坏游戏平衡、修改游戏数据或进行其他违法行为。我们强烈呼吁读者遵守法律法规,尊重游戏开发者的劳动成果和游戏规则,并在合法范围内进行学习和研究。如因不当使用本文所述方法导致任何违法行为或造成任何不良后果,责任将完全由使用者自行承担。

问题

某次学习某游戏包体资源时候发现资源格式为png但是打开却显示不出来。很奇怪 该游戏采用2dx xxtea加密 并且我也已经解密 最后发现图片头信息中有

RIFF@? WEBPVP8X

这一行关键词 百度一番后发现这是webp格式 通过修改后缀 .webp 即可打开预览

webp转换png jpg

市面上有很多工具可以批量转换 我推荐使用XnConvert免费又好用

思考我们怎么使用到游戏中

  1. 通过XnConvert每次手动去转换后使用

  1. 编写批处理集成到工作流中
#-*- coding: utf-8 -*-
import xxtea
import os
from PIL import Image
import sys

# 文件后缀
file_ext = [
    '.png',
    '.jpg'
]

ErrorDict = {}


def png2webp(input_path, output_path):
    """Converts a png image to a webp image"""
    try:
      im = Image.open(input_path) 
      if im.mode == "P":
        im = im.convert('RGBA')
        print("mode:"+ im.mode + " path=>" +  output_path)
        #return
      im.save(output_path, 'webp')
      #print("success=>"+output_path)
    except:
      ErrorDict[input_path] = 1
    
def batch_convert(input_dir, output_dir):
    """Converts all png images in a directory to webp"""
    totalFile = 0
    files = os.walk(input_dir)
    for path, dir_list, file_list in files:
        for directory in dir_list:
            relevant_path = os.path.relpath(os.path.join(path, directory), input_dir)
            new_path = os.path.join(output_dir, relevant_path) 
            if os.path.isfile(new_path):
                os.remove(new_path)
            if not os.path.isdir(new_path):
                os.mkdir(new_path) 
                
        for filename in file_list: 
          # 原文件绝对路径
          orig_path = os.path.join(path, filename) 
          # 源文件相对路径,方便计算解密文件路径
          relevant_path = os.path.relpath(orig_path, input_dir)
          # 解密文件绝对路径
          new_path = os.path.join(output_dir, relevant_path)
          new_path = os.path.join(output_dir, relevant_path)
          if filename.endswith('.png') or filename.endswith('.jpg'): 
            #output_filename = os.path.splitext(filename)[0] + '.webp'
            #output_path = os.path.join(new_path, output_filename)
            totalFile += 1
            png2webp(orig_path, new_path)
          else:
             orig_file = open(orig_path, "rb")
             encrypt_bytes = orig_file.read()
             orig_file.close() 
             new_file = open(new_path, "wb")
             new_file.write(encrypt_bytes)
             new_file.close()
             
    print("convert success num :" + str(totalFile))
    for word, count in ErrorDict.items():
      print("convert error path:" + word)
 
if __name__ == '__main__':
	# Example usage:
    if len( sys.argv ) < 2:
      inDir = raw_input("Enter your in DirName: ")
    if len( sys.argv ) < 3:
      outDir = raw_input("Enter your out DirName: ")
    else:
      inDir = sys.argv[1]
      outDir = sys.argv[2]
      #print(inDir,outDir)
    #batch_convert('H:\\unpackLua\\pythonUnpckLuac\\out', 'H:\\unpackLua\\pythonUnpckLuac\\out2')
    batch_convert(inDir,outDir)

`
链接:https://pan.baidu.com/s/1KLfrluK9NSoRlfFTANCofg?pwd=wato 提取码:wato ` —

  1. 使cocos2dx-lua 引擎支持渲染(目前cocos2dx-3.13已经支持)

做好以上几步编译引擎即可打包apk进行测试


以下文章参考

Webp容器规范