using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using LitJson; using ProtoCSStruct; using ServerComm; using Sog; using Sog.IO; namespace Game { public class MonitorHandler : BasePacketHandler { public override void Dispose() { } public override int GetServiceType() { return GameServiceType.MonitorHandler; } public override void HandlerClientPacket(PlayerSession playerSession, StructPacket packet) { var player = playerSession.Player; if (player == null) { TraceLog.Error( $"MonitorHandler.HandlerClientPacket player is null sessionid {playerSession.SessionID}"); return; } switch (packet.MsgID) { case (int)CSGameMsgID.GmCmdReq: GetGmCmdInfo(player); break; } } public override void HandlerServerPacket(uint serverID, StructPacket packet) { switch (packet.MsgID) { case (int)SSGameMsgID.OperationMonitorReq: MonitorServer(serverID, packet); break; case (int)SSGameMsgID.SyncPresetRes: PresetSvc.RefreshSyncPresetDataRes(packet); break; case (int)SSGameMsgID.GmUpdateUserRoleReq: MonitorRoleData(serverID, packet); break; case (int)SSGameMsgID.GmImportUserRoleReq: ImportRoleData(serverID, packet); break; } } public static void MonitorServer(uint serverID, StructPacket packet) { var req = packet.GetMessage(); SSOperationMonitorRes res = new SSOperationMonitorRes(); switch (req.Type.ToString()) { case "Command": { var infos = GmCommandMgr.Instance.GetAllGmCmd(); JsonData result = new JsonData(); foreach (var info in infos) { var arg = req.Args.Get(0).GetString(); if (arg != "") { if (!info.Key.ToLower().Contains(arg.ToLower())) { continue; } } JsonData json = new JsonData(); json["cmd"] = info.Key; JsonData param = new JsonData(); for (int i = 0; i < info.Value.Args.Count; i++) { var kv = info.Value.Args[i]; var k = kv.Key; var v = kv.Value; JsonData p = new JsonData { ["type"] = v, ["name"] = k }; param.Add(p); } json["params"] = param.ToJson(); json["desc"] = info.Value.Desc; json["help"] = info.Value.Help; var group = EnumHelper.GetEnumDescription(info.Value.Group); if (string.IsNullOrEmpty(group)) { group = EnumHelper.GetEnumDescription(GmGroup.SERVER); } json["group"] = group; result.Add(json); } BuildRes(ref req, ref res, result.ToJson()); } break; case "OnlineData": { var online = GameServerUtils.GetGameServerData().realmOnline; JsonData result = new JsonData(); var realmIds = GetReqRealmIds(req); foreach (KeyValuePair pair in online) { JsonData json = new JsonData(); if (Array.IndexOf(realmIds, pair.Key) < 0) { continue; } json["realmId"] = pair.Key; json["value"] = pair.Value; result.Add(json); } BuildRes(ref req, ref res, result.ToJson()); } break; case "ServerData": { JsonData result = new JsonData(); result["appId"] = GameServerUtils.GetAppID().ToString(); result["time"] = GameServerUtils.GetTimeSecond().ToString(); BuildRes(ref req, ref res, result.ToJson()); } break; } GameServerUtils.GetPacketSender() .SendToWorldServer((int)SSGameMsgID.OperationMonitorRes, ref res, packet.ObjectID); } private static void GetGmCmdInfo(PlayerOnGame player) { var infos = GmCommandMgr.Instance.GetAllGmCmd(); var res = new CSGmCmdInfoRes(); var allGm = GameServerUtils.GetServerConfig().isAllGM; if (allGm == 0) { var uids = GameServerUtils.GetServerConfig().gmUid; if (!uids.Contains(player.UserID)) { player.SendToClient((int)CSGameMsgID.GmCmdRes, ref res); return; } } foreach (var mapping in infos) { if (mapping.Value.Ignore) { continue; } var cmd = mapping.Key; var info = mapping.Value; var gmCmdInfo = new GMCmdInfo(); gmCmdInfo.Desc.SetString(info.Desc); gmCmdInfo.Cmd.SetString(cmd); var help = info.Help; gmCmdInfo.Help.SetString(help); var group = EnumHelper.GetEnumDescription(info.Group); if (string.IsNullOrEmpty(group)) { group = EnumHelper.GetEnumDescription(GmGroup.SERVER); } gmCmdInfo.Group.SetString(group); foreach (var args in info.Args) { gmCmdInfo.Args.Add(new GMCmdArg() { Name = new FixedStructString32(args.Key), Type = new FixedStructString8(args.Value) }); } res.Api.Add(gmCmdInfo); } player.SendToClient((int)CSGameMsgID.GmCmdRes, ref res); } public static void ImportRoleData(uint serverId, StructPacket packet) { var req = packet.GetMessage(); PlayerOnGame player = GameServerUtils.GetPlayerTableOp().GetPlayerByUid(packet.ObjectID); if (player == null) { //不在线,转到db执行 req.Model = 1; GameServerUtils.GetPacketSender() .SendToWorldServer((int)SSGameMsgID.GmImportUserRoleReq, ref req, packet.ObjectID); return; } player.RoleData = req.Data.RoleData; player.RoleBase = req.Data.RoleBase; player.unimportanceData = req.Data.UnimportanceData; player.DropRateData = req.Data.DropRateData; player.MakeDirty(); var res = new SSGmImportUserRoleInfoRes(); res.Ret = 0; res.Id = req.Id; GameServerUtils.GetPacketSender() .SendToWorldServer((int)SSGameMsgID.GmImportUserRoleRes, ref res, packet.ObjectID); } public static void MonitorRoleData(uint serverID, StructPacket packet) { var req = packet.GetMessage(); var res = new SSGmUpdateUserRoleInfoRes(); res.Id = req.Id; PlayerOnGame player = GameServerUtils.GetPlayerTableOp().GetPlayerByUid(packet.ObjectID); if (player == null) { //不在线,转到db执行 req.Model = 1; GameServerUtils.GetPacketSender() .SendToWorldServer((int)SSGameMsgID.GmUpdateUserRoleReq, ref req, packet.ObjectID); return; } var successful = UpdateRoleData(req, ref player.RoleData); if (!successful) { res.Ret = -1; GameServerUtils.GetPacketSender() .SendToWorldServer((int)SSGameMsgID.GmUpdateUserRoleRes, ref res, packet.ObjectID); return; } player.MakeDirty(); GameServerUtils.GetPacketSender() .SendToWorldServer((int)SSGameMsgID.GmUpdateUserRoleRes, ref res, packet.ObjectID); } private static bool UpdateRoleData(SSGmUpdateUserRoleInfoReq req, ref DBRoleData roleData) { var newValue = req.NewValue.GetString(); string sub = req.SubFieldPath.GetString(); sub = sub.Substring(0, sub.Length - 1); var subNameList = sub.Split("."); if (subNameList.Length == 0) { return false; } if (sub.Contains("[")) { RoleDataHelper.UpdateExistArrayData(ref roleData, subNameList, newValue); return true; } //路径中没有数组 RoleDataHelper.UpdateNoArrayData(ref roleData, subNameList, newValue); return true; } private static void BuildRes(ref SSOperationMonitorReq req, ref SSOperationMonitorRes res, string json) { res.Type = req.Type; res.Id = req.Id; res.Ret = 0; res.Args.CopyFrom(ref req.Args); RepeatedFixedStructString16384_8 rets = new RepeatedFixedStructString16384_8(); rets.Add(json); res.Result.CopyFrom(ref rets); } private static int[] GetReqRealmIds(SSOperationMonitorReq req) { var split = req.Args.Get(1).ToString().Split(","); var ids = new int[split.Length]; for (int i = 0; i < split.Length; i++) { ids[i] = int.Parse(split[i]); } return ids; } } }