From b18567003c8e7f4c22bdec5a8d279f6aa8c845d2 Mon Sep 17 00:00:00 2001 From: peter-r-g Date: Mon, 15 May 2023 12:15:49 -0400 Subject: [PATCH] Add IsServerCommandSupported extension method --- .../Extensions/ITypeSymbolExtensions.cs | 35 +++++++++++++++++++ .../Extensions/TypeSyntaxExtensions.cs | 17 ++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/SboxAnalyzers/Extensions/ITypeSymbolExtensions.cs b/SboxAnalyzers/Extensions/ITypeSymbolExtensions.cs index 178b109..2a32cfa 100644 --- a/SboxAnalyzers/Extensions/ITypeSymbolExtensions.cs +++ b/SboxAnalyzers/Extensions/ITypeSymbolExtensions.cs @@ -40,6 +40,27 @@ internal static class ITypeSymbolExtensions "System.Collections.Generic.IDictionary" ); + /// + /// Contains a set of all types that are supported for server commands in S&box. + /// + internal static ImmutableHashSet ServerCommandSupportedTypes = ImmutableHashSet.Create( + typeof( int ).FullName, + typeof( uint ).FullName, + typeof( long ).FullName, + typeof( ulong ).FullName, + typeof( float ).FullName, + typeof( double ).FullName, + typeof( bool ).FullName, + typeof( string ).FullName, + "Sandbox.Vector2", + "Sandbox.Vector3", + "Sandbox.Vector4", + "Sandbox.Rotation", + "Sandbox.Angles", + "Sandbox.Color", + "Sandbox.RangedFloat" + ); + /// /// Returns whether or not the can be networked. /// @@ -91,6 +112,20 @@ internal static bool IsNetworkable( this ITypeSymbol symbol) return false; } + /// + /// Returns whether or not the is supported in server commands. + /// + /// The symbol to check if it is networkable in server commands. + /// Whether or not the is supported in server commands. + internal static bool IsServerCommandSupported( this ITypeSymbol symbol ) + { + // Fast path, enums are supported. + if ( symbol.TypeKind == TypeKind.Enum ) + return true; + + return ServerCommandSupportedTypes.Contains( symbol.ToNameString( false ) ); + } + /// /// Returns a type string of a . /// diff --git a/SboxAnalyzers/Extensions/TypeSyntaxExtensions.cs b/SboxAnalyzers/Extensions/TypeSyntaxExtensions.cs index 25cc655..060c1db 100644 --- a/SboxAnalyzers/Extensions/TypeSyntaxExtensions.cs +++ b/SboxAnalyzers/Extensions/TypeSyntaxExtensions.cs @@ -22,7 +22,22 @@ internal static bool IsNetworkable( this TypeSyntax syntax, SemanticModel semant return namedTypeSymbol.IsNetworkable(); } - + + /// + /// Returns whether or not a is supported in server commands. + /// + /// The to check. + /// The semantic model context around the compilation. + /// Whether or not a is supported in server commands. + internal static bool IsServerCommandSupported( this TypeSyntax syntax, SemanticModel semanticModel ) + { + var symbol = semanticModel.GetSymbolInfo( syntax ); + if ( symbol.Symbol is not INamedTypeSymbol namedTypeSymbol ) + return false; + + return namedTypeSymbol.IsServerCommandSupported(); + } + /// /// Returns whether or not a is equal to another . ///