public static void Update(PlayerMod player) { if (!Active) return; if (player.KnockedOut || player.player.dead) { Close(); return; } List<Option> PickedOptions = new List<Option>(); foreach (OptionHolder oh in OptionMenus) { if (oh.Selected > -1) { //WidthStack = 64; PickedOptions.Add(oh.Options[oh.Selected]); } } bool Cancel = MainMod.orderCallButton.JustPressed; if (Cancel) { if (OptionMenus.Count == 1) { Close(); } else { OptionMenus.RemoveAt(OptionMenus.Count - 1); } } else { OptionHolder oh = OptionMenus[OptionMenus.Count - 1]; for (int o = 0; o < oh.Options.Count; o++) { bool ButtonPress = MainMod.DButtonPress == o; if (ButtonPress && oh.Options[o].Enabled) { oh.Selected = o; //PickedOptions.Add(oh.Options[o]); UponPickingOption(oh.Options[o], player, PickedOptions); } } MainMod.DButtonPress = 255; } }
////////////////////////////////////////////////////////////// /// <summary> /// This method is an easy way to add a new command option to /// the appropriate places. /// </summary> /// /// <param name="opt">the CommandOption to add</param> /// <param name="l">the ICommandListener to notify</param> ////////////////////////////////////////////////////////////// private void AddOption(CommandOption opt, ICommandListener l) { OptionHolder holder = new OptionHolder(opt, l); // this fix is necessary because .NET seems to throw an // exception if the key to a hashtable find is null string lname = opt.LongName == null ? "" : opt.LongName; char c = opt.ShortName; // sanity check for existing options OptionHolder lobj = (OptionHolder)_longOpts[lname]; OptionHolder sobj = (OptionHolder)_shortOpts[c]; if (lobj != null || sobj != null) { string desc = null; Console.Error.Write("warning: overriding option"); Console.Error.Write(" '"); if (lobj != null) { Console.Error.Write(lname); desc = lobj.listener.Description; } else if (sobj != null) { Console.Error.Write(c); desc = sobj.listener.Description; } Console.Error.Write("' from '"); Console.Error.Write(desc); Console.Error.Write("' by '"); Console.Error.Write(l.Description); Console.Error.WriteLine("'."); } // set up the maps _longOpts[lname] = holder; if (c != (char)0) { _shortOpts[c] = holder; } }
////////////////////////////////////////////////////////////// /// <summary> /// This method controls what happens when a missing argument /// for an option is encountered. /// </summary> /// /// <param name="val">the OptionHolder</param> ////////////////////////////////////////////////////////////// private void HandleMissingArg(OptionHolder val) { string hlp = val.option.Help; if (hlp == null || hlp.Length == 0) { hlp = "<arg>"; } string name = val.option.LongName; if (name == null || name.Length == 0) { StringBuilder buf = new StringBuilder(); buf.Append(_sswitch); buf.Append(val.option.ShortName); name = buf.ToString(); } else { StringBuilder buf = new StringBuilder(_lswitch); buf.Append(name); name = buf.ToString(); } string msg = "error: option " + name + " requires parameter '" + hlp + "'."; if (_exitmissing) { Console.Error.Write(msg); Console.Error.WriteLine(" Exiting."); Usage(); System.Environment.Exit(_exitstatus); } else { Console.Error.Write(msg); Console.Error.WriteLine(" Ignored."); } }
public static void AddOptionHolder() { OptionHolder oh = new OptionHolder(); OptionMenus.Add(oh); }
private int ProcessArg(int argc, string[] args) { OptionHolder val = null; string s = args[argc]; if (s == null || s.Length == 0) { return(--argc); } char c0 = s[0]; int slen = s.Length; int idx = s.IndexOf("="); if ((_sswitch == c0) && (slen > 1) && !(s.StartsWith(_lswitch))) { // we have one of the following: // // 1. a switch // 2. a posix option // 3. a set of combined options if (slen == 2) { val = (OptionHolder)_shortOpts[s[1]]; } else if (slen > 2) { val = (OptionHolder)_longOpts[s]; if (val == null) { // must be combined switches return(ExpandSwitches(s.Substring(1), argc, args)); } } } else if (s.StartsWith(_lswitch)) { // must be a long option string key; if (idx != -1) { key = s.Substring(_lswitch.Length, idx - 2); } else { key = s.Substring(_lswitch.Length); } val = (OptionHolder)_longOpts[key]; } else { _leftovers.Add(s); return(argc); } // if we get here should have a value if (val == null) { Console.Error.WriteLine("error: unknown option specified (" + s + ")"); Usage(); System.Environment.Exit(_exitstatus); return(args.Length); } string arg = null; // handle the option if (val.option.ExpectsArgument) { // check to make sure that there's no // '=' sign. if (idx != -1) { arg = s.Substring(idx + 1); if (arg.Length == 0) { HandleMissingArg(val); } } else { if (++argc < args.Length) { arg = args[argc]; } else { HandleMissingArg(val); return(++argc); } } } // give the option a chance to do what it // wants val.option.OptionMatched(arg); // notify the listeners val.listener.OptionMatched(val.option, arg); return(argc); }
private int ExpandSwitches(string sw, int argc, string[] args) { OptionHolder oh = null; char ch = (char)0; string arg = null; for (int i = 0; i < sw.Length; ++i) { ch = sw[i]; oh = (OptionHolder)_shortOpts[ch]; if (oh == null) { Console.Error.WriteLine("error: unknown option '" + ch + "' specified."); System.Environment.Exit(_exitstatus); return(args.Length); } if (oh.option is JoinedCommandOption) { if (i == 0) { arg = sw.Substring(1); oh.option.OptionMatched(arg); oh.listener.OptionMatched(oh.option, arg); break; } else { Console.Error.WriteLine("error: unknown option '" + ch + "' specified."); System.Environment.Exit(_exitstatus); return(args.Length); } } else { if (oh.option.ExpectsArgument && (i == sw.Length - 1)) { if (++argc < args.Length) { arg = args[argc]; } else { HandleMissingArg(oh); } } else if (oh.option.ExpectsArgument) { Console.Error.WriteLine("error: invalid option combination '" + sw + "'"); System.Environment.Exit(_exitstatus); return(args.Length); } } // match the option oh.option.OptionMatched(arg); oh.listener.OptionMatched(oh.option, arg); } return(argc); }