From c2d45420190e02447a79a680199ab4636ce986e2 Mon Sep 17 00:00:00 2001
From: hxp <ale99527@vip.qq.com>
Date: 星期二, 20 五月 2025 15:49:29 +0800
Subject: [PATCH] 16 卡牌服务端(优化备档存取逻辑,GetServerID改为取接口;调整单服StartDB渠口、启动加载数据流程,支持服层级每秒、每分Process)

---
 ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 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..0a1b958 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/CommFunc.py
@@ -28,6 +28,8 @@
 import subprocess
 import binascii
 import GameWorld
+import traceback
+import zlib
 #---------------------------------------------------------------------
 #全局变量
 
@@ -363,6 +365,55 @@
     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 参数

--
Gitblit v1.8.0