Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Source.CodeAnalysis.ConVars/ConVarRegistrationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ static void Analyze(Symbols s, INamedTypeSymbol type, List<TypeModel> models, Li
break;
}
case IMethodSymbol method: {
AttributeData? attribute = FindAttribute(method, s.ConCommandAttribute);
if (attribute == null) {
AttributeData[] attributes = method.GetAttributes().Where(a => DerivesFrom(a.AttributeClass, s.ConCommandAttribute)).ToArray();
if (attributes.Length == 0) {
for (IMethodSymbol? overridden = method.OverriddenMethod; overridden != null; overridden = overridden.OverriddenMethod) {
if (FindAttribute(overridden, s.ConCommandAttribute) != null) {
candidates = true;
Expand All @@ -271,12 +271,14 @@ static void Analyze(Symbols s, INamedTypeSymbol type, List<TypeModel> models, Li
}

candidates = true;
Member? member = AnalyzeCommand(s, type, method, attribute, nameable, isStruct);
if (member == null)
reflect = true;
else {
member.Id = nextId++;
model.Members.Add(member);
foreach (AttributeData attribute in attributes) {
Member? member = AnalyzeCommand(s, type, method, attribute, nameable, isStruct);
if (member == null)
reflect = true;
else {
member.Id = nextId++;
model.Members.Add(member);
}
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Source.Common/Commands/ConCommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Indicates the method will be constructed as a concommand.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ConCommandAttribute : Attribute {
public readonly string? Name;
public readonly string? HelpText;
Expand Down
51 changes: 26 additions & 25 deletions Source.Engine/EngineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,35 +555,36 @@ static void ConVar_RegisterByReflection(IEngineAPI engineAPI, ICvar cvar, Type t

for (int i = 0; i < methods.Length; i++) {
MethodInfo method = methods[i];
ConCommandAttribute? cmdAttr = method.GetCustomAttribute<ConCommandAttribute>();
if (cmdAttr == null)
ConCommandAttribute[] cmdAttrs = [.. method.GetCustomAttributes<ConCommandAttribute>()];
if (cmdAttrs.Length == 0)
continue;

ConCommandAttribute attribute = method.GetCustomAttribute<ConCommandAttribute>()!; // < never null!
object? instance = method.IsStatic ? null : DetermineInstance(engineAPI, type, true, method.Name);

// Lets see if we can find a FnCommandCompletionCallback...
FnCommandCompletionCallback? completionCallback = null;
if (attribute.AutoCompleteMethod != null)
type.TryExtractMethodDelegate(instance, x => x.Name == attribute.AutoCompleteMethod, out completionCallback);

// Construct a new ConCommand
string cmdName = attribute.Name ?? method.Name;
string? helpText = attribute.HelpText;
FCvar flags = attribute.Flags;

ConCommand cmd;

if (method.TryToDelegate<FnCommandCallbackVoid>(instance, out var callbackVoid))
cmd = new ConCommand(cmdName, callbackVoid, helpText, flags, completionCallback);
else if (method.TryToDelegate<FnCommandCallback>(instance, out var callback))
cmd = new ConCommand(cmdName, callback, helpText, flags, completionCallback);
else if (method.TryToDelegate<FnCommandCallbackSourced>(instance, out var callbackSourced))
cmd = new ConCommand(cmdName, callbackSourced, helpText, flags, completionCallback);
else
throw new ArgumentException("Cannot dynamically produce ConCommand with the arguments we were given");

cvar.RegisterConCommand(cmd);
foreach (ConCommandAttribute attribute in cmdAttrs) {
// Lets see if we can find a FnCommandCompletionCallback...
FnCommandCompletionCallback? completionCallback = null;
if (attribute.AutoCompleteMethod != null)
type.TryExtractMethodDelegate(instance, x => x.Name == attribute.AutoCompleteMethod, out completionCallback);

// Construct a new ConCommand
string cmdName = attribute.Name ?? method.Name;
string? helpText = attribute.HelpText;
FCvar flags = attribute.Flags;

ConCommand cmd;

if (method.TryToDelegate<FnCommandCallbackVoid>(instance, out var callbackVoid))
cmd = new ConCommand(cmdName, callbackVoid, helpText, flags, completionCallback);
else if (method.TryToDelegate<FnCommandCallback>(instance, out var callback))
cmd = new ConCommand(cmdName, callback, helpText, flags, completionCallback);
else if (method.TryToDelegate<FnCommandCallbackSourced>(instance, out var callbackSourced))
cmd = new ConCommand(cmdName, callbackSourced, helpText, flags, completionCallback);
else
throw new ArgumentException("Cannot dynamically produce ConCommand with the arguments we were given");

cvar.RegisterConCommand(cmd);
}
}
}
}
Expand Down
244 changes: 242 additions & 2 deletions Source.Engine/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ public void GMOD_BuildConVarUpdateMessageBF(bf_write buf, FCvar flags, bool nonD
#endif

[ConCommand(helpText: "Exits the engine")]
[ConCommand("exit", "Exit the engine.")]
void quit(in TokenizedCommand args) {
#if !SWDS
if (!args.FindArg("prompt").IsEmpty) {
Expand All @@ -1481,8 +1482,247 @@ void quit(in TokenizedCommand args) {
HostState.Shutdown();
}

[ConCommand("exit", "Exit the engine.")]
void exit(in TokenizedCommand args) => quit(in args);
static ulong atoui64(ReadOnlySpan<char> str) {
ulong val = 0;
int pos = 0;

if (str.Length > 1 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
pos = 2;
while (pos < str.Length) {
char c = str[pos++];
if (c >= '0' && c <= '9')
val = (val << 4) + c - '0';
else if (c >= 'a' && c <= 'f')
val = (val << 4) + c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
val = (val << 4) + c - 'A' + 10;
else
return val;
}
return val;
}

if (str.Length > 0 && str[0] == '\'')
return str.Length > 1 ? str[1] : 0ul;

while (pos < str.Length) {
char c = str[pos++];
if (c < '0' || c > '9')
return val;
val = val * 10 + c - '0';
}

return val;
}

static string KickMessage(in TokenizedCommand args, int first) {
StringBuilder message = new();
for (int i = first; i < args.ArgC(); i++) {
if (i > first)
message.Append(' ');
message.Append(args[i]);
}

if (message.Length > 511)
message.Length = 511;

return message.ToString();
}

[ConCommand("kickid", "Kick a player by userid or uniqueid, with a message.")]
void kickid(in TokenizedCommand args, CommandSource source, int clientSlot) {
if (!sv.IsActive()) {
ConMsg("No server running, noone to kick.\n");
return;
}

if (args.ArgC() <= 1) {
ConMsg("Usage: kickid < userid | steamid | steamid64 > { message }\n");
return;
}

string searchString = "";
string message;
ulong id = atoui64(args[1]);
if (id != 0)
message = KickMessage(in args, 2);
else if (args.ArgC() >= 6 && args[1].StartsWith("STEAM_", StringComparison.OrdinalIgnoreCase) && args[2].Contains(':') && args[4].Contains(':')) {
searchString = $"{args[1]}:{args[3]}:{args[5]}";
message = KickMessage(in args, 6);
}
else {
searchString = new(args[1]);
message = KickMessage(in args, 2);
}

if (searchString.Length > 127)
searchString = searchString[..127];

bool steamID64 = (id >> 32) != 0;
EUniverse universe = EUniverse.k_EUniversePublic;
if (steamID64) {
Source.Common.Steam.ISteamUtils? steamUtils = Steam3Client().SteamUtils();
if (steamUtils != null)
universe = steamUtils.GetConnectedUniverse();
}

IClient? client = null;
int i;
for (i = 0; i < sv.GetClientCount(); i++) {
client = sv.GetClient(i)!;

if (!client.IsConnected())
continue;

if (client.IsHLTV())
continue;

if (id != 0) {
if (steamID64) {
if (sv.GetPlayerInfo(i, out PlayerInfo info)) {
if (info.FriendsID == 0 && !info.FakePlayer)
break;

CSteamID steamID = new(new AccountID_t(info.FriendsID), universe, info.FakePlayer ? EAccountType.k_EAccountTypeAnonGameServer : EAccountType.k_EAccountTypeIndividual);
if (steamID.m_SteamID == id)
break;
}
}
else if (client.GetUserID() == (long)id)
break;
}
else if (stricmp(client.GetNetworkIDString(), searchString) == 0)
break;
}

if (i < sv.GetClientCount()) {
if (!sv.IsDedicated() && client!.GetPlayerSlot() == 0) {
Msg("Preventing kick of local server host!\n");
return;
}

if (source != CommandSource.Command && !sv.IsDedicated() && sv.GetClient(clientSlot) == client) {
Msg("Preventing kick of yourself!\n");
return;
}

if (message.Length != 0)
client!.Disconnect(message);
else if (source == CommandSource.Command)
client!.Disconnect("Kicked from server");
else
client!.Disconnect($"Kicked by {sv.GetClient(clientSlot)!.GetClientName()}");

return;
}

if (steamID64)
ConMsg($"steamid64 \"{id}\" not found\n");
else if (id != 0)
ConMsg($"userid \"{id}\" not found\n");
else
ConMsg($"steamid \"{searchString}\" not found\n");
}

[ConCommand("kick", "Kick a player by name.")]
void kick(in TokenizedCommand args, CommandSource source, int clientSlot) {
if (!sv.IsActive()) {
ConMsg("No server running, noone to kick.\n");
return;
}

if (args.ArgC() <= 1) {
ConMsg("Usage: kick < name >\n");
return;
}

ReadOnlySpan<char> name = args.ArgS();
if (name.Length > 63)
name = name[..63];

if (name.IsEmpty)
return;

if (name[0] == '"' && name[^1] == '"')
name = name.Length > 1 ? name[1..^1] : name[1..];

IClient? client = null;
int i;
for (i = 0; i < sv.GetClientCount(); i++) {
client = sv.GetClient(i)!;

if (!client.IsConnected())
continue;

if (client.IsHLTV())
continue;

if (stricmp(client.GetClientName(), name) == 0)
break;
}

if (i < sv.GetClientCount()) {
if (!sv.IsDedicated() && client!.GetPlayerSlot() == 0) {
Msg("Preventing kick of local server host!\n");
return;
}

if (source == CommandSource.Command)
client!.Disconnect("Kicked from server");
else {
if (!sv.IsDedicated() && sv.GetClient(clientSlot) == client)
return;

client!.Disconnect($"Kicked by {sv.GetClient(clientSlot)!.GetClientName()}");
}
}
else
ConMsg($"name \"{name}\" not found\n");
}

[ConCommand("kickall", "Kicks everybody connected with a message.")]
void kickall(in TokenizedCommand args, CommandSource source, int clientSlot) {
if (!sv.IsActive()) {
ConMsg("No server running, noone to kick.\n");
return;
}

ReadOnlySpan<char> message = args.ArgS();
if (message.Length > 127)
message = message[..127];

for (int i = 0; i < sv.GetClientCount(); i++) {
IClient client = sv.GetClient(i)!;

if (!client.IsConnected())
continue;

if (!sv.IsDedicated() && client.GetPlayerSlot() == 0) {
Msg("Preventing kick of local server host!\n");
continue;
}

if (source != CommandSource.Command && !sv.IsDedicated() && sv.GetClient(clientSlot) == client)
continue;

if (client.IsHLTV())
continue;

if (source == CommandSource.Command) {
if (!message.IsEmpty)
client.Disconnect(message);
else
client.Disconnect("Kicked from server");
}
else {
ReadOnlySpan<char> who = sv.GetClient(clientSlot)!.GetClientName();
if (!message.IsEmpty)
client.Disconnect($"Kicked by {who} : {message}");
else
client.Disconnect($"Kicked by {who}");
}
}
}

readonly ConVar voice_recordtofile = new("voice_recordtofile", "0", 0, "Record mic data and decompressed voice data into 'voice_micdata.wav' and 'voice_decompressed.wav'");
readonly ConVar voice_inputfromfile;
Expand Down
7 changes: 7 additions & 0 deletions Source.Engine/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public static void OutOfBandPrint(NetSocketType sock, NetAddress? adr, ReadOnlyS
Singleton<Net>().SendPacket(null!, sock, adr, msg.GetData(), msg.BytesWritten);
}

Source.Engine.Server.Filter? _filter = null; Source.Engine.Server.Filter Filter => _filter ??= Singleton<Source.Engine.Server.Filter>();

public bool StringToAdr(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPEndPoint? ep) {
host = host.SliceNullTerminatedString();
ep = null;
Expand Down Expand Up @@ -282,6 +284,11 @@ public void ProcessSocket(NetSocketType sock, IConnectionlessPacketHandler handl
break;
}

if (Filter.ShouldDiscard(packet.From)) {
Filter.SendBan(packet.From);
continue;
}

unsafe {
int header = packet.Message.ReadLong();

Expand Down
Loading
Loading