I am looking for a way to implement common code (C#, JS, Python, C++) into Unity at runtime.
The idea is to ask the player to solve a puzzle that involve code; kinda like these games where you have to hack doors or vaults But instead than write a simple âfill in the missing partâ, I would like to expose a decent interface that allow someone to actually
create variables and functions, and run them
have a separate environment so they canât mess up with the game or with the computer itself
get a 1:1 replica of what would it be, if they were running the code outside the game.
I did try to code my own parser, but takes forever and it will never be 1:1 to the real world. On top of that, I need to implement something that already exist, so it is not really efficient at all.
Is there any open source/official/paid way to do this? I can go for the simplified version that I wrote, but I would love to offer a realistic experience instead.
Thanks mgear; so LUA is fundamentally the only one that can be used in Unity?
My objective is not much to have them interact with the interface to manipulate game objects; it is more like âsolve the equationâ kinda of approach. So given a specific piece of code, the final result should be a specific value, and the parser will elaborate the code written by the user, to figure out if that solution is acceptable or not, considering the constrains of the language used.
Kinda like to open the door you need to move 4 bytes from a registry to another, using a buffer as transition point. This can be done in few ways, so thatâs easy to implement. But if you ask to solve a sorting, there are many algorithms that can be used; and as such, you want to cover all the cases. This is why I canât write my own parser, without oversimplify the whole activity, or spend years just to code something that is similar enough to a real parser for a language.
I had a picture in my mind, like creating on the fly a virtualenv, and have the commands run in that limited environment; so it wonât affect anything, and the only thing going out is the output of the function written. Even if I can cover the basics (variables, functions, constants, to be able to write basic algorithms), that would be enough to start with.
The problem is simple: the language used in your Miniscript is neither C#, JS, C++, Python, BASH or Java
If I need a syntax-precise command in one language, that will be different from a syntax-precise command in another language; as expected.
Fundamentally I donât need a scripting language to drive the application objects; I need a 1:1 parser that takes commands in one of the languages mentioned earlier, and execute them in a protect area of the application. You can call it a terminal emulator or a virtual env and so on; but fundamentally need to parse code written in one language, validate it and execute it.
Your solution is nice, but it is not validating code written in other languages, beside the one you implemented (unless I read wrong on the description).
No, youâre right, itâs its own language. Not any of those you mentioned.
I donât fully understand why you need it to be one of those languages. (I mean, BASH? Seriously? ) But thatâs OK, I trust you do.
Unfortunately Iâm not sure what to suggest in that case. Itâs possible to run C# code dynamically, at least on the desktop (I donât think it works on iOS). But doing so opens you up to all sorts of hacks, no matter how much you try to lock things down. I donât know any way to run such code in an airtight sandbox.
yeah⌠should it run on all platforms or pc only?
for pc you have good selection of interpreters, python and others.
and probably you could even have some small c++ compiler included in your folder, you pass the code to it,
it would output to file and you check the output, but of course would need some proper regex filters to avoid users building something dangerousâŚ(which might be difficult?)
maybe could even run the code on some cloud server, iâd think linux has better options for easy sandboxing?
(or at least it wouldnt break the users machine in any case)
Yeah, actually running the code online isnât a bad idea. I have a handful of apps on my phone that work that way, allowing you to dink around with Python, JavaScript, etc⌠as long as you have a network connection.
Yep, that was the issue; your solution is great to add scripting, but sadly I need the whole language implementation for the ones I did mention.
Bash is cool Imagine if you play something like uplink with a real BASH environment
The languages are used so someone that knows how to program in the âreal worldâ, can get more fun out of the game. For a car simulator enthusiast, the best part is to have a car that is reacting like a real one, so he/she can use real world notions to drive the simulated car. In my case, knowing a programming language does augment the experience of playing a game, where you actually write real code while in game. It may be niche, but it is not like I am going to sell it anyway
take a look at roslyn then. (google âgithub roslynâ)
thats the codename for the actual c# compiler. (well, there is mono with its own compiler as well, but its not as widely used).
Thereâs no easy way to sandbox this though. Youâd have to do your own syntax analysis and binding steps which will be a ton of work, but is possible).
It can compile code at runtime (but it is pretty slow, on the order of tens of milliseconds, so you definitely wonât get anything like âcompile every frameâ if you plan to run at 60fps)
Maybe that could work for you?
As for the 1:1 output, yeah, thats exactly what the c# compiler does, except that it also has a neat feature of being able to compile stuff and give you classes / delegates you can use at runtime.
Preventing âmessing upâ will likely be the hardest thing here because thereâs ton of ways to do stuff in c#.
Hereâs a hint to prevent you from going into the wrong direction from the get go:
Lets assume youâre trying to be âsmartâ and are preventing file and registry access etcâŚ
And all you give the user to interact with is your âclass MySafelyDesignedClassâ.
Then imagine whatâd happen here: typeof(MySafelyDesignedClass).Assembly.GetTypes().Find(t => t.Name == "FileInfo").GetMethod("Delete") ...
And there are other ways that donât even rely on reflection like thatâŚ
edit: (i realize the above code isnât exactly right, FileInfo is defined somewhere else, but the point still stands, you can easily load arbitrary stuff that way too :P)
Just windows would be fine; OSX at most, but it is not really a thing.
I see, so you would say that it is easier to send out the code via network, and run it maybe on a remote virtual box? I know cloud9 that is a 100% web based compiler and editorâŚI used it while I was playing with the Beaglebone.
It would be great to have something like hackerank; they support a ton of languages, and I am really curious to know how do they do that
So at this point, the solutions are very limited: either I create a web service that collect scripts, and send them to the appropriate compiler, or I write something customized for each languageâŚI never thought it would be so complicate to write a terminal simulator that would be mimicking a computer. Thanks for the hints!