|
| 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