Converting to EF, need to work on TS

This commit is contained in:
Lani Aung
2021-11-19 23:04:15 -07:00
parent 1fd14ebafd
commit 744fc6bc57
43 changed files with 2159 additions and 542 deletions
+18 -11
View File
@@ -22,7 +22,7 @@ namespace fxl.codes.kisekae.Services
_logger = logger;
}
public void ReadConfigurationToDto(ConfigurationDto dto, IDictionary<string, CelDto> cels, IDictionary<string, PaletteDto> palettes)
public void ReadConfigurationToDto(Configuration dto, IDictionary<string, Cel> cels, IDictionary<string, Palette> palettes)
{
var initialPositions = new StringBuilder();
@@ -41,10 +41,10 @@ namespace fxl.codes.kisekae.Services
dto.BorderIndex = int.Parse(borderValue);
break;
case '%':
dto.Palettes.Add(SetPalette(line, palettes));
UpdatePalette(line, palettes);
break;
case '#':
dto.Cels.Add(SetCel(line, dto, cels));
dto.Cels.Add(SetCelConfig(line, dto, cels, palettes.Values.ToArray()));
break;
case '$':
case ' ':
@@ -55,11 +55,11 @@ namespace fxl.codes.kisekae.Services
SetInitialPositions(dto, initialPositions.ToString());
}
private CelConfigDto SetCel(string line, IKisekaeFile configuration, IDictionary<string, CelDto> cels)
private static CelConfig SetCelConfig(string line, Configuration configuration, IDictionary<string, Cel> cels, IReadOnlyList<Palette> palettes)
{
var cel = new CelConfigDto
var cel = new CelConfig
{
ConfigId = configuration.Id
Configuration = configuration
};
if (line.Contains("%t"))
@@ -81,11 +81,17 @@ namespace fxl.codes.kisekae.Services
if (string.Equals(groupName, "FileName"))
{
cel.CelId = cels[group.Value.ToLowerInvariant()].Id;
cel.Cel = cels[group.Value.ToLowerInvariant()];
continue;
}
var property = typeof(CelConfigDto).GetProperty(groupName);
if (string.Equals(groupName, "PaletteIndex"))
{
cel.Palette = palettes[int.Parse(group.Value)];
continue;
}
var property = typeof(CelConfig).GetProperty(groupName);
if (property == null) continue;
var value = group.Value.Trim();
@@ -106,18 +112,19 @@ namespace fxl.codes.kisekae.Services
}
}
cel.Palette ??= palettes[0];
return cel;
}
private static PaletteDto SetPalette(string line, IDictionary<string, PaletteDto> palettes)
private static void UpdatePalette(string line, IDictionary<string, Palette> palettes)
{
var parts = line.Split(';');
var palette = palettes[parts[0].Trim().ToLowerInvariant().Replace("%", "")];
if (parts.Length > 1) palette.Comment = parts[1].Trim();
return palette;
}
private static void SetInitialPositions(ConfigurationDto dto, string positions)
private static void SetInitialPositions(Configuration dto, string positions)
{
var sets = positions.Split('$', StringSplitOptions.RemoveEmptyEntries);
foreach (var set in sets)
+54 -129
View File
@@ -1,161 +1,98 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using fxl.codes.kisekae.Entities;
using fxl.codes.kisekae.Extensions;
using fxl.codes.kisekae.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Npgsql;
namespace fxl.codes.kisekae.Services
{
public class DatabaseService
{
private const string ResolutionRegexPattern = @"\((?<Width>[0-9]*).(?<Height>[0-9]*)\)";
private readonly ConfigurationReaderService _configurationReaderService;
private readonly IDbContextFactory<KisekaeContext> _contextFactory;
private readonly string _connectionString;
private readonly FileParserService _fileParserService;
private readonly ILogger<DatabaseService> _logger;
private readonly IsolatedStorageFile _storage;
public DatabaseService(ILogger<DatabaseService> logger,
IConfiguration configuration,
ConfigurationReaderService configurationReaderService,
FileParserService fileParserService)
FileParserService fileParserService,
IDbContextFactory<KisekaeContext> contextFactory)
{
_logger = logger;
_configurationReaderService = configurationReaderService;
_fileParserService = fileParserService;
_connectionString = configuration.GetConnectionString("kisekae");
_contextFactory = contextFactory;
_storage = IsolatedStorageFile.GetUserStoreForApplication();
}
public async Task<IEnumerable<KisekaeDto>> GetAll()
public IEnumerable<Kisekae> GetAll()
{
await using var connection = new NpgsqlConnection(_connectionString);
await connection.OpenAsync();
using var context = _contextFactory.CreateDbContext();
return context.KisekaeSets
.Include(x => x.Configurations)
.ToArray();
}
using var multi = await connection.QueryMultipleAsync("select * from kisekae; select * from configuration");
var files = multi.Read<KisekaeDto>();
var configs = multi.Read<ConfigurationDto>();
var dictionary = configs.ToLookup(x => x.KisekaeId);
await connection.CloseAsync();
foreach (var kiss in files) kiss.Configurations = dictionary[kiss.Id];
return files;
public Configuration GetConfig(int id)
{
using var context = _contextFactory.CreateDbContext();
return context.Configurations
.Include(x => x.Cels).ThenInclude(x => x.Cel)
.Include(x => x.Kisekae).ThenInclude(x => x.Palettes).ThenInclude(x => x.Colors)
.FirstOrDefault(x => x.Id == id);
}
public async void StoreToDatabase(IFormFile file)
{
var memoryStream = await GetAsMemoryStream(file);
var checksum = Convert.ToBase64String(await new SHA256Managed().ComputeHashAsync(memoryStream));
var checksum = Convert.ToBase64String(await SHA256.Create().ComputeHashAsync(memoryStream));
memoryStream.Position = 0; // Reset for re-read
await using var connection = new NpgsqlConnection(_connectionString);
await connection.OpenAsync();
var transaction = await connection.BeginTransactionAsync();
await using var context = await _contextFactory.CreateDbContextAsync();
var existing = await context.KisekaeSets
.FirstOrDefaultAsync(x => string.Equals(x.FileName, file.FileName)
|| string.Equals(x.CheckSum, checksum));
if (existing != null) return;
try
_fileParserService.UnzipLzh(file, memoryStream);
var directory = Path.GetFileNameWithoutExtension(file.FileName);
var kisekae = new Kisekae
{
var existing = GetExisting(connection, file.FileName, checksum);
if (existing != null) return;
FileName = file.FileName,
CheckSum = checksum
};
_fileParserService.UnzipLzh(file, memoryStream);
var directory = Path.GetFileNameWithoutExtension(file.FileName);
var kisekae = new KisekaeDto
{
Filename = file.FileName,
Name = directory,
Checksum = checksum
};
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
await SetInnerFiles(kisekae, directory, filenames);
kisekae.Id = await connection.InsertAsync(kisekae);
foreach (var config in kisekae.Configurations)
_configurationReaderService.ReadConfigurationToDto(config,
kisekae.Cels.ToDictionary(x => x.FileName.ToLowerInvariant()),
kisekae.Palettes.ToDictionary(x => x.FileName.ToLowerInvariant()));
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
await SetInnerFiles(connection, directory, filenames, kisekae.Id);
SetPaletteColors(kisekae.Palettes);
using var saved = await connection.QueryMultipleAsync(
"select * from configuration where kisekae_id = @id; select * from cel where kisekae_id = @id; select * from palette where kisekae_id = @id",
new { id = kisekae.Id });
var configs = saved.Read<ConfigurationDto>().ToList();
var cels = saved.Read<CelDto>().ToDictionary(x => x.Filename.ToLowerInvariant());
var palettes = saved.Read<PaletteDto>().ToDictionary(x => x.Filename.ToLowerInvariant());
foreach (var config in configs)
{
_configurationReaderService.ReadConfigurationToDto(config, cels, palettes);
await connection.InsertAsync<CelConfigDto>(config.Cels);
await connection.UpdateAsync<PaletteDto>(config.Palettes);
await connection.UpdateAsync(config);
}
var paletteColors = SetPaletteColors(palettes.Values);
await connection.InsertAsync<PaletteColorDto>(paletteColors);
await transaction.CommitAsync();
}
catch (Exception e)
foreach (var celConfig in kisekae.Configurations.SelectMany(config => config.Cels))
{
await transaction.RollbackAsync();
_logger.LogError(e, $"Error saving file {file.FileName}");
_fileParserService.RenderCel(celConfig);
}
await connection.CloseAsync();
await context.AddAsync(kisekae);
await context.SaveChangesAsync();
}
public async Task<PlaysetModel> LoadConfig(int configId)
private async Task SetInnerFiles(Kisekae kisekae, string directory, IEnumerable<string> filenames)
{
await using var connection = new NpgsqlConnection(_connectionString);
await connection.OpenAsync();
var config = await connection.QuerySingleAsync<ConfigurationDto>("select * from configuration where id = @configId", new { configId });
var celIds = await connection.QueryAsync<int>("select cel_id from cel_config where config_id = @configId", new { configId });
var renders = await connection.QueryAsync<CelRenderDto>($"select * from cel_render where cel_id in ({string.Join(",", celIds)})");
if (!renders.Any())
{
using var reader = await connection.QueryMultipleAsync($"select * from cel where id in ({string.Join(",", celIds)});"
+ "select * from palette where kisekae_id = @kisekaeId", new { kisekaeId = config.KisekaeId });
var cels = reader.Read<CelDto>();
var palettes = reader.Read<PaletteDto>().ToDictionary(x => x.Id);
}
await connection.CloseAsync();
return null;
}
private KisekaeDto GetExisting(IDbConnection connection, string filename, string checksum)
{
var existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.filename = @Filename", new { Filename = filename });
if (existing != null)
{
_logger.LogInformation($"Already existing upload {filename} under id {existing.Id}");
return existing;
}
existing = connection.QuerySingleOrDefault<KisekaeDto>("select * from kisekae where kisekae.checksum = @Checksum", new { Checksum = checksum });
if (existing == null) return null;
_logger.LogInformation($"Already existing upload (checksum: {checksum}) under id {existing.Id} and filename {existing.Filename}");
return existing;
}
private async Task SetInnerFiles(IDbConnection connection, string directory, IEnumerable<string> filenames, int id)
{
var files = new List<IKisekaeFile>();
foreach (var filename in filenames)
{
await using var reader = _storage.OpenFile(Path.Combine(directory, filename), FileMode.Open);
@@ -167,40 +104,32 @@ namespace fxl.codes.kisekae.Services
switch (Path.GetExtension(filename).ToLower())
{
case ".cel":
files.Add(new CelDto
kisekae.Cels.Add(new Cel
{
Filename = filename,
Data = bytes,
KisekaeId = id
FileName = filename,
Data = bytes
});
break;
case ".cnf":
files.Add(new ConfigurationDto
kisekae.Configurations.Add(new Configuration
{
Filename = filename,
Data = Encoding.ASCII.GetString(bytes),
KisekaeId = id
Name = filename,
Data = Encoding.ASCII.GetString(bytes)
});
break;
case ".kcf":
files.Add(new PaletteDto
kisekae.Palettes.Add(new Palette
{
Filename = filename,
Data = bytes,
KisekaeId = id
FileName = filename,
Data = bytes
});
break;
}
}
await connection.InsertAsync(files.OfType<CelDto>());
await connection.InsertAsync(files.OfType<ConfigurationDto>());
await connection.InsertAsync(files.OfType<PaletteDto>());
}
private List<PaletteColorDto> SetPaletteColors(IEnumerable<PaletteDto> palettes)
private void SetPaletteColors(IEnumerable<Palette> palettes)
{
var paletteColors = new List<PaletteColorDto>();
foreach (var palette in palettes)
{
var colors = _fileParserService.ParsePalette(palette, out var groups, out var colorsPerGroup);
@@ -209,23 +138,19 @@ namespace fxl.codes.kisekae.Services
for (var colorIndex = 0; colorIndex < colorsPerGroup; colorIndex++)
{
var color = colors[groupIndex * colorsPerGroup + colorIndex];
paletteColors.Add(new PaletteColorDto
palette.Colors.Add(new PaletteColor
{
Group = groupIndex,
PaletteId = palette.Id,
Hex = color.ToHex()
});
}
}
return paletteColors;
}
private static async Task<MemoryStream> GetAsMemoryStream(IFormFile file)
{
var stream = new MemoryStream();
await file.CopyToAsync(stream);
return stream;
}
}
+40 -53
View File
@@ -7,7 +7,6 @@ using System.Linq;
using System.Reflection;
using System.Text;
using fxl.codes.kisekae.Entities;
using fxl.codes.kisekae.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
@@ -79,16 +78,16 @@ namespace fxl.codes.kisekae.Services
_storage.DeleteFile(file.FileName);
}
public Color[] ParsePalette(PaletteDto palette, out int groups, out int colorsPerGroup)
public Color[] ParsePalette(Palette palette, out int groups, out int colorsPerGroup)
{
groups = 0;
colorsPerGroup = 0;
if (palette.Data.Length == 0) return null;
if (!palette.Data[..KissHeader.Length].SequenceEqual(KissHeader)) return GetColors(palette.Data);
// Verify palette mark?
if (palette.Data[4] != 16) _logger.LogError($"{palette.Filename} is not a valid palette file");
if (palette.Data[4] != 16) _logger.LogError($"{palette.FileName} is not a valid palette file");
var colorDepth = Convert.ToInt32(palette.Data[5]);
colorsPerGroup = BitConverter.ToInt16(palette.Data, 8);
@@ -97,16 +96,16 @@ namespace fxl.codes.kisekae.Services
return GetColors(palette.Data[32..], colorDepth, colorsPerGroup, groups);
}
public void ParseCel(string directory, CelModel cel, IEnumerable<PaletteModel> palettes)
public void RenderCel(CelConfig celConfig)
{
_logger.LogTrace($"Reading cel {cel.FileName} from {directory}");
var buffer = ReadToBuffer(directory, cel.FileName);
if (buffer.Length == 0) return;
_logger.LogTrace($"Reading cel {celConfig.Cel.FileName}");
var buffer = celConfig.Cel?.Data?.ToArray();
if (buffer == null || buffer.Length == 0) return;
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
{
// Verify cel mark?
if (buffer[4] != 32) _logger.LogError($"{cel.FileName} is not a valid cel file");
if (buffer[4] != 32) _logger.LogError($"{celConfig.Cel.FileName} is not a valid cel file");
var pixelBits = Convert.ToInt32(buffer[5]);
var width = BitConverter.ToInt16(buffer, 8);
@@ -114,33 +113,24 @@ namespace fxl.codes.kisekae.Services
var xOffset = BitConverter.ToInt16(buffer, 12);
var yOffset = BitConverter.ToInt16(buffer, 14);
cel.ImageByPalette = GetCelImages(buffer[32..], palettes.ToArray(), width, height, pixelBits);
cel.Offset = new Coordinate(xOffset, yOffset);
cel.Height = height;
cel.Width = width;
celConfig.Cel.OffsetX = xOffset;
celConfig.Cel.OffsetY = yOffset;
celConfig.Cel.Height = height;
celConfig.Cel.Width = width;
celConfig.Render = GetCelImage(buffer[32..], celConfig.Palette, width, height, pixelBits);
}
else
{
var width = BitConverter.ToInt16(buffer, 0);
var height = BitConverter.ToInt16(buffer, 2);
cel.ImageByPalette = GetCelImages(buffer[4..], palettes.ToArray(), width, height);
cel.Height = height;
cel.Width = width;
celConfig.Cel.Height = height;
celConfig.Cel.Width = width;
celConfig.Render = GetCelImage(buffer[4..], celConfig.Palette, width, height);
}
}
private byte[] ReadToBuffer(string directory, string filename)
{
using var stream = _storage.OpenFile(Path.Combine(directory, filename), FileMode.Open);
if (!stream.CanRead) throw new InvalidDataException();
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return buffer;
}
private Color[] GetColors(IReadOnlyCollection<byte> bytes, int depth = 12, int colorsPerGroup = 16, int groups = 10)
{
_logger.LogTrace("Converting byte array to colors");
@@ -163,41 +153,38 @@ namespace fxl.codes.kisekae.Services
return colors;
}
private Dictionary<int, string> GetCelImages(IReadOnlyCollection<byte> bytes,
IReadOnlyList<PaletteModel> palettes,
int width,
int height,
int pixelBits = 4)
private Render GetCelImage(IReadOnlyCollection<byte> bytes,
Palette palette,
int width,
int height,
int pixelBits = 4)
{
_logger.LogTrace("Converting byte array to base64 encoded gifs per palette");
var dictionary = new Dictionary<int, string>();
var values = GetValues(bytes, pixelBits);
for (var paletteIndex = 0; paletteIndex < palettes.Count; paletteIndex++)
using var bitmap = new Image<Rgba32>(width, height);
var index = 0;
for (var row = 0; row < height; row++)
{
var palette = palettes[paletteIndex];
using var bitmap = new Image<Rgba32>(width, height);
var index = 0;
for (var row = 0; row < height; row++)
var span = bitmap.GetPixelRowSpan(row);
for (var column = 0; column < width; column++)
{
var span = bitmap.GetPixelRowSpan(row);
for (var column = 0; column < width; column++)
{
var value = values[index];
span[column] = value == 0 || value >= palette.Colors.Length ? Transparent : palette.Colors[value];
index++;
}
if (width % 2 != 0 && pixelBits == 4) index++;
var value = values[index];
span[column] = value == 0 || value >= palette.Colors.Count ? Transparent : Color.ParseHex(palette.Colors[value].Hex);
index++;
}
using var memory = new MemoryStream();
bitmap.SaveAsGif(memory);
dictionary.Add(paletteIndex, Convert.ToBase64String(memory.ToArray()));
if (width % 2 != 0 && pixelBits == 4) index++;
}
return dictionary;
using var memory = new MemoryStream();
bitmap.SaveAsGif(memory);
return new Render
{
Image = memory.ToArray()
};
}
private static int[] GetValues(IReadOnlyCollection<byte> bytes, int bits = 4)