Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arrayCache.Length NullReferenceException #76

Closed
lmoadeck-Lunity opened this issue Nov 30, 2023 · 5 comments · Fixed by #86
Closed

arrayCache.Length NullReferenceException #76

lmoadeck-Lunity opened this issue Nov 30, 2023 · 5 comments · Fixed by #86

Comments

@lmoadeck-Lunity
Copy link

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=OmsiHook
StackTrace:
at OmsiHook.MemArrayBase`1.get_Count() in D:\users\suityan\Source\Repos\Omsi-Extensions2\OmsiHook\MemArrayBase.cs:line 26
do not know sure why it happens

@lmoadeck-Lunity
Copy link
Author

ok so it is confirmed that it crashed after i spawned my bus

@lmoadeck-Lunity
Copy link
Author

i used the CLI project

@space928
Copy link
Owner

space928 commented Nov 30, 2023 via email

@lmoadeck-Lunity
Copy link
Author

i used the original CLI and did not change anything.
stack trace:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at OmsiHook.MemArrayBase`1.get_Count() in D:\users\suityan\Source\Repos\Omsi-Extensions2\OmsiHook\MemArrayBase.cs:line 26
   at OmsiExtensionsCLI.DXTests.CreateTexture() in D:\users\suityan\Source\Repos\Omsi-Extensions2\OmsiExtensionsCLI\Program.cs:line 104
   at OmsiExtensionsCLI.Program.Main(String[] args) in D:\users\suityan\Source\Repos\Omsi-Extensions2\OmsiExtensionsCLI\Program.cs:line 45

D:\users\suityan\Source\Repos\Omsi-Extensions2\OmsiExtensionsCLI\bin\Debug\net6.0-windows\OmsiExtensionsCLI.exe (process 27948) exited with code 0

@space928
Copy link
Owner

space928 commented Dec 1, 2023

Ah, ok I see what the problem is. It seems that the vehicle's script texture array is null in Omsi, which means OmsiHook can't dereference it. In the future I might make it so that MemArrays handle this behaviour a bit better. For now though, if you need to use access script textures (to use the example in OmsiExtensionsCLI) you can either: make sure your bus has script textures, or wrap the MemArray access in a try catch block, or test if the scriptTexes.WrappedArray property is null before accessing scriptTexes.Count.

So for instance in the CreateTexture() method of Program.cs, change it to include this:

// [...]

var scriptTexes = omsi.Globals.PlayerVehicle.ComplObjInst.ScriptTextures;
//+++++++++++ BEGIN
if (scriptTexes.WrappedArray == null)
    return;  // The player vehicle has no script textures to replace
//+++++++++++ END
for (int i = 0; i < scriptTexes.Count; i++)
{
    var old = scriptTexes[i];
    Console.WriteLine($"Replacing script tex: texPn:uint[{old.TexPn?.Length??-1}], color:{old.color}, tex:{old.tex} with tex:{texture.TextureAddress}");
    scriptTexes[i] = new()
    {
        TexPn = old.TexPn,
        color = old.color,
        tex = unchecked((IntPtr)texture.TextureAddress)
    };
}

@space928 space928 linked a pull request Jan 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants