Пример #1
0
        private static void Pack(RantPackage package, string contentPath)
        {
            foreach (var path in Directory.EnumerateFiles(contentPath, "*.*", SearchOption.AllDirectories)
                     .Where(p =>
                            p.EndsWith(".rant", StringComparison.OrdinalIgnoreCase) ||
                            p.EndsWith(".rants", StringComparison.OrdinalIgnoreCase) ||
                            p.EndsWith(".rantpgm", StringComparison.OrdinalIgnoreCase)))
            {
                RantProgram pattern;
                switch (Path.GetExtension(path).ToLower())
                {
                case ".rantpgm":
                    pattern = RantProgram.LoadFile(path);
                    break;

                default:
                    pattern = RantProgram.CompileFile(path);
                    break;
                }
                string relativePath;
                TryGetRelativePath(contentPath, path, out relativePath, true);
                pattern.Name = relativePath;
                package.AddResource(pattern);
                Console.WriteLine("+ " + pattern.Name);
            }

            foreach (string path in Directory.GetFiles(contentPath, "*.table", SearchOption.AllDirectories))
            {
                Console.WriteLine("+ " + path);
                var table = RantDictionaryTable.FromStream(Path.GetFileNameWithoutExtension(path), File.Open(path, FileMode.Open));
                package.AddResource(table);
            }
        }
Пример #2
0
        private static void PrintOutput(RantEngine engine, string source, bool isFile = false)
        {
            try
            {
                var sw = new Stopwatch();

                sw.Start();
                var pattern = isFile
                    ? source.EndsWith(".rantpgm")
                        ? RantProgram.LoadFile(source)
                        : RantProgram.CompileFile(source)
                    : RantProgram.CompileString(source);
                sw.Stop();

                var compileTime = sw.Elapsed;

                sw.Restart();
                var output = USE_SEED
                    ? engine.Do(pattern, SEED, 0, PATTERN_TIMEOUT)
                    : engine.Do(pattern, 0, PATTERN_TIMEOUT);
                sw.Stop();

                var runTime = sw.Elapsed;

                bool writeToFile = !string.IsNullOrEmpty(Property("out"));
                foreach (var chan in output)
                {
                    if (chan.Name != "main")
                    {
                        if (Flag("main"))
                        {
                            continue;
                        }
                        if (!writeToFile)
                        {
                            ForegroundColor = ConsoleColor.DarkCyan;
                            WriteLine($"{chan.Name}:");
                            ResetColor();
                        }
                    }
                    ForegroundColor = ConsoleColor.Green;
                    if (chan.Value.Length > 0)
                    {
                        if (pattern.Type == RantProgramOrigin.File && writeToFile)
                        {
                            string path = Property("out");
                            File.WriteAllText(
                                Path.Combine(Path.GetDirectoryName(path),
                                             Path.GetFileNameWithoutExtension(path) +
                                             (chan.Name != "main"
                                        ? $".{chan.Name}{Path.GetExtension(path)}"
                                        : Path.GetExtension(path))),
                                chan.Value);
                        }
                        else
                        {
#if DEBUG
                            WriteLine($"'{chan.Value}'");
#else
                            WriteLine(chan.Value.Normalize(NormalizationForm.FormC));
#endif
                        }
                    }
                    else if (!writeToFile)
                    {
                        ForegroundColor = ConsoleColor.DarkGray;
                        if (pattern.Type != RantProgramOrigin.File)
                        {
                            WriteLine("[Empty]");
                        }
                    }
                    ResetColor();
                    WriteLine();
                }

                if (!Flag("nostats"))
                {
                    PrintStats(
                        new Stat("Seed",
                                 $"{output.Seed:X16}{(output.BaseGeneration != 0 ? ":" + output.BaseGeneration : string.Empty)}"),
                        new Stat(isFile && source.EndsWith(".rantpgm") ? "Load Time" : "Compile Time", compileTime.ToString("c")),
                        new Stat("Run Time", runTime.ToString("c"))
                        );
                    WriteLine();
                }
                if (isFile && Flag("wait"))
                {
                    Console.ReadKey();
                }
            }
#if !DEBUG
            catch (RantRuntimeException e)
            {
                ForegroundColor = ConsoleColor.Red;
                WriteLine(e);
            }
            catch (RantCompilerException e)
            {
                ForegroundColor = ConsoleColor.Yellow;
                WriteLine(e.Message);
            }
            catch (Exception e)
            {
                WriteLine(e.ToString()); // Print the whole stack trace if it isn't a Rant error
            }
#endif
            finally
            {
                ResetColor();
            }
        }