-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathServerWrappers.cs
205 lines (163 loc) · 4.87 KB
/
ServerWrappers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using CitizenFX.Core.Native;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if MONO_V2
using CitizenFX.Core;
using static CitizenFX.Server.Native.Natives;
namespace CitizenFX.Server
{
public class Player : Shared.Player
{
internal Player(Remote remote)
{
m_handle = remote.GetPlayerHandle();
}
#else
using static CitizenFX.Core.Native.API;
namespace CitizenFX.Core
{
public class Player
{
private string m_handle;
public string Handle => m_handle;
#endif
internal Player(string sourceString)
{
if (sourceString.StartsWith("net:"))
{
sourceString = sourceString.Substring(4);
}
#if IS_FXSERVER
else if (sourceString.StartsWith("internal-net:"))
{
sourceString = sourceString.Substring(13);
}
#endif
m_handle = sourceString;
}
public int Ping => GetPlayerPing(m_handle);
public int LastMsg => GetPlayerLastMsg(m_handle);
public IdentifierCollection Identifiers => new IdentifierCollection(this);
public string EndPoint => GetPlayerEndpoint(m_handle);
public void Drop(string reason) => DropPlayer(m_handle, reason);
#if MONO_V2
public override string Name => GetPlayerName(m_handle);
public override StateBag State => new StateBag("player:" + m_handle);
public Ped Character => Ped.FromPlayerHandle(m_handle);
public override Shared.IPed GetCharacter() => Character;
public static implicit operator Player(Remote remote) => new Player(remote.GetPlayerHandle());
#else
public string Name => GetPlayerName(m_handle);
public StateBag State => new StateBag("player:" + m_handle);
public Ped Character => Ped.FromPlayerHandle(m_handle);
#endif
public void TriggerEvent(string eventName, params object[] args)
{
#if MONO_V2
CoreNatives.TriggerClientEventInternal(eventName, m_handle, args);
#else
var argsSerialized = MsgPackSerializer.Serialize(args);
unsafe
{
fixed (byte* serialized = &argsSerialized[0])
{
Function.Call(Hash.TRIGGER_CLIENT_EVENT_INTERNAL, eventName, m_handle, serialized, argsSerialized.Length);
}
}
#endif
}
public void TriggerLatentEvent(string eventName, int bytesPerSecond, params object[] args)
{
#if MONO_V2
CoreNatives.TriggerLatentClientEventInternal(eventName, m_handle, args, bytesPerSecond);
#else
var argsSerialized = MsgPackSerializer.Serialize(args);
unsafe
{
fixed (byte* serialized = &argsSerialized[0])
{
Function.Call(Hash.TRIGGER_LATENT_CLIENT_EVENT_INTERNAL, eventName, m_handle, serialized, argsSerialized.Length, bytesPerSecond);
}
}
#endif
}
protected bool Equals(Player other) => string.Equals(Handle, other.Handle);
/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != this.GetType())
{
return false;
}
return Equals((Player) obj);
}
/// <inheritdoc />
public override int GetHashCode() => (m_handle != null ? m_handle.GetHashCode() : 0);
public static bool operator ==(Player left, Player right) => Equals(left, right);
public static bool operator !=(Player left, Player right) => !Equals(left, right);
}
public class IdentifierCollection : IEnumerable<string>
{
private Player m_player;
internal IdentifierCollection(Player player)
{
m_player = player;
}
public IEnumerator<string> GetEnumerator()
{
#if MONO_V2
int numIndices = GetNumPlayerIdentifiers(m_player.m_handle);
for (var i = 0; i < numIndices; i++)
{
yield return GetPlayerIdentifier(m_player.m_handle, i);
}
#else
int numIndices = GetNumPlayerIdentifiers(m_player.Handle);
for (var i = 0; i < numIndices; i++)
{
yield return GetPlayerIdentifier(m_player.Handle, i);
}
#endif
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Gets the identifier value of a particular type.
/// </summary>
/// <example>
/// string steamId = player.Identifiers["steam"];
/// </example>
/// <param name="type">The identifier type to return.</param>
/// <returns>The identifier value (without prefix), or null if it could not be found.</returns>
public string this[string type] => this.FirstOrDefault(id => id.Split(':')[0].Equals(type, StringComparison.InvariantCultureIgnoreCase))?.Split(new char[] { ':' }, 2)?.Last();
}
public class PlayerList : IEnumerable<Player>
{
public IEnumerator<Player> GetEnumerator()
{
int numIndices = GetNumPlayerIndices();
for (var i = 0; i < numIndices; i++)
{
yield return new Player(GetPlayerFromIndex(i));
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Player this[int netId] => new Player(netId.ToString());
public Player this[string name] => this.FirstOrDefault(player => player.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
}
}