Calling C Code From Mono/.NET

Mono is, in my view, a potentially great option for game scripting. Perhaps the most obvious example of this in action is the Unity game engine, and there are a number of benefits to Mono over, say, rolling your own scripting language:

  • Nice IDE's like Visual Studio and Xamarin, with code completion and all that good stuff
  • The Mono Soft-Mode Debugger makes it easy to step through code in an IDE
  • It's (relatively) fast, i.e fast for a scripting language
  • C# is type-safe, but easy to learn, a lot of people know it, and there's plenty of learning resources on the web
  • .NET interface can be used by modders

So I started to play around with embedding Mono, and providing a way for C# code to call code in a C++ application, a bit like this:

Read More

Integrating Mono.Cecil with Unity

Have you seen JB Evain's talk at Unite? If you haven't, then you probably can't, as it has been inexplicably taken down. The subject matter is mostly about using a library called Mono.Cecil to actually alter a .NET assembly, allowing for both injecting and removing code. This gives us the ability to create C# post-processors which operate on the compiled assembly, not the code files. I've been writing some pretty repetitive code of late, and so I figured I'd try using this approach to simplify things. There is a problem with this though - there isn't an "on assembly built" event in Unity that we can hook in to.

Options:

  1. Compile our code to a managed DLL, and set up Visual Studio to run the assembly post processor on it before placing it in the output directory.
  2. Hack it up.

One of the things I really hate is a shitty workflow, and (at least at the moment) I find using managed DLLs in Unity to be conducive to exactly that. For example, if I double click on a message in the console, it won't open the file in my IDE like it does when I'm letting Unity compile everything for me. In addition, I'd like to have the option to distribute what I'm working on as an Asset Store editor extension, and imposing upon users that they must compile their code to a managed DLL and import it into their project is pretty much out of the question if you ask me. So, I opted for option #2.

Read More

Mono and mkbundle on Windows

A problem for a fair few Mono and .NET developers is "what if the user doesn't have Mono or .NET installed?" You can bootstrap the .NET installer into an .msi installer, but if you really need to just zip up a few files and send them to someone, and have your application just work, then you might need mkbundle.

Mkbundle is a great tool included in the Mono framework, which allows you to embed the Mono runtime with your application, into a single executable. On OSX you can just use MonoMac (especially if you want to distribute something on the Mac App Store), but on Windows we'll need to crank up the good ol' command line!

Right, the first thing to say, is that on Windows, mkbundle is sort of broken (at least in Mono 2.10.5 which I'm using), the code is pretty straightforward, so I might have a go at patching it up if I get some time in the next few months, I've never contributed to an open-source project before, first time for everything. Anyway, here's the list of workarounds I had to use to get an application to bundle and run:

Read More

MonoDevelop and Loading Embedded Resources

I recently had an unusual amount of difficulty just loading an embedded resource in a MonoDevelop C# project. I couldn't find a great deal of useful information through googling, and in the end found out how to do it by looking through the GTK# source code of all things. So for anyone else wondering, here's how I achieved it:

Select the file in the solution view

Read More