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; }
}
}
+14
View File
@@ -0,0 +1,14 @@
namespace fxl.codes.kisekae.Models
{
public class Coordinate
{
public Coordinate(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
}
}
+5 -1
View File
@@ -1,12 +1,16 @@
using System;
using fxl.codes.kisekae.Services;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
namespace fxl.codes.kisekae.Models
{
public class PaletteModel
{
public PaletteModel(string line)
internal PaletteModel(ILogger<ConfigurationReaderService> logger, string line)
{
logger.LogTrace($"Parsing palette line {line}");
var parts = line.Split(';');
FileName = parts[0].Replace("%", "").Trim();
+3 -2
View File
@@ -1,15 +1,16 @@
using System.Collections.Generic;
using SixLabors.ImageSharp;
namespace fxl.codes.kisekae.Models
{
public class PlaysetModel
{
public int[] CurrentPalettes = new int[10];
public int Height { get; set; }
public int Width { get; set; }
public int BorderColorIndex { get; set; }
public Color BorderColor { get; set; }
public List<PaletteModel> Palettes { get; } = new();
public List<CelModel> Cels { get; } = new();
public int[] CurrentPalettes = new int[10];
}
}
-14
View File
@@ -1,14 +0,0 @@
namespace fxl.codes.kisekae.Models
{
public class Point
{
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
}
}