using System.Collections.Concurrent; namespace ReferenceClientProxyImplementation.Patches.Implementations; public class PatchSet(IServiceProvider sp) { public ConcurrentDictionary ActivePatchStates { get; } = []; public List Patches { get; } = sp.GetServices().OrderBy(x => x.GetOrder()).ToList(); public async Task ApplyPatches(string relativeName, byte[] content) { var i = 0; var patches = Patches .Where(p => p.Applies(relativeName, content)) .OrderBy(p => p.GetOrder()) .ToList(); if (!patches.Any()) { Console.WriteLine($"No patches required for {relativeName}!"); return content; } lock (ActivePatchStates) ActivePatchStates[relativeName] = new() { CurrentPatch = patches[0].GetName(), CurrentPatchIndex = 0, TotalPatchCount = patches.Count }; foreach (var patch in patches) { // if (patch.Applies(relativeName, content)) { lock (ActivePatchStates) { ActivePatchStates[relativeName].CurrentPatchIndex = patches.IndexOf(patch); ActivePatchStates[relativeName].CurrentPatch = patch.GetName(); } var defaultColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkBlue; Console.Write($"{relativeName,32} ==> "); Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine($"Running task {++i}/{patches.Count}: {patch.GetName()} (Type<{patch.GetType().Name}>)"); Console.ForegroundColor = defaultColor; content = await patch.Execute(relativeName, content); // } } lock (ActivePatchStates) ActivePatchStates.Remove(relativeName, out _); Console.WriteLine($"Finished patching {relativeName}, {ActivePatchStates.Count} files still processing"); return content; } } public class PatchState { public required string CurrentPatch { get; set; } public int CurrentPatchIndex { get; set; } = 0; public required int TotalPatchCount { get; set; } }