using System.Collections.Concurrent; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Wonderking.Game.Data; using Wonderking.Game.Reader; namespace Server.Services; public class ItemObjectPoolService : IHostedService { readonly ConcurrentDictionary _itemObjectPool = new(); private readonly ItemReader _itemReader; public ItemObjectPoolService(IConfiguration configuration) { _itemReader = new ItemReader(configuration.GetSection("Game").GetSection("Data").GetValue("Path") ?? string.Empty); } public Task StartAsync(CancellationToken cancellationToken) { var amountOfEntries = _itemReader.GetAmountOfEntries(); ParallelEnumerable.Range(0, (int)amountOfEntries).AsParallel().ForAll(i => { var itemObject = _itemReader.GetEntry((uint)i); _itemObjectPool.TryAdd(itemObject.ItemID, itemObject); }); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { _itemReader.Dispose(); return Task.CompletedTask; } public ItemObject GetItem(ushort itemId) { return _itemObjectPool[itemId]; } public bool ContainsItem(ushort itemId) { return _itemObjectPool.ContainsKey(itemId); } public IQueryable QueryItems() { return _itemObjectPool.AsReadOnly().Values.AsQueryable(); } }