PSO2SERVER/Server/Models/PSOData.cs
2024-12-10 13:33:57 +08:00

130 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using PSO2SERVER.Json;
using PSO2SERVER.Protocol;
namespace PSO2SERVER.Models
{
// 对象类型枚举
public enum ObjectType : ushort
{
Unknown = 0x0000,
/// 玩家对象。
Player = 0x0004,
/// 区域对象。
Map = 0x0005,
/// 大部分的对象和NPC。
Object = 0x0006,
/// 一些可破坏的物体(例如一些树木)。
StaticObject = 0x0007,
/// 任务对象。11
Quest = 0x000B,
/// 队伍对象。13
Party = 0x000D,
/// 世界地图池中的对象。16
World = 0x0010,
/// 非玩家可控的伙伴。22
APC = 0x0016,
/// 未定义的类型。
Undefined = 0xFFFF
}
// 对象头部结构体
public class ObjectHeader
{
/// 对象的ID。
[JsonProperty("id")]
public uint ID { get; set; } = 0;
[JsonProperty("unk")]
public uint Padding { get; set; } = 0;
[JsonProperty("entity_type")]
[JsonConverter(typeof(ObjectTypeConverter))]
/// 对象的类型。
public ObjectType ObjectType { get; set; } = ObjectType.Unknown;
/// 对象所在的区域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;
}
}
// 只包含ID和类型的构造函数
public ObjectHeader(uint id, ObjectType type)
{
ID = id;
ObjectType = type;
}
// 包含ID、类型和区域ID的构造函数
public ObjectHeader(uint id, ObjectType type, ushort mapid)
{
ID = id;
ObjectType = type;
MapID = mapid;
}
// 从数据流中读取数据并填充到当前结构体
public void ReadObjectHeaderFromStream(PacketReader reader)
{
ID = reader.ReadUInt32(); // 读取对象ID
Padding = reader.ReadUInt32(); // 读取填充部分
ObjectType = (ObjectType)reader.ReadUInt16(); // 读取对象类型
MapID = reader.ReadUInt16(); // 读取区域ID
}
// 将当前结构体的数据写入到数据流
public void WriteObjectHeaderToStream(PacketWriter writer)
{
writer.Write(ID); // 写入对象ID
writer.Write(Padding); // 写入填充部分
writer.Write((ushort)ObjectType); // 写入对象类型
writer.Write(MapID); // 写入区域ID
}
// 获取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);
}
// ToString 方法,用来返回 ObjectHeader 的字符串表示
public override string ToString()
{
return $"ObjectHeader [ID={ID}, Padding={Padding}, ObjectType={ObjectType}, MapID={MapID}]";
}
}
}