Steamworks and IL2CPP

Thursday, February 15, 2018

Unity 2018 supports IL2CPP on windows standalone. But when you run Facepunch.Steamworks with it, you get errors, like.
NotSupportedException: IL2CPP does not support marshaling 
delegates that point to instance methods to native code. 

So yesterday I set out to fix that. I'm guessing a lot of people are going to be doing the same, so here's some info.

The Problem

Pretty clear, you can't turn a instance method (a class function) into a function pointer and pass it to c++ with il2cpp. But you can pass static functions.

So the solution is to convert all of the functions you pass to c++ into static functions and use those instead. How hard that is depends on your implementation.

The Next Problem

Your next problem is going to be
NotSupportedException: To marshal a manged method, please add 
an attribute named 'MonoPInvokeCallback' to the method definition

This seems to be a little bit of il2cpp black magic. I don't know where the MonoPInvokeCallbackAttribute is defined, but we don't want to be including unity or mono dlls in Facepunch.Steamworks.

Thankfully, where it's defined doesn't matter. You can define your own version. in FPSW I did it like this.

internal class MonoPInvokeCallbackAttribute : Attribute
{
public MonoPInvokeCallbackAttribute() { }
}


Then add that to any function you're gonna be marshalling as a function pointer.

[MonoPInvokeCallback]
internal static void OnResult( IntPtr param )
{
OnResultWithInfo( param, false, 0 );
}


I've seen implementations of MonoPInvokeCallback taking a System.Type in the constructor, with people passing in a delegate type. That doesn't seem to be needed in my experience (but fair warning, I've only tested it on 64bit windows builds).

 

More Rubbish

Your Portfolio Gave Me Diarrhea

Friday, October 1, 2021

Motivation

Friday, September 24, 2021

We got married

Thursday, September 16, 2021

Unity Quiz

Sunday, September 6, 2020

Everything We Watched In Lockdown

Thursday, September 3, 2020

Monitor Upgrade

Monday, February 5, 2018

Steamworks Libraries

Wednesday, January 31, 2018

Procedural Generation

Saturday, January 27, 2018

Unity 2018.1

Friday, January 19, 2018

TimeSince

Tuesday, January 16, 2018