I have moved!

I've moved my blog
CLICK HERE

Tuesday 7 October 2008

Unpacking the .NET Framework Source Code

Microsoft "released" the source of the .NET framework a while ago. The details are here:

http://referencesource.microsoft.com/

But of course, it's not that simple. You're not supposed to be able to read the source willy-nilly; you're only supposed to be able to see it when you're debugging a program. So there's some instructions to set it up with Visual Studio. I got this to work once, but since I applied Service Pack 1 it hasn't worked - I just get blank files.

Then there's this curious page:

http://referencesource.microsoft.com/netframework.aspx

As of today, it just has three Microsoft Installer packages that you can download, which have cryptic names. No indication of the significance of each package, or whether you need all of them. It all stinks of Microsoft grudgingly complying with the outcome of a legal dispute with the European Union, doesn't it?

And if you download them and run them, it puts some intriguingly large files on your disk. They are all called source.zip.tmp. They are definitely not zip files.

But they do contain the source. And it turns out to be very easy to unpack them! Just use a program like this:

using System;
using System.Diagnostics;
using System.IO;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Please specify the input file and the output directory");
                return;
            }

            StreamReader reader = new StreamReader(args[0]);
            StreamWriter writer = null;

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] parts = line.Split(',');

                if (parts.Length == 4)
                {
                    int number;
                    if (int.TryParse(parts[0], out number))
                    {
                        if (parts[1].StartsWith("/"))
                        {
                            string path = Path.Combine(args[1], parts[1].Substring(1).Replace('/', '\\'));

                            // Hack-around for paths that are too long for the file system...
                            path = path.Replace("DEVDIV\\depot\\DevDiv\\releases\\", "");
                            
                            string dir = Path.GetDirectoryName(path);
                            if (!Directory.Exists(dir))
                                Directory.CreateDirectory(dir);

                            if (writer != null)
                                writer.Dispose();

                            writer = new StreamWriter(path);
                            continue;
                        }
                    }
                }

                Debug.Assert(writer != null);
                writer.WriteLine(line);
            }

            if (writer != null)
                writer.Dispose();
        }
    }
}

So I can only assume that Microsoft intended people to write the above program, or something similar. Judging from the EULA, unpacking the source, just so that you can search it and read it more conveniently, is perfectly okay.

It's certainly the most obscure and convoluted installation procedure I've ever encountered. Wouldn't it have been simpler to just put some zip files up on the site in the first place?