PSO2SERVER/Server/Models/PSOData.cs

130 lines
4.0 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using PSO2SERVER.Json;
using PSO2SERVER.Protocol;
2024-09-10 00:31:40 +08:00
2024-09-10 01:13:20 +08:00
namespace PSO2SERVER.Models
2024-09-10 00:31:40 +08:00
{
2024-11-25 23:33:41 +08:00
// 对象类型枚举
public enum ObjectType : ushort
2024-09-10 00:31:40 +08:00
{
2024-11-25 23:33:41 +08:00
Unknown = 0x0000,
/// 玩家对象。
Player = 0x0004,
/// 区域对象。
Map = 0x0005,
/// 大部分的对象和NPC。
Object = 0x0006,
/// 一些可破坏的物体(例如一些树木)。
StaticObject = 0x0007,
/// 任务对象。11
Quest = 0x000B,
/// 队伍对象。13
Party = 0x000D,
/// 世界地图池中的对象。16
World = 0x0010,
/// 非玩家可控的伙伴。22
APC = 0x0016,
/// 未定义的类型。
2024-09-21 13:11:22 +08:00
Undefined = 0xFFFF
2024-09-10 00:31:40 +08:00
}
2024-11-25 23:33:41 +08:00
// 对象头部结构体
public class ObjectHeader
2024-09-10 00:31:40 +08:00
{
2024-11-25 23:33:41 +08:00
/// 对象的ID。
[JsonProperty("id")]
public uint ID { get; set; } = 0;
[JsonProperty("unk")]
public uint Padding { get; set; } = 0;
[JsonProperty("entity_type")]
[JsonConverter(typeof(ObjectTypeConverter))]
2024-11-25 23:33:41 +08:00
/// 对象的类型。
public ObjectType ObjectType { get; set; } = ObjectType.Unknown;
2024-11-25 23:33:41 +08:00
/// 对象所在的区域ID。玩家对象没有设置这个字段。
[JsonProperty("map_id")]
public ushort MapID { get; set; } = 0;
//初始化结构体
public ObjectHeader()
{;
}
public struct ObjHeaderStruct
{
public uint ID { get; set; }
public uint Padding { get; set; }
public ObjectType ObjectType { get; set; }
public ushort MapID { get; set; }
// 结构体构造函数
public ObjHeaderStruct(uint id, uint padding, ObjectType objectType, ushort mapID)
{
ID = id;
Padding = padding;
ObjectType = objectType;
MapID = mapID;
}
// 结构体构造函数
public ObjHeaderStruct(uint id, ObjectType objectType)
{
ID = id;
Padding = 0;
ObjectType = objectType;
MapID = 0;
}
}
2024-09-10 00:31:40 +08:00
2024-11-25 23:33:41 +08:00
// 只包含ID和类型的构造函数
public ObjectHeader(uint id, ObjectType type)
2024-09-10 00:31:40 +08:00
{
2024-09-21 13:11:22 +08:00
ID = id;
ObjectType = type;
}
2024-11-25 23:33:41 +08:00
// 包含ID、类型和区域ID的构造函数
public ObjectHeader(uint id, ObjectType type, ushort mapid)
2024-09-21 13:11:22 +08:00
{
ID = id;
ObjectType = type;
MapID = mapid;
2024-09-10 00:31:40 +08:00
}
2024-11-25 23:33:41 +08:00
// 从数据流中读取数据并填充到当前结构体
2024-12-06 19:47:18 +08:00
public void ReadObjectHeaderFromStream(PacketReader reader)
2024-09-22 11:14:48 +08:00
{
2024-11-25 23:33:41 +08:00
ID = reader.ReadUInt32(); // 读取对象ID
Padding = reader.ReadUInt32(); // 读取填充部分
2024-11-25 23:33:41 +08:00
ObjectType = (ObjectType)reader.ReadUInt16(); // 读取对象类型
MapID = reader.ReadUInt16(); // 读取区域ID
2024-09-22 11:14:48 +08:00
}
2024-11-25 23:33:41 +08:00
// 将当前结构体的数据写入到数据流
2024-12-06 19:47:18 +08:00
public void WriteObjectHeaderToStream(PacketWriter writer)
2024-09-22 11:14:48 +08:00
{
2024-11-25 23:33:41 +08:00
writer.Write(ID); // 写入对象ID
writer.Write(Padding); // 写入填充部分
2024-11-25 23:33:41 +08:00
writer.Write((ushort)ObjectType); // 写入对象类型
writer.Write(MapID); // 写入区域ID
2024-09-22 11:14:48 +08:00
}
// 获取ObjHeaderStruct结构体
public ObjHeaderStruct ToObjHeaderStruct()
{
return new ObjHeaderStruct(ID, Padding, ObjectType, MapID);
}
// 从ObjHeaderStruct结构体构造ObjectHeader
public static ObjectHeader FromObjHeaderStruct(ObjHeaderStruct objHeaderStruct)
{
return new ObjectHeader(objHeaderStruct.ID, objHeaderStruct.ObjectType, objHeaderStruct.MapID);
}
2024-12-10 13:33:57 +08:00
// ToString 方法,用来返回 ObjectHeader 的字符串表示
public override string ToString()
{
return $"ObjectHeader [ID={ID}, Padding={Padding}, ObjectType={ObjectType}, MapID={MapID}]";
}
2024-09-10 00:31:40 +08:00
}
}