/// <summary> /// Inserts a new CommandObjectPair object in the list at the position sorted by the command name /// See: https://stackoverflow.com/a/12172412/785111 /// </summary> /// <param name="item">An object of struct type SdtdConsole.OL</param> private static void AddSortedCommandObjectPair(CommandObjectPair item) { var commandObjectPairs = SdtdConsole.Instance.GetCommandObjectPairs(); var index = Array.BinarySearch(commandObjectPairs.ToArray(), item, _commandObjectPairComparer); if (index < 0) { index = ~index; } commandObjectPairs.Insert(index, item); //Log.Debug($"Inserted new command object pair at index {index} of {commandObjectPairs.Count-1}."); }
/// <summary> /// Registers the given command object with it's command names into the Console. /// The command object or command names must not already exist in the console. /// To make all command changes persistent, SaveChanges() must be called afterwards. /// Adapted from: SdtdConsole.RegisterCommands /// </summary> /// <param name="commandObject"></param> private static void AddCommand(DynamicCommand commandObject) { if (commandObject == null) { throw new ArgumentNullException(nameof(commandObject)); } var commands = commandObject.GetCommands(); if (commands == null || commands.Length == 0 || commands.All(string.IsNullOrEmpty)) { throw new ArgumentException("No command name(s) defined."); } if (SdtdConsole.Instance.GetCommandObjects().Contains(commandObject)) { throw new ArgumentException($"The command object \"{commands.Join(" ")}\" already exists and cannot be registered twice."); } foreach (string command in commands) { if (string.IsNullOrEmpty(command)) { continue; } if (CommandExists(command)) { throw new ArgumentException($"The command \"{command}\" already exists and cannot be registered twice."); } var commandObjectPair = new CommandObjectPair(command, commandObject); AddSortedCommandObjectPair(commandObjectPair); } AddCommandObjectSorted(commandObject); }