login sequence packets

This commit is contained in:
rafi1212122
2023-05-26 20:54:18 +07:00
parent 24f6271036
commit 55b943c6f7
6 changed files with 194 additions and 12 deletions

View File

@@ -1,4 +1,6 @@
namespace Common.Utils
using System.Diagnostics;
namespace Common.Utils
{
public class Logger
{
@@ -39,6 +41,13 @@
Console.ResetColor();
}
public void Trail(params string[] msg)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($"\t→ {string.Join(' ', msg)}");
Console.ResetColor();
}
public void Error(params string[] message)
{
Console.ForegroundColor = ConsoleColor.White;
@@ -53,6 +62,10 @@
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine(string.Join("\t", message));
Console.ResetColor();
#if DEBUG
StackTrace trace = new(true);
Trail(trace.ToString());
#endif
}
public void Debug(params string[] message)

14
GameServer/Game/Player.cs Normal file
View File

@@ -0,0 +1,14 @@
using Common.Database;
namespace PemukulPaku.GameServer.Game
{
public class Player
{
public User.UserScheme User;
public Player(User.UserScheme user)
{
User = user;
}
}
}

View File

@@ -0,0 +1,41 @@
using Common.Resources.Proto;
using Common.Database;
namespace PemukulPaku.GameServer.Handlers
{
[PacketCmdId(CmdId.GetPlayerTokenReq)]
internal class GetPlayerTokenReqHandler : IPacketHandler
{
public void Handle(Session session, Packet _packet)
{
GetPlayerTokenReq Packet = _packet.GetDecodedBody<GetPlayerTokenReq>();
GetPlayerTokenRsp Rsp = new () { };
User.UserScheme? CurrentUser = User.FromToken(Packet.AccountToken);
if (CurrentUser == null || CurrentUser.Uid != uint.Parse(Packet.AccountUid))
{
Rsp.retcode = GetPlayerTokenRsp.Retcode.AccountVerifyError;
Rsp.Msg = "Account verification failed, please re-login!";
}
else
{
session.Player = new Game.Player(CurrentUser);
Rsp = new()
{
retcode = GetPlayerTokenRsp.Retcode.Succ,
Uid = CurrentUser.Uid,
Token = CurrentUser.Token,
AccountType = Packet.AccountType,
AccountUid = Packet.AccountUid,
UserType = 4,
HoyolabAccountUid = Packet.AccountUid,
FightserverIp = 186782306,
FightserverPort = 2096693423
};
}
session.Send(GameServer.Packet.FromProto(Rsp, CmdId.GetPlayerTokenRsp));
}
}
}

View File

@@ -0,0 +1,35 @@
using Common;
using Common.Database;
using Common.Resources.Proto;
using Newtonsoft.Json;
namespace PemukulPaku.GameServer.Handlers
{
[PacketCmdId(CmdId.PlayerLoginReq)]
internal class PlayerLoginReqHandler : IPacketHandler
{
public void Handle(Session session, Packet packet)
{
User.UserScheme User = session.Player.User;
PlayerLoginRsp Rsp = new()
{
retcode = PlayerLoginRsp.Retcode.Succ,
IsFirstLogin = User.IsFirstLogin,
RegionName = Global.config.Gameserver.RegionName,
CgType = User.IsFirstLogin ? CGType.CgStart : CGType.CgSevenChapter,
RegionId = 248,
LoginSessionToken = 1,
PsychoKey = 0
};
session.Send(Packet.FromProto(Rsp, CmdId.PlayerLoginRsp), Packet.FromProto(new GetMpDataRsp()
{
retcode = GetMpDataRsp.Retcode.Succ,
DataType = MpDataType.MpDataPunishTime,
op_type = GetMpDataRsp.OpType.UpdateData,
PunishEndTime = 0
}, CmdId.GetMpDataRsp));
}
}
}

View File

@@ -26,7 +26,7 @@ namespace PemukulPaku.GameServer
if (PacketName == null)
{
c.Error($"CMD ID {CmdId} NOT RECOGNIZED!");
c.Warn($"CmdId {CmdId} not recognized!");
}
HeadMagic = buf.Take(4).ToArray();
@@ -35,20 +35,52 @@ namespace PemukulPaku.GameServer
BodyLen = BinaryPrimitives.ReadUInt32BigEndian(buf.AsSpan(30));
Body = buf.Skip(34 + headerLen).Take((int)BodyLen).ToArray();
TailMagic = buf.Skip(buf.Length - 4).ToArray();
}
public T GetDecodedBody<T>()
{
T SerializedBody = default!;
try
{
MemoryStream ms = new(Body);
object SerializedBody = Serializer.NonGeneric.Deserialize(typeof(Common.Global).Assembly.GetType($"Common.Resources.Proto.{PacketName}"), ms)!;
c.Debug(JsonConvert.SerializeObject(SerializedBody));
SerializedBody = Serializer.Deserialize<T>(ms)!;
}
catch
{
c.Error($"Failed to deserialized packet with Common.Resources.Proto.{PacketName}");
string? PacketName = Enum.GetName(typeof(CmdId), CmdId);
c.Error($"Failed to deserialize {PacketName ?? CmdId.ToString()}!");
}
return SerializedBody;
}
public static Packet FromProto<T>(T proto, CmdId cmdId)
{
MemoryStream stream = new ();
Serializer.Serialize(stream, proto);
byte[] data = stream.ToArray();
byte[] buf = new byte[38 + data.Length];
Array.Fill(buf, (byte)0);
BinaryPrimitives.WriteUInt32BigEndian(buf, 0x1234567);
BinaryPrimitives.WriteUInt16BigEndian(buf.AsSpan(4), 1);
BinaryPrimitives.WriteUInt16BigEndian(buf.AsSpan(6), 0);
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(8), 0);
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(12), 0);
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(16), 0);
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(20), 0);
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(24), (uint)cmdId);
BinaryPrimitives.WriteUInt16BigEndian(buf.AsSpan(28), 0);
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(30), (uint)data.Length);
data.CopyTo(buf.AsSpan(34));
BinaryPrimitives.WriteUInt32BigEndian(buf.AsSpan(34 + data.Length), 0x89abcdef);
return new Packet(buf);
}
}
[AttributeUsage(AttributeTargets.Class)]
public class PacketCmdId : Attribute
{
public CmdId Id { get; }
@@ -90,14 +122,11 @@ namespace PemukulPaku.GameServer
c.Log("Finished Loading Packet Handlers");
}
}
[PacketCmdId(CmdId.PlayerLoginReq)]
public class PlayerLoginReqHandler : IPacketHandler
{
public void Handle(Session session, Packet packet)
public static IPacketHandler? GetPacketHandler(CmdId cmdId)
{
throw new NotImplementedException();
Handlers.TryGetValue(cmdId, out IPacketHandler? handler);
return handler;
}
}
}

View File

@@ -1,5 +1,8 @@
using System.Net.Sockets;
using Common;
using Common.Resources.Proto;
using Common.Utils;
using PemukulPaku.GameServer.Game;
namespace PemukulPaku.GameServer
{
@@ -8,6 +11,7 @@ namespace PemukulPaku.GameServer
public readonly string Id;
public readonly TcpClient Client;
public readonly Logger c;
public Player Player = default!;
public Session(string id, TcpClient client)
{
@@ -96,7 +100,53 @@ namespace PemukulPaku.GameServer
public void ProcessPacket(byte[] packet)
{
_ = new Packet(packet);
Packet _packet = new(packet);
string PacketName = Enum.GetName(typeof(CmdId), _packet.CmdId)!;
try
{
CmdId cmdId = (CmdId)Enum.ToObject(typeof(CmdId), _packet.CmdId);
IPacketHandler? handler = PacketFactory.GetPacketHandler(cmdId);
if (handler == null)
{
c.Warn($"{PacketName} not handled!");
return;
}
c.Log(PacketName);
handler.Handle(this, _packet);
}
catch(Exception ex)
{
if ((int)Global.config.VerboseLevel > 0)
{
c.Error(ex.Message);
}
}
}
public void Send(params Packet[] packets)
{
foreach (Packet packet in packets)
{
string PacketName = Enum.GetName(typeof(CmdId), packet.CmdId)!;
try
{
Client.GetStream().Write(packet.Raw, 0, packet.Raw.Length);
c.Log(PacketName);
}
catch (Exception ex)
{
c.Error($"Failed to send {PacketName}:" + ex.Message);
}
}
}
public override string ToString()
{
return Id;
}
}
}