private static StringBuilder PrintCommandHelpDetails(PromptCommand cmd, int indent = Indent) { var str = new StringBuilder().Append(PrintCommandSummaryHelp(cmd, indent)); var list = cmd.MethodInfo.GetParameters(); foreach (var info in list) { str.AppendLine($"{string.Empty.PadRight(indent+2)}{info.Name.PadRight(PadCommand - 2)} {info.ParameterType}"); } return(str); }
// Run the given command private bool RunCommand(PromptCommand pCmd, string commandText, IPromptConfiguration config) { var classInstance = GetCommandClassInstance(pCmd, config); if (classInstance == null) { return(false); // Something when wrong } if (pCmd.MethodInfo == null) { return(false); } var parameters = pCmd.MethodInfo.GetParameters(); if (parameters.Length == 0) { // Invoke with 'null' as the parameter array pCmd.MethodInfo.Invoke(classInstance, null); return(true); } // Remove the Command Text from the var parameterText = commandText.Remove(0, pCmd.CommandText.Length).Trim(); // Build a Parameter List to match the Parameters types of the Method // If the Last parameter is a string, the reminding text after the split can be considered part of that parameter // Split the text but all for escaped strings var parametersSplit = GetParameterString(parameters, parameterText); if (parametersSplit == null) { return(false); } var parameterList = new List <object>(); for (int i = 0; i < parameters.Length; i++) { var ob = Configuration.ParameterConvert(parametersSplit[i], parameters[i].ParameterType); if (ob == null) { Warning = "Failed parsing command, incorrect parameter type"; return(false); } parameterList.Add(ob); } pCmd.MethodInfo.Invoke(classInstance, parameterList.ToArray()); return(true); }
public ICommand ResolveCommand(JObject joCommand) { ICommand result = null; var commandName = JSONUtil.GetCommandName(joCommand); if (commandName == null) { return(null); } if (commandName == "load-json") { result = new LoadJSON(); } else if (commandName == "save-json") { result = new SaveJSON(); } else if (commandName == "prompt") { result = new PromptCommand(); } else if (commandName == "run-script") { result = new RunScript(); } else if (commandName == "communicate") { result = new Communicate(); } else if (commandName == "say") { result = new Communicate(); } //else if (commandName == "run-rules") { return new RunRules(); } else if (commandName == "assert") { return(new Assert()); } else if (commandName == "store") { return(new Store()); } return(result); }
public SimpleScript() { Name = "simple"; var echo = new EchoCommand(); var prompt = new PromptCommand(); Actions = new[] { new AndAction( new CommandAction(echo, echo.FindDefaultAction(), new object[] { "Hello World" }), new PipeAction(new[] { new CommandAction(prompt, prompt.FindAction("set")), new CommandAction(echo, echo.FindAction("echoed")) }, "$")), }; }
public ICommand GetCommand( string CommandName, ICommandProcessor CommandProcessor ) { ICommand NewCommand = null; switch( CommandName ) { case "cd": NewCommand = new ChangeDirCommand( CommandProcessor, _Terminal ); break; case "cwd": NewCommand = new CurrentDirCommand( CommandProcessor, _Terminal ); break; case "exit": NewCommand = new ExitCommand( CommandProcessor, _Terminal ); break; case "prompt": NewCommand = new PromptCommand( CommandProcessor, _Terminal ); break; case "set": NewCommand = new SetCommand( CommandProcessor, _Terminal ); break; case "ver": NewCommand = new VersionCommand( CommandProcessor, _Terminal ); break; case "msn": NewCommand = new MsnCommand( CommandProcessor, _Terminal ); break; case "page": NewCommand = new PageCommand( CommandProcessor, _Terminal ); break; case "showpage": NewCommand = new ShowpageCommand( CommandProcessor, _Terminal ); break; case "textpage": NewCommand = new TextpageCommand( CommandProcessor, _Terminal ); break; } return NewCommand; }
// Get the Class Instance for the command that is to be run private object GetCommandClassInstance(PromptCommand pCmd, IPromptConfiguration config) { // If this class instance exists in the configuration store then return it var classInstance = StoreCommandPromptClasses.FirstOrDefault(promptClass => promptClass.GetType() == pCmd.ClassType); if (classInstance != null) { // Great Work Class Instance Already created return(classInstance); } // We only Deal with the first Constructor (Restriction on current design) var constructorInfo = pCmd.ClassType.GetConstructors(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public).FirstOrDefault(); if (constructorInfo == null) { Console.WriteLine($"Cannot create of type {pCmd.ClassType} due to no public constructor"); return(null); // Cannot create class Whoops } // OtherWise, Create var constructorParameters = constructorInfo.GetParameters(); classInstance = constructorParameters.Length == 0 ? Activator.CreateInstance(pCmd.ClassType) // The order is important here, Lets hope Linq gets it right : Activator.CreateInstance(pCmd.ClassType, constructorInfo.GetParameters().Select(parameterInfo => InjectPromptClass(config, parameterInfo.ParameterType)).ToArray()); // If the Class has a Custom Attribute to keep this between commands, then store it in the configuration if (pCmd.KeepClassInstance) { StoreCommandPromptClasses.Add(classInstance); } return(classInstance); }
public static ServerResponse ProcessCommand(Site site, int portalId, int retry, string commandLine, int currentPage) { var res = new ServerResponse(); if (retry == 0) { res.Status = ServerResponseStatus.Error; return(res); } var token = Newtonsoft.Json.JsonConvert.DeserializeObject <JwtToken>(site.Token.Decrypt()); var promptUrl = string.Format("{0}/API/PersonaBar/Command/Cmd", site.Url); if (portalId > -1) { promptUrl += string.Format("/{0}", portalId); } var request = WebRequest.Create(promptUrl); request.ContentType = "application/json; charset=utf-8"; request.Method = WebRequestMethods.Http.Post; request.Headers.Add("Authorization", "Bearer " + token.accessToken); var reqCmd = new PromptCommand() { CmdLine = commandLine, CurrentPage = currentPage }; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string json = Newtonsoft.Json.JsonConvert.SerializeObject(reqCmd); streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } try { var response = (HttpWebResponse)request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { res.Contents = sr.ReadToEnd(); } return(res); } catch (WebException ex) { switch (((HttpWebResponse)ex.Response).StatusCode) { case HttpStatusCode.Unauthorized: var renew = RenewToken(site); if (renew.Status != ServerResponseStatus.Success) { res.Status = renew.Status; return(res); } site.Token = renew.Contents.Encrypt(); var sites = SiteList.Instance(); var listKey = ""; foreach (var s in sites.Sites) { if (s.Value.Url == site.Url) { listKey = s.Key; } } if (!string.IsNullOrEmpty(listKey)) { sites.SetSite(listKey, site.Url, renew.Contents); } return(ProcessCommand(site, portalId, retry - 1, commandLine, currentPage)); default: res.Status = ServerResponseStatus.Error; return(res); } } }
public static void executeCommand(ICommand command) { if (command.GetType().Equals(typeof(SpeakerCommand))) //speaker { SpeakerCommand speakerCommand = (SpeakerCommand)command; currentSpeaker = speakerCommand.speaker; GameObject speakerBox = GameObject.Find("Speaker"); Text speakerText = speakerBox.GetComponentInChildren <Text>(); Image speakerImage = speakerBox.GetComponentInChildren <Image>(); if (speakerCommand.speaker.Equals("")) { speakerText.color = Color.clear; speakerImage.color = Color.clear; } else { speakerText.color = Color.white; speakerImage.color = new Color(1f, 1f, 1f, 0.4f); speakerText.text = speakerCommand.speaker; } advanceScript(); } else if (command.GetType().Equals(typeof(TextCommand))) //text { TextCommand textCommand = (TextCommand)command; GameObject.Find("Dialogue").GetComponentInChildren <Text>().text = textCommand.text; } else if (command.GetType().Equals(typeof(PortraitCommand))) //portrait { PortraitCommand portraitCommand = (PortraitCommand)command; Image portrait = GameObject.Find(portraitCommand.side == 'L' ? "LeftPortrait" : "RightPortrait").GetComponentInChildren <Image>(); if (portraitCommand.character.Equals("")) { portrait.color = Color.clear; } else { portrait.color = Color.white; portrait.sprite = Resources.Load <Sprite>("Characters\\" + portraitCommand.character + "Girl"); } advanceScript(); } else if (command.GetType().Equals(typeof(BackgroundCommand))) //background { BackgroundCommand backgroundCommand = (BackgroundCommand)command; Image background = GameObject.Find("Background").GetComponentInChildren <Image>(); if (backgroundCommand.name.Equals("")) { background.color = Color.black; } else { background.color = Color.white; background.sprite = Resources.Load <Sprite>("Backgrounds\\" + backgroundCommand.name); } advanceScript(); } else if (command.GetType().Equals(typeof(LinkCommand))) //link { LinkCommand linkCommand = (LinkCommand)command; readScript(linkCommand.fileName); advanceScript(); } else if (command.GetType().Equals(typeof(AddPointsCommand))) //addpoints { AddPointsCommand addPointsCommand = (AddPointsCommand)command; switch (addPointsCommand.character) { case "Java": GameControl.control.JavaLovePoints += addPointsCommand.points; break; case "JSHTML": GameControl.control.JSHTMLLovePoints += addPointsCommand.points; break; case "C++": GameControl.control.CPPLovePoints += addPointsCommand.points; break; case "C#": GameControl.control.CSLovePoints += addPointsCommand.points; break; case "Python": GameControl.control.PYLovePoints += addPointsCommand.points; break; } advanceScript(); } else if (command.GetType().Equals(typeof(PromptCommand))) //prompt { PromptCommand promptCommand = (PromptCommand)command; isPrompting = true; int i = 0; List <GameObject> promptButtons = new List <GameObject>(); foreach (string choice in promptCommand.getChoices()) { GameObject promptButton = Instantiate(Resources.Load <GameObject>("Prefabs\\PromptButton"), new Vector3(0, 0), Quaternion.identity); promptButtons.Add(promptButton); promptButton.transform.SetParent(GameObject.Find("Background").transform); promptButton.transform.position = new Vector3(Screen.width / 2, (Screen.height - 55) - i * 40); promptButton.GetComponentInChildren <Text>().text = choice; promptButton.GetComponentInChildren <Button>().onClick.AddListener(delegate { isPrompting = false; foreach (GameObject button in promptButtons) { Destroy(button); } currentScript.InsertRange(commandIndex, promptCommand.getConsequences(choice)); advanceScript(); }); i++; } } else if (command.GetType().Equals(typeof(DateCheckCommand))) //datecheck { DateCheckCommand dateCheckCommand = (DateCheckCommand)command; switch (dateCheckCommand.character) { case "Java": if (GameControl.control.JavaLovePoints >= 15) { //date start } break; case "JSHTML": if (GameControl.control.JSHTMLLovePoints >= 15) { //date start } break; case "C++": if (GameControl.control.CPPLovePoints >= 15) { //date start } break; case "C#": if (GameControl.control.CSLovePoints >= 15) { //date start } break; case "Python": if (GameControl.control.PYLovePoints >= 15) { //date start } break; } } else if (command.GetType().Equals(typeof(FinalCheckCommand))) //finalcheck { FinalCheckCommand finalCheckCommand = (FinalCheckCommand)command; PromptCommand promptCommand = new PromptCommand(); if (GameControl.control.JavaLovePoints >= 15) { List <ICommand> commandList = new List <ICommand>(); commandList.Add(new LinkCommand("\\Home\\Narrator\\Endings\\Java.txt")); promptCommand.addConsequences("Java", commandList); } if (GameControl.control.JSHTMLLovePoints >= 15) { List <ICommand> commandList = new List <ICommand>(); commandList.Add(new LinkCommand("\\Home\\Narrator\\Endings\\HTML&JS.txt")); promptCommand.addConsequences("HTML & JS", commandList); } if (GameControl.control.CPPLovePoints >= 15) { List <ICommand> commandList = new List <ICommand>(); commandList.Add(new LinkCommand("\\Home\\Narrator\\Endings\\C++.txt")); promptCommand.addConsequences("C++", commandList); } if (GameControl.control.CSLovePoints >= 15) { List <ICommand> commandList = new List <ICommand>(); commandList.Add(new LinkCommand("\\Home\\Narrator\\Endings\\C#.txt")); promptCommand.addConsequences("C#", commandList); } if (GameControl.control.PYLovePoints >= 15) { List <ICommand> commandList = new List <ICommand>(); commandList.Add(new LinkCommand("\\Home\\Narrator\\Endings\\Python.txt")); promptCommand.addConsequences("Python", commandList); } List <ICommand> noneCommandList = new List <ICommand>(); noneCommandList.Add(new LinkCommand("\\Home\\Narrator\\Endings\\None.txt")); promptCommand.addConsequences("None", noneCommandList); currentScript.Insert(commandIndex, promptCommand); advanceScript(); } }
public static List <ICommand> parse(string script) { List <ICommand> commands = new List <ICommand>(); bool textBlock = false; bool prompt = false; PromptCommand currentPrompt = null; string currentChoice = null; string currentConsequences = ""; foreach (string line in script.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)) { if (!string.IsNullOrEmpty(line.Trim())) { string[] lineData = line.Trim().Split('|'); if (textBlock) { if (lineData[0].Equals("[end]")) { textBlock = false; } else { commands.Add(new TextCommand(lineData[0])); } } else if (prompt) { if (currentPrompt == null) { currentPrompt = new PromptCommand(); } if (currentChoice == null) { if (lineData[0].Equals("[end]")) { commands.Add(currentPrompt); currentPrompt = null; prompt = false; } else { currentChoice = lineData[0]; } } else { if (lineData[0].Equals("[end]")) { currentPrompt.addConsequences(currentChoice, parse(currentConsequences)); currentConsequences = ""; currentChoice = null; } else { currentConsequences += line + "\r\n"; } } } else { Command command; try { command = (Command)(Enum.Parse(typeof(Command), lineData[0].ToUpper())); } catch (ArgumentException e) { throw new ArgumentException("Invalid command: " + lineData[0]); } switch (command) { case Command.SPEAKER: commands.Add(new SpeakerCommand(lineData[1])); break; case Command.TEXT: commands.Add(new TextCommand(lineData[1])); break; case Command.SPEAKERTEXT: commands.Add(new SpeakerCommand(lineData[1])); commands.Add(new TextCommand(lineData[2])); break; case Command.TEXTBLOCK: textBlock = true; break; case Command.PORTRAIT: commands.Add(new PortraitCommand(lineData[1], lineData[2][0] /*, lineData[3][0]*/)); break; case Command.BACKGROUND: commands.Add(new BackgroundCommand(lineData[1])); break; case Command.LINK: commands.Add(new LinkCommand(lineData[1])); break; case Command.ADDPOINTS: commands.Add(new AddPointsCommand(lineData[1], Int16.Parse(lineData[2]))); break; case Command.PROMPT: prompt = true; break; case Command.DATECHECK: commands.Add(new DateCheckCommand(lineData[1])); break; case Command.FINALCHECK: commands.Add(new FinalCheckCommand()); break; } } } } return(commands); }
private static StringBuilder PrintCommandSummaryHelp(PromptCommand cmd, int indent = Indent) { return(new StringBuilder().AppendLine($"{string.Empty.PadRight(indent)}{cmd.CommandText.PadRight(PadCommand)} {cmd.HelpText,-30}")); }
public PromptCommand(PromptCommand pcommand) { _name = pcommand.Name; _path = pcommand.Path; _arguments = pcommand.Arguments; }
public CmdPromptCmdletContext(CommandContext context, PromptCommand prompt) : base(context, prompt) { }