From 4b242bb3cfef576c1c3cd70942ddfa0108a6e8c0 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 28 Dec 2023 14:20:59 +0100 Subject: [PATCH] Change design loading. --- Glamourer/Designs/DesignManager.cs | 51 ++++++++++++++++++++---------- OtterGui | 2 +- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Glamourer/Designs/DesignManager.cs b/Glamourer/Designs/DesignManager.cs index 80cd0e0..b46e4e0 100644 --- a/Glamourer/Designs/DesignManager.cs +++ b/Glamourer/Designs/DesignManager.cs @@ -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 /// 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> 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}."); diff --git a/OtterGui b/OtterGui index 4df65fb..15203ed 160000 --- a/OtterGui +++ b/OtterGui @@ -1 +1 @@ -Subproject commit 4df65fb330f3746b7836c39cb96d1e36a53bcec0 +Subproject commit 15203edf1dba72713f508b798048c56ad969fb95