diff --git a/moe.yuyui.weebsights-port/Items/ItemGenerator.cs b/moe.yuyui.weebsights-port/Items/ItemGenerator.cs index 17c2ffe..f081309 100644 --- a/moe.yuyui.weebsights-port/Items/ItemGenerator.cs +++ b/moe.yuyui.weebsights-port/Items/ItemGenerator.cs @@ -4,15 +4,17 @@ 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) +public class ItemGenerator(ISptLogger logger, CustomItemService customItemService, DatabaseService databaseService) { public IEnumerable GenerateWeebSights() where T : IWeebSightEnum { @@ -61,7 +63,34 @@ public class ItemGenerator(ISptLogger logger, CustomItemService c 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}"); + } + } } \ No newline at end of file diff --git a/moe.yuyui.weebsights-port/Mod.cs b/moe.yuyui.weebsights-port/Mod.cs index d37c8dc..bc9aab7 100644 --- a/moe.yuyui.weebsights-port/Mod.cs +++ b/moe.yuyui.weebsights-port/Mod.cs @@ -1,23 +1,23 @@ -using moe.yuyui.weebsights_port.Assorts; +using System.Diagnostics; +using moe.yuyui.weebsights_port.Assorts; using moe.yuyui.weebsights_port.Enums; using moe.yuyui.weebsights_port.Items; using moe.yuyui.weebsights_port.Locales; using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.DI; using SPTarkov.Server.Core.Models.Utils; +using SPTarkov.Server.Core.Services; namespace moe.yuyui.weebsights_port; [Injectable(TypePriority = OnLoadOrder.PostSptModLoader + 1)] -public class Mod(ISptLogger logger, EnglishLocale englishLocale, MechanicAssort mechanicAssort, ItemGenerator itemGenerator): IOnLoad +public class Mod(ISptLogger logger, + MechanicAssort mechanicAssort, + ItemGenerator itemGenerator): IOnLoad { public Task OnLoad() { - if (!englishLocale.LoadLocales()) - { - logger.Error("[Weeb Iron Sights] Failed to load locales, you may see garbled text"); - } - + var stopWatch = Stopwatch.StartNew(); var sightsMbus = itemGenerator.GenerateWeebSights(); var sightsMcx = itemGenerator.GenerateWeebSights(); if (!mechanicAssort.InjectAssortFromItemClone(sightsMbus)) @@ -25,10 +25,14 @@ public class Mod(ISptLogger logger, EnglishLocale englishLocale, MechanicAs logger.Critical("[Weeb Iron Sights] Failed to inject MBUS Sights"); } + if (!mechanicAssort.InjectAssortFromItemClone(sightsMcx)) { logger.Critical("[Weeb Iron Sights] Failed to inject MCX Sights"); } + + stopWatch.Stop(); + logger.Success($"[Weeb Iron Sights] Loaded in {stopWatch.ElapsedMilliseconds} ms"); return Task.CompletedTask; } } \ No newline at end of file diff --git a/moe.yuyui.weebsights-port/Models/WeebSight.cs b/moe.yuyui.weebsights-port/Models/WeebSight.cs index 65d2fbd..da5a490 100644 --- a/moe.yuyui.weebsights-port/Models/WeebSight.cs +++ b/moe.yuyui.weebsights-port/Models/WeebSight.cs @@ -2,4 +2,4 @@ using SPTarkov.Server.Core.Models.Common; namespace moe.yuyui.weebsights_port.Models; -public record WeebSight(MongoId Id, string Asset, MongoId? CloneFromTpl); +public record WeebSight(MongoId Id, string Asset, MongoId CloneFromTpl);