using System.Reflection; using Microsoft.Extensions.Logging; using moe.yuyui.weebsights_port.Interfaces; using moe.yuyui.weebsights_port.Models; using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.DI; using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Eft.Common.Tables; using SPTarkov.Server.Core.Models.Spt.Mod; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Services.Mod; namespace moe.yuyui.weebsights_port.Items; [Injectable(TypePriority = OnLoadOrder.PostDBModLoader + 2)] public class ItemGenerator(ISptLogger logger, CustomItemService customItemService, DatabaseService databaseService) { public IEnumerable GenerateWeebSights() where T : IWeebSightEnum { FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static ).Where(f => f.FieldType == typeof(WeebSight)).ToArray(); foreach (var field in fields) { WeebSight? sight = field.GetValue(typeof(WeebSight)) as WeebSight; if (sight == null) { logger.Error($"[Weeb Iron Sights] Failed to convert field {field.Name} of type {field.FieldType} into WeebSight"); continue; } NewItemFromCloneDetails clonedItem = new() { NewId = sight.Id, ItemTplToClone = sight.CloneFromTpl, ParentId = "55818ac54bdc2d5b648b456e", // Ironsight HandbookParentId = "5b5f746686f77447ec5d7708", // CATEGORY HandbookPriceRoubles = 4000, OverrideProperties = new TemplateItemProperties() { Ergonomics = 3, CreditsPrice = 4000, Prefab = new Prefab() { Path = $"assets/content/items/mods/sights rear/{sight.Asset}.bundle" }, }, Locales = new Dictionary { { "en", new LocaleDetails() { Name = $"{sight.Id} TODO LOCALE LOADER FROM JSON", ShortName = "WEEBSIGHT", Description = "DESCRIPTION", } } } }; var itemCreation = customItemService.CreateItemFromClone(clonedItem); if (itemCreation.Success is false or null) { logger.Error($"[Weeb Iron Sights] Failed to clone item {sight.CloneFromTpl} into {sight.Id}"); itemCreation.Errors?.ForEach(e => logger.Critical("[Weeb Iron Sights] " + e)); continue; } AddItemToFilters(sight); yield return itemCreation; } } private void AddItemToFilters(WeebSight sight) { var itemsWithSlots = databaseService.GetTemplates().Items.Where(i => i.Value.Properties?.Slots?.Count() > 0); foreach (var item in itemsWithSlots) { var backIronSightSlot = item.Value.Properties?.Slots?.FirstOrDefault(s => s.Name == "mod_sight_rear"); if (backIronSightSlot == null) { continue; } var slotFilter = backIronSightSlot?.Properties?.Filters?.FirstOrDefault(f => f.Filter?.Contains(sight.CloneFromTpl) ?? false); if (slotFilter == null) { continue; } if (slotFilter.Filter?.Add(sight.Id) is false) { logger.Error("[Weeb Iron Sights] Failed to add filter to item " + item.Key); continue; } logger.Success($"[Weeb Iron Sights] Added {sight.Id} to filter on item {item.Key}"); } } }