Introducing Lunt

Last summer, a friend and I started to write a little Paradroid clone. Since my only real experience as a game programmer was with XNA and MonoGame, I had grown fond of the XNA Content Pipeline, and naturally wanted something similar for this project.

The game was going to be cross platform (Windows and Linux) so the asset pipeline had to be cross platform as well. The natural step was therefore to see if the MonoGame Content Builder (MGCB for short) could be used for this.

Alas, MonoGame is a port of XNA, and since they need to maintain backwards compatibility; they also kept the XNB output format. Since we had no wish to implement reading of the XNB format to be able to use MGCB, in combination with the difficulties involved with debugging the MGCB pipeline and the lack of unit tests, we decided to build something simple of our own.

Bender

How does it work?

The Lunt pipeline works almost exactly like the XNA Content pipeline. It's composed of two mandatory components (the importer and the writer) and one or many optional processors.

The importers role is to read asset data from a stream and to create a managed representation of it. If a processor is defined for an asset, the managed representation will be sent there to be either transformed (a change of type) or processed in some way. The last step in the pipeline is the actual writing of the asset back to a stream, and this is the writer's responsibility.

The stream I mention is normally a file stream, but can be any kind of stream thanks to the built-in extensibility points.

Pipeline

A more technical explaination of the pipeline can be found in the Lunt Wiki.

Key points

  • Build engine not tied to a specific hosting provider or output format.
  • Supports incremental builds.
  • Easy to debug the build pipeline by creating a debug project and referencing the Lunt build engine assembly.
  • The ease of C++/CLI to call into native libraries, makes it really simple to use existing functionality such as Assimp or FreeType as part of the pipeline.

Finally

I will probably continue to write posts about Lunt here from time to time, so if you're interested in knowing more, bookmark or add this blog to the RSS reader of your choice.

You can find the source code and more information on the GitHub page at https://github.com/Lunt/Lunt.

Displaying TeamCity build status on GitHub

Have you ever wanted to display the current CI build status for a TeamCity project in your GitHub README? I did but couldn't find any good, straight forward information about how to do it. Turns out it's quite simple.

Build Status

The markup example below shows the build status icon with a link to the TeamCity build status page.

[![Build Status](http://buildserver.com/app/rest/builds/buildType:id:BUILD_CONFIG_ID/statusIcon)](http://buildserver.com/viewType.html?buildTypeId=BUILD_CONFIG_ID&guest=1)

Of course, you'll have to replace buildserver.com with your own build server, and BUILD_CONFIG_ID with the build configuration ID of your project. You can find the ID under "General Settings" for your TeamCity project.

You also need to enable guest user login in the TeamCity administration page for the build status page to be available to everyone.

System.IO.BinaryWriter and C++

A while back I had to read string data written by C#'s BinaryWriter in C++. I was initially a little bit confused about how the data was written but after using Reflector it turned out that the write method prefixes the string with a 7-bit encoded integer.

The encoded integer indicates the length of the written string, and is more commonly known as UTF-7 encoding.

The short example below shows how the string length can be read from a standard input stream.

int32_t BinaryReader::read7BitEncodedInteger(ifstream* stream)
{
    char current;
    int32_t index = 0, result = 0;
    do
    {
        stream->read((char*)&current, sizeof(char));
        result |= (current & 127) << index;
        index += 7;
    }
    while((current & 128) != 0);
    return result;
}

Culture agnostic string comparisons

Something I've seen a lot at different clients is naive string comparison. The most common case is to do something involving String.ToLower() on both strings that are being compared and then an equality comparison of the result.

This is not always the correct way of doing it, and especially not if you at some point in time want people who's using different culture settings than you to run your application. You can read more about this problem in this excellent blog post by Jeff Atwood.

What you should do is simply to use the overloads of the String.Equals method that takes a StringComparison, or use a StringComparer directly.

[Fact]
public void Test_String_Equality()
{
   // Given
   string first = "HELLO WORLD";
   string second = "hello world";

   // When
   bool result = first.Equals(second, StringComparison.OrdinalIgnoreCase);

   // Then
   Assert.True(result);
}

The StringComparer is also very valuable to use with collections when you need to do a lookup for a specific string (i.e. HashSet<string> or IDictionary<String, T>).

[Fact]
public void Test_String_Lookup()
{
   // Given
   var dictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
   dictionary.Add("HELLO WORLD", 42);

   // When
   var result = dictionary.ContainsKey("hello world");

   // Then
   Assert.True(result);
}

How to dynamically add attributes to a class with Castle.Core

Yesterday I was working on some unit tests that ensured that some user derived classes passed to a method were decorated with a specific attribute.

Since the mocking framework I used (Moq) did not support dynamically adding of attributes to a mocked type, my first approach was simply to create some private test classes that contained different permutations of the specified attribute, like this:

[TheAttribute(5)]
public class FakeClassWithAnAttributeSetToANumberLessThanTen
{
}

[TheAttribute(15)]
public class FakeClassWithAnAttributeSetToANumberGreaterThanTen
{
}

[TheAttribute(10, DefaultStuffz=Stuffz.Random)]
public class FakeClassWithAnAttributeSetWithDefaultStuffz
{
}

Alas, as the amount of tests grew, I realized that this wasn't a viable solution.

Finally I found a quite nice solution by using Castle.DynamicProxy available in the Castle project. I have personally never used the Castle framework before, but the experience using it was quite nice.

public class MyClass
{
}

[AttributeUsage(AttributeTargets.Class)]
public class MyClassAttribute : Attribute
{
}

To create an instance of MyClass, dynamically decorated with a MyClassAttribute you simply do as below.

public static MyClass CreateClassDecoratedWithAttribute()
{
    // Get the attribute constructor.
    Type[] ctorTypes = Type.EmptyTypes;
    var ctor = typeof(MyClassAttribute).GetConstructor(ctorTypes);
    Debug.Assert(ctor != null, "Could not get constructor for attribute.");

    // Create an attribute builder.
    object[] attributeArguments = new object[] { };
    var builder = new System.Reflection.Emit.CustomAttributeBuilder(ctor, attributeArguments);

    // Create the proxy generation options.
    // This is how we tell Castle.DynamicProxy how to create the attribute.
    var proxyOptions = new Castle.DynamicProxy.ProxyGenerationOptions();
    proxyOptions.AdditionalAttributes.Add(builder);

    // Create the proxy generator.
    var proxyGenerator = new Castle.DynamicProxy.ProxyGenerator();

    // Create the class proxy.
    var classArguments = new object[] { };
    return (MyClass)proxyGenerator.CreateClassProxy(typeof(MyClass), proxyOptions, classArguments);
}

And now you can test that this actually works.

[Fact]
public void Created_Class_Should_Be_Decorated_With_Attribute()
{
    // Given
    var myClass = CreateClassDecoratedWithAttribute();

    // When
    var attribute = myClass.GetType().GetCustomAttributes(typeof(MyClassAttribute), true)[0];

    // Then
    Assert.NotNull(attribute);
    Assert.IsType<MyClassAttribute>(attribute);
}