using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using BITKit; using BITKit.Mod; using HighlightPlus; using Microsoft.Extensions.Logging; using UnityEngine; using Object = UnityEngine.Object; namespace Net.Project.B.Interaction { public class WorldHighlightService:IDisposable { private readonly IWorldInteractionService _interactionService; private readonly ILogger _logger; private HighlightProfile _defaultProfile; private readonly ConcurrentDictionary _dictionary = new(); public WorldHighlightService(IWorldInteractionService interactionService, ILogger logger) { _interactionService = interactionService; _logger = logger; _interactionService.OnInteraction += OnInteraction; _logger.LogInformation("场景互动高亮初始化中"); Initialize(); } private async void Initialize() { _defaultProfile =await ModService.LoadAsset("highlight_interactable"); _logger.LogInformation("场景互动高亮已初始化完成"); } private void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4) { if (_defaultProfile is null) return; switch (arg3) { case WorldInteractionProcess.Performed: { if (_dictionary.TryGetValue(arg2, out var effect) is false) return; effect.HitFX(); } break; case WorldInteractionProcess.Hover: { if(arg2.WorldObject is not GameObject gameObject || !gameObject)return; if (_dictionary.ContainsKey(arg2)) return; if(gameObject.GetComponentsInChildren().Any() is false)return; var effect = gameObject.AddComponent(); effect.profile = _defaultProfile; effect.ProfileReload(); effect.ignoreObjectVisibility = true; effect.highlighted = true; _dictionary.TryAdd(arg2, effect); } break; default: { if (_dictionary.TryRemove(arg2, out var effect2) is false) return; if (effect2) Object.Destroy(effect2); break; } } } public void Dispose() { _interactionService.OnInteraction -= OnInteraction; } } }