Files
sbox-public/engine/Sandbox.Access/Config/AccessRules.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

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 ) );
}
}