Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit f7ed76e

Browse files
committed
Add programmable block blacklist.
1 parent de6c0e8 commit f7ed76e

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace EssentialsPlugin.Settings
2+
{
3+
using System;
4+
using Utility;
5+
6+
[Serializable]
7+
public class BlacklistItem
8+
{
9+
private string _type;
10+
public string Type
11+
{
12+
get { return _type; }
13+
set
14+
{
15+
_type = value;
16+
switch ( BlacklistManager.Instance.ValidateBlacklistItem( this ) )
17+
{
18+
case BlacklistManager.BlacklistVerificationEnum.InvalidType:
19+
throw new ArgumentException( $"{_type} is not a valid type!" );
20+
21+
case BlacklistManager.BlacklistVerificationEnum.InvalidMember:
22+
throw new ArgumentException( $"{_member} is not a valid member of {_type}!" );
23+
24+
case BlacklistManager.BlacklistVerificationEnum.NoType:
25+
throw new ArgumentException( "You mus give a type if you specify a member!" );
26+
}
27+
}
28+
}
29+
30+
private string _member;
31+
public string Member
32+
{
33+
get { return _member; }
34+
set
35+
{
36+
_member = value;
37+
switch (BlacklistManager.Instance.ValidateBlacklistItem(this))
38+
{
39+
case BlacklistManager.BlacklistVerificationEnum.InvalidType:
40+
throw new ArgumentException($"{_type} is not a valid type!");
41+
42+
case BlacklistManager.BlacklistVerificationEnum.InvalidMember:
43+
throw new ArgumentException($"{_member} is not a valid member of {_type}!");
44+
45+
case BlacklistManager.BlacklistVerificationEnum.NoType:
46+
throw new ArgumentException("You mus give a type if you specify a member!");
47+
}
48+
}
49+
}
50+
}
51+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Sandbox.Common;
6+
using Sandbox.ModAPI;
7+
using System.Threading.Tasks;
8+
9+
namespace EssentialsPlugin.Utility
10+
{
11+
using System.Collections.Specialized;
12+
using System.Reflection;
13+
using Settings;
14+
using VRage.Collections;
15+
using VRage.Library.Collections;
16+
using VRage.Scripting;
17+
18+
public class BlacklistManager
19+
{
20+
private static BlacklistManager _instance;
21+
22+
public static BlacklistManager Instance
23+
{
24+
get
25+
{
26+
if ( _instance == null )
27+
_instance = new BlacklistManager();
28+
return _instance;
29+
}
30+
}
31+
32+
public void _blacklistItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
33+
{
34+
UpdateBlacklist();
35+
}
36+
37+
public void UpdateBlacklist()
38+
{
39+
var blacklist = MyScriptCompiler.Static.Whitelist.OpenIngameBlacklistBatch();
40+
var types = new MyConcurrentHashSet<Type>();
41+
var memberDict = new MyConcurrentDictionary<Type, List<string>>();
42+
Parallel.ForEach( PluginSettings.Instance.BlacklistItems, item =>
43+
{
44+
if ( string.IsNullOrEmpty( item.Type ) )
45+
return;
46+
47+
var targetType = FindTypeInAllAssemblies( item.Type );
48+
if ( targetType == null )
49+
return;
50+
51+
if(string.IsNullOrEmpty( item.Member ))
52+
lock ( types )
53+
types.Add( targetType );
54+
55+
var members = targetType.GetMember( item.Member );
56+
if ( members.Length != 0 )
57+
{
58+
if(!memberDict.ContainsKey( targetType ))
59+
memberDict.Add( targetType, new List<string>() );
60+
memberDict[targetType].Add( item.Member );
61+
}
62+
} );
63+
64+
if ( types.Count > 0 )
65+
{
66+
blacklist.AddTypes( types.ToArray() );
67+
foreach(var type in types)
68+
Essentials.Log.Info( $"Added type {type.Name} to PB blacklist." );
69+
}
70+
foreach ( var entry in memberDict )
71+
{
72+
blacklist.AddMembers( entry.Key, entry.Value.ToArray() );
73+
foreach ( var name in entry.Value )
74+
{
75+
Essentials.Log.Info( $"Added {entry.Key.Name}.{name} to PB blacklist." );
76+
}
77+
}
78+
}
79+
80+
private Type FindTypeInAllAssemblies( string typeName )
81+
{
82+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
83+
foreach ( var assembly in assemblies )
84+
{
85+
var types = assembly.GetTypes();
86+
foreach ( var type in types )
87+
{
88+
if ( type.Name == typeName )
89+
{
90+
return type;
91+
}
92+
}
93+
}
94+
return null;
95+
}
96+
97+
public enum BlacklistVerificationEnum
98+
{
99+
Ok,
100+
InvalidType,
101+
InvalidMember,
102+
NoType,
103+
Empty
104+
}
105+
106+
public BlacklistVerificationEnum ValidateBlacklistItem( BlacklistItem item )
107+
{
108+
if ( string.IsNullOrEmpty( item.Type ) && string.IsNullOrEmpty( item.Member ) )
109+
return BlacklistVerificationEnum.Empty;
110+
if ( string.IsNullOrEmpty( item.Type ) && !string.IsNullOrEmpty( item.Member ) )
111+
return BlacklistVerificationEnum.NoType;
112+
113+
var targetType = FindTypeInAllAssemblies( item.Type );
114+
115+
if ( targetType != null && string.IsNullOrEmpty( item.Member ))
116+
return BlacklistVerificationEnum.Ok;
117+
118+
if (targetType == null)
119+
return BlacklistVerificationEnum.InvalidType;
120+
121+
if ( targetType.GetMember( item.Member ).Length > 0 )
122+
return BlacklistVerificationEnum.Ok;
123+
else
124+
return BlacklistVerificationEnum.InvalidMember;
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)