mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-02-07 21:21:32 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
64 lines
1.3 KiB
C#
64 lines
1.3 KiB
C#
using Microsoft.CodeAnalysis;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Sandbox;
|
|
|
|
public partial class AccessRules
|
|
{
|
|
public List<Regex> Whitelist = new();
|
|
public List<Regex> Blacklist = new();
|
|
|
|
public AccessRules()
|
|
{
|
|
InitAssemblyList();
|
|
|
|
// TODO add rules based on config
|
|
AddRange( Rules.BaseAccess );
|
|
AddRange( Rules.Types );
|
|
AddRange( Rules.Numerics );
|
|
AddRange( Rules.Reflection );
|
|
AddRange( Rules.Exceptions );
|
|
AddRange( Rules.Diagnostics );
|
|
AddRange( Rules.Async );
|
|
AddRange( Rules.CompilerGenerated );
|
|
}
|
|
|
|
void AddRange( IEnumerable<string> rules )
|
|
{
|
|
foreach ( var line in rules )
|
|
{
|
|
AddRule( line );
|
|
}
|
|
}
|
|
|
|
void AddRule( string line )
|
|
{
|
|
var wildcard = line.Trim();
|
|
|
|
bool blacklist = wildcard.StartsWith( '!' );
|
|
if ( blacklist )
|
|
wildcard = wildcard[1..];
|
|
|
|
wildcard = Regex.Escape( wildcard ).Replace( "\\*", ".*" );
|
|
wildcard = $"^{wildcard}$";
|
|
|
|
var regex = new Regex( wildcard, RegexOptions.Compiled );
|
|
|
|
if ( blacklist )
|
|
Blacklist.Add( regex );
|
|
else
|
|
Whitelist.Add( regex );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if call is in the whitelist
|
|
/// </summary>
|
|
public bool IsInWhitelist( string test )
|
|
{
|
|
if ( Blacklist.Any( x => x.IsMatch( test ) ) )
|
|
return false;
|
|
|
|
return Whitelist.Any( x => x.IsMatch( test ) );
|
|
}
|
|
}
|