citizenfx / fivem

The source code for the Cfx.re modification frameworks, such as FiveM, RedM and LibertyM, as well as FXServer.
https://cfx.re/
3.52k stars 2.07k forks source link

Mono: Calling GameplayCamera.Matrix causes a NullReferenceException (b1604+b2545) #1884

Open justalemon opened 1 year ago

justalemon commented 1 year ago

I was trying to use World.GetCrosshairCoordinates and I stumbled upon a NullReferenceException when calling said function. I was able to trace back this exception to the Matrix property of GameplayCamera thanks to this test code:

using System.Threading.Tasks;
using CitizenFX.Core;

namespace MatrixTesting.Client;

public class Testing : BaseScript
{
    public Testing()
    {
        Tick += OnTick;
    }

    private async Task OnTick()
    {
        Matrix matrix = GameplayCamera.Matrix;
    }
}

Which will produce the following exception:

[    614438] [b2545_GTAProce]             MainThrd/ Failed to run a tick for Testing: System.NullReferenceException: Object reference not set to an instance of an object
[    614438] [b2545_GTAProce]             MainThrd/   at System.Buffer.memcpy4 (System.Byte* dest, System.Byte* src, System.Int32 size) [0x00002] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
[    614438] [b2545_GTAProce]             MainThrd/   at System.Buffer.Memcpy (System.Byte* dest, System.Byte* src, System.Int32 size) [0x00073] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
[    614438] [b2545_GTAProce]             MainThrd/   at System.String.memcpy (System.Byte* dest, System.Byte* src, System.Int32 size) [0x00000] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
[    614438] [b2545_GTAProce]             MainThrd/   at (wrapper unknown) CitizenFX.Core.Matrix:PtrToStructure (intptr,object)
[    614438] [b2545_GTAProce]             MainThrd/   at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)
[    614438] [b2545_GTAProce]             MainThrd/   at System.Runtime.InteropServices.Marshal.PtrToStructure[T] (System.IntPtr ptr) [0x00000] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
[    614438] [b2545_GTAProce]             MainThrd/   at CitizenFX.Core.Native.MemoryAccess.ReadMatrix (System.IntPtr pointer) [0x00000] in C:\gl\builds\master\fivem\code\client\clrcore\Native.cs:181 
[    614438] [b2545_GTAProce]             MainThrd/   at CitizenFX.Core.GameplayCamera.get_Matrix () [0x00000] in C:\gl\builds\master\fivem\code\client\clrcore\External\Camera.cs:551 
[    614438] [b2545_GTAProce]             MainThrd/   at MatrixTesting.Client.Testing+<OnTick>d__1.MoveNext () [0x00008] in D:\Proyectos\Testing\MatrixTesting\MatrixTesting.Client\Testing.cs:15 

I tested game build 2545, I'm not sure if this can be replicated with other game versions.

If more information is needed, feel free to let me know.

justalemon commented 1 year ago

Was able to replicate this with the same traceback on b1604.

gottfriedleibniz commented 1 year ago

The fork of SHVDN used by FiveM (for reference 1103 is its last documented GameVersion) does not include NativeMemory.cs.

Besides Entity.cs, all uses of MemoryAddress will return IntPtr.Zero and will throw an exception in the subsequent memcpy (gross; see CFX-TODO in clrcore). You could maybe turn this issue into a feature request?

A semi-related question for bubble is what is the licensing of clrcore/External? (This part was always unclear to me, given its history. Hopefully this question does not come off as asking maliciously)