From dc0b92c1e2fe9f3d24c183b325dad54d088735c1 Mon Sep 17 00:00:00 2001 From: hxp <ale99527@vip.qq.com> Date: 星期四, 10 七月 2025 17:01:24 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.1.20:10010/r/Project_SG_ServerCode --- ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py | 62 +++++++++++++++++++++++++++++++ 1 files changed, 62 insertions(+), 0 deletions(-) diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py index 8916fa9..7f0f50b 100644 --- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py +++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py @@ -28,6 +28,9 @@ import subprocess import binascii import GameWorld +import traceback +import shutil +import zlib #--------------------------------------------------------------------- #全局变量 @@ -363,6 +366,65 @@ 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 + +def DelFolder(path, makeDir=False): + ## 删除文件夹 + if os.path.exists(path): + # 删除整个目录(包括自身) + shutil.rmtree(path) + if makeDir: + # 重新创建空目录(保留原目录名) + os.makedirs(path) + return + #提示除零错误的EVAL ## # @param 参数 -- Gitblit v1.8.0