Math is hard
This commit is contained in:
@@ -22,9 +22,16 @@ namespace fxl.codes.kisekae.Services
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void ReadConfigurationToDto(Configuration dto, IDictionary<string, Cel> cels, IDictionary<string, Palette> palettes)
|
||||
public void ReadConfiguration(Configuration dto,
|
||||
IDictionary<Configuration, int> backgroundColors,
|
||||
IDictionary<string, Cel> cels,
|
||||
Dictionary<string, Palette> palettes)
|
||||
{
|
||||
_logger.LogInformation($"Reading {dto.Name}");
|
||||
var initialPositions = new StringBuilder();
|
||||
var backgroundColorIndex = 0;
|
||||
var paletteOrder = new List<Palette>();
|
||||
var celLines = new List<string>();
|
||||
|
||||
foreach (var line in dto.Data.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
|
||||
switch (line.ToCharArray()[0])
|
||||
@@ -38,13 +45,13 @@ namespace fxl.codes.kisekae.Services
|
||||
case '[':
|
||||
var borderValue = line[1..];
|
||||
if (borderValue.Contains(';')) borderValue = borderValue.Split(';')[0].Trim();
|
||||
dto.BorderIndex = int.Parse(borderValue);
|
||||
backgroundColorIndex = int.Parse(borderValue);
|
||||
break;
|
||||
case '%':
|
||||
UpdatePalette(line, palettes);
|
||||
SetPaletteOrder(line, palettes, paletteOrder);
|
||||
break;
|
||||
case '#':
|
||||
dto.Cels.Add(SetCelConfig(line, dto, cels, palettes.Values.ToArray()));
|
||||
celLines.Add(line);
|
||||
break;
|
||||
case '$':
|
||||
case ' ':
|
||||
@@ -52,7 +59,9 @@ namespace fxl.codes.kisekae.Services
|
||||
break;
|
||||
}
|
||||
|
||||
foreach (var line in celLines) dto.Cels.Add(SetCelConfig(line, dto, cels, paletteOrder));
|
||||
SetInitialPositions(dto, initialPositions.ToString());
|
||||
backgroundColors.Add(dto, backgroundColorIndex);
|
||||
}
|
||||
|
||||
private static CelConfig SetCelConfig(string line, Configuration configuration, IDictionary<string, Cel> cels, IReadOnlyList<Palette> palettes)
|
||||
@@ -105,7 +114,7 @@ namespace fxl.codes.kisekae.Services
|
||||
var success = int.TryParse(id, out var result);
|
||||
if (!success) continue;
|
||||
|
||||
if (cel.Sets == Set.Unset)
|
||||
if (cel.Sets == Set.None)
|
||||
cel.Sets = (Set)(2 ^ result);
|
||||
else
|
||||
cel.Sets |= (Set)(2 ^ result);
|
||||
@@ -117,31 +126,36 @@ namespace fxl.codes.kisekae.Services
|
||||
return cel;
|
||||
}
|
||||
|
||||
private static void UpdatePalette(string line, IDictionary<string, Palette> palettes)
|
||||
private static void SetPaletteOrder(string line, IReadOnlyDictionary<string, Palette> palettes, ICollection<Palette> paletteOrder)
|
||||
{
|
||||
var parts = line.Split(';');
|
||||
var palette = palettes[parts[0].Trim().ToLowerInvariant().Replace("%", "")];
|
||||
if (parts.Length > 1) palette.Comment = parts[1].Trim();
|
||||
paletteOrder.Add(palette);
|
||||
}
|
||||
|
||||
private static void SetInitialPositions(Configuration dto, string positions)
|
||||
{
|
||||
var sets = positions.Split('$', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var set in sets)
|
||||
for (var index = 0; index < sets.Length; index++)
|
||||
{
|
||||
var set = sets[index];
|
||||
var value = set.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
var paletteGroup = int.Parse(value[0]);
|
||||
|
||||
for (var innerIndex = 1; innerIndex < value.Length; innerIndex++)
|
||||
{
|
||||
if (value[innerIndex] == "*") continue;
|
||||
var point = value[innerIndex].Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (value[innerIndex].Contains('*')) continue;
|
||||
var point = value[innerIndex].Trim().Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var cel in dto.Cels.Where(x => x.Mark == innerIndex - 1))
|
||||
{
|
||||
cel.PaletteGroup = paletteGroup;
|
||||
cel.X = int.Parse(point[0]);
|
||||
cel.Y = int.Parse(point[1]);
|
||||
cel.Positions.Add(new CelPosition
|
||||
{
|
||||
Set = index,
|
||||
X = int.Parse(point[0]),
|
||||
Y = int.Parse(point[1])
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ using fxl.codes.kisekae.Entities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SixLabors.ImageSharp;
|
||||
using Configuration = fxl.codes.kisekae.Entities.Configuration;
|
||||
|
||||
namespace fxl.codes.kisekae.Services
|
||||
{
|
||||
@@ -47,6 +49,8 @@ namespace fxl.codes.kisekae.Services
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.Configurations
|
||||
.Include(x => x.Cels).ThenInclude(x => x.Render)
|
||||
.Include(x => x.Cels).ThenInclude(x => x.Cel)
|
||||
.Include(x => x.Cels).ThenInclude(x => x.Positions)
|
||||
.Include(x => x.Kisekae).ThenInclude(x => x.Palettes).ThenInclude(x => x.Colors)
|
||||
.AsSplitQuery()
|
||||
.FirstOrDefault(x => x.Id == id);
|
||||
@@ -55,7 +59,7 @@ namespace fxl.codes.kisekae.Services
|
||||
public async void StoreToDatabase(IFormFile file)
|
||||
{
|
||||
var memoryStream = await GetAsMemoryStream(file);
|
||||
var checksum = Convert.ToBase64String(await SHA256.Create().ComputeHashAsync(memoryStream));
|
||||
var checksum = Convert.ToBase64String(SHA256.Create().ComputeHash(memoryStream.GetBuffer()));
|
||||
memoryStream.Position = 0; // Reset for re-read
|
||||
|
||||
await using var context = await _contextFactory.CreateDbContextAsync();
|
||||
@@ -76,20 +80,24 @@ namespace fxl.codes.kisekae.Services
|
||||
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
|
||||
await SetInnerFiles(kisekae, directory, filenames);
|
||||
|
||||
var backgroundColors = new Dictionary<Configuration, int>();
|
||||
foreach (var config in kisekae.Configurations)
|
||||
_configurationReaderService.ReadConfigurationToDto(config,
|
||||
_configurationReaderService.ReadConfiguration(config,
|
||||
backgroundColors,
|
||||
kisekae.Cels.ToDictionary(x => x.FileName.ToLowerInvariant()),
|
||||
kisekae.Palettes.ToDictionary(x => x.FileName.ToLowerInvariant()));
|
||||
|
||||
SetPaletteColors(kisekae.Palettes);
|
||||
|
||||
foreach (var (config, colorIndex) in backgroundColors)
|
||||
config.BackgroundColorHex = kisekae.Palettes.First()?.Colors[colorIndex].Hex ?? Color.White.ToHex();
|
||||
foreach (var celConfig in kisekae.Configurations.SelectMany(config => config.Cels))
|
||||
{
|
||||
_fileParserService.RenderCel(celConfig);
|
||||
}
|
||||
|
||||
await context.AddAsync(kisekae);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
//_storage.DeleteDirectory(directory);
|
||||
}
|
||||
|
||||
private async Task SetInnerFiles(Kisekae kisekae, string directory, IEnumerable<string> filenames)
|
||||
|
||||
Reference in New Issue
Block a user