private void AddContent(List <string> sourceCode, StringBuilder output) { var codeBlockHandler = new CodeBlockHandler(output); for (var i = 0; i < sourceCode.Count; i++) { var line = sourceCode[i]; var trimmed = line.TrimStart(); if (trimmed.StartsWith("//md ") || trimmed.Equals("//md")) { codeBlockHandler.Exit(); trimmed = trimmed.Remove(0, trimmed.Length > 5 ? 5 : 4); output.AppendLine(trimmed); } else if (trimmed.StartsWith("//md_hide-next")) { i++; } else if (trimmed.StartsWith("//md_sc")) { codeBlockHandler.Exit(); var shortcode = trimmed.Replace("//md_sc ", ""); HandleShortcode(shortcode, output); } else if (trimmed.StartsWith("#region hidden")) { var nestLevel = 1; i++; var description = trimmed.Replace("#region hidden:", ""); if (description != "" && trimmed.Contains("hidden:")) { output.AppendLine(); output.AppendLine(line.Replace("#region hidden:", "/* ") + " */"); output.AppendLine(); } while (i < sourceCode.Count) { var nextLine = sourceCode[i].Trim(); if (nextLine.StartsWith("#region")) { nestLevel++; } else if (nextLine.StartsWith("#endregion")) { nestLevel--; } if (nestLevel == 0) { break; } i++; } } else if (codeBlockHandler.IsInside) { output.AppendLine(line); } else if (!string.IsNullOrEmpty(trimmed)) { codeBlockHandler.Enter(); output.AppendLine(line); } else { output.AppendLine(); } } codeBlockHandler.Exit(); }
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(); }