public AcceptedSwitch(string Name, EventType Event_Type, double Delay, SwitchAcceptedHandler Handler = null, object Param = null) { this.Name = Name; this.Event_Type = Event_Type; this.Delay = Delay; this.Handler = Handler; this.Param = Param; }
/// <summary> /// Scan all statically defined switch handlers in mode classes and wire up handling events /// </summary> private void scan_switch_handlers() { // Get all methods in the mode class that match a certain regular expression Type t = this.GetType(); MethodInfo[] methods = t.GetMethods(); string regexPattern = "sw_(?<name>[a-zA-Z0-9]+)_(?<state>open|closed|active|inactive)(?<after>_for_(?<time>[0-9]+)(?<units>ms|s))?"; Regex pattern = new Regex(regexPattern); foreach (MethodInfo m in methods) { MatchCollection matches = pattern.Matches(m.Name); string switchName = ""; string switchState = ""; bool hasTimeSpec = false; double switchTime = 0; string switchUnits = ""; foreach (Match match in matches) { int i = 0; foreach (Group group in match.Groups) { if (group.Success == true) { string gName = pattern.GroupNameFromNumber(i); string gValue = group.Value; if (gName == "name") { switchName = gValue; } if (gName == "state") { switchState = gValue; } if (gName == "after") { hasTimeSpec = true; } if (gName == "time") { switchTime = Int32.Parse(gValue); } if (gName == "units") { switchUnits = gValue; } } i++; } } if (switchName != "" && switchState != "") { if (hasTimeSpec && switchUnits == "ms") { switchTime = switchTime / 1000.0; } SwitchAcceptedHandler swh = (SwitchAcceptedHandler)Delegate.CreateDelegate(typeof(SwitchAcceptedHandler), this, m); add_switch_handler(switchName, switchState, switchTime, swh); } } }
/// <summary> /// Adds a switch handler to the list /// </summary> /// <param name="Name">Switch Name</param> /// <param name="Event_Type">'open', 'closed', 'active' or 'inactive'</param> /// <param name="Delay">float number of seconds that the state should be held before invoking the handler, /// or None if it should be invoked immediately.</param> /// <param name="Handler">The handler to invoke</param> public void add_switch_handler(string Name, string Event_Type, double Delay = 0, SwitchAcceptedHandler Handler = null) { EventType adjusted_event_type; if (Event_Type == "active") { if (Game.Switches[Name].Type == SwitchType.NO) { adjusted_event_type = EventType.SwitchClosedDebounced; } else { adjusted_event_type = EventType.SwitchOpenDebounced; } } else if (Event_Type == "inactive") { if (Game.Switches[Name].Type == SwitchType.NO) { adjusted_event_type = EventType.SwitchOpenDebounced; } else { adjusted_event_type = EventType.SwitchClosedDebounced; } } else if (Event_Type == "closed") { adjusted_event_type = EventType.SwitchClosedDebounced; } else { adjusted_event_type = EventType.SwitchOpenDebounced; } Switch sw = Game.Switches[Name]; AcceptedSwitch asw = new AcceptedSwitch(Name, adjusted_event_type, Delay, Handler, sw); if (!_accepted_switches.Contains(asw)) { _accepted_switches.Add(asw); } }