Пример #1
0
 public PermissionManager(IDatabase database, BotClient botClient, ISettingManager settings)
 {
     Database          = database;
     BotClient         = botClient;
     Settings          = settings;
     CachedPermissions = CachedDictionary.FromSource((ulong?key) => LoadPerms(key), new TimeSpan(0, 30, 0), 100);
 }
        // ReSharper restore UnusedMember.Local

        public override void RuntimeInitialize(EventInfo eventInfo)
        {
            // An event always has a declaring type.
            Contract.Assume(eventInfo.DeclaringType != null);

            base.RuntimeInitialize(eventInfo);

            Type declaringType = eventInfo.DeclaringType;

            _declaringGenericType = declaringType.IsGenericType ? declaringType.GetGenericTypeDefinition() : declaringType;

            _addEmptyEventHandlers = new CachedDictionary <Type, Action <object> >(type =>
            {
                // Find the type in which the constructor is defined.
                // Needed since events can be private, and those wouldn't be returned otherwise, even when searching a flattened hierarchy.
                Type baseType = type.GetMatchingGenericType(_declaringGenericType);

                EventInfo runtimeEvent = baseType.GetEvents(ReflectionHelper.ClassMembers).Where(e => e.Name == eventInfo.Name).First();

                MethodInfo delegateInfo          = DelegateHelper.MethodInfoFromDelegateType(runtimeEvent.EventHandlerType);
                ParameterExpression[] parameters = delegateInfo.GetParameters().Select(p => Expression.Parameter(p.ParameterType)).ToArray();
                Delegate emptyDelegate
                    = Expression.Lambda(runtimeEvent.EventHandlerType, Expression.Empty(), "EmptyDelegate", true, parameters).Compile();

                // Create the delegate which adds the empty handler to an instance.
                MethodInfo addMethod = runtimeEvent.GetAddMethod(true);
                if (addMethod.IsPublic)
                {
                    return(instance => runtimeEvent.AddEventHandler(instance, emptyDelegate));
                }

                return(instance => addMethod.Invoke(instance, new object[] { emptyDelegate }));
            });
        }
Пример #3
0
            public static cachedGuildSettings CacheSettings(CustomDiscordSetting settings)
            {
                var cache = new List <CachedDictionary>();

                foreach (var a in settings.overrideChannelsCats)
                {
                    var _settings = new CachedDictionary
                    {
                        overrideKey    = a.Key,
                        overideChanels = a.Value
                    };

                    cache.Add(_settings);
                }

                var value = new cachedGuildSettings {
                    discordSettingsUncached = settings
                };

                // value.discordSettingsUncached = removeDictionary(value.discordSettingsUncached);


                value.discordSettingCached = cache;
                //value.discordSettingsUncached.overrideChannelsCats = new Dictionary<List<ulong>, OverideChannmels>();

                return(value);
            }
 public void OnConstructorEntry(MethodExecutionArgs args)
 {
     if (!_isInitialized)
     {
         _addEmptyEventHandlers[args.Instance.GetType()](args.Instance);
         _isInitialized = true;
     }
 }
Пример #5
0
    public static object ToObj(BonValue v, Type t, object old, HashSet <string> fields = null)
    {
        if (v == null)
        {
            return(null);
        }
        switch (t.Name)
        {
        case "Byte": return((byte)v.AsInt);

        case "SByte": return((sbyte)v.AsInt);

        case "Int16": return((short)v.AsInt);

        case "UInt16": return((ushort)v.AsInt);

        case "Int32": return(v.AsInt);

        case "UInt32": return((uint)v.AsInt);

        case "Int64": return(v.AsLong);

        case "UInt64": return((ulong)v.AsLong);

        case "Single": return(v.AsFloat);

        case "Double": return(v.AsDouble);

        case "Boolean": return(v.AsBoolean);

        case "String": return(v.AsString);

        case "Byte[]": return(v.AsBinary);

        case "List`1": {
            BonArray arr = v.AsBonArray;
            if (arr == null)
            {
                return(null);
            }
            int   num = arr.Count;
            IList l   = null;
            if (old != null)
            {
                l = (IList)old;
            }
            else
            {
                l = (IList)Activator.CreateInstance(t, num);
            }
            Type t2 = t.GetGenericArguments()[0];
            l.Clear();
            for (int i = 0; i < num; i++)
            {
                l.Add(ToObj(arr[i], t2, null, fields));
            }
            return(l);
        }

        case "Dictionary`2": {
            BonDocument doc = v.AsBonDocument;
            if (doc == null)
            {
                return(null);
            }
            int         num = doc.Count;
            IDictionary d   = null;
            if (old != null)
            {
                d = (IDictionary)old;
            }
            else
            {
                d = (IDictionary)Activator.CreateInstance(t, num);
            }
            Type[] t2s = t.GetGenericArguments();
            Type   tk  = t2s[0];
            Type   t2  = t2s[1];
            for (int i = 0; i < num; i++)
            {
                BonElement el  = doc[i];
                object     key = null;
                switch (tk.Name)
                {
                case "Int32": key = Convert.ToInt32(el.name); break;

                case "Int64": key = Convert.ToInt64(el.name); break;

                case "String": key = el.name; break;

                default: {
                    if (tk.IsEnum)
                    {
                        key = Enum.ToObject(tk, Convert.ToInt32(el.name));
                    }
                    break;
                }
                }
                if (key != null)
                {
                    BonValue v2  = el.value;
                    object   obj = null;
                    if (d.Contains(key))
                    {
                        obj = ToObj(v2, t2, d[key], fields);
                    }
                    else
                    {
                        obj = ToObj(v2, t2, null, fields);
                    }
                    if (obj == null)
                    {
                        d.Remove(key);
                    }
                    else
                    {
                        d[key] = obj;
                    }
                }
            }
            return(d);
        }

        default: {
            if (t.IsEnum)
            {
                return(Enum.ToObject(t, v.AsInt));
            }
            if (t.IsArray)
            {
                BonArray arr = v.AsBonArray;
                if (arr == null)
                {
                    return(null);
                }
                int  num = arr.Count;
                Type t2  = t.GetElementType();
                var  obj = Array.CreateInstance(t2, num);
                for (int i = 0; i < num; i++)
                {
                    obj.SetValue(ToObj(arr[i], t2, null, fields), i);
                }
                return(obj);
            }
            if (!v.IsBonDocument)
            {
                return(null);
            }
            {
                BonDocument doc = v.AsBonDocument;
                string      _t_ = doc.GetString("_t_");
                if (_t_ != null)
                {
                    try {
                        t = Type.GetType(_t_);
                    } catch (Exception) {
                        Debug.LogWarning("δÕÒµ½ÀàÐÍ: " + doc["_t_"].AsString);
                        return(null);
                    }
                }
                ClassInfo ci  = ClassInfo.Get(t);
                object    obj = old;
                Dictionary <string, List <DataMonitor> > dataMonitors = null;
                bool monitorObj = false;
                if (old != null)
                {
                    monitorObj = dataMonitor.TryGetValue(old, out dataMonitors);
                }
                if (obj == null)
                {
                    obj = Activator.CreateInstance(t);
                }
                if (obj is IBon)
                {
                    ((IBon)obj).FromBon(doc);
                    return(obj);
                }
                CachedDictionary <string, FldInfo> fis = ci.fields;
                bool isValueType = t.IsValueType;
                int  num         = doc.Count;
                for (int i = 0; i < num; i++)
                {
                    BonElement el = doc[i];
                    if (fields != null && !fields.Contains(el.name))
                    {
                        continue;
                    }
                    FldInfo fi;
                    if (fis.TryGetValue(el.name, out fi))
                    {
                        List <DataMonitor> fieldMonitors = null;
                        bool monitorField = monitorObj && dataMonitors.TryGetValue(fi.name, out fieldMonitors) && fieldMonitors.Count > 0;
                        if ((fi.type.IsValueType || fi.type == typeof(string) || fi.type.IsEnum) && !monitorField)
                        {
                            fi.SetValue(obj, ToObj(el.value, fi.type, null, fi.subFields));
                        }
                        else
                        {
                            object oldv = fi.GetValue(obj);
                            object newv = ToObj(el.value, fi.type, oldv, fi.subFields);
                            fi.SetValue(obj, newv);
                            if (monitorField && (fi.type.IsClass || oldv != newv))
                            {
                                dataChanged.AddRange(fieldMonitors);
                            }
                        }
                    }
                }
                return(obj);
            }
        }
        }
    }
		// ReSharper restore UnusedMember.Local

		public override void RuntimeInitialize( EventInfo eventInfo )
		{
			// An event always has a declaring type.
			Contract.Assume( eventInfo.DeclaringType != null );

			base.RuntimeInitialize( eventInfo );

			Type declaringType = eventInfo.DeclaringType;
			_declaringGenericType = declaringType.IsGenericType ? declaringType.GetGenericTypeDefinition() : declaringType;

			_addEmptyEventHandlers = new CachedDictionary<Type, Action<object>>( type =>
			{
				// Find the type in which the constructor is defined.
				// Needed since events can be private, and those wouldn't be returned otherwise, even when searching a flattened hierarchy.
				Type baseType = type.GetMatchingGenericType( _declaringGenericType );

				EventInfo runtimeEvent = baseType.GetEvents( ReflectionHelper.ClassMembers ).Where( e => e.Name == eventInfo.Name ).First();

				MethodInfo delegateInfo = DelegateHelper.MethodInfoFromDelegateType( runtimeEvent.EventHandlerType );
				ParameterExpression[] parameters = delegateInfo.GetParameters().Select( p => Expression.Parameter( p.ParameterType ) ).ToArray();
				Delegate emptyDelegate
					= Expression.Lambda( runtimeEvent.EventHandlerType, Expression.Empty(), "EmptyDelegate", true, parameters ).Compile();

				// Create the delegate which adds the empty handler to an instance.
				MethodInfo addMethod = runtimeEvent.GetAddMethod( true );
				if ( addMethod.IsPublic )
				{
					return instance => runtimeEvent.AddEventHandler( instance, emptyDelegate );
				}

				return instance => addMethod.Invoke( instance, new object[] { emptyDelegate } );
			} );
		}
Пример #7
0
 public void SetUp()
 {
     _worker       = new DictionaryWorker();
     _cachedString = new CachedDictionary <string>(_worker.GetDict, TimeSpan.FromMilliseconds(100));
 }
 public bool TryGetValue(TKey _key, out TValue outValue)
 {
     return(CachedDictionary.TryGetValue(_key, out outValue));
 }
Пример #9
0
 public void SetUp()
 {
     _worker = new DictionaryWorker();
     _cachedString = new CachedDictionary<string>(_worker.GetDict, TimeSpan.FromMilliseconds(100));
 }