Roslyn C# – Runtime Compiler 2.0


Roslyn C# allows you to compile and load C# source code at runtime using the .Net compiler platform, making it easy to add mod support, in-game coding, extensibility and much more to your game


by Trivial Interactive


★★★★★ star rating
Price History +

NOTE - Requires desktop platform with Mono backend by default, but other platforms and IL2CPP support is possible with limitations using a free addon as described here.


Roslyn C# is a fully featured compiler and runtime API for compiling, loading and executing external C# source code and managed assemblies (.dll) as part of your game. The asset aims to simply the process of compiling and loading code by providing simple but powerful API's to compile from source strings, files, syntax trees, C# projects and folders. Additionally there are async variants provided for all compile API's so that your game can continue to run while compiling occurs in the background.


Forum | Discord | Documentation | Scripting Reference | Samples | FAQ | Support


Code Security

One of the main problems with loading external code (In the case of modding for example) is the possiblity for the code to be potentially harmful if it may have come from an untrusted source. Roslyn C# has you covered in this regard with the code security feature.

All code loaded via Roslyn C# has our proprietary code security system perform static anaysis of the IL bytecode to search for potentially harmful features. By creating custom code validation rules you can detect and prevent loading of an assembly if it includes references to illegal assemblies, namespaces, types, or uses unsafe contexts or platform invokes. The system will report every occurrence of a reference that is deemed illegal and output a detailed report to help the user fix security issues if they arrise.


Execution Security

New feature for version 2.0 of the asset!

Another issue with running external or user input source code is that the game has no ability to control the execution of such code, meaning that the code could potentially crash the host game if an infinite loop is written either maliciously or by mistake. A normal solution to this problem could be to run the user code on a background thread, however that becomes an issue if the user code needs to access the Unity API, which can only be done from the main thread.

For version 2.0 of the asset we have addressed this issue by injecting runtime security checks into compiled code that will throw an exception if a loop has exectured for X amount of time or X iterations. It means that the game can recover gracefully if and infinite loop is entred, and the game will only hang for a couple fof seconds before continuing rather than crashing the app.


Features

  • Compile and Load C# scripts at runtime
  • Use the latest C# language features
  • Fast execution - Once compiled, external code will run as fast as game code
  • Simple and easy to use API for assembly, type and instance reflection
  • Support for non-concrete communication using script proxies
  • Automatic type construction using correct method (AddComponent, CreateInstance, new)
  • Code security validation means that unsafe code can be identified and discarded
  • Code security generates a detailed report upon failure containing information about illegal types used and every usage occurrence in the external code
  • Execution security ensures that loaded code cannot crash the host game with long or infinite loops
  • Fully commented C# source code included
  • Comprehensive documentation of the API for quick and easy reference

  

New For 2.0

Check out what is new for version 2.0 of the asset over on the forum.