c# compiled with CodeDom sandboxing

I am curious if anyone has any experience using .net CodeDom to compile c# code at runtime, specifically with sandboxing that code. I got code to compile at runtime and I can access my main assembly from my runtime code the problem is that I can access everything and I am hoping to allow the user to edit these scripts.

So I need some way to say what is allowed in and out of the code I am compiling (obviously I could just scrub the code for any illicit code but that would be a nearly impossible task to get right).

I have seen that some people have unity working with python and another person got it working with lua, so if either of you are around and might be able to shed some light that would be appreciated as well, but I am not sure if either of your methods allowed sandboxing or not.

Thanks in advance.

Edit: I forgot to mention, I am at the moment linking the Runtime compiled code with UnityEngine.dll which is a problem because it can access everything including GameObject.CreatePrimative and other fun things like that. I was going to try to link it with just a small portion of my code, maybe one script, but I dont see how to do that in unity. So if you know how to get a single script to compile into an assemby that would probably solve my problem.

Hi there

I have been trying to get something VERY similar to this working over the last couple of days. I cannot give you an answer, but here is my most critical points, maybe some of it can help you or steer you in some direction:

  1. You can create/compile a dll/assemly dynamically using a batch file together with a tool called mcs. This tool comes with the mono installation, but Microsoft also has one called csc. It takes command prompt commands, here is an example of the command I use to create my dll:

mcs -target:library -out:C:\testAssembly\MyAssembly.dll -r:/Applications/Unity/Unity.app/Contents/Frameworks/UnityEngine.dll MyAssembly.cs

I can send you a link to the list of commands if you need it. In fact, here is the link

  1. You can use the following link, by AngryAnt, to load this dll dynamically

http://eej.dk/angryant/general/tipsandtricks/downloading-the-hydra/

  1. Apparently with Unity3, you can include components in dll’s, but I’m not sure how that works and if it will solve your problem. See this post

http://answers.unity3d.com/questions/23449/can-i-store-dlls-in-asset-bundles

I hope this can maybe give you some ideas, although you probibly know this stuff allready;)

Cheers

Thanks that helped me get further this morning. In the editor I am now able to load a c# script, compile it so it only has access to precompiled dll that I included into my project. That allows me to have the precompiled dll be a gateway between my users c# script and my game.

This works, obviously it will need fleshed out so the gateway will actually be useful. However my problem now is that if I compile my game to a standalone exe for windows it errors when trying to compile the c# script. The error in question is

System.TypeInitializationException: An Exception was thrown by the type initializer for Mono.CSharp.CSharpCodeCompiler → System.IO.FileNotFoundException: Windows Mono path not found

So my first thought is, obviously my standalone cant find mono, but I am under the impression the standalone exe’s ship with their own copy of Mono. I am making sure to select the full .net 2.0 not the subset.

any other ideas I should try?

it ships with its own copy of mono but thats limited to what unity needs in the player, not a fully fledged mono install bomb. so no compiler etc present

I see several projects that involve compiling scripts, if they are doing that in unity they almost surely are doing it using CodeDom. Maybe not though, but I have seen mixed responses to this most of which just say make sure you are using .net 2.0 for compatibility instead of .net 2.0 Subset.

And if this is the case, if my target machine had a full install of mono would they be able to use CodeDom stuff in my game?

Thanks

Hi DP, this is a questions as well as a headsup

I just tried to compile my cs file into a dll and a new error came up saying that the mcs compiler is not compatible with generics (.net 2.0). So I tried the gmcs compiler, but it gives me a long error about some unhandled exception and illegal enum value error 2051. I tried to google it, but there seems to be very little info on the net,

Have you had this problem or maybe any ideas?