altmp / altv-issues

Issues and roadmap for alt:V project
93 stars 16 forks source link

C# client-side error when Action<...>.Invoke() #2383

Open axeok opened 4 days ago

axeok commented 4 days ago

Description of the problem

Up to recently everything was okay until something happened with Actions in C#.

I've met trouble invoking action with primitive types, while I never encountered this issue before (I was using it succesfully before).

Invoke action with primitive type (maybe any structure) - Object reference not set to an instance of an object. Invoke action with string (and custom classes) - OK. Dynamic invoke with primitive type - OK. Foreach invoke action.GetInvocationList() - OK.

Error happens only with .Invoke() method with primitive(structure) types.

With client config disableRestrictedSandbox = true error was gone and .Invoke() works perfectly.

image image image

Reproduction steps

using AltV.Net.Client;
using AltV.Net.Client.Elements.Data;

namespace TestProject.Client;

public class TestResource : Resource
{
    private delegate void TestDelegate(int value);

    private Action<int>? _test1;
    private Action<ConsoleColor>? _test2;
    private Action<string>? _test3;
    private TestDelegate? _test4;

    public override void OnStart()
    {
        Alt.Logger.LogInfo("Started TestResource");

        _test1 += value => Alt.Logger.LogInfo($"Test1: {value}");
        _test2 += value => Alt.Logger.LogInfo($"Test2: {value}");
        _test3 += value => Alt.Logger.LogInfo($"Test3: {value}");
        _test4 += value => Alt.Logger.LogInfo($"Test4: {value}");

        Alt.OnKeyDown += AltOnOnKeyDown;
    }

    private void AltOnOnKeyDown(Key key)
    {
        if (key != Key.J)
        {
            return;
        }

        try
        {
            _test1?.Invoke(5);
        }
        catch (Exception e)
        {
            Alt.Logger.LogError($"_test1: {e}");
        }

        try
        {
            _test2?.Invoke(ConsoleColor.Green);
        }
        catch (Exception e)
        {
            Alt.Logger.LogError($"_test2: {e}");
        }

        try
        {
            _test3?.Invoke("Invoke");
        }
        catch (Exception e)
        {
            Alt.Logger.LogError($"_test3: {e}");
        }

        _test4?.Invoke(11111);

        _test1?.DynamicInvoke(123);
        _test2?.DynamicInvoke(ConsoleColor.Red);
        _test3?.DynamicInvoke("Dynamic invoke");
    }

    public override void OnStop()
    {
        Alt.Logger.LogInfo("Stopped TestResource");
    }
}

Expected behaviour

.Invoke() method works as well

Additional context

No response

Operating system

Windows 11

Version

16.2.26

Crashdump ID

No response

Confirmation of issue's presence

Kaiwoknats commented 3 days ago

We had the same problem when we tried to use a dynamically set Action and then trying to trigger it crashed the client without any logs for us. We then found a workaround and it was just using a class for it like a class named "dummyClass" with one Property of type boolean and everything worked fine again. That wrapping is sad but worked for us.

axeok commented 3 days ago

DynamicInvoke and using delegate instead of Action also works. But... I think we need fix for default Action)