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

To marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition.-- at Plugins.Discord.DiscordRpc.Initialize #186

Closed
franckmolto opened this issue May 28, 2018 · 16 comments

Comments

@franckmolto
Copy link

franckmolto commented May 28, 2018

I m having this issue with discord rpc under Unity 2018.1 , when using il2cpp as build mode, targeting windows.
I dont have this problem in the editor as it's mono.

I tried this:

public delegate void InitializeCallback(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId);
[MonoPInvokeCallback(typeof(InitializeCallback))]
[DllImport("discord-rpc", EntryPoint = "Discord_Initialize", CallingConvention = CallingConvention.Cdecl)]
public static extern void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId);

but it doesnt help. Not sure if I'm doing the right thing

@msciotti
Copy link
Collaborator

Hey there. I have not dug into (or heard of before this issue) compiling/using the SDK with IL2CPP in Unity, so I can't give you a quick answer. It looks like mono is complaining about things that may not be be a problem using the standard C# Unity stuff.

Is IL2CPP a commonly used build mode with Unity? Are many other devs struggling with this as well?

@franckmolto
Copy link
Author

Thank you for your answer :).

The standard Unity stuff is now to compile in c++ with il2cpp, it's a build option in Unity, that is mean to be the correct way of doing things. It is said to be faster.

It's not a problem related to my project, but I couldnt find help on google about it.

@msciotti
Copy link
Collaborator

It seems like this may be related:

https://garry.tv/2018/02/15/steamworks-and-il2cpp/

Perhaps try defining your own attribute like this? I can ask @snowyote to take a look when he has time.

@franckmolto
Copy link
Author

I tried with the same MonoPInvokeCallback on all discordrpc functions, but with no luck

@franckmolto
Copy link
Author

I can ask @snowyote to take a look when he has time.

thank you

@franckmolto
Copy link
Author

franckmolto commented Jun 4, 2018

@msciotti
Copy link
Collaborator

msciotti commented Jun 4, 2018

Talked with him about this. Neither of us are really well versed in IL2CPP, so we can't say for sure what the fix would be right away.

Considering you're the first to come across this issue, it seems like a somewhat non-standard way of doing things. I can put it on the docket to be addressed, but it's not a super high priority for us. If you figure out the changes and make a PR, we'll be happy to review.

@mdsitton
Copy link

mdsitton commented Jun 6, 2018

    [MonoPInvokeCallback(typeof(DiscordRpc.ErrorCallback))]
    public static void ErrorCallback(int errorCode, string message)
    {
        Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message));
    }

Changing the Callbacks to be static class members and doing the MonoPInvoke stuff works for me.

@franckmolto
Copy link
Author

Yes it works, thank you !

@msciotti
Copy link
Collaborator

msciotti commented Jun 6, 2018

Thank you very much for your help @mdsitton! Marking this as closed.

@dylanh724
Copy link

dylanh724 commented Oct 20, 2018

Windows il2cpp is now standard in Unity 2018.2+, going forward (and was already standard for iOS and... Android?):

This is a stable version of Unity, meaning we must permanently resolve this issue, if you want Unity devs to continue using Discord RP: Please see the above referenced issue to try to port this to an il2cpp-friendly build for Unity ^

EDIT: Thread revival @ #243
^ Let's continue to chat here.

@msciotti
Copy link
Collaborator

I'm perfectly happy accepting a PR for the static class member changes if it won't break existing implementations (and it shouldn't?).

Unless you have suggestions for a more "real" solution. As mentioned above, neither myself nor snowyote are well-versed in this il2cpp stuff, so we're going a bit on community knowledge here.

@joshpeterson
Copy link

Hey, I'm a dev at Unity working on IL2CPP, so I thought I might help here.

The use of the MonoPInvokeCallback attribute for cases where a managed delegate is marshaled to native code is required for both IL2CPP and Mono AOT. As it turns out, Unity won't be supporting Mono AOT on many platforms soon (and currently we don't support too many), but this is a more general AOT issue.

The AOT code generator needs to know which managed methods could be called from native code, as it needs to generate marshaling wrappers. Doing this for all managed methods is not feasible, so the MonoPInvokeCallback attribute guides the AOT code generator.

While IL2CPP is now an option for desktop standalone player builds in Unity 2018.2 and later, we plan to continue to support Mono JIT for those platforms. We don't plan to require IL2CPP as is the case on iOS, for example.

Still, using MonoPInvokeCallback is the correct solution here, and should have no impact for other cases. The chances of it breaking something else are pretty low (or zero).

@dylanh724
Copy link

dylanh724 commented Nov 2, 2018

[MonoPInvokeCallback(typeof(DiscordRpc.ErrorCallback))]
public static void ErrorCallback(int errorCode, string message)
{
    Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message));
}

@mdsitton Heya, while this 100% seems to be the right course of action, changing this causes a few domino effects on other things: What else did you change to get this working? I tried a few things but couldn't get it right.

If you help us out @ #243 , there's $50 with your name on it.

@joshpeterson
Copy link

@dylanh724: Can to describe the other effects this causes in more detail?

@StarKrafto
Copy link

It should possibly be noted that Debug.Log() can only be called within the main thread. So, if you call ErrorCallback() from another thread, the Debug Output will not work. You have to follow the Synchronisation rules between threads in this case.

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

No branches or pull requests

6 participants