Streamlining

This commit is contained in:
Lani Aung
2021-10-10 09:28:59 -06:00
parent 5646b3451f
commit e09f00945b
9 changed files with 105 additions and 99 deletions
+31 -32
View File
@@ -1,57 +1,56 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using fxl.codes.kisekae.Services;
using Microsoft.Extensions.Logging;
namespace fxl.codes.kisekae.Models
{
public class CelModel
{
public const string Regex = @"#(\d*)\.?(\d*)\s*([\w\d\-]*\.[cCeElL]*)\s*\*?(\d*)?\s*\:?([\d\s]*)?;?([\w\d\-\s\%]*)";
private const string Regex = @"#(?<Id>\d*)\.?(?<Fix>\d*)\s*(?<FileName>[\w\d\-]*\.[cCeElL]*)\s*\*?(?<PaletteId>\d*)?\s*\:?(?<Sets>[\d\s]*)?;?(?<Comment>[\w\d\-\s\%]*)";
public Dictionary<int, string> ImageByPalette = new();
public CelModel(string line)
internal CelModel(ILogger<ConfigurationReaderService> logger, string line)
{
logger.LogTrace($"Parsing cel line {line}");
var matcher = new Regex(Regex);
var match = matcher.Match(line);
foreach (var groupName in matcher.GetGroupNames())
{
if (groupName == "0") continue;
var property = typeof(CelModel).GetProperty(groupName);
if (property == null) continue;
match.Groups.TryGetValue(groupName, out var group);
if (string.IsNullOrEmpty(group?.Value)) continue;
var value = group.Value.Trim();
switch (groupName)
if (string.IsNullOrEmpty(group?.Value))
{
case "1":
Id = int.Parse(value);
break;
case "2":
Fix = int.Parse(value);
break;
case "3":
FileName = value;
break;
case "4":
PaletteId = int.Parse(value);
break;
case "5":
foreach (var id in value.Split(' ', StringSplitOptions.RemoveEmptyEntries)) Sets[int.Parse(id)] = true;
break;
case "6":
Comment = value;
break;
logger.LogWarning($"Unable to find regex group {groupName} in {line}");
continue;
}
var value = group.Value.Trim();
if (property.PropertyType == typeof(string)) property.SetValue(this, value);
if (property.PropertyType == typeof(int)) property.SetValue(this, int.Parse(value));
if (property.PropertyType != Sets.GetType()) continue;
foreach (var id in value.Split(' ', StringSplitOptions.RemoveEmptyEntries))
{
var success = int.TryParse(id, out var result);
if (success) Sets[result] = true;
else logger.LogWarning($"Unable to parse {id} as cel sets");
}
}
}
public int Id { get; }
public int Fix { get; }
public string FileName { get; }
public int PaletteId { get; }
public int Id { get; init; }
public int Fix { get; init; }
public string FileName { get; init; }
public int PaletteId { get; init; }
public bool[] Sets { get; } = new bool[10];
public string Comment { get; }
public Point[] InitialPositions { get; } = new Point[10];
public string Comment { get; init; }
public Coordinate[] InitialPositions { get; } = new Coordinate[10];
public string DefaultImage => ImageByPalette.ContainsKey(PaletteId) ? ImageByPalette[PaletteId] : null;
public Coordinate Offset { get; set; }
}
}