Saving to DB, need to render
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using fxl.codes.kisekae.Entities;
|
||||
using fxl.codes.kisekae.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@@ -10,91 +11,129 @@ namespace fxl.codes.kisekae.Services
|
||||
{
|
||||
public class ConfigurationReaderService
|
||||
{
|
||||
public const string ResolutionRegexPattern = @"\((?<Width>[0-9]*).(?<Height>[0-9]*)\)";
|
||||
private readonly FileParserService _fileParser;
|
||||
private const string CelRegex = @"#(?<Group>\d*)\.?(?<Fix>\d*)\s*(?<FileName>[\w\d\-]*\.[cCeElL]*)\s*\*?"
|
||||
+ @"(?<PaletteId>\d*)?\s*\:?(?<Sets>[\d\s]*)?;?(?<Comment>[\w\d\-\s\%]*)";
|
||||
|
||||
private const string ResolutionRegexPattern = @"\((?<Width>[0-9]*).(?<Height>[0-9]*)\)";
|
||||
|
||||
private readonly ILogger<ConfigurationReaderService> _logger;
|
||||
|
||||
public ConfigurationReaderService(ILogger<ConfigurationReaderService> logger, FileParserService fileParser)
|
||||
public ConfigurationReaderService(ILogger<ConfigurationReaderService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_fileParser = fileParser;
|
||||
}
|
||||
|
||||
public PlaysetModel ParseStream(Stream fileStream, string directory = null)
|
||||
public void ReadConfigurationToDto(ConfigurationDto dto, IDictionary<string, CelDto> cels, IDictionary<string, PaletteDto> palettes)
|
||||
{
|
||||
var model = new PlaysetModel();
|
||||
var initialPositions = new StringBuilder();
|
||||
var borderColorIndex = 0;
|
||||
|
||||
using var reader = new StreamReader(fileStream);
|
||||
var data = reader.ReadToEnd();
|
||||
|
||||
SetPlaysetProperties(data, model, initialPositions);
|
||||
|
||||
SetInitialPositions(model, initialPositions.ToString());
|
||||
if (string.IsNullOrEmpty(directory)) return model;
|
||||
|
||||
foreach (var palette in model.Palettes) _fileParser.ParsePalette(directory, palette);
|
||||
|
||||
var celIndex = 0;
|
||||
foreach (var cel in model.Cels.Where(x => !string.IsNullOrEmpty(x.FileName)))
|
||||
{
|
||||
_fileParser.ParseCel(directory, cel, model.Palettes);
|
||||
cel.ZIndex = (model.Cels.Count - celIndex) * 10;
|
||||
celIndex++;
|
||||
}
|
||||
|
||||
if (model.Palettes.Any() && model.Palettes[0].Colors.Any()) model.BorderColor = model.Palettes[0].Colors[borderColorIndex];
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
public void SetPlaysetProperties(string data, PlaysetModel model, StringBuilder initialPositions)
|
||||
{
|
||||
if (string.IsNullOrEmpty(data)) return;
|
||||
|
||||
foreach (var line in data.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
foreach (var line in dto.Data.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
|
||||
switch (line.ToCharArray()[0])
|
||||
{
|
||||
case '(':
|
||||
var resolutionRegex = new Regex(ResolutionRegexPattern);
|
||||
var resolutionMatch = resolutionRegex.Match(line);
|
||||
model.Width = int.Parse(resolutionMatch.Groups["Width"].Value);
|
||||
model.Height = int.Parse(resolutionMatch.Groups["Height"].Value);
|
||||
dto.Width = int.Parse(resolutionMatch.Groups["Width"].Value);
|
||||
dto.Height = int.Parse(resolutionMatch.Groups["Height"].Value);
|
||||
break;
|
||||
case '[':
|
||||
var borderValue = line[1..];
|
||||
if (borderValue.Contains(';')) borderValue = borderValue.Split(';')[0].Trim();
|
||||
model.BorderColorIndex = int.Parse(borderValue);
|
||||
dto.BorderIndex = int.Parse(borderValue);
|
||||
break;
|
||||
case '%':
|
||||
model.Palettes.Add(new PaletteModel(_logger, line));
|
||||
dto.Palettes.Add(SetPalette(line, palettes));
|
||||
break;
|
||||
case '#':
|
||||
model.Cels.Add(new CelModel(_logger, line));
|
||||
dto.Cels.Add(SetCel(line, dto, cels));
|
||||
break;
|
||||
case '$':
|
||||
case ' ':
|
||||
initialPositions.Append(line.Replace("\\r\\n", "").Replace("\\n", ""));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SetInitialPositions(dto, initialPositions.ToString());
|
||||
}
|
||||
|
||||
private static void SetInitialPositions(PlaysetModel model, string positions)
|
||||
private CelConfigDto SetCel(string line, IKisekaeFile configuration, IDictionary<string, CelDto> cels)
|
||||
{
|
||||
var cel = new CelConfigDto
|
||||
{
|
||||
ConfigId = configuration.Id
|
||||
};
|
||||
|
||||
if (line.Contains("%t"))
|
||||
{
|
||||
var opacity = line[line.IndexOf("%t", StringComparison.InvariantCultureIgnoreCase)..].Trim();
|
||||
opacity = opacity.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[0];
|
||||
opacity = opacity.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)[0];
|
||||
opacity = opacity.Replace("%t", "");
|
||||
cel.Transparency = int.Parse(opacity);
|
||||
}
|
||||
|
||||
var matcher = new Regex(CelRegex);
|
||||
var match = matcher.Match(line);
|
||||
|
||||
foreach (var groupName in matcher.GetGroupNames())
|
||||
{
|
||||
match.Groups.TryGetValue(groupName, out var group);
|
||||
if (string.IsNullOrEmpty(group?.Value)) continue;
|
||||
|
||||
if (string.Equals(groupName, "FileName"))
|
||||
{
|
||||
cel.CelId = cels[group.Value.ToLowerInvariant()].Id;
|
||||
continue;
|
||||
}
|
||||
|
||||
var property = typeof(CelConfigDto).GetProperty(groupName);
|
||||
if (property == null) continue;
|
||||
|
||||
var value = group.Value.Trim();
|
||||
if (property.PropertyType == typeof(string)) property.SetValue(cel, value);
|
||||
if (property.PropertyType == typeof(int)) property.SetValue(cel, int.Parse(value));
|
||||
|
||||
if (property.PropertyType != cel.Sets.GetType()) continue;
|
||||
|
||||
foreach (var id in value.Split(' ', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var success = int.TryParse(id, out var result);
|
||||
if (!success) continue;
|
||||
|
||||
if (cel.Sets == Set.Unset)
|
||||
cel.Sets = (Set)(2 ^ result);
|
||||
else
|
||||
cel.Sets |= (Set)(2 ^ result);
|
||||
}
|
||||
}
|
||||
|
||||
return cel;
|
||||
}
|
||||
|
||||
private static PaletteDto SetPalette(string line, IDictionary<string, PaletteDto> 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)
|
||||
{
|
||||
var sets = positions.Split('$', StringSplitOptions.RemoveEmptyEntries);
|
||||
for (var index = 0; index < sets.Length; index++)
|
||||
foreach (var set in sets)
|
||||
{
|
||||
var value = sets[index].Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
var value = set.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
for (var innerIndex = 1; innerIndex < value.Length; innerIndex++)
|
||||
{
|
||||
if (value[innerIndex] == "*") continue;
|
||||
var point = value[innerIndex].Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var cel in model.Cels.Where(x => x.Id == innerIndex - 1))
|
||||
cel.InitialPositions[index] = new Coordinate(int.Parse(point[0]), int.Parse(point[1]));
|
||||
foreach (var cel in dto.Cels.Where(x => x.Id == innerIndex - 1))
|
||||
{
|
||||
cel.X = int.Parse(point[0]);
|
||||
cel.Y = int.Parse(point[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+69
-21
@@ -9,8 +9,8 @@ using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using Dapper.Contrib.Extensions;
|
||||
using fxl.codes.kisekae.Entities;
|
||||
using fxl.codes.kisekae.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -61,7 +61,7 @@ namespace fxl.codes.kisekae.Services
|
||||
await connection.OpenAsync();
|
||||
|
||||
var queryParams = new { Id = id, ConfigId = configId };
|
||||
var cels = connection.Query<CelConfigDto>("select * from cel_config where configId = @ConfigId", queryParams);
|
||||
var cels = connection.Query<CelConfigDto>("select * from cel_config where config_id = @ConfigId", queryParams);
|
||||
if (cels == null)
|
||||
{
|
||||
var config = connection.QuerySingle<ConfigurationDto>(
|
||||
@@ -99,28 +99,58 @@ namespace fxl.codes.kisekae.Services
|
||||
|
||||
public async void StoreToDatabase(IFormFile file)
|
||||
{
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
var memoryStream = await GetAsMemoryStream(file);
|
||||
var checksum = Convert.ToBase64String(await new SHA256Managed().ComputeHashAsync(memoryStream));
|
||||
memoryStream.Position = 0; // Reset for re-read
|
||||
|
||||
var existing = GetExisting(connection, file.FileName, checksum);
|
||||
if (existing != null) return;
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
var transaction = await connection.BeginTransactionAsync();
|
||||
|
||||
_fileParserService.UnzipLzh(file, memoryStream);
|
||||
var directory = Path.GetFileNameWithoutExtension(file.FileName);
|
||||
var kisekae = new KisekaeDto
|
||||
try
|
||||
{
|
||||
Filename = file.FileName,
|
||||
Name = directory,
|
||||
Checksum = checksum
|
||||
};
|
||||
var existing = GetExisting(connection, file.FileName, checksum);
|
||||
if (existing != null) return;
|
||||
|
||||
kisekae.Id = await connection.InsertAsync(kisekae);
|
||||
_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, "*")}");
|
||||
SetInnerFiles(directory, filenames, kisekae.Id);
|
||||
kisekae.Id = await connection.InsertAsync(kisekae);
|
||||
|
||||
var filenames = _storage.GetFileNames($"{Path.Combine(directory, "*")}");
|
||||
await SetInnerFiles(connection, directory, filenames, kisekae.Id);
|
||||
|
||||
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)
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
_logger.LogError(e, $"Error saving file {file.FileName}");
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
@@ -141,7 +171,7 @@ namespace fxl.codes.kisekae.Services
|
||||
return existing;
|
||||
}
|
||||
|
||||
private async void SetInnerFiles(string directory, IEnumerable<string> filenames, int id)
|
||||
private async Task SetInnerFiles(IDbConnection connection, string directory, IEnumerable<string> filenames, int id)
|
||||
{
|
||||
var files = new List<IKisekaeFile>();
|
||||
foreach (var filename in filenames)
|
||||
@@ -181,14 +211,32 @@ namespace fxl.codes.kisekae.Services
|
||||
}
|
||||
}
|
||||
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
await connection.InsertAsync(files.OfType<CelDto>());
|
||||
await connection.InsertAsync(files.OfType<ConfigurationDto>());
|
||||
await connection.InsertAsync(files.OfType<PaletteDto>());
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
private List<PaletteColorDto> SetPaletteColors(IEnumerable<PaletteDto> palettes)
|
||||
{
|
||||
var paletteColors = new List<PaletteColorDto>();
|
||||
foreach (var palette in palettes)
|
||||
{
|
||||
var colors = _fileParserService.ParsePalette(palette, out var groups, out var colorsPerGroup);
|
||||
|
||||
for (var groupIndex = 0; groupIndex < groups; groupIndex++)
|
||||
for (var colorIndex = 0; colorIndex < colorsPerGroup; colorIndex++)
|
||||
{
|
||||
var color = colors[groupIndex * colorsPerGroup + colorIndex];
|
||||
paletteColors.Add(new PaletteColorDto
|
||||
{
|
||||
Group = groupIndex,
|
||||
PaletteId = palette.Id,
|
||||
Hex = color.ToHex()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return paletteColors;
|
||||
}
|
||||
|
||||
private static async Task<MemoryStream> GetAsMemoryStream(IFormFile file)
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.IO.IsolatedStorage;
|
||||
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;
|
||||
@@ -78,27 +79,22 @@ namespace fxl.codes.kisekae.Services
|
||||
_storage.DeleteFile(file.FileName);
|
||||
}
|
||||
|
||||
public void ParsePalette(string directory, PaletteModel palette)
|
||||
public Color[] ParsePalette(PaletteDto palette, out int groups, out int colorsPerGroup)
|
||||
{
|
||||
_logger.LogTrace($"Reading palette {palette.FileName} from {directory}");
|
||||
var buffer = ReadToBuffer(directory, palette.FileName);
|
||||
if (buffer.Length == 0) return;
|
||||
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 (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
|
||||
{
|
||||
// Verify palette mark?
|
||||
if (buffer[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);
|
||||
groups = BitConverter.ToInt16(palette.Data, 10);
|
||||
|
||||
var colorDepth = Convert.ToInt32(buffer[5]);
|
||||
var colorsPerGroup = BitConverter.ToInt16(buffer, 8);
|
||||
var groups = BitConverter.ToInt16(buffer, 10);
|
||||
|
||||
palette.Colors = GetColors(buffer[32..], colorDepth, colorsPerGroup, groups);
|
||||
}
|
||||
else
|
||||
{
|
||||
palette.Colors = GetColors(buffer);
|
||||
}
|
||||
return GetColors(palette.Data[32..], colorDepth, colorsPerGroup, groups);
|
||||
}
|
||||
|
||||
public void ParseCel(string directory, CelModel cel, IEnumerable<PaletteModel> palettes)
|
||||
|
||||
Reference in New Issue
Block a user