From 58bc2b1894584733a134b741402a7a9ac6908ecc Mon Sep 17 00:00:00 2001 From: Somebody Date: Thu, 6 Jun 2024 18:07:26 +0800 Subject: [PATCH] for debug log --- Common/Configuration/ConfigContainer.cs | 3 +- GameServer/Server/Connection.cs | 49 ++++++++++++++++--- .../Recv/Player/HandlerPlayerGetTokenCsReq.cs | 3 ++ Program/Program/EntryPoint.cs | 7 +-- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/Common/Configuration/ConfigContainer.cs b/Common/Configuration/ConfigContainer.cs index 1d42af7d..fdc1a2c7 100644 --- a/Common/Configuration/ConfigContainer.cs +++ b/Common/Configuration/ConfigContainer.cs @@ -73,6 +73,7 @@ namespace EggLink.DanhengServer.Configuration public ServerAnnounce ServerAnnounce { get; set; } = new ServerAnnounce(); public ServerProfile ServerProfile { get; set; } = new ServerProfile(); public bool AutoCreateUser { get; set; } = true; + public bool SavePersonalDebugFile { get; set; } = false; } public class ServerAnnounce @@ -103,6 +104,6 @@ namespace EggLink.DanhengServer.Configuration public class MuipServerConfig { - public string AdminKey { get; set; } = ""; + public string AdminKey { get; set; } = "None"; } } diff --git a/GameServer/Server/Connection.cs b/GameServer/Server/Connection.cs index 646608a1..4d49f31f 100644 --- a/GameServer/Server/Connection.cs +++ b/GameServer/Server/Connection.cs @@ -25,10 +25,12 @@ public partial class Connection public static readonly List BANNED_PACKETS = []; public bool IsOnline = true; private static readonly Logger Logger = new("GameServer"); -#if DEBUG public static readonly Dictionary LogMap = []; public static readonly List IgnoreLog = [CmdIds.PlayerHeartBeatCsReq, CmdIds.PlayerHeartBeatScRsp, CmdIds.SceneEntityMoveCsReq, CmdIds.SceneEntityMoveScRsp, CmdIds.GetShopListCsReq, CmdIds.GetShopListScRsp]; -#endif + + public string DebugFile = ""; + public StreamWriter? writer = null; + public Connection(KcpConversation conversation, IPEndPoint remote) { Conversation = conversation; @@ -57,8 +59,7 @@ public partial class Connection IsOnline = false; } -#if DEBUG - public static void LogPacket(string sendOrRecv, ushort opcode, byte[] payload) + public void LogPacket(string sendOrRecv, ushort opcode, byte[] payload) { try { @@ -75,14 +76,46 @@ public partial class Connection #pragma warning restore CS8600 JsonFormatter? formatter = JsonFormatter.Default; string? asJson = formatter.Format(packet); - Logger.Debug($"{sendOrRecv}: {LogMap[opcode.ToString()]}({opcode})\r\n{asJson}"); - } catch + var output = $"{sendOrRecv}: {LogMap[opcode.ToString()]}({opcode})\r\n{asJson}"; +#if DEBUG + Logger.Debug(output); +#endif + if (DebugFile != "" && ConfigManager.Config.ServerOption.SavePersonalDebugFile) + { + StreamWriter? sw = GetWriter(); + sw.WriteLine($"[{DateTime.Now:HH:mm:ss}] [GameServer] [DEBUG] " + output); + sw.Flush(); + } + } + catch { - Logger.Debug($"{sendOrRecv}: {LogMap[opcode.ToString()]}({opcode})"); + var output = $"{sendOrRecv}: {LogMap[opcode.ToString()]}({opcode})"; +#if DEBUG + Logger.Debug(output); +#endif + if (DebugFile != "" && ConfigManager.Config.ServerOption.SavePersonalDebugFile) + { + StreamWriter? sw = GetWriter(); + sw.WriteLine($"[{DateTime.Now:HH:mm:ss}] [GameServer] [DEBUG] " + output); + sw.Flush(); + } } } -#endif + private StreamWriter GetWriter() + { + // Create the file if it doesn't exist + var file = new FileInfo(DebugFile); + if (!file.Exists) + { + Directory.CreateDirectory(file.DirectoryName!); + File.Create(DebugFile).Dispose(); + } + + writer ??= new StreamWriter(DebugFile, true); + return writer; + } + private async Task ReceiveLoop() { while (!CancelToken.IsCancellationRequested) diff --git a/GameServer/Server/Packet/Recv/Player/HandlerPlayerGetTokenCsReq.cs b/GameServer/Server/Packet/Recv/Player/HandlerPlayerGetTokenCsReq.cs index d8d6b729..b395ce31 100644 --- a/GameServer/Server/Packet/Recv/Player/HandlerPlayerGetTokenCsReq.cs +++ b/GameServer/Server/Packet/Recv/Player/HandlerPlayerGetTokenCsReq.cs @@ -5,6 +5,8 @@ using EggLink.DanhengServer.Database.Player; using EggLink.DanhengServer.Game.Player; using EggLink.DanhengServer.Proto; using EggLink.DanhengServer.Server.Packet.Send.Player; +using EggLink.DanhengServer.Util; +using Microsoft.EntityFrameworkCore; namespace EggLink.DanhengServer.Server.Packet.Recv.Player { @@ -36,6 +38,7 @@ namespace EggLink.DanhengServer.Server.Packet.Recv.Player connection.Player = new PlayerInstance(int.Parse(req.AccountUid)); else connection.Player = new PlayerInstance(pd); + connection.DebugFile = Path.Combine(ConfigManager.Config.Path.LogPath, "Debug/", $"{req.AccountUid}/", $"Debug-{DateTime.Now:yyyy-MM-dd HH-mm-ss}.log"); connection.Player.OnLogin(); connection.Player.Connection = connection; connection.SendPacket(new PacketPlayerGetTokenScRsp(connection)); diff --git a/Program/Program/EntryPoint.cs b/Program/Program/EntryPoint.cs index a3e1c526..2f918d23 100644 --- a/Program/Program/EntryPoint.cs +++ b/Program/Program/EntryPoint.cs @@ -153,9 +153,8 @@ namespace EggLink.DanhengServer.Program var elapsed = DateTime.Now - time; logger.Info($"Done in {elapsed.TotalSeconds.ToString()[..4]}s! Type '/help' to get help of commands."); -#if DEBUG GenerateLogMap(); -#endif + if (GetConfig().ServerOption.EnableMission) { logger.Warn("Mission system is enabled. This is a feature that is still in development and may not work as expected. If you encounter any issues, please report them to the developers."); @@ -177,8 +176,6 @@ namespace EggLink.DanhengServer.Program DatabaseHelper.SaveDatabase(); } -#if DEBUG - private static void GenerateLogMap() { // get opcode from CmdIds @@ -190,7 +187,5 @@ namespace EggLink.DanhengServer.Program Connection.LogMap.Add(value.ToString(), name); } } - -#endif } }