/// <summary> /// Handles when a new type has been loaded into a <see cref="TypeFactory"/>. /// </summary> /// <param name="typeFactory">The type factory.</param> /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param> /// <exception cref="ArgumentException"><see cref="TypeFactoryLoadedEventArgs.LoadedType"/> does not contain /// the <see cref="AIAttribute"/>.</exception> /// <exception cref="DuplicateKeyException">The loaded type in <paramref name="e"/> was already loaded.</exception> protected virtual void OnLoadTypeHandler(TypeFactory typeFactory, TypeFactoryLoadedEventArgs e) { var aiAttributes = e.LoadedType.GetCustomAttributes(typeof(AIAttribute), false).Cast <AIAttribute>(); if (aiAttributes.Count() == 0) { throw new ArgumentException( string.Format("Expected loaded AI Type {0} to have one or more AIAttributes.", e.LoadedType), "e"); } foreach (var aiAttribute in aiAttributes) { var id = aiAttribute.ID; // Ensure the ID is not already in use if (_aiByID.ContainsKey(id)) { const string errmsg = "Failed to load AI `{0}` - AIID `{1}` is already in use by Type `{2}`"; var err = string.Format(errmsg, e.LoadedType, id, _aiByID[id]); if (log.IsFatalEnabled) { log.Fatal(err); } Debug.Fail(err); throw new DuplicateKeyException(err); } _aiByID.Add(id, e.LoadedType); if (log.IsInfoEnabled) { log.InfoFormat("Loaded AI `{0}` from Type `{1}`.", e.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); } }
/// <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="DuplicateKeyException">The type in <paramref name="e"/> was already loaded.</exception> static void OnLoadTypeHandler(TypeFactory sender, TypeFactoryLoadedEventArgs e) { var instance = (NPCChatResponseActionBase)sender.GetTypeInstance(e.Name); // Make sure the name is not already in use if (ContainsResponseAction(instance.Name)) { const string errmsg = "Could not add Type `{0}` - a response action 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 DuplicateKeyException(err); } // Add the value to the Dictionary _instances.Add(instance.Name, instance); if (log.IsDebugEnabled) { log.DebugFormat("Loaded NPC chat response action `{0}` from Type `{1}`.", instance.Name, e.LoadedType); } }
void HandleLoadType(TypeFactory factory, TypeFactoryLoadedEventArgs e) { // Create the instance var instance = (IStatusEffect <TStatType, TStatusEffectType>)TypeFactory.GetTypeInstance(e.LoadedType); // Ensure we don't already have the StatusEffectType being handled if (_statusEffects.ContainsKey(instance.StatusEffectType)) { const string errmsg = "StatusEffects Dictionary already contains StatusEffectType `{0}`."; Debug.Fail(string.Format(errmsg, instance.StatusEffectType)); if (log.IsFatalEnabled) { log.FatalFormat(errmsg, instance.StatusEffectType); } throw new DuplicateKeyException(string.Format(errmsg, instance.StatusEffectType)); } // Add the instance _statusEffects.Add(instance.StatusEffectType, instance); if (log.IsInfoEnabled) { log.InfoFormat("Created status effect object `{0}` for StatusEffectType `{1}`.", instance, instance.StatusEffectType); } }
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="TextFilter"/> type is added to the factory. /// </summary> /// <param name="factory">The factory.</param> /// <param name="e">The <see cref="NetGore.Collections.TypeFactoryLoadedEventArgs"/> instance containing the event data.</param> /// <exception cref="TypeException">Duplicate text filter names found.</exception> static void FactoryTypeAdded(TypeFactory factory, TypeFactoryLoadedEventArgs e) { var instance = (TextFilter)TypeFactory.GetTypeInstance(e.LoadedType); var key = instance.DisplayName; // Ensure the name is not already in use if (_filterInstancesByName.ContainsKey(key)) { const string errmsg = "A text filter with the name `{0}` already exists!"; throw new TypeException(string.Format(errmsg, key), e.LoadedType); } // Add the instace _filterInstancesByName.Add(key, instance); }
/// <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)); } }
void TypeFactoryLoadedHandler(TypeFactory typeFactory, TypeFactoryLoadedEventArgs e) { var instance = (ISkill <TSkillType, TStatType, TCharacter>)TypeFactory.GetTypeInstance(e.LoadedType); if (_skills.ContainsKey(instance.SkillType)) { const string errmsg = "Skills Dictionary already contains SkillType `{0}`."; Debug.Fail(string.Format(errmsg, instance.SkillType)); if (log.IsFatalEnabled) { log.FatalFormat(errmsg, instance.SkillType); } throw new DuplicateKeyException(string.Format(errmsg, instance.SkillType)); } _skills.Add(instance.SkillType, instance); if (log.IsInfoEnabled) { log.InfoFormat("Created skill object for SkillType `{0}`.", instance.SkillType); } }
/// <summary> /// Handles when a new type has been loaded into a <see cref="TypeFactory"/>. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="TypeFactoryLoadedEventArgs"/> instance containing the event data.</param> void typeFactory_TypeLoaded(TypeFactory sender, TypeFactoryLoadedEventArgs e) { try { // Instantiate the type Tool tool; try { tool = (Tool)TypeFactory.GetTypeInstance(e.LoadedType, this); } catch (Exception ex) { const string errmsg = "Failed to instantiate tool type `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, e.LoadedType, ex); } Debug.Fail(string.Format(errmsg, e.LoadedType, ex)); return; } Debug.Assert(tool.GetType() == e.LoadedType); // Add the event hooks SetToolListeners(tool, true); // Add to the collection try { _tools.Add(e.LoadedType, tool); } catch (Exception ex) { const string errmsg = "Failed to add tool `{0}` (type: {1}) to collection. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, tool, tool.GetType(), ex); } Debug.Fail(string.Format(errmsg, tool, tool.GetType(), ex)); tool.Dispose(); return; } // Add to the settings manager _toolState.Add(tool); // Notify the Tool about all the containers already in this ToolManager foreach (var container in ToolTargetContainers) { try { tool.InvokeToolTargetContainerAdded(container); } catch (Exception ex) { // When there is an error when adding, log it but move on const string errmsg = "Error when notifying `{0}` about container `{1}`. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, tool, container, ex); } Debug.Fail(string.Format(errmsg, tool, container, ex)); } } // Notify listeners if (ToolAdded != null) { ToolAdded.Raise(this, EventArgsHelper.Create(tool)); } } catch (Exception ex) { const string errmsg = "Unexpected error while trying to load tool from type `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, e.LoadedType, ex); } Debug.Fail(string.Format(errmsg, e.LoadedType, ex)); } }