System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Unable to resolve service for type 'FabiusTimer.FabiusTimerApi' while attempting to activate 'FabiusTimer.Player.Replay.Playback'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at FabiusTimer.Plugin.Load(Boolean hotReload) in E:\github\FabiusTimer\FabiusTimer\Plugin.cs:line 49
at CounterStrikeSharp.API.Core.Plugin.PluginContext.Load(Boolean hotReload) in /home/runner/work/CSSharp/CSSharp/managed/CounterStrikeSharp.API/Core/Plugin/PluginContext.cs:line 236
at CounterStrikeSharp.API.Core.Plugin.PluginContext.<>c__DisplayClass36_0.b__0() in /home/runner/work/CSSharp/CSSharp/managed/CounterStrikeSharp.API/Core/Plugin/PluginContext.cs:line 124
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at CounterStrikeSharp.API.Core.FunctionReference.b__18_0(fxScriptContext* context) in /home/runner/work/CSSharp/CSSharp/managed/CounterStrikeSharp.API/Core/FunctionReference.cs:line 100
что не так?
using CounterStrikeSharp.API.Core;
using FabiusTimer.Helpers;
using System.Text;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using FabiusTimer.Configs;
using FabiusTimer.Player;
using FabiusTimer.Player.Replay;
using FabiusTimerApi.Models;
using Microsoft.Extensions.Localization;
namespace FabiusTimer.Managers;
public class HudManager
{
private readonly Plugin _plugin;
private readonly PlayersManager _playersManager;
private readonly IMapManager _mapManager;
private readonly IPlayback _playback;
private readonly StyleConfig _styleConfig;
private readonly IStringLocalizer _localizer;
public HudManager(Plugin plugin, PlayersManager playersManager, EventManager eventManager,
Config<StyleConfig> styleConfig, IMapManager mapManager, IPlayback playback)
{
_plugin = plugin;
_playersManager = playersManager;
_mapManager = mapManager;
_playback = playback;
_styleConfig = styleConfig.Value;
_localizer = plugin.Localizer;
plugin.AddCommand("css_hud", "", ToggleHud);
plugin.AddCommand("css_showkeys", "", ToggleHudKeys);
plugin.AddCommand("css_keys", "", ToggleHudKeys);
plugin.RegisterListener<Listeners.OnTick>(OnTick);
}
private void ToggleHud(CCSPlayerController? player, CommandInfo _)
{
if (player == null) return;
var timerPlayer = _playersManager.Players[player];
var data = timerPlayer?.Data;
if (data is null) return;
data.IsHudEnabled ^= true;
timerPlayer?.PrintToChat(data.IsHudEnabled ? "hud.enabled" : "hud.disabled");
timerPlayer?.SetPlayerCookie("hud_enabled", data.IsHudEnabled);
}
private void ToggleHudKeys(CCSPlayerController? player, CommandInfo _)
{
if (player == null) return;
var timerPlayer = _playersManager.Players[player];
var data = timerPlayer?.Data;
if (data is null) return;
data.IsHudKeysEnabled ^= true;
timerPlayer?.PrintToChat(data.IsHudKeysEnabled ? "hud_keys.enabled" : "hud_keys.disabled");
timerPlayer?.SetPlayerCookie("hud_keys_enabled", data.IsHudKeysEnabled);
}
private void OnTick()
{
foreach (var player in Utilities.GetPlayers().Where(u => u.IsValidPlayer()))
{
try
{
if (!_playersManager.TryGetPlayer(player, out var timerPlayer)) return;
var data = timerPlayer.Data;
if (data is null) return;
CalculateHud(timerPlayer, data);
if (data.TimerStarted)
{
StopTimerIfInNoClip(player, data);
}
DisplayHud(timerPlayer, data);
}
catch (Exception)
{
// ignore
}
}
}
private void StopTimerIfInNoClip(CCSPlayerController player, Data data)
{
var playerPawn = player.PlayerPawn.Value;
if (playerPawn != null && playerPawn.IsNoClip())
{
data.TimerStarted = false;
data.Time = 0;
}
}
private void DisplayHud(TimerPlayer timerPlayer, Data data)
{
var controller = timerPlayer.Controller!;
if (controller.PawnIsAlive)
{
// if (data.IsHudKeysEnabled)
// controller.PrintToCenter(data.HudKeysText);
if (data.IsHudEnabled)
controller.PrintToCenterHtml(data.HudText);
}
else
{
DisplayObserverHud(controller, timerPlayer);
}
}
private void DisplayObserverHud(CCSPlayerController player, TimerPlayer timerPlayer)
{
try
{
var observerServices = player.ObserverPawn?.Value?.ObserverServices;
if (observerServices == null || observerServices.ObserverMode == (int)ObserverMode_t.OBS_MODE_NONE) return;
if (observerServices.ObserverMode is (int)ObserverMode_t.OBS_MODE_IN_EYE
or (int)ObserverMode_t.OBS_MODE_CHASE)
{
var handle = observerServices.ObserverTarget.Value?.Handle;
if (handle is null) return;
var targetController = new CCSPlayerPawn(handle.Value).OriginalController.Value;
if (targetController is null) return;
var text = string.Empty;
if (targetController.IsBot)
{
var data = timerPlayer.Data!;
var replayBot = _playback.ReplayBot;
if (replayBot is null) return;
var controller = replayBot.Controller;
if (controller == targetController)
{
var currentFormatTime = Utils.FormatTime(replayBot.Time);
var formattedSpeed = ((Vector)replayBot.Velocity).Length2D().ToString("000");
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(
$"<font class='fontSize-m' color='#818181'>{replayBot.Route} | REPLAY | Tier {_mapManager.Tier}</font><br>");
stringBuilder.AppendLine(
$"<font class='fontSize-l' color='purple'>{currentFormatTime}</font><br>");
stringBuilder.AppendLine($"<font class='fontSize-l' color='#beefc7'>{formattedSpeed}</font><br>");
var style = replayBot.Style;
if (style != null)
{
stringBuilder.AppendLine(
$"<font class='fontSize-m' color='{style.HtmlColor ?? "white"}'>{style.Name}</font><br>");
}
if (data.IsHudKeysEnabled)
{
stringBuilder.AppendLine(
$"<font class='fontSize-m'>{GetHudKeysText(replayBot.Buttons)}</font>");
}
data.HudText = $"<span>{stringBuilder}</span>";
text = $"<span>{stringBuilder}</span>";
}
}
else
{
var data = _playersManager.Players[targetController]?.Data;
if (data is null) return;
text = data.HudText;
}
player.PrintToCenterHtml(text);
}
}
catch (Exception e)
{
// ignore yomayo
}
}
public void CalculateHud(TimerPlayer timerPlayer, Data data)
{
var controller = timerPlayer.Controller;
if (controller?.Connected != PlayerConnectedState.PlayerConnected) return;
if (data.IsHudEnabled)
{
var wrTime = _mapManager.GetWorldRecord(timerPlayer.CurrentStyle, timerPlayer.Route)?.Time ?? 0;
var totalRecords = _mapManager.GetTotalRecordCount(timerPlayer.CurrentStyle, timerPlayer.Route);
var pbTime = timerPlayer.PersonalBestTime;
var buttons = controller.Buttons;
var currentFormatTime = Utils.FormatTime(data.Time);
var pbFormatTime = Utils.FormatTime(pbTime);
var wrFormatTime = Utils.FormatTime(wrTime);
var formattedSpeed = controller.PlayerPawn.Value?.AbsVelocity.Length2D().ToString("000") ?? "000";
var stringBuilder = new StringBuilder();
var timeColor = GetTimeColor(timerPlayer, wrTime);
stringBuilder.AppendLine(
$"<font class='fontSize-m' color='#818181'>{timerPlayer.Route} | [{timerPlayer.Position}/{(totalRecords > 0 ? totalRecords : "-")}] | Tier {_mapManager.Tier}</font><br>");
stringBuilder.AppendLine(data.TimerStarted
? $"<font class='fontSize-l' color='{timeColor}'>{currentFormatTime}</font><br>"
: "<font class='fontSize-l' color='white'>▬▬▬▬▬▬</font><br>");
stringBuilder.AppendLine(
$"<font class='fontSize-sm' color='#818181'>PB: {(pbTime != 0 ? pbFormatTime : "N/A")} | WR: {(wrTime != 0 ? wrFormatTime : "N/A")}</font><br>");
stringBuilder.AppendLine($"<font class='fontSize-l' color='#beefc7'>{formattedSpeed}</font><br>");
//stringBuilder.AppendLine($"<font class='fontSize-m' color='purple'>Sync: {data.Sync:00.0}%</font><br>");
var style = timerPlayer.CurrentStyle;
if (style != null)
{
stringBuilder.AppendLine(
$"<font class='fontSize-m' color='{style.HtmlColor ?? "white"}'>{style.Name}</font><br>");
}
if (data.IsHudKeysEnabled)
{
stringBuilder.AppendLine($"<font class='fontSize-m'>{data.HudKeysText}</font>");
data.HudKeysText = GetHudKeysText(buttons);
}
data.HudText = $"<span>{stringBuilder}</span>";
}
if (data.TimerStarted)
{
data.Time++;
}
}
private string GetTimeColor(TimerPlayer timerPlayer, int wrTime)
{
var time = timerPlayer.Data?.Time;
var pbTime = timerPlayer.PersonalBestTime;
return time > pbTime && pbTime != 0
? "#ff0000"
: time > wrTime && wrTime != 0
? "#d5ffba"
: "#00ff00";
}
private string GetHudKeysText(PlayerButtons buttons)
{
return $"{((buttons & PlayerButtons.Moveleft) != 0 ? "A" : "_")} " +
$"{((buttons & PlayerButtons.Forward) != 0 ? "W" : "_")} " +
$"{((buttons & PlayerButtons.Moveright) != 0 ? "D" : "_")} " +
$"{((buttons & PlayerButtons.Back) != 0 ? "S" : "_")} - " +
$"{((buttons & PlayerButtons.Jump) != 0 ? "J" : "_")} " +
$"{((buttons & PlayerButtons.Duck) != 0 ? "C" : "_")}";
}
}
public static class Extensions
{
public static bool IsValidPlayer(this CCSPlayerController player)
{
return player.PlayerPawn.Value != null && player.IsValid && player.PlayerPawn.Value.IsValid;
}
public static bool IsNoClip(this CCSPlayerPawn playerPawn)
{
return playerPawn.MoveType is MoveType_t.MOVETYPE_NOCLIP ||
playerPawn.ActualMoveType is MoveType_t.MOVETYPE_NOCLIP;
}
}
public override void Load(bool hotReload)
{
_configSystem.RegisterCommands(this, "css_fabiustimer_reload");
_zonesManager = _serviceProvider.GetRequiredService<IZonesManager>();
_playersManager = _serviceProvider.GetRequiredService<PlayersManager>();
_eventManager = _serviceProvider.GetRequiredService<EventManager>();
_hudManager = _serviceProvider.GetRequiredService<HudManager>(); // 49 LINE
_menuManager = _serviceProvider.GetRequiredService<MenuManager>();
_mapManager = _serviceProvider.GetRequiredService<IMapManager>();
_database = _serviceProvider.GetRequiredService<Database>();
_commands = _serviceProvider.GetRequiredService<Commands>();
_api = _serviceProvider.GetRequiredService<FabiusTimerApi>();
Capabilities.RegisterPluginCapability(IFabiusTimerApi.Capability, () => _api);
_eventManager.RegisterEvent<EventRoundStart>(EventRoundStart);
Task.Run(_database.CreateZoneTableAsync);
Task.Run(_database.CreatePlayersTableAsync);
Task.Run(_database.CreateMapTiersTableAsync);
}