| | |
| | | #"""Version = 2025-05-09 12:20"""
|
| | | #-------------------------------------------------------------------------------
|
| | |
|
| | | import CommFunc
|
| | | import GameWorld
|
| | | import PyGameData
|
| | | import DBEventTrig
|
| | |
| | | import DBFamily
|
| | | import DBMail
|
| | |
|
| | | import binascii
|
| | | import time
|
| | | import zlib
|
| | | import os
|
| | |
|
| | | BakRoot = "C:\ServerRealTimeBackup"
|
| | | BakFileType = ".bak"
|
| | | BackupCopyMax = 3
|
| | |
|
| | | #临时变量,之后优化
|
| | | g_loadErr = False
|
| | | Map2ServerID = {
|
| | | 10010:87,
|
| | | 10090:89,
|
| | | }
|
| | |
|
| | | def OnServerStart():
|
| | | GameWorld.DebugLog("地图服务器启动")
|
| | |
| | | def LoadServerDataBackup():
|
| | | ## 服务器公共数据备档加载
|
| | | global g_loadErr
|
| | | mapID = GameWorld.GetMap().GetMapID()
|
| | | if mapID not in Map2ServerID:
|
| | | return
|
| | | serverName = "S%s" % Map2ServerID[mapID]
|
| | | serverID = GameWorld.GetGameWorld().GetServerID()
|
| | | serverName = "S%s" % serverID
|
| | | BakDir = os.path.join(BakRoot, serverName)
|
| | | GameWorld.Log("加载备档: %s" % BakDir)
|
| | | bakFileList = GetBakFileSortList(BakDir)
|
| | |
| | | GameWorld.Log("读取备档: %s" % bakFilePath)
|
| | |
|
| | | f = open(bakFilePath, 'rb')
|
| | | compressed_data = f.read().strip()
|
| | | compressed_data = f.read()
|
| | | f.close()
|
| | |
|
| | | try:
|
| | | decompressed_data = zlib.decompress(compressed_data)
|
| | | bakData = binascii.a2b_hex(decompressed_data)
|
| | | except:
|
| | | bakData = CommFunc.Decompress(compressed_data)
|
| | | if not bakData:
|
| | | g_loadErr = True
|
| | | raise
|
| | |
|
| | |
| | | def ServerDataBackup():
|
| | | ## 服务器公共数据定时备档,暂时直接存盘
|
| | | if g_loadErr:
|
| | | GameWorld.ErrLog("加载备档已异常,暂时不在存储备档")
|
| | | GameWorld.ErrLog("加载备档已异常,暂时不再存储备档,防止已经错误加载的数据覆盖")
|
| | | return
|
| | | mapID = GameWorld.GetMap().GetMapID()
|
| | | if mapID not in Map2ServerID:
|
| | | return
|
| | | serverName = "S%s" % Map2ServerID[mapID]
|
| | | serverID = GameWorld.GetGameWorld().GetServerID()
|
| | | serverName = "S%s" % serverID
|
| | | BakDir = os.path.join(BakRoot, serverName)
|
| | | BackupCopyMax = 3
|
| | | GameWorld.Log("服务器备档: %s" % serverName)
|
| | |
|
| | | if not os.path.exists(BakDir):
|
| | |
| | | bakFilePath = os.path.join(BakDir, "%s_%s%s" % (serverName, curTime, BakFileType))
|
| | | bakData = GetSavePyData()
|
| | | GameWorld.Log("Bak:%s, len=%s, %s" % (serverName, len(bakData), bakFilePath))
|
| | | compressed_data = CommFunc.Compress(bakData)
|
| | | if not compressed_data:
|
| | | GameWorld.SendGameError("ServerDataBackupError")
|
| | | return
|
| | | GameWorld.Log("compress len=%s" % len(compressed_data))
|
| | | |
| | | try:
|
| | | compressed_data = zlib.compress(bakData, 9) #最大压缩
|
| | | GameWorld.Log("compress len=%s" % len(compressed_data))
|
| | | fp = open(bakFilePath, "wb")
|
| | | fp.write(compressed_data)
|
| | | fp.close()
|
| | |
| | | pyGameDataMgr = GetDBDataMgr()
|
| | | result = pyGameDataMgr.GetSaveData()
|
| | | GameWorld.Log("GetSavePyData!! id = %s-%s"%(id(pyGameDataMgr), len(result)))
|
| | | result = binascii.b2a_hex(result)
|
| | | #GameWorld.DebugLog("GetSavePyData!! result = %s-%s"%(result, len(result)))
|
| | | # 字节码在C++转化会发生错误must be string without null bytes, not str,但是可以正常保存,错误会在下次调用便宜接口才会触发
|
| | | # 暂时改成字符串返回,暂无解决方案
|
| | | return result
|
| | | # return str(len(result)) + "|" + result
|
| | |
|
| | | def LoadPyGameData(gameBuffer, pos):
|
| | | pyGameDataMgr = GetDBDataMgr()
|