- Home /
IL2CPP mod support
I have some very basic mod support that loads DLLs from a specific folder as Assemblies and inspects it's types. If it finds an "IMod", it calls it's respective Awake() method. I will spare you the code, since it needs to un-/subscribe to the Appdomain.CurrentDomain.AssemblyLoad
event, check for other assembly references etc. - and it works in the editor either way. However it does not work with an IL2CPP built standalone player.
Do I need to do some extra steps? Is it even possible?
Answer by Bunny83 · Jun 09, 2020 at 10:19 AM
No, it's not possible. IL2CPP is an AOT platform (Ahead Of Time compilation). So when you build your game / application all your managed IL code will be converted to native C++ code and compiled into native code. So at runtime of your game there is no mono / .NET environment. Some reflection methods still work which can be inferred at compiletime by the cross compiler. However anything that requires dynamic code generation does not work.
However it should still be possible to use native OS calls (like LoadLibrary, GetProcAddress, ...) to load native DLLs manually. However designing a proper interop interface and get all communication between your game and your mod / plugin working can be tricky. Of course your mod / plugin has to be written in native code as well (probably with a C style interface). So mods has to be written in something like C, C++ or any other programming language which generates native code libraries.
Depending on the level of modding support you want to offer it's probably better to just integrate a scripting language / environment like LUA into your game. It also helps with security issues for both sides (so you can better protect core parts of your game and also make it much harder for your community to spread malicious code).
If you really want to load managed assemblies at runtime you can't use IL2CPP and have to stick with Mono as backend.
Just like expected - what a bummer.
I want mod support along the lines of Cities Skylines, where you'd reference a C# dll (and Unity DLLs), which are now distributed with the "backup" folder in an IL2CPP build, and are thus able to extend the code base, having access to AAAALLLLL of the types and functionality of the game itself.
So my guess is that a scripting language wouldn't be powerful enough, because it would be limited to what kinds of mods I can think of myself.
1) I guess everything is possible but how difficult would it be to have a "second program" running alongside the game that is a .NET AppDomain ("$$anonymous$$odDomain"), which calls into the native code? Isn't this the fundamental way $$anonymous$$ono-games worked before/still work today?
2) Is it not possible to magically IL2CPP that bod-boi mod code, generating DLLs (keeping all types and such of course) which could then get loaded in, even if it can only happen in a game launcher type application?
3) Otherwise - how would someone who knows C++ link their code to my game? What is the equivalent of adding a reference to Assembly-CSharp to the "mod project", which enables you to see the types, with an IL2CPP build?
But either way your answer was invaluable - thank you. Even mentioning malicious code? Seems like you're able to read my $$anonymous$$d... But - as said - I BELIEVE (I know nothing in that regard), that a scripting language is limited in regards to functionality. EDIT: If it isn't clear, I basically want modders to simply comile a DLL and place it into the designated folder. Furthermore, the game should detect these mods and manage them in an options menu.
Your answer
Follow this Question
Related Questions
Keep assemblies in separated dll instead of GameAssembly.dll 0 Answers
Can/should mcs.rsp files be used to reference my own class libraries? 0 Answers
Compile Error (CS0246) appears for every script in the project. 0 Answers
Why does Assembly-CSharp reference UnityEditor.dll 0 Answers
in Unity 5, how can I get/set an asset's AssetBundle assignment via scripting? 1 Answer