Change design loading.

This commit is contained in:
Ottermandias 2023-12-28 14:20:59 +01:00
parent dd5c56de9d
commit 4b242bb3cf
2 changed files with 36 additions and 17 deletions

View file

@ -1,7 +1,11 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Dalamud.Utility;
using Glamourer.Events;
using Glamourer.Interop.Penumbra;
@ -18,7 +22,7 @@ namespace Glamourer.Designs;
public class DesignManager
{
private readonly CustomizeService _customizations;
private readonly CustomizeService _customizations;
private readonly ItemManager _items;
private readonly HumanModelList _humans;
private readonly SaveService _saveService;
@ -48,29 +52,44 @@ public class DesignManager
/// </summary>
public void LoadDesigns()
{
_humans.Awaiter.Wait();
_customizations.Awaiter.Wait();
_items.ItemData.Awaiter.Wait();
var stopwatch = Stopwatch.StartNew();
_designs.Clear();
List<(Design, string)> invalidNames = new();
var skipped = 0;
foreach (var file in _saveService.FileNames.Designs())
var skipped = 0;
ThreadLocal<List<(Design, string)>> designs = new(() => [], true);
Parallel.ForEach(_saveService.FileNames.Designs(), (f, _) =>
{
try
{
var text = File.ReadAllText(file.FullName);
var text = File.ReadAllText(f.FullName);
var data = JObject.Parse(text);
var design = Design.LoadDesign(_customizations, _items, data);
if (design.Identifier.ToString() != Path.GetFileNameWithoutExtension(file.Name))
invalidNames.Add((design, file.FullName));
if (_designs.Any(f => f.Identifier == design.Identifier))
throw new Exception($"Identifier {design.Identifier} was not unique.");
design.Index = _designs.Count;
_designs.Add(design);
designs.Value!.Add((design, f.FullName));
}
catch (Exception ex)
{
Glamourer.Log.Error($"Could not load design, skipped:\n{ex}");
++skipped;
Interlocked.Increment(ref skipped);
}
});
List<(Design, string)> invalidNames = [];
foreach (var (design, path) in designs.Values.SelectMany(v => v))
{
if (design.Identifier.ToString() != Path.GetFileNameWithoutExtension(path))
invalidNames.Add((design, path));
if (_designs.Any(d => d.Identifier == design.Identifier))
{
Glamourer.Log.Error($"Could not load design, skipped: Identifier {design.Identifier} was not unique.");
++skipped;
continue;
}
design.Index = _designs.Count;
_designs.Add(design);
}
var failed = MoveInvalidNames(invalidNames);
@ -79,7 +98,7 @@ public class DesignManager
$"Moved {invalidNames.Count - failed} designs to correct names.{(failed > 0 ? $" Failed to move {failed} designs to correct names." : string.Empty)}");
Glamourer.Log.Information(
$"Loaded {_designs.Count} designs.{(skipped > 0 ? $" Skipped loading {skipped} designs due to errors." : string.Empty)}");
$"Loaded {_designs.Count} designs in {stopwatch.ElapsedMilliseconds} ms.{(skipped > 0 ? $" Skipped loading {skipped} designs due to errors." : string.Empty)}");
_event.Invoke(DesignChanged.Type.ReloadedAll, null!);
}
@ -191,10 +210,10 @@ public class DesignManager
public void ChangeColor(Design design, string newColor)
{
var oldColor = design.Color;
if (oldColor == newColor)
if (oldColor == newColor)
return;
design.Color = newColor;
design.Color = newColor;
design.LastEdit = DateTimeOffset.UtcNow;
_saveService.QueueSave(design);
Glamourer.Log.Debug($"Changed color of design {design.Identifier}.");

@ -1 +1 @@
Subproject commit 4df65fb330f3746b7836c39cb96d1e36a53bcec0
Subproject commit 15203edf1dba72713f508b798048c56ad969fb95