From c63ffd10aecb12b2e09dae603cf9a0f824f6482c Mon Sep 17 00:00:00 2001
From: hxp <ale99527@vip.qq.com>
Date: 星期三, 10 十二月 2025 15:46:15 +0800
Subject: [PATCH] 16 卡牌服务端(合成目标物品与材料物品不在同一背包时支持合成;)
---
ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py | 76 ++++++++++++++++++++++++++++++++++++++
1 files changed, 76 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..78aa02d 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py
@@ -28,6 +28,10 @@
import subprocess
import binascii
import GameWorld
+import traceback
+import shutil
+import zlib
+import json
#---------------------------------------------------------------------
#全局变量
@@ -56,6 +60,16 @@
returnStr += chr(string.atoi(hexStr[i:i+2],16))
return returnStr
+def B2Hex(buf, upper=True, space=True):
+ hex_str = binascii.b2a_hex(buf)
+ # 转换为大写
+ if upper:
+ hex_str = hex_str.upper()
+ # 每两个字符添加一个空格
+ if space:
+ hex_str = ' '.join(hex_str[i:i+2] for i in range(0, len(hex_str), 2))
+ return hex_str
+
#获取异常信息#(try:...except:..GetExceptionInfo())
##
# @param 参数
@@ -363,6 +377,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 参数
@@ -374,6 +447,9 @@
except ZeroDivisionError:
return "Division is Zero"
+def JsonDump(dumpObj):
+ return json.dumps(dumpObj, ensure_ascii=False)
+
##生成指定文件(如par:r'E:\开发版本\Data\logo\formName1.log')
#def MakeAppointFile(par):
# dir = os.path.dirname(par) # 获得文件目录
--
Gitblit v1.8.0