Jump to content

Idea: Real-time in-game compilation of C# mods, a la kOS


Recommended Posts

Last night, I had an idea for a scripting framework that would allow for in-game coding and execution of true C# code, like kOS but with all the power of C# behind it.

The .NET framework contains its own compiler. This could be used to generate an assembly with certain pre-defined method names, such as "Init" or "Loop". The mod could then execute those methods at the correct time.

The main difficulty that I see in this is unloading the assembly when we want to recompile. There are ways to use Application Domains to forcibly unload an assembly, but I do not know them yet.

Aside from that, does anyone know any reasons why this might not work?

Link to comment
Share on other sites

Is there any meaningful difference between an interface to the compiler and getting the actual compiler? Right now I've got some test code that takes in a string representing source code and spits out a functioning .NET assembly. Admittedly, the only code I've tried so far is trivial, but it seems to work OK.

Link to comment
Share on other sites

Microsoft's .net Framework does contain a compiler, even with our old version: http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider%28v=vs.90%29.aspx

Actually multiple different ways, though afaik none of them is supported on all platforms/versions. Thats pretty much the only reason i know for Roslyn...

I tried to use AppDomains once on a different game, and it usually ended killing my entire app. So yeah, forcefully unloading an assembly isn't rly sth .net likes. It also would not be necessary for kOS-like scripts, btw.

Also what should be the big benefit of using C#? You could directly use the ksp-api, though most of it isn't well documented and usually not suitable for an auto pilot, so you would have to write some kind of wrapper api anyway.

You would have to use state machines (Enumerators, since we are on pre-.net4) very frequently, what isn't as easy to understand for someone new to scripting as kOS's simplified api. You also don't have the awesome dev environment... though if you do, you can just compile it anyway. To make it short: I also have thought about that once, after kOS was been released and my conclusion was that it wasn't worth the effort for me. Though it should definitively work.

Link to comment
Share on other sites

At the end of the day, what I'm imagining is a way to access the KSP API directly and dynamically from in-game. C# is my "native tongue", I'm much more familiar with it than any other language and often do these kinds of dirty tricks at my day job.

Additionally, using C# gets us a much more robust language and compiler, one that has a hundred MS engineers with unlimited freedom behind it, as opposed to anything one person could shoehorn into a mod API. I mean no slight to the author of kOS, I just see a possible different path.

Link to comment
Share on other sites

What I'm imagining is a way to access the KSP API directly and dynamically from in-game.

But what does that mean? I only see a few advantages:

- Its C# and some of us are already familiar with it

- All benefits C# gives us (performance, ...) including multiple powerful dev environments

- Compile-when-loading, so non-C#-users don't have to install a compiler (though also loosing the matching advantage)

- Loading a piece of code multiple times/ loading an update version without restarting KSP, though sth like that can be achieved anyway when loading assemblies manually

Does "directly" mean you don't want an kOS-Like wrapper? Well, this wrapper should actually help you, so you for example don't have to fiddle around with KSP's bunch of different reference frames. Does "dynamically" mean that you can reload a modified assembly?

Edited by Faark
Link to comment
Share on other sites

By "directly and dynamically", I meant that I want a kOS-like console in the game that I can type C# code into. Essentially this would be a tool for fiddling with the API, not so much an autopiloting thing.

Link to comment
Share on other sites

I'm not sure about C#, but what the Mono contained inside Unity that comes with KSP definitely does have is the Boo compiler.

Boo is also the language specifically suited for constructing domain-specific languages, even, so a plugin that presents you with such a domain-specific language and works with scripts on disk the way kOS does is quite practical. I actually considered something like this for RasterPropMonitor, because the first I've heard of Boo was in the description of how how Kerbin Orbiter MFDs were supposed to work -- but then decided that would be overkill for what it actually needs to do.

Link to comment
Share on other sites

By "directly and dynamically", I meant that I want a kOS-like console in the game that I can type C# code into. Essentially this would be a tool for fiddling with the API, not so much an autopiloting thing.

Ah okay, you basically want sth like Visual Studios or MonoDevelops "Immediate Window"? Well, we could have the second, if squad would just share a development build of KSP with us. There would still be a few additional steps to follow, but a proper debugger would make building mods so much easier. No idea why they cannot or do not want to offer such an dev build for modders. Do you have a direct line? Maybe you could ask them...

Also MonoDevelops Immediate Window is kind of limited. I'm especially missing the auto completion. Have to try out UnityVS, though that does only support VS Professional+, so i doubt Squad would ever want to negotiate an agreement with them.

Edited by Faark
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...