/// <summary> /// Returns a new component instance if it should be created, else null. /// </summary> public bool TryCreateInstance(RunLocation runningOn, object attachedTo, out ComponentInstanceDescription result) { Log.Entered(); result = null; if (!ShouldRunOn(runningOn)) { return(false); } try { if (!InvokeConditionFunc(attachedTo)) { return(false); } } catch (Exception e) { Logger.Log($"Error invoking condition func from {this}: {e}", Severity.Level.ERROR); if (Debug) { Notification.Notify($"Error invoking condition func for {ComponentClass.FullName}.", 10000, Severity.Level.ERROR); } return(false); } object instance; List <ComponentEventAction> actions; try { instance = CreateInstance(attachedTo); actions = EventMethods.Where(x => x.ShouldRunOn(runningOn)).Select(x => x.ToEventAction(instance)).ToList(); } catch (Exception e) { Logger.Log($"Error instantiating component {this}: {e}", Severity.Level.ERROR); if (Debug) { Notification.Notify($"Error instantiating {ComponentClass.FullName}.", 10000, Severity.Level.ERROR); } return(false); } Log.Debug("Created instance of " + ComponentClass); result = new ComponentInstanceDescription() { ComponentClass = ComponentClass, ComponentInstance = instance, EventActions = actions, }; return(true); }
/// <summary> /// Defines a particular group to load from the given assembly when the session starts. /// Should be called once within game instance before a session is loaded, e.g. within IPlugin.Init(). /// Assembly should be determined with Assembly.GetExecutingAssembly(). /// </summary> /// <remarks> /// Assembly could default to CallingAssembly, but inlining and tail-recursion make it difficult to ensure that's coming from the right place. /// </remarks> public static void LoadOnInit(int groupId, Assembly assembly) { Log.Debug($"LoadOnInit group: {groupId}, assembly: {assembly.GetName().FullName}"); InitGroupsByAssembly[assembly] = groupId; }