/// <summary> Registers a <see cref="Keybind"/>. </summary> /// <param name="keybind"> The <see cref="Keybind"/> to register. </param> /// <exception cref="KeybindInvalidException"> Thrown when <paramref name="keybind"/> has an invalid <see cref="Keybind.Name"/> or <see cref="Keybind.ComesBefore"/>. </exception> /// <exception cref="KeybindRegisteredTooLateException"> Thrown when this method is called after PreLoad. </exception> /// <remarks> Registered keybinds must have a unique <see cref="Keybind.Name"/> and a valid <see cref="Keybind.ComesBefore"/>. </remarks> /// <seealso cref="Register(IEnumerable{Keybind})"/> public static void Register(Keybind keybind) { if (Main.hasPreloaded) { throw new KeybindRegisteredTooLateException(keybind); } Reg.ValidateKeybind(keybind); Reg.registeredNames.Add(keybind.Name); Reg.keybinds.Add(keybind); Reg.AddKeybindPosition(keybind); }
private static void ValidateKeybind(Keybind keybind) { if (!keybind.Name.StartsWith(Keybind.KEYBIND_PREFIX)) { throw new KeybindInvalidException(keybind, KeybindInvalidException.Reason.NameMissingPrefix); } else if (Reg.registeredNames.Contains(keybind.Name) || MethodKeybindExtractor.VanillaKeybinds.Contains(keybind.Name)) { throw new KeybindInvalidException(keybind, KeybindInvalidException.Reason.NameTaken); } else if (keybind.ComesBefore is object && !MethodKeybindExtractor.VanillaKeybinds.Contains(keybind.ComesBefore)) { throw new KeybindInvalidException(keybind, KeybindInvalidException.Reason.ComesBeforeMissing); } }
private static void AddKeybindPosition(Keybind keybind) { if (keybind.ComesBefore is null) { Reg.endOfListKeybinds.Add(keybind); } else { if (Reg.comesBefore.ContainsKey(keybind.ComesBefore)) { Reg.comesBefore[keybind.ComesBefore].Add(keybind); } else { Reg.comesBefore[keybind.ComesBefore] = new List <Keybind> { keybind }; } } }
internal KeybindInvalidException(Keybind keybind, Reason reason) : base( $@"Attempted to register keybind {reason switch