修不好修不好 好烦

This commit is contained in:
Longfeng Qin 2024-09-20 16:10:43 +08:00
parent b25486bb46
commit 1bb52cc659
14 changed files with 182 additions and 40 deletions

View File

@ -704,7 +704,7 @@ namespace PSO2SERVER
var fakeChar = new Character
{
CharacterID = 12345678 + new Random().Next(),
Character_ID = 12345678 + new Random().Next(),
Player = fakePlayer,
Name = playerName,
Looks = client.Character.Looks,

View File

@ -261,12 +261,12 @@ namespace PSO2SERVER.Models
[Key]
public int Id { get; set; }
public int CharacterID { get; set; }
public int player_id { get; set; }
public uint unk1 { get; set; }
public uint voice_type { get; set; }
public ushort unk2 { get; set; }
public short voice_pitch { get; set; }
public int Character_ID { get; set; }
public int Player_ID { get; set; }
public uint Unk1 { get; set; }
public uint Voice_Type { get; set; }
public ushort Unk2 { get; set; }
public short Voice_Pitch { get; set; }
public string Name { get; set; }
@ -288,7 +288,7 @@ namespace PSO2SERVER.Models
public LooksParam Looks { get; set; }
public uint unk3 { get; set; }
public uint Unk3 { get; set; }
public byte[] JobsBinary
{
@ -298,7 +298,6 @@ namespace PSO2SERVER.Models
w.WriteStruct(Jobs);
return w.ToArray();
}
set
{
Jobs = Helper.ByteArrayToStructure<JobParam>(value);
@ -308,7 +307,7 @@ namespace PSO2SERVER.Models
public JobParam Jobs { get; set; }
public string unk4 { get; set; }
public string Unk4 { get; set; }
public virtual Player Player { get; set; }
}

View File

@ -0,0 +1,25 @@
using System;
using PSO2SERVER.Models;
using PSO2SERVER.Packets.PSOPackets;
namespace PSO2SERVER.Packets.Handlers
{
[PacketHandlerAttr(0x0E, 0x28)]
class PlayerIsBusyState : PacketHandler
{
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
if (context.Character == null)
return;
//var info = string.Format("[<--] 接收到的数据 (hex): ");
//Logger.WriteHex(info, data);
foreach (var c in Server.Instance.Clients)
{
if (c.Character == null || c.CurrentZone != context.CurrentZone)
continue;
c.SendPacket(new NewBusyStatePacket(context.User.PlayerId, BusyState.Busy));
}
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using PSO2SERVER.Models;
using PSO2SERVER.Packets.PSOPackets;
namespace PSO2SERVER.Packets.Handlers
{
[PacketHandlerAttr(0x0E, 0x29)]
class PlayerIsNotBusyState : PacketHandler
{
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
if (context.Character == null)
return;
//var info = string.Format("[<--] 接收到的数据 (hex): ");
//Logger.WriteHex(info, data);
foreach (var c in Server.Instance.Clients)
{
if (c.Character == null || c.CurrentZone != context.CurrentZone)
continue;
c.SendPacket(new NewBusyStatePacket(context.User.PlayerId, BusyState.NotBusy));
}
}
}
}

View File

@ -1,6 +1,7 @@
using PSO2SERVER.Database;
using PSO2SERVER.Packets.PSOPackets;
using PSO2SERVER.Party;
using System;
using System.Linq;
namespace PSO2SERVER.Packets.Handlers
@ -28,28 +29,37 @@ namespace PSO2SERVER.Packets.Handlers
var reader = new PacketReader(data, position, size);
var pkt = reader.ReadStruct<CharacterSelectedPacket>();
var charId = pkt.CharId;
//Logger.Write("id {0}", charId);
if (context.Character == null) // On character create, this is already set.
{
using (var db = new ServerEf())
{
var character = db.Characters.Where(c => c.CharacterID == pkt.CharId).First();
if (character == null || character.Player.PlayerId != context.User.PlayerId)
try
{
Logger.WriteError("数据库中未找到 {0} 角色ID {1} ({2})"
, context.User.Username
, pkt.CharId
, context.User.PlayerId
var character = db.Characters.FirstOrDefault(c => c.Character_ID == charId);
if (character == null || character.Player == null || character.Player.PlayerId != context.User.PlayerId)
{
Logger.WriteError("数据库中未找到 {0} 角色ID {1} ({2})"
, context.User.Username
, charId
, context.User.PlayerId
);
context.Socket.Close();
return;
context.Socket.Close();
return;
}
context.Character = character;
}
catch (Exception ex)
{
Logger.WriteError("查询角色时发生异常: {0}", ex.Message);
context.Socket.Close();
}
context.Character = character;
}
}
// 将客户端加入空余的队伍中

View File

@ -50,12 +50,12 @@ namespace PSO2SERVER.Packets.Handlers
if (existingCharacters.Count > 0)
{
// Increment ID if characters already exist
newCharacter.CharacterID = existingCharacters.Max(c => c.CharacterID) + 1;
newCharacter.Character_ID = existingCharacters.Max(c => c.Character_ID) + 1;
}
else
{
// Start with ID 1 if no characters exist
newCharacter.CharacterID = 1;
newCharacter.Character_ID = 1;
}
//Logger.Write("newCharacter.CharacterId {0} {1}", newCharacter.CharacterId, context.User.PlayerId);

View File

@ -2,6 +2,7 @@
using System.IO;
using PSO2SERVER.Packets.PSOPackets;
using PSO2SERVER.Database;
using static PSO2SERVER.Packets.PSOPackets.CharacterDeletionPacket;
namespace PSO2SERVER.Packets.Handlers
{
@ -22,7 +23,7 @@ namespace PSO2SERVER.Packets.Handlers
{
foreach (var character in db.Characters)
if (character.CharacterID == id)
if (character.Character_ID == id)
{
db.Characters.Remove(character);
db.ChangeTracker.DetectChanges();
@ -31,12 +32,14 @@ namespace PSO2SERVER.Packets.Handlers
// Detect the deletion and save the Database
if (db.ChangeTracker.HasChanges())
{
db.SaveChanges();
}
}
// Disconnect for now
// TODO: What do we do after a deletion?
context.Socket.Close();
context.SendPacket(new CharacterDeletionPacket(DeletionStatus.Success));
//context.SendPacket(new CharacterDeletionPacket(DeletionStatus.UndeletableItems));
}
#endregion

View File

@ -22,11 +22,11 @@ namespace PSO2SERVER.Packets.Handlers
foreach (var client in ServerApp.Instance.Server.Clients)
{
if (client.Character.CharacterID == id)
if (client.Character.Character_ID == id)
{
var infoPacket = new GuildInfoPacket(context.Character);
context.SendPacket(infoPacket);
Logger.Write("[NFO] Sent guild info to " + client.Character.CharacterID);
Logger.Write("[NFO] Sent guild info to " + client.Character.Character_ID);
break;
}
}

View File

@ -6,11 +6,21 @@ using System.Text;
namespace PSO2SERVER.Packets.PSOPackets
{
public enum BusyState
{
NotBusy,
Busy,
}
public class NewBusyStatePacket : Packet
{
private int _user_playerid;
private BusyState _state;
public NewBusyStatePacket()
public NewBusyStatePacket(int user_playerid, BusyState state)
{
_user_playerid = user_playerid;
_state = state;
}
#region implemented abstract members of Packet
@ -18,6 +28,8 @@ namespace PSO2SERVER.Packets.PSOPackets
public override byte[] Build()
{
var pkt = new PacketWriter();
pkt.WriteStruct(new ObjectHeader((uint)_user_playerid, EntityType.Player));
pkt.Write((uint)_state);
return pkt.ToArray();
}

View File

@ -42,7 +42,7 @@ namespace PSO2SERVER.Packets.PSOPackets
{
var chars = db.Characters
.Where(w => w.Player.PlayerId == _PlayerId)
.OrderBy(o => o.CharacterID) // TODO: Order by last played
.OrderBy(o => o.Character_ID) // TODO: Order by last played
.Select(s => s);
writer.Write((uint)chars.Count()); // Number of characters
@ -52,20 +52,21 @@ namespace PSO2SERVER.Packets.PSOPackets
foreach (var ch in chars)
{
writer.Write((uint)ch.CharacterID);
writer.Write((uint)ch.Character_ID);
writer.Write((uint)_PlayerId);
for (var i = 0; i < 0x10; i++)
writer.Write((byte)0);
writer.WriteFixedLengthUtf16(ch.Name, 16);
//Logger.WriteInternal("[CHR] 新增名为 {0} 的新角色.", ch.Name);
writer.Write((uint)0);
writer.WriteStruct(ch.Looks); // Note: Pre-Episode 4 created looks doesn't seem to work anymore
writer.WriteStruct(ch.Jobs);
for (var i = 0; i < 0xFC; i++)
writer.Write((byte)3);
for (var i = 0; i < 0x94; i++)
writer.Write((byte)0);
}
}

View File

@ -8,9 +8,67 @@ namespace PSO2SERVER.Packets.PSOPackets
{
public class CharacterDeletionPacket : Packet
{
public DeletionStatus Status { get; }
public CharacterDeletionPacket()
public enum DeletionStatus : uint
{
/// <summary>
/// Character has items which prevent deletion.
/// </summary>
UndeletableItems,
/// <summary>
/// Character has been scheduled for deletion.
/// </summary>
Success
}
public struct ItemId
{
/// <summary>
/// Item type.
/// </summary>
public ushort ItemType { get; set; }
/// <summary>
/// Item category.
/// </summary>
public ushort Id { get; set; }
/// <summary>
/// Item ID after appraisal.
/// </summary>
public ushort Unk3 { get; set; }
/// <summary>
/// Item ID.
/// </summary>
public ushort SubId { get; set; }
}
public struct CharacterDeletePacket
{
/// <summary>
/// Deletion request status.
/// </summary>
public DeletionStatus Status { get; set; }
public uint Unk1 { get; set; }
public List<ItemId> Unk2 { get; set; }
public List<ItemId> Unk3 { get; set; }
public List<ItemId> Unk4 { get; set; }
public List<ItemId> Unk5 { get; set; }
public List<ItemId> Unk6 { get; set; }
}
public CharacterDeletionPacket(DeletionStatus status)
{
Status = status;
}
#region implemented abstract members of Packet
@ -18,12 +76,15 @@ namespace PSO2SERVER.Packets.PSOPackets
public override byte[] Build()
{
var pkt = new PacketWriter();
pkt.Write((uint)Status);
pkt.Write((uint)0);
pkt.WriteMagic(0, 0x33D4, 0xC4);
return pkt.ToArray();
}
public override PacketHeader GetHeader()
{
return new PacketHeader(0x11, 0x08, PacketFlags.None);
return new PacketHeader(0x11, 0x08, PacketFlags.PACKED);
}
#endregion

View File

@ -24,7 +24,7 @@ namespace PSO2SERVER.Packets.PSOPackets
writer.Write((byte) 0);
// Character ID
writer.Write((uint) _character.CharacterID);
writer.Write((uint) _character.Character_ID);
// Padding?
for (var i = 0; i < 4; i++)

View File

@ -139,11 +139,14 @@ namespace PSO2SERVER.Packets
public unsafe void WriteStruct<T>(T structure) where T : struct
{
var strArr = new byte[Marshal.SizeOf(structure)];
int size = Marshal.SizeOf(structure);
//Logger.Write($"Writing {size} bytes of structure: {structure}");
var strArr = new byte[size];
fixed (byte* ptr = strArr)
{
Marshal.StructureToPtr(structure, (IntPtr) ptr, false);
Marshal.StructureToPtr(structure, (IntPtr)ptr, false);
}
Write(strArr);

View File

@ -190,6 +190,8 @@
<Compile Include="Packets\Handlers\0B-QuestHandler\0B-19-QuestDifficultyRequestHandler.cs" />
<Compile Include="Packets\Handlers\0B-QuestHandler\0B-30-QuestCounterHandler.cs" />
<Compile Include="Packets\Handlers\--UNK.cs" />
<Compile Include="Packets\Handlers\0E-PartyHandler\0E-29-PlayerIsNotBusyState.cs" />
<Compile Include="Packets\Handlers\0E-PartyHandler\0E-28-PlayerIsBusyState.cs" />
<Compile Include="Packets\Handlers\0E-PartyHandler\0E-19-UNK.cs" />
<Compile Include="Packets\Handlers\11-ClientHandler\11-06-DeleteCharacter.cs" />
<Compile Include="Packets\Handlers\11-ClientHandler\11-0D-PingTimestampResponse.cs" />