LemonUIbyLemon / LemonUI

LemonUI for .NET (FiveM, RageMP, RagePluginHook and ScriptHookVDotNet 3)
MIT License
182 stars 41 forks source link

[FiveM] dynamic types not working after loading this library #85

Closed Sander0542 closed 1 year ago

Sander0542 commented 2 years ago

After this library has been loaded by the FiveM client, the dynamic type doesn't work anymore.

I ran the same code, but without LemonUI. That worked without any exceptions. Why could this happen?


Stacktrace:

[     39172] [b2545_GTAProce]             MainThrd/ Failed to run a tick for ClientMain: System.TypeLoadException: Could not resolve type with token 0100000e
[     39172] [b2545_GTAProce]             MainThrd/   at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesDataInternal (System.Reflection.ICustomAttributeProvider)
[     39172] [b2545_GTAProce]             MainThrd/   at System.MonoCustomAttrs.GetCustomAttributesData (System.Reflection.ICustomAttributeProvider obj) [0x0000e] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Reflection.CustomAttributeData.GetCustomAttributes (System.Reflection.Assembly target) [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at Mono.CSharp.ImportedAssemblyDefinition.ReadAttributes () [0x00000] in /mnt/idfk/meow/mono/mcs/mcs/import.cs:1719 
[     39172] [b2545_GTAProce]             MainThrd/   at Mono.CSharp.MetadataImporter.GetAssemblyDefinition (System.Reflection.Assembly assembly) [0x00027] in /mnt/idfk/meow/mono/mcs/mcs/import.cs:972 
[     39172] [b2545_GTAProce]             MainThrd/   at Mono.CSharp.ReflectionImporter.ImportAssembly (System.Reflection.Assembly assembly, Mono.CSharp.RootNamespace targetNamespace) [0x00000] in /mnt/idfk/meow/mono/mcs/mcs/reflection.cs:86 
[     39172] [b2545_GTAProce]             MainThrd/   at Microsoft.CSharp.RuntimeBinder.DynamicContext.Create () [0x000c6] in /mnt/idfk/meow/mono/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/DynamicContext.cs:104 
[     39172] [b2545_GTAProce]             MainThrd/   at Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.FallbackInvokeMember (System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args, System.Dynamic.DynamicMetaObject errorSuggestion) [0x00006] in /mnt/idfk/meow/mono/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpInvokeMemberBinder.cs:156 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Dynamic.DynamicObject+MetaDynamic.BindInvokeMember (System.Dynamic.InvokeMemberBinder binder, System.Dynamic.DynamicMetaObject[] args) [0x0003c] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Dynamic.InvokeMemberBinder.Bind (System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args) [0x00016] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Dynamic.DynamicMetaObjectBinder.Bind (System.Object[] args, System.Collections.ObjectModel.ReadOnlyCollection`1[T] parameters, System.Linq.Expressions.LabelTarget returnLabel) [0x000c6] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T] (System.Runtime.CompilerServices.CallSite`1[T] site, System.Object[] args) [0x00019] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.CompilerServices.CallSiteOps.Bind[T] (System.Runtime.CompilerServices.CallSiteBinder binder, System.Runtime.CompilerServices.CallSite`1[T] site, System.Object[] args) [0x00000] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,bool)
[     39172] [b2545_GTAProce]             MainThrd/   at Kifflom.FiveM.RP.Client.Exports.SpawnManager.SetAutoSpawn (System.Boolean enabled) [0x00000] in C:\GitHub\Kifflom\Kifflom.FiveM.RP\src\client\Kifflom.FiveM.RP.Client.Exports\SpawnManager.cs:40 
[     39172] [b2545_GTAProce]             MainThrd/   at Kifflom.FiveM.RP.Client.Scripts.ClientMain+<PlayerJoinedTick>d__6.MoveNext () [0x00020] in C:\GitHub\Kifflom\Kifflom.FiveM.RP\src\client\Kifflom.FiveM.RP.Client\Scripts\ClientMain.cs:81 
[     39172] [b2545_GTAProce]             MainThrd/ --- End of stack trace from previous location where exception was thrown ---
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[     39172] [b2545_GTAProce]             MainThrd/   at Kifflom.FiveM.RP.Client.Logging.BaseScript+<>c__DisplayClass5_0+<<add_Tick>b__0>d.MoveNext () [0x00086] in C:\GitHub\Kifflom\Kifflom.FiveM.RP\src\client\Kifflom.FiveM.RP.Client.Logging\BaseScript.cs:30 
[     39172] [b2545_GTAProce]             MainThrd/ 
justalemon commented 2 years ago

Hm... weird.

What specific LemonUI features are you using?

Sander0542 commented 2 years ago

It happens when I load the library and not even use any of the features. Just adding the NuGet packages triggers the error.

When I add all the .cs files to the project and compile it there it works.

justalemon commented 2 years ago

It happens when I load the library and not even use any of the features

Including the fxmanifest.lua reference? It needs to be added as a client script.

Sander0542 commented 2 years ago

I tried with and without the reference in the client_scripts property in the fxmanifest.lua

Sander0542 commented 2 years ago

This code sample doesn't work while using LemonUI.FiveM


public class ClientMain : BaseScript
{
    public ClientMain()
    {
        Tick += PlayerJoinedTick;
    }

    private async Task PlayerJoinedTick()
    {
        if (API.NetworkIsPlayerActive(API.PlayerId()))
        {
            Exports["spawnmanager"].setAutoSpawn(false);

            Tick -= PlayerJoinedTick;
        }
    }
}
Sander0542 commented 2 years ago

I did some testing myself and was able to build a NuGet package which works.

This commit fixes the error (I only tested with FiveM) https://github.com/LemonUIbyLemon/LemonUI/commit/292e1f303b479661f59e7ab68b2d5bc2aaba518b

justalemon commented 2 years ago

Ill take a deeper look tomorrow Sunday.

TomStilson commented 2 years ago

I'm gettting the same problem and I think its related to Exports in CitizenFX.Core. For some reason, if LemonUI is loaded, whenever you use Exports, the exception System.TypeLoadException: Could not resolve type with token 0100000e will be thrown.

justalemon commented 2 years ago

Pinpointed the problem back to version 1.3.0, so this was caused by something between 1.2.0 and 1.3.0.

Ill try to see what I can do, but I would expect this to be fixed in the FiveM codebase instead of a LemonUI patch, as is not a LemonUI bug.

justalemon commented 2 years ago

image

Sander0542 commented 2 years ago

When I updated the build system in my branch, I got it to work with the latest FiveM version. I am not very experienced with the C# compiler, but maybe you could take a look at my branch and see what could have fixed it.

One change that stands out is the removal of the .net suffix in assembly name of the FiveM csproj

<AssemblyName>$(AssemblyName).net</AssemblyName>

Commit: https://github.com/LemonUIbyLemon/LemonUI/commit/292e1f303b479661f59e7ab68b2d5bc2aaba518b

Compare: https://github.com/LemonUIbyLemon/LemonUI/compare/master...Kifflom-Creations:LemonUI:master

Repo: https://github.com/Kifflom-Creations/LemonUI

justalemon commented 2 years ago

That could work, yes. But this appears to be a FiveM bug that needs to be diagnosed and fixed upstream.

By the way, have you tried to just rename LemonUI.FiveM.net.dll to LemonUI.FiveM.dll?

Sander0542 commented 2 years ago

By the way, have you tried to just rename LemonUI.FiveM.net.dll to LemonUI.FiveM.dll?

No, I have not tested that

justalemon commented 2 years ago

Interesting... image

justalemon commented 2 years ago

Managed to trigger the same FiveM crash again:

image

justalemon commented 2 years ago

Ill end up using the solution suggested by @Sander0542

Looks like FiveM doesn't knows how to deal with the libraries set as client_script(s), so you will always see one of this:

The behavior changes depending on the LemonUI version. Recent versions (1.3-master) show the exception, while old versions (alpha-1.2) show the crash.

Not gonna lie, I have been trying to fix this shit for months, fucking burnout got me every single time.

As it turns out, it was FiveM all along.

justalemon commented 2 years ago

@Sander0542 would you be kind to send the Pull Request? So you get the credit for the commit :)

Sander0542 commented 2 years ago

I created a pull request with the required changes https://github.com/LemonUIbyLemon/LemonUI/pull/99

If you want me to implement GitVersion, so you don't have to manually change the version for every release, then let me know and I'll create another PR with that part