Esempio n. 1
0
        private void GenerateExample <TRoom>(IExampleGrid2D <TRoom> example)
        {
            AssetsFolder = Path.Combine(outputFolder, example.Options.DocsFileName);
            Directory.CreateDirectory(AssetsFolder);

            var className = example.GetType().Name;

            sourceCodeParser = new SourceCodeParser(Path.Combine(sourceFolder, $"{className}.cs"));

            var sourceCode    = sourceCodeParser.GetMethod(example.Options.EntryPointMethod, false);
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("---");
            stringBuilder.AppendLine($"title: {example.Options.Name}");
            stringBuilder.AppendLine("---");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("import { Gallery, GalleryImage } from \"@theme/Gallery\";");
            stringBuilder.AppendLine();

            // stringBuilder.AppendLine($"> The source code for this example can be found at the end of this page.");
            stringBuilder.AppendLine();

            AddContent(sourceCode, stringBuilder);

            File.WriteAllText(Path.Combine(outputFolder, $"{example.Options.DocsFileName}.md"), stringBuilder.ToString());

            if (example.Options.IncludeResults)
            {
                AddResults(stringBuilder, example);
            }

            if (example.Options.IncludeSourceCode)
            {
                AddSourceCode(stringBuilder);
            }

            Console.WriteLine(stringBuilder.ToString());
            File.WriteAllText(Path.Combine(outputFolder, $"{example.Options.DocsFileName}.md"), stringBuilder.ToString());

            example.Run();
        }
Esempio n. 2
0
        private void AddResults <TRoom>(StringBuilder output, IExampleGrid2D <TRoom> example)
        {
            var sourceCode       = sourceCodeParser.GetMethod("GetResults", false);
            var results          = example.GetResults().ToList();
            var codeBlockHandler = new CodeBlockHandler(output);

            output.AppendLine("## Results");
            output.AppendLine();

            var resultsCounter = 0;

            foreach (var line in sourceCode)
            {
                var trimmed = line.Trim();

                if (trimmed.StartsWith("//md"))
                {
                    codeBlockHandler.Exit();

                    if (trimmed.Length > 5)
                    {
                        trimmed = trimmed.Remove(0, 5);
                    }
                    else
                    {
                        trimmed = trimmed.Remove(0, 4);
                    }

                    output.AppendLine(trimmed);
                }
                else if (trimmed.Contains("yield") && !trimmed.Contains("yield break"))
                {
                    codeBlockHandler.Exit();

                    var levelDescription = results[resultsCounter];
                    var initStopwatch    = new Stopwatch();
                    initStopwatch.Start();
                    var generator = new GraphBasedGeneratorGrid2D <TRoom>(levelDescription);
                    generator.InjectRandomGenerator(new Random(0));
                    initStopwatch.Stop();

                    var layoutDrawer = new GraphBasedGenerator.Grid2D.Drawing.SVGLayoutDrawer <TRoom>();
                    var oldMapDrawer = new DungeonDrawer <TRoom>();

                    var times = new List <long>();

                    for (int i = 0; i < 4; i++)
                    {
                        var generatorStopwatch = new Stopwatch();
                        generatorStopwatch.Start();
                        var level = generator.GenerateLayout();
                        generatorStopwatch.Stop();

                        times.Add(generatorStopwatch.ElapsedMilliseconds);

                        Console.WriteLine(generatorStopwatch.ElapsedMilliseconds + initStopwatch.ElapsedMilliseconds);

                        var svg = layoutDrawer.DrawLayout(level, 800, forceSquare: true, flipY: true, fixedFontSize: 30);
                        File.WriteAllText(Path.Combine(AssetsFolder, $"{resultsCounter}_{i}.svg"),
                                          svg);

                        var bitmap = oldMapDrawer.DrawLayout(level, new DungeonDrawerOptions()
                        {
                            Width             = 2000,
                            Height            = 2000,
                            PaddingPercentage = 0.1f,
                        });
                        bitmap.Save(Path.Combine(AssetsFolder, $"{resultsCounter}_{i}.png"));
                    }

                    var summaryDrawer = new GeneratorSummaryDrawer <TRoom>();
                    var summary       = summaryDrawer.Draw(levelDescription, 5000, generator);
                    summary.Save(Path.Combine(AssetsFolder, $"{resultsCounter}_summary.png"));

                    output.AppendLine();
                    output.AppendLine("<Gallery cols={2}>");
                    for (int i = 0; i < 4; i++)
                    {
                        output.AppendLine(
                            $"<GalleryImage src={{require('./{example.Options.DocsFileName}/{resultsCounter}_{i}.png').default}} />");
                    }

                    output.AppendLine("</Gallery>");

                    output.AppendLine();
                    output.AppendLine("<div style={{ textAlign: 'center', marginTop: '-15px' }}>");
                    output.AppendLine();
                    output.AppendLine($"*Average time to generate the level: {((times.Average() + initStopwatch.ElapsedMilliseconds) / 1000).ToString("F", CultureInfo.InvariantCulture)}s ({((initStopwatch.ElapsedMilliseconds) / 1000d).ToString("F", CultureInfo.InvariantCulture)}s init, {((times.Average()) / 1000).ToString("F", CultureInfo.InvariantCulture)}s generation itself)*");
                    output.AppendLine();
                    output.AppendLine("</div>");
                    output.AppendLine();

                    resultsCounter++;
                }
                else if (codeBlockHandler.IsInside)
                {
                    output.AppendLine(line);
                }
                else if (!string.IsNullOrEmpty(trimmed))
                {
                    codeBlockHandler.Enter();
                    output.AppendLine(line);
                }
                else
                {
                    output.AppendLine();
                }
            }

            codeBlockHandler.Exit();
        }