Skip to content

Commit

Permalink
Add IsServerCommandSupported extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-r-g committed May 15, 2023
1 parent a73b457 commit b185670
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
35 changes: 35 additions & 0 deletions SboxAnalyzers/Extensions/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ internal static class ITypeSymbolExtensions
"System.Collections.Generic.IDictionary<TKey, TValue>"
);

/// <summary>
/// Contains a set of all types that are supported for server commands in S&box.
/// </summary>
internal static ImmutableHashSet<string> 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"
);

/// <summary>
/// Returns whether or not the <see cref="ITypeSymbol"/> can be networked.
/// </summary>
Expand Down Expand Up @@ -91,6 +112,20 @@ internal static bool IsNetworkable( this ITypeSymbol symbol)
return false;
}

/// <summary>
/// Returns whether or not the <see cref="ITypeSymbol"/> is supported in server commands.
/// </summary>
/// <param name="symbol">The symbol to check if it is networkable in server commands.</param>
/// <returns>Whether or not the <see cref="ITypeSymbol"/> is supported in server commands.</returns>
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 ) );
}

/// <summary>
/// Returns a type string of a <see cref="ITypeSymbol"/>.
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion SboxAnalyzers/Extensions/TypeSyntaxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ internal static bool IsNetworkable( this TypeSyntax syntax, SemanticModel semant

return namedTypeSymbol.IsNetworkable();
}


/// <summary>
/// Returns whether or not a <see cref="TypeSyntax"/> is supported in server commands.
/// </summary>
/// <param name="syntax">The <see cref="TypeSyntax"/> to check.</param>
/// <param name="semanticModel">The semantic model context around the compilation.</param>
/// <returns>Whether or not a <see cref="TypeSyntax"/> is supported in server commands.</returns>
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();
}

/// <summary>
/// Returns whether or not a <see cref="TypeSyntax"/> is equal to another <see cref="TypeSyntax"/>.
/// </summary>
Expand Down

0 comments on commit b185670

Please sign in to comment.