static void TypeLoadedHandler(TypeFactory sender, TypeFactoryLoadedEventArgs e)
        {
            var atbType = typeof(ActionDisplayScriptAttribute);
            var mpdType = typeof(ActionDisplayScriptHandler);

            const BindingFlags bindFlags =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Static;

            // Search through every method in the class
            foreach (var method in e.LoadedType.GetMethods(bindFlags))
            {
                // Get all of the ActionDisplayScriptAttributes for the method (should only be one)
                var atbs = (ActionDisplayScriptAttribute[])method.GetCustomAttributes(atbType, true);
                if (atbs.Length > 1)
                {
                    const string errmsg = "Multiple ActionDisplayScriptAttributes found for method `{0}`.";
                    Debug.Fail(string.Format(errmsg, method.Name));
                    throw new TypeException(string.Format(errmsg, method.Name));
                }

                // Create the delegates for the methods
                foreach (var atb in atbs)
                {
                    ActionDisplayScriptHandler del;
                    try
                    {
                        del = (ActionDisplayScriptHandler)Delegate.CreateDelegate(mpdType, null, method, true);
                        if (del == null)
                        {
                            const string errmsg = "Failed to create ActionDisplayScriptHandler delegate for `{0}`.";
                            throw new TypeException(string.Format(errmsg, atb));
                        }
                    }
                    catch (Exception ex)
                    {
                        const string errmsg =
                            "Failed to create ActionDisplayScriptHandler delegate for method `{0}`. Make sure it is a static method and contains the correct parameters.";
                        Debug.Fail(string.Format(errmsg, method));
                        throw new InstantiateTypeException(string.Format(errmsg, method), ex);
                    }

                    // Ensure the name is unique
                    if (_scriptHandlers.ContainsKey(atb.Name))
                    {
                        const string errmsg =
                            "Found multiple ActionDisplayScriptHandlers with the name `{0}`. Names must be unique.";
                        Debug.Fail(string.Format(errmsg, atb.Name));
                        throw new InstantiateTypeException(string.Format(errmsg, atb.Name), mpdType);
                    }

                    _scriptHandlers.Add(atb.Name, del);
                }
            }
        }
        /// <summary>
        /// Handles when a <see cref="Type"/> is loaded into the <see cref="TypeFactory"/>.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param>
        void LoadTypeHandler(TypeFactory sender, TypeFactoryLoadedEventArgs e)
        {
            // Skip private nested types as they cannot be called by the controller anyways
            if (e.LoadedType.IsNested && !e.LoadedType.IsPublic)
                return;

            // Filter out types to ignore
            if (_typesToIgnore != null && _typesToIgnore.Contains(e.LoadedType))
                return;

            // Check for attribute
            var attribs = e.LoadedType.GetCustomAttributes(false);
            if (attribs == null || attribs.Length == 0)
                _missingAttributeHandler(this, EventArgsHelper.Create(e.LoadedType));
        }
Example #3
0
        /// <summary>
        /// Handles when a new type has been loaded into the <see cref="TypeFactory"/>.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param>
        /// <exception cref="ArgumentException">The loaded type in <paramref name="e"/> was invalid or already loaded.</exception>
        static void OnLoadTypeHandler(TypeFactory sender, TypeFactoryLoadedEventArgs e)
        {
            var instance = (NPCChatConditionalBase)sender.GetTypeInstance(e.Name);

            // Make sure the name is not already in use
            if (ContainsConditional(instance.Name))
            {
                const string errmsg =
                    "Could not add Type `{0}` - a NPC chat conditional named `{1}` already exists as Type `{2}`.";
                var err = string.Format(errmsg, e.LoadedType, instance.Name, _instances[instance.Name].GetType());
                if (log.IsFatalEnabled)
                    log.Fatal(err);
                Debug.Fail(err);
                throw new ArgumentException(err);
            }

            // Add the value to the Dictionary
            _instances.Add(instance.Name, instance);

            if (log.IsDebugEnabled)
                log.DebugFormat("Loaded NPC chat conditional `{0}` from Type `{1}`.", instance.Name, e.LoadedType);
        }
 /// <summary>
 /// Handles when a new type has been loaded into the <see cref="DynamicEntityFactoryBase"/>.
 /// </summary>
 /// <param name="typeFactory">The type factory.</param>
 /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param>
 protected virtual void OnLoadTypeHandler(TypeFactory typeFactory, TypeFactoryLoadedEventArgs e)
 {
     if (log.IsDebugEnabled)
         log.DebugFormat("Loaded DynamicEntity `{0}` from Type `{1}`.", e.Name, e.LoadedType);
 }