| | |
| | | import subprocess
|
| | | import binascii
|
| | | import GameWorld
|
| | | import traceback
|
| | | import zlib
|
| | | #---------------------------------------------------------------------
|
| | | #全局变量
|
| | |
|
| | |
| | | hStr="0x"+str(red+green+blue)
|
| | | return hStr
|
| | |
|
| | | def Compress(data, use_hex=False):
|
| | | """压缩数据
|
| | | @param use_hex: 可选十六进制转换(非必要不建议)导致压缩效率降低(体积可能翻倍),且增加转换出错风险。
|
| | | @return: None or compresseData
|
| | | """
|
| | | try:
|
| | | if use_hex:
|
| | | data = binascii.b2a_hex(data)
|
| | | compresseData = zlib.compress(data, 9)
|
| | | return compresseData
|
| | | except:
|
| | | GameWorld.ErrLog('压缩数据 - > %s' % str(traceback.format_exc()))
|
| | | return
|
| | | |
| | | def Decompress(compressed_data, expect_hex=False):
|
| | | """解压数据,处理格式和完整性检查
|
| | | @param use_hex: 可选十六进制转换,与压缩参数一致
|
| | | @return: None or data
|
| | | """
|
| | | |
| | | # 检查最小长度(zlib 头部至少 2 字节)
|
| | | if len(compressed_data) < 2:
|
| | | GameWorld.ErrLog("解压失败 -> 压缩数据长度异常.")
|
| | | return
|
| | | |
| | | try:
|
| | | # 尝试标准 zlib 解压
|
| | | data = zlib.decompress(compressed_data)
|
| | | except zlib.error:
|
| | | GameWorld.ErrLog("解压失败(尝试调整参数).")
|
| | | try:
|
| | | # 尝试 GZIP 格式
|
| | | data = zlib.decompress(compressed_data, wbits=31)
|
| | | except zlib.error:
|
| | | # 尝试原始 DEFLATE
|
| | | data = zlib.decompress(compressed_data, wbits=-15)
|
| | | |
| | | if expect_hex:
|
| | | try:
|
| | | if len(data) % 2 != 0:
|
| | | GameWorld.ErrLog("解压失败 -> 十六进制字符串长度非偶数.")
|
| | | return
|
| | | return binascii.a2b_hex(data)
|
| | | except TypeError:
|
| | | GameWorld.ErrLog("解压失败 -> 无效的十六进制数据.")
|
| | | return
|
| | | |
| | | return data
|
| | |
|
| | | #提示除零错误的EVAL
|
| | | ##
|
| | | # @param 参数
|