partiusfabaa / cs2-ranks

51 stars 18 forks source link

Error raised when there is a lot of players on the server. #26

Closed RonanLOUARN closed 9 months ago

RonanLOUARN commented 9 months ago

Hi, I get this error when we have more than 4/5 players on the server. When this happens, we can no longer use the !top command.

2024-01-25 17:18:15.044 +01:00 [EROR] (cssharp:Core) Error invoking callback
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Runtime.InteropServices.Marshal.ReadByte(IntPtr ptr, Int32 ofs)
   at CounterStrikeSharp.API.Core.ScriptContext.GetResult(Type type, Byte* ptr) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/ScriptContext.cs:line 451
   at CounterStrikeSharp.API.Core.NativeAPI.GetSchemaValueByName[T](IntPtr instance, Int32 returntype, String classname, String propname) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/API.cs:line 1101
   at CounterStrikeSharp.API.Core.CBasePlayerController.get_PlayerName() in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/Objects.g.cs:line 5103
   at Ranks.Ranks.EventPlayerDeath(EventPlayerDeath event, GameEventInfo info)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at CounterStrikeSharp.API.Core.FunctionReference.<>c__DisplayClass3_0.<.ctor>b__0(fxScriptContext* context) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/FunctionReference.cs:line 82

I think it's due to this function in both plugins.

private void UpdateUserStatsLocal(CCSPlayerController? player, string msg = "",
        int exp = 0, bool increase = true, int kills = 0, int death = 0, int assist = 0,
        int shoots = 0, int hits = 0, int headshots = 0, int roundwin = 0, int roundlose = 0)
    {
        if (player == null || _config.MinPlayers > PlayersCount() ||
            Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules!
                .WarmupPeriod) return;

        if (!_users.TryGetValue(player.SteamID, out var user)) return;

        user.name = player.PlayerName;

        exp = exp == -1 ? 0 : exp;

        if (increase)
            user.value += exp;
        else
            user.value -= exp;

        user.kills += kills;
        user.deaths += death;
        user.assists += assist;
        user.round_lose += roundlose;
        user.round_win += roundwin;
        user.headshots += headshots;
        user.hits += hits;
        user.shoots += shoots;

        if (user.value <= 0) user.value = _config.InitialExperiencePoints;

        var nextXp = GetExperienceToNextLevel(player);
        if (exp != 0 && _config.ShowExperienceMessages)
            Server.NextFrame(() => SendMessageToSpecificChat(player,
                $"{(increase ? "\x0C+" : "\x02-")}{exp} XP \x08{msg} {(nextXp == 0 ? string.Empty : $"{Localizer["next_level", nextXp]}")}"));
    }
partiusfabaa commented 9 months ago

i'll take a look