ExportRuntime() public method

public ExportRuntime ( ErrorHandler errorHandler = null ) : Runtime.Story
errorHandler ErrorHandler
return Runtime.Story
Beispiel #1
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.testMode)
            {
                opts.inputFile = "test.ink";
            }

            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);
            }

            if (opts.stressTest)
            {
                StressTestContentGenerator stressTestContent = null;
                TimeOperation("Generating test content", () => {
                    stressTestContent = new StressTestContentGenerator(100);
                });

                Console.WriteLine("Generated ~{0}k of test ink", stressTestContent.sizeInKiloChars);

                inputString = stressTestContent.content;
            }
            else
            {
                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);
                }
            }

            InkParser parser = null;

            Parsed.Story  parsedStory = null;
            Runtime.Story story       = null;
            errors         = new List <string> ();
            warnings       = new List <string> ();
            authorMessages = new List <string> ();
            var pluginManager = new PluginManager(pluginNames);

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

            // Loading a normal ink file (as opposed to an already compiled json file)
            if (!inputIsJson)
            {
                TimeOperation("Creating parser", () => {
                    parser = new InkParser(inputString, opts.inputFile, OnError);
                });

                TimeOperation("Parsing", () => {
                    parsedStory = parser.Parse();
                });

                TimeOperation("PostParsePlugins", () => {
                    pluginManager.PostParse(parsedStory);
                });

                if (parsedStory != null && errors.Count == 0)
                {
                    if (opts.countAllVisits)
                    {
                        parsedStory.countAllVisits = true;
                    }

                    TimeOperation("Exporting runtime", () => {
                        story = parsedStory.ExportRuntime(OnError);
                    });

                    TimeOperation("PostParsePlugins", () => {
                        pluginManager.PostExport(parsedStory, story);
                    });
                }
            }

            // 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);
            }

            // JSON round trip testing
            //if (opts.testMode) {
            //    var jsonStr = story.ToJsonString ();
            //    Console.WriteLine (jsonStr);

            //    Console.WriteLine ("---------------------------------------------------");

            //    var reloadedStory = new Runtime.Story (jsonStr);
            //    var newJsonStr = reloadedStory.ToJsonString ();
            //    Console.WriteLine (newJsonStr);

            //    story = reloadedStory;
            //}

            // Play mode
            // Test mode may use "-tp" in commmand line args to specify that
            // the test script is also played
            if (opts.playMode)
            {
                _playing = true;

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

                var player = new CommandLinePlayer(story, false, parsedStory, 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.currentPath;
                    if (path != null)
                    {
                        storyPath = path.ToString();
                    }
                    throw new System.Exception(e.Message + " (Internal story path: " + storyPath + ")", e);
                }
            }

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

                try {
                    File.WriteAllText(opts.outputFile, jsonStr, System.Text.Encoding.UTF8);
                } catch {
                    Console.WriteLine("Could not write to output file '" + opts.outputFile + "'");
                    Environment.Exit(ExitCodeError);
                }
            }
        }
Beispiel #2
0
        CommandLineTool(string[] args)
        {
            if (ProcessArguments(args) == false)
            {
                ExitWithUsageInstructions();
            }

            if (opts.testMode)
            {
                opts.inputFile = "test.ink";
            }

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

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

            string inputString   = null;
            string rootDirectory = System.IO.Directory.GetCurrentDirectory();

            if (opts.workingDirectory != null)
            {
                rootDirectory = Path.GetFullPath(opts.workingDirectory);
            }

            if (opts.stressTest)
            {
                StressTestContentGenerator stressTestContent = null;
                TimeOperation("Generating test content", () => {
                    stressTestContent = new StressTestContentGenerator(100);
                });

                Console.WriteLine("Generated ~{0}k of test ink", stressTestContent.sizeInKiloChars);

                inputString = stressTestContent.content;
            }
            else
            {
                try {
                    string fullFilename = opts.inputFile;
                    if (!Path.IsPathRooted(fullFilename))
                    {
                        fullFilename = Path.Combine(rootDirectory, fullFilename);
                    }

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

            InkParser parser = null;

            Parsed.Story  parsedStory = null;
            Runtime.Story story       = null;
            errors         = new List <string> ();
            warnings       = new List <string> ();
            authorMessages = new List <string> ();
            var pluginManager = new PluginManager(pluginNames);

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

            // Loading a normal ink file (as opposed to an already compiled json file)
            if (!inputIsJson)
            {
                TimeOperation("Creating parser", () => {
                    parser = new InkParser(inputString, opts.inputFile, rootDirectory, OnError);
                });

                TimeOperation("Parsing", () => {
                    parsedStory = parser.Parse();
                });

                TimeOperation("PostParsePlugins", () => {
                    pluginManager.PostParse(parsedStory);
                });

                if (parsedStory != null)
                {
                    if (opts.countAllVisits)
                    {
                        parsedStory.countAllVisits = true;
                    }

                    TimeOperation("Exporting runtime", () => {
                        story = parsedStory.ExportRuntime(OnError);
                    });

                    TimeOperation("PostParsePlugins", () => {
                        pluginManager.PostExport(parsedStory, story);
                    });
                }
            }

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

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

            PrintMessages(authorMessages, ConsoleColor.Green);
            PrintMessages(warnings, ConsoleColor.Blue);
            PrintMessages(errors, ConsoleColor.Red);

            if (story == null)
            {
                Environment.Exit(ExitCodeError);
            }

            // JSON round trip testing
//            if (opts.testMode) {
//                var jsonStr = story.ToJsonString (indented:true);
//                Console.WriteLine (jsonStr);
//
//                Console.WriteLine ("---------------------------------------------------");
//
//                var reloadedStory = Runtime.Story.CreateWithJson (jsonStr);
//                var newJsonStr = reloadedStory.ToJsonString (indented: true);
//                Console.WriteLine (newJsonStr);
//
//                story = reloadedStory;
//            }

            // Play mode
            // Test mode may use "-tp" in commmand line args to specify that
            // the test script is also played
            if (opts.playMode)
            {
                // Always allow ink external fallbacks
                story.allowExternalFunctionFallbacks = true;

                var player = new CommandLinePlayer(story, false, parsedStory);
                player.Begin();
            }

            // Compile mode
            else
            {
                var jsonStr = story.ToJsonString(opts.indentedJson);

                try {
                    File.WriteAllText(opts.outputFile, jsonStr, System.Text.Encoding.UTF8);
                } catch {
                    Console.WriteLine("Could write to output file '" + opts.outputFile + "'");
                    Environment.Exit(ExitCodeError);
                }
            }
        }