ToString() 공개 메소드

public ToString ( ) : string
리턴 string
        /// <summary>
        /// Exports the current state to json format, in order to save the game.
        /// </summary>
        /// <returns>The save state in json format.</returns>
        public string ToJson()
        {
            var writer = new SimpleJson.Writer();

            WriteJson(writer);
            return(writer.ToString());
        }
예제 #2
0
        void PrintAllMessages()
        {
            // { "issues": ["ERROR: blah", "WARNING: blah"] }
            if (opts.jsonOutput)
            {
                var writer = new Runtime.SimpleJson.Writer();

                writer.WriteObjectStart();
                writer.WritePropertyStart("issues");
                writer.WriteArrayStart();
                foreach (string msg in _authorMessages)
                {
                    writer.Write(msg);
                }
                foreach (string msg in _warnings)
                {
                    writer.Write(msg);
                }
                foreach (string msg in _errors)
                {
                    writer.Write(msg);
                }
                writer.WriteArrayEnd();
                writer.WritePropertyEnd();
                writer.WriteObjectEnd();
                Console.Write(writer.ToString());
            }

            // Human consumption
            else
            {
                PrintIssues(_authorMessages, ConsoleColor.Green);
                PrintIssues(_warnings, ConsoleColor.Blue);
                PrintIssues(_errors, ConsoleColor.Red);
            }

            _authorMessages.Clear();
            _warnings.Clear();
            _errors.Clear();
        }
예제 #3
0
        CommandLineTool(string[] args)
        {
            // Set console's output encoding to UTF-8
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            if (ProcessArguments(args) == false)
            {
                ExitWithUsageInstructions();
            }

            if (opts.inputFile == null)
            {
                ExitWithUsageInstructions();
            }

            string inputString      = null;
            string workingDirectory = Directory.GetCurrentDirectory();

            if (opts.outputFile == null)
            {
                opts.outputFile = Path.ChangeExtension(opts.inputFile, ".ink.json");
            }

            if (!Path.IsPathRooted(opts.outputFile))
            {
                opts.outputFile = Path.Combine(workingDirectory, opts.outputFile);
            }

            try {
                string fullFilename = opts.inputFile;
                if (!Path.IsPathRooted(fullFilename))
                {
                    fullFilename = Path.Combine(workingDirectory, fullFilename);
                }

                // Make the working directory the directory for the root ink file,
                // so that relative paths for INCLUDE files are correct.
                workingDirectory = Path.GetDirectoryName(fullFilename);
                Directory.SetCurrentDirectory(workingDirectory);

                // Now make the input file relative to the working directory,
                // but just getting the file's actual name.
                opts.inputFile = Path.GetFileName(fullFilename);

                inputString = File.ReadAllText(opts.inputFile);
            }
            catch {
                Console.WriteLine("Could not open file '" + opts.inputFile + "'");
                Environment.Exit(ExitCodeError);
            }

            var inputIsJson = opts.inputFile.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase);

            if (inputIsJson && opts.stats)
            {
                Console.WriteLine("Cannot show stats for .json, only for .ink");
                Environment.Exit(ExitCodeError);
            }

            Parsed.Story  parsedStory = null;
            Runtime.Story story       = null;
            Compiler      compiler    = null;

            // Loading a normal ink file (as opposed to an already compiled json file)
            if (!inputIsJson)
            {
                compiler = new Compiler(inputString, new Compiler.Options {
                    sourceFilename = opts.inputFile,
                    pluginNames    = pluginNames,
                    countAllVisits = opts.countAllVisits,
                    errorHandler   = OnError,
                    fileHandler    = this
                });

                // Only want stats, don't need to code-gen
                if (opts.stats)
                {
                    parsedStory = compiler.Parse();

                    // Print any errors
                    PrintAllMessages();

                    // Generate stats, then print as JSON
                    var stats = Ink.Stats.Generate(compiler.parsedStory);

                    var writer = new Runtime.SimpleJson.Writer();

                    writer.WriteObjectStart();
                    writer.WriteProperty("words", stats.words);
                    writer.WriteProperty("knots", stats.knots);
                    writer.WriteProperty("stitches", stats.stitches);
                    writer.WriteProperty("functions", stats.functions);
                    writer.WriteProperty("choices", stats.choices);
                    writer.WriteProperty("gathers", stats.gathers);
                    writer.WriteProperty("diverts", stats.diverts);
                    writer.WriteObjectEnd();

                    Console.WriteLine(writer.ToString());

                    return;
                }

                // Full compile
                else
                {
                    story = compiler.Compile();
                }
            }

            // Opening up a compiled json file for playing
            else
            {
                story = new Runtime.Story(inputString);

                // No purpose for loading an already compiled file other than to play it
                opts.playMode = true;
            }

            PrintAllMessages();

            if (story == null || _errors.Count > 0)
            {
                Environment.Exit(ExitCodeError);
            }

            // Play mode
            if (opts.playMode)
            {
                _playing = true;

                // Always allow ink external fallbacks
                story.allowExternalFunctionFallbacks = true;

                var player = new CommandLinePlayer(story, false, compiler, opts.keepOpenAfterStoryFinish);

                //Capture a CTRL+C key combo so we can restore the console's foreground color back to normal when exiting
                Console.CancelKeyPress += OnExit;

                try {
                    player.Begin();
                } catch (Runtime.StoryException e) {
                    if (e.Message.Contains("Missing function binding"))
                    {
                        OnError(e.Message, ErrorType.Error);
                        PrintAllMessages();
                    }
                    else
                    {
                        throw e;
                    }
                } catch (System.Exception e) {
                    string storyPath = "<END>";
                    var    path      = story.state.currentPathString;
                    if (path != null)
                    {
                        storyPath = path.ToString();
                    }
                    throw new System.Exception(e.Message + " (Internal story path: " + storyPath + ")", e);
                }
            }

            // Compile mode
            else
            {
                var jsonStr = story.ToJson();

                try {
                    File.WriteAllText(opts.outputFile, jsonStr, System.Text.Encoding.UTF8);
                } catch {
                    Console.WriteLine("Could not write to output file '" + opts.outputFile + "'");
                    Environment.Exit(ExitCodeError);
                }
            }
        }
예제 #4
0
        public void Begin()
        {
            EvaluateStory();

            var rand = new Random();

            while (story.currentChoices.Count > 0 || this.keepOpenAfterStoryFinish)
            {
                var choices = story.currentChoices;

                var    choiceIdx        = 0;
                bool   choiceIsValid    = false;
                string userDivertedPath = null;

                // autoPlay: Pick random choice
                if (autoPlay)
                {
                    choiceIdx = rand.Next() % choices.Count;
                }

                // Normal: Ask user for choice number
                else
                {
                    if (!_jsonOutput)
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;

                        // Add extra newline to ensure that the choice is
                        // on a separate line.
                        Console.WriteLine();

                        int i = 1;
                        foreach (Choice choice in choices)
                        {
                            Console.WriteLine("{0}: {1}", i, choice.text);
                            i++;
                        }
                    }

                    else
                    {
                        var writer = new Runtime.SimpleJson.Writer();
                        writer.WriteObjectStart();
                        writer.WritePropertyStart("choices");
                        writer.WriteArrayStart();
                        foreach (var choice in choices)
                        {
                            writer.Write(choice.text);
                        }
                        writer.WriteArrayEnd();
                        writer.WritePropertyEnd();
                        writer.WriteObjectEnd();
                        Console.WriteLine(writer.ToString());
                    }


                    do
                    {
                        if (!_jsonOutput)
                        {
                            // Prompt
                            Console.Write("?> ");
                        }

                        else
                        {
                            // Johnny Five, he's alive!
                            Console.Write("{\"needInput\": true}");
                        }

                        string userInput = Console.ReadLine();

                        // If we have null user input, it means that we're
                        // "at the end of the stream", or in other words, the input
                        // stream has closed, so there's nothing more we can do.
                        // We return immediately, since otherwise we get into a busy
                        // loop waiting for user input.
                        if (userInput == null)
                        {
                            if (_jsonOutput)
                            {
                                Console.WriteLine("{\"close\": true}");
                            }
                            else
                            {
                                Console.WriteLine("<User input stream closed.>");
                            }
                            return;
                        }

                        var result = _compiler.ReadCommandLineInput(userInput);

                        if (result.output != null)
                        {
                            if (_jsonOutput)
                            {
                                var writer = new Runtime.SimpleJson.Writer();
                                writer.WriteObjectStart();
                                writer.WriteProperty("cmdOutput", result.output);
                                writer.WriteObjectEnd();
                                Console.WriteLine(writer.ToString());
                            }
                            else
                            {
                                Console.WriteLine(result.output);
                            }
                        }

                        if (result.requestsExit)
                        {
                            return;
                        }

                        if (result.divertedPath != null)
                        {
                            userDivertedPath = result.divertedPath;
                        }

                        if (result.choiceIdx >= 0)
                        {
                            if (result.choiceIdx >= choices.Count)
                            {
                                if (!_jsonOutput)  // fail silently in json mode
                                {
                                    Console.WriteLine("Choice out of range");
                                }
                            }
                            else
                            {
                                choiceIdx     = result.choiceIdx;
                                choiceIsValid = true;
                            }
                        }
                    } while(!choiceIsValid && userDivertedPath == null);
                }

                Console.ResetColor();

                if (choiceIsValid)
                {
                    story.ChooseChoiceIndex(choiceIdx);
                }
                else if (userDivertedPath != null)
                {
                    story.ChoosePathString(userDivertedPath);
                    userDivertedPath = null;
                }

                EvaluateStory();
            }
        }
예제 #5
0
        void EvaluateStory()
        {
            while (story.canContinue)
            {
                story.Continue();

                _compiler.RetrieveDebugSourceForLatestContent();

                if (_jsonOutput)
                {
                    var writer = new Runtime.SimpleJson.Writer();
                    writer.WriteObjectStart();
                    writer.WriteProperty("text", story.currentText);
                    writer.WriteObjectEnd();
                    Console.WriteLine(writer.ToString());
                }
                else
                {
                    Console.Write(story.currentText);
                }

                var tags = story.currentTags;
                if (tags.Count > 0)
                {
                    if (_jsonOutput)
                    {
                        var writer = new Runtime.SimpleJson.Writer();
                        writer.WriteObjectStart();
                        writer.WritePropertyStart("tags");
                        writer.WriteArrayStart();
                        foreach (var tag in tags)
                        {
                            writer.Write(tag);
                        }
                        writer.WriteArrayEnd();
                        writer.WritePropertyEnd();
                        writer.WriteObjectEnd();
                        Console.WriteLine(writer.ToString());
                    }
                    else
                    {
                        Console.WriteLine("# tags: " + string.Join(", ", tags));
                    }
                }

                Runtime.SimpleJson.Writer issueWriter = null;
                if (_jsonOutput && (_errors.Count > 0 || _warnings.Count > 0))
                {
                    issueWriter = new Runtime.SimpleJson.Writer();
                    issueWriter.WriteObjectStart();
                    issueWriter.WritePropertyStart("issues");
                    issueWriter.WriteArrayStart();

                    if (_errors.Count > 0)
                    {
                        foreach (var errorMsg in _errors)
                        {
                            issueWriter.Write(errorMsg);
                        }
                    }
                    if (_warnings.Count > 0)
                    {
                        foreach (var warningMsg in _warnings)
                        {
                            issueWriter.Write(warningMsg);
                        }
                    }

                    issueWriter.WriteArrayEnd();
                    issueWriter.WritePropertyEnd();
                    issueWriter.WriteObjectEnd();
                    Console.WriteLine(issueWriter.ToString());
                }

                if (_errors.Count > 0 && !_jsonOutput)
                {
                    foreach (var errorMsg in _errors)
                    {
                        Console.WriteLine(errorMsg, ConsoleColor.Red);
                    }
                }

                if (_warnings.Count > 0 && !_jsonOutput)
                {
                    foreach (var warningMsg in _warnings)
                    {
                        Console.WriteLine(warningMsg, ConsoleColor.Blue);
                    }
                }

                _errors.Clear();
                _warnings.Clear();
            }

            if (story.currentChoices.Count == 0 && keepOpenAfterStoryFinish)
            {
                if (_jsonOutput)
                {
                    Console.WriteLine("{\"end\": true}");
                }
                else
                {
                    Console.WriteLine("--- End of story ---");
                }
            }
        }
예제 #6
0
        public string RequestExternalFunctionResult(string functionName, object[] args)
        {
            var arguments = "";

            for (var i = 0; i < args.Length; i++)
            {
                if (args[i] is string)
                {
                    args[i] = "\"" + args[i] + "\"";
                }
                arguments += args[i];
                if (i + 1 < args.Length)
                {
                    arguments += ", ";
                }
            }

            if (!_jsonOutput)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("Enter result for " + functionName + "(" + arguments + ") > ");
                Console.ResetColor();
            }
            else
            {
                var writer = new Runtime.SimpleJson.Writer();
                writer.WriteObjectStart();
                writer.WriteProperty("external-function", functionName);
                if (arguments != "")
                {
                    writer.WritePropertyStart("arguments");
                    writer.WriteArrayStart();
                    writer.Write(arguments);
                    writer.WriteArrayEnd();
                    writer.WritePropertyEnd();
                }
                writer.WriteObjectEnd();
                Console.WriteLine(writer.ToString());
            }

            string userInput = Console.ReadLine();

            // If we have null user input, it means that we're
            // "at the end of the stream", or in other words, the input
            // stream has closed, so there's nothing more we can do.
            // We return immediately, since otherwise we get into a busy
            // loop waiting for user input.
            if (userInput == null)
            {
                if (_jsonOutput)
                {
                    Console.WriteLine("{\"close\": true}");
                }
                else
                {
                    Console.WriteLine("<User input stream closed.>");
                }
            }

            return(userInput);
        }