oecs-sharp/OECS.PlayTest/PlayLog.cs

57 lines
1.6 KiB
C#

using System.Text;
namespace OECS.PlayTest;
/// <summary>
/// Builds a sectioned play log with header, sections, and final snapshot,
/// then saves it to a .playlog file.
/// </summary>
public class PlayLog
{
public string Header { get; set; } = "";
public string FinalSnapshot { get; set; } = "";
private readonly List<(string Title, List<string> Lines)> _sections = new();
/// <summary>
/// Adds a section with the given title and lines.
/// </summary>
public void AddSection(string title, IEnumerable<string> lines)
{
_sections.Add((title, lines.ToList()));
}
/// <summary>
/// Builds the complete play log text.
/// </summary>
public string Build()
{
var sb = new StringBuilder();
sb.AppendLine(Header);
sb.AppendLine(new string('=', Header.Length));
sb.AppendLine();
foreach (var (title, lines) in _sections)
{
sb.AppendLine($"--- {title} ---");
foreach (var line in lines)
sb.AppendLine($" {line}");
sb.AppendLine();
}
sb.AppendLine("--- Final State ---");
sb.AppendLine(FinalSnapshot);
return sb.ToString();
}
/// <summary>
/// Saves the play log to AppContext.BaseDirectory/playlogs/<paramref name="filename"/>
/// and returns the full path.
/// </summary>
public string SaveTo(string filename)
{
var dir = Path.Combine(AppContext.BaseDirectory, "playlogs");
Directory.CreateDirectory(dir);
var path = Path.Combine(dir, filename);
File.WriteAllText(path, Build());
return path;
}
}