66 lines
2.1 KiB
C#
66 lines
2.1 KiB
C#
using System.Text;
|
|
|
|
namespace Domain;
|
|
|
|
public class ExpIniFileGenerator(uint rowCount, uint oddColumnCount, uint evenColumnCount)
|
|
{
|
|
private const string _lineEnding = "\r\n";
|
|
|
|
public string Generate(IEnumerable<FrameExpInfo> frameInfos)
|
|
{
|
|
var groups = frameInfos
|
|
.DistinctBy(q => new {q.Row, q.Column})
|
|
.GroupBy(q => q.Row, (row, columns) => new { Row = row, Columns = columns });
|
|
var sb = new StringBuilder();
|
|
AppendLine(sb, oddColumnCount.ToString());
|
|
AppendLine(sb, evenColumnCount.ToString());
|
|
AppendLine(sb, rowCount.ToString());
|
|
|
|
foreach (var row in groups.OrderBy(q => q.Row))
|
|
{
|
|
bool? isPreviousContainAnything = null;
|
|
var stateAccumulator = 0u;
|
|
foreach (var column in row.Columns)
|
|
{
|
|
isPreviousContainAnything ??= column.IsContainAnything;
|
|
|
|
if (isPreviousContainAnything == column.IsContainAnything)
|
|
{
|
|
stateAccumulator++;
|
|
}
|
|
else
|
|
{
|
|
sb.Append(GetExpValue(isPreviousContainAnything.Value, stateAccumulator));
|
|
stateAccumulator = 0u;
|
|
isPreviousContainAnything = column.IsContainAnything;
|
|
}
|
|
}
|
|
|
|
if (stateAccumulator > 0 && isPreviousContainAnything.HasValue)
|
|
{
|
|
sb.Append(GetExpValue(isPreviousContainAnything.Value, stateAccumulator));
|
|
}
|
|
sb.Append(_lineEnding);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static void AppendLine(StringBuilder sb, string text)
|
|
{
|
|
sb.Append(text);
|
|
sb.Append(_lineEnding);
|
|
}
|
|
|
|
private static string GetExpValue(bool isContainAnything, uint accumulator)
|
|
{
|
|
return string.Concat(accumulator.ToString(), isContainAnything ? 'e' : 'z');
|
|
}
|
|
|
|
public class FrameExpInfo
|
|
{
|
|
public uint Row { get; init; }
|
|
public uint Column { get; init; }
|
|
public bool IsContainAnything { get; set; }
|
|
}
|
|
} |