Wednesday, March 18, 2009

MIX09

Go to the http://videos.visitmix.com/MIX09/ website and check out the rest of this year's presentations.

Saturday, March 14, 2009

Rotten apples

I bought my first Apple product (a MacBook) late 2007, I was a proud owner back then, and it was a absolutely wonderful design.

For roughly 6 months, I learned new things about Mac OS X and it was good. I was motivated to learn more about UNIX, and dug into Mac OS X development with Xcode. I had never intended that the MacBook would replace my desktop use enterily, but through a number of unforeseen events it has. However, it wasn't possible for me to entirely leave the Win32 platform behind, I'm a Win32 guy at heart, and I managed to bring that to Mac OS X with the help of VMware Fusion.

As of this date, I own, a late 20007 MacBook, a Apple Mighty Mouse and a 4G iPod nano. I've been using Apple products and Mac OS X for about 18 months now...

From this point on, it's all down hill... and I believe this Mac vs. PC debate is mostly silly, and in large, fueled by people with no knowledge of how a computer actually works. Some people will argue that they don't care about that, and they shouldn't be caring about it, because it is not in their job description to care about it and I respect that, but don't go make false accusations about things you don't understand.

The reason why I'm abandoning ship and going back to Windows, is because of Microsoft, they fix problems. I'll admit that Windows back in the day, was a troubled experience, but so was all pre-Mac OS X releases. Microsoft today, is very keen on listening and helping out, and since hardware, that runs Windows, ain't proprietary Apple branded and redicoulously expensive. Any third-party with the knowledge and experience can fix driver issues. I've had issues with OpenGL support on my late 2007 MacBook, and while there were driver updates for Windows, Apple left MacBook users like me, in the dark to rot. And that's another reason why I'm going back, Windows is an open platform, not open source or free as in freedom but accessible, by any hardware. And these graphics issues happen to have been solved by Intel, but there is no Mac OS X download, just a Windows version...

Mac OS X didn't really shine until they went POSIX and replaced their kernel with a UNIX variant. But what's distrubing about that, is that Linus Torvalds (in this Tech Talk) calls on Mac OS X to be an even worse platform for software development than Windows, and Linus knows a thing or two about UNIX. It's like Ballmer said, we're paying $500 more, for a brand! And while that's a feat in itself, I doubt Apple can keep this up for much longer. While the MacBook and Apple logo is everywhere, surprisingly few people run Mac OS X.

Right now, I'm in the process of installing Windows on my MacBook, but while downloading some required bits to format my iPod, I'm getting a disk full error. I bring up Finder, it says I have 166 GiB of disk space available... This is just one of the many problems I've experienced with Mac OS X, and it's about to go away.

HOWTO: Install Windows Vista/Server 2008 on a late 2007 MacBook3,1

First off, this is about liberating the MacBook, you'll only need some of the Boot Camp drivers and the firmware update which emulates a BIOS. Once you have this you're done with Mac OS X. You'll only need the Mac OS X installation DVD once. Derek Hatchard blogs about the specifics. You just use it to partion your hard drive with a MBR. To my knowledge no MacBook supports booting of an USB stick so you'll have to have a Windows installation DVD. The rest is simple, you can boot the Windows installation of the DVD as you would normally. My disc drive gave me a hard time but being presistant helped, after poping in and out many times it eventually completed the installation, be presistent!

I had major issues with the suttering audio and had to install a very specific driver to get around this. After manually and forcefully installing the Broadcom v5.10.38.26, "Broadcom 4322AG 802.11a/b/g/draft-n Wi-Fi Adapter" things began to work fine again. Unfortenetly, I could not find a reliable way to determine which hardware matches what driver, it looked like most MacBook users went with the Broadcom 4322AG driver so you could start with that, it worked for me.

Saturday, December 27, 2008

Shearing my life

I could seriously do for a indefinite timeout. With all the things in the world that needs to get done, 24 hour days ain't nowhere near enough.

I been wanting to make a website based around some 3D element. I got curious and looked at websites that did similar things, trying to figure out the simplest way of 3D-2D mapping. Skewing or shear mapping (which is another fancy name for a bizarre transform) actually preforms the kind of transform I've been wanting to do and it isn't really that difficult.

I began figuring out the geometric interpretation. I'm very much so a visual person in that I have to picture things before I can argue about their function. The above picture just came to by applying a really trivial skew transform over a period of time.

...but I need more time, all the things to do, all the things to see. I have to write up my Master's thesis, I got two jobs on the side which has to get done this week and I should really look for a third more permanent job. My time at DICE has been most wonderful, its truly a great place to be working at but I'm not certain I'll be able to continue once my internship is up. EA announced a 1000 people layoff 'til the end of March. My hope is that they let HR hire people along the side...

And apparently some stupid company got a 10-year old patent approved today. The consequence is that if you generate icons based on the contents of a file, you will be slapped with a lawsuit for patent infringement. Microsoft, Apple and apparently Google are on trail. What the hell is up with the USPTO!?

Sunday, August 17, 2008

Piracy, causality and PC gaming

Piracy is the result of an elite group of reverse engineers that mostly got bored with everything else and took upon themselves to face a greater challenge. Software (and games in particular) is not 'cracked' because it's a good for you. It's the way some talented people choose to deal with boredom. And in a sense combating piracy in the realm of software engineering through legal actions is like a Sisyphean challenge. There will always exist bored, beautifully talented people that will be able to circumvent the inner workings of whatever DRM software you imagine.

If you want to pickup and play a game, the time it takes to get from that feeling in your brain to actually playing, will probably end up determining if you're going to play at all. There's this notion of availability which I do believe that consoles have in advantage but it's not exclusive to consoles. Getting from the point at which you decide to play a game up to the moment where you've bought and installed the game is an important deciding factor when combating piracy. Which ties together accessibility and distribution, that is, the very core of information technology.

The following quote was taken from the Hanselminutes #89 podcast (the context is console security).

"You know, one of the things people don't understand is some of us have like scanning electron microscopes in our living room."

He puts up a slide that has a picture of his living room and darn if there isn't a scanning electron microscope next to his couch and he uses that. He sands off the epoxy on the top of the chips of the things he's trying to hack and he uses this electron microscope to read the internal circuitry to find out what it does.

Outsmarting an army of people with an endless amount of resources, is just silly i.e. ByteShield which have written a very interesting and probably plausible way of truly protecting software, is flawed in that it's a system that people will take great pride in circumventing.

But what about availability? I'm congratulating Valve for their premium free of charge service Steam. Because it's everything the industry needs to be. Almost $2 billion in sales was collected from Steam-like services in 2007 (that roughly 21% of the grand total according to the ESA).

Download services that won't cut it, will basically sell you a single download opportunity and once that window closes your purchased content is forfeit. One of the reasons I enjoy Steam so much is that I can bring my games with me, anywhere I go. I just install their bootstrapping software Steam and let the software prepare the games I wish to play. It's as simple as that.

I'm saying that it all comes down to this very moment, were the sheer number of clicks you'll have to make to get the game to your hard drive is a deciding factor whether you're going to buy that game or not. People that understand piracy will be able to grab a seemingly bug free copy of the game for no money at all and start playing without having to invest any time into the process of acquire the game itself. This is where you (as a business exec) will have to be smart and competitive.

Now, Steam is free of charge, no subscription required, it's not pay-per-view and that's important, because you cannot compete with people that crack software just for the kicks of it. Not by taking money for something they'll provide for free. This is all about value and aggressive pricing. Even though these talented reverse engineers are great at what they do, they make mistakes and the quality can vary a lot. Many cracked games bring about new bugs, bugs that otherwise would not occur and any non-engineer would presume the game is faulty and might end up NOT buying the game based on false grounds. But with cleaver marketing, you'll pay that extra money to get that full experience.

Peter Moore is a man I've come to respect. He said this in a response to some rather harsh legal actions taken by Atari, Codemasters, (Topware, Reality Pump and Techland).

[Suing consumers] didn't work for the music industry. I'm not a huge fan of trying to punish your consumer. Albeit these people have clearly stolen intellectual property, I think there are better ways of resolving this within our power as developers and publishers.
Yes, we've got to find solutions. We absolutely should crack down on piracy. People put a lot of blood, sweat and tears into their content and deserve to get paid for it. It's absolutely wrong, it is stealing.
But at the same time I think there are better solutions than chasing people for money. I'm not sure what they are, other than to build game experiences that make it more difficult for there to be any value in pirating games.
If we learned anything from the music business, they just don't win any friends by suing their consumers. Speaking personally, I think our industry does not want to fall foul of what happened with music.

Now, if you want people to buy your software, you'll have to find value in the purchasing of your product as a full service package, not in the sequence of bytes alone that you'll end up shipping. For this, there's no single answer, instead there are numerous of carefully planned steps that need to take place.

Make a great game and if you think shipping NOW with what you got is the only way out, you're already fighting a losing battle. At this point you're better off saving what you can and starting over. It's hard yes but it is the right thing.

Michal Kicinski, co-founder of CD Projekt. Managed to sell 1 million units of The Witcher by sticking to a specific audience. You can read the rest here. What will you do to make sure your game sell?

Tuesday, July 1, 2008

Fast binary visualization (to hex conversions) and back

This spin-off started with the immutable System.Data.Linq.Binary class. It's somewhat irritating because it does not expose any read-only capabilities. As such one must either rely on the ToString method which surrounds the base64 encoded value in quotes or the ToArray method which uses defensive copying.

At this point, I wonder why they never defined a public getter, like with the string class (string s; s[0]). Since what I need is to be able to serialize this object. Object serialization comes in many flavors, I've chosen one which utilizes reflection to emit a parser collection of strongly typed serializes. It's way faster than what you get with any of the built-in stuff but I've purposely chosen to ignore the System.Data.Linq.Binary because I didn't use it, up until now.

SQL to LINQ maps timestamps to the Binary data type and you need timestamps to apply optimistic concurrency. So I have to able to treat them as well. And this is where this hack'ish hex stuff comes in.

In the end, timestamps are 8-bytes in size. So I accept the defensive copying overhead that takes place. I rely on the ToArray method and .ctor(byte[]) for getting and setting the internal representation.

When you look at hex, the 4 high and low bits of a byte is usually referred to as a nibble. A nibble map to 1 hexadecimal character, usually in the range of [0-9ABCDEF] but why? There really no relation between the binary value or the ASCII representation.

I gave it some thought, looked at the bit patterns and this is what I came up with.

Each nibble represent a 4-bit integer in the range of 0-15. As such if I were to use a lookup table I could map this space to any 16 characters, but I chose to not do that. Instead I searched the byte range for a bit pattern which would not involve any lookup map and still remain a presumably safe ASCII representation.

Funny enough the commercial at '@' character happens to not use the low 4-bits of a byte and the following 16 characters constitutes "ABCDEFGIHJKLMNOP", the beginning of the alphabet. What's even more coincidental is that the caret notation, is exactly this (Ctrl+C is really ASCII character at zero-based index 3).

But enough talk, let's look at the code:

public static string Serialize( this System.Data.Linq.Binary binary )
{
    StringBuilder sb = new StringBuilder();
    byte[] bytes = binary.ToArray();
    for ( int i = 0 ; i < bytes.Length ; i++ )
    {
        // Unpack
        sb.Append( (char)('@' | (bytes[i] >> 4)) ); // '@' 0x40 | (i & 0xf)
        sb.Append( (char)('@' | (bytes[i] & 0xf)) );
    }
    return sb.ToString();
}

public static System.Data.Linq.Binary ToBinary( string s )
{
    byte[] bytes = new byte[s.Length >> 1];
    for ( int i = 0 ; i < bytes.Length ; i++ )
    {
        // Pack
        bytes[i] = (byte)((s[i << 1] << 4) | (s[(i << 1) + 1] & 0xf)); 
    }
    return new System.Data.Linq.Binary( bytes );
}

The above code in C# can be used as is. But those of you that think bit manipulating is a bit daunting, I recommend you to take a while to understand the code (make sure you fully understand the impact of explicit and implicit type casts in conjunction with bit arithmetic).

Each nibble does not occupy the 4 high bits of each byte and since we want to be able to represent them as strings, the bitwise | with '@' (0x40) will give us a very nice ASCII representation. One which is also very efficient to parse, as each nibble is stored unmodified together with the '@' character. We simply bitwise & with mask 0xf to get just the low part, and then bit shift everything else into place. Another nice property of this method is that the ordering of the binary value does not change with the textual representation.

This bi-directional relationship actually wants me to stop using the old fashion hexadecimal representation altogether and start using this caret notation stuff. I reckon it's just wrapping your head around another numerical representation.

There's 10 kinds of people, right?
Those who speak binary and those who don't.

Update July 25, '08: I've observed that the commercial at character '@' is sometimes encoded, despite the fact that any documents I could find on the subject says otherwise. So if you want a pure ASCII and URL-safe binary representation add 1 just before the serialization and subtract 1 just after the de-serialization. It will do the trick.

Saturday, May 31, 2008

Type-casting as bugs in C#

I've been noticing more and more that people use the 'as' keyword in C# to presumably do safe type casts. But enough is enough, it's not ideally the way to actually go about type conversions, nor does it have any benefits.

To start things of, the expression 'instance1 as SomeType' keyword is just the equivalent of '(instance1 is SomeType ? (SomeType)instance1 : null)' and the following code will throw an exception anyway, iff the type is not instance of SomeType... So why do you type cast this way?

[TestMethod]
public void Index()
{
    // Setup
    HomeController controller = new HomeController();

    // Execute
    ViewResult result = controller.Index() as ViewResult;

    // Verify
    ViewDataDictionary viewData = 
        result.ViewData as ViewDataDictionary;

    Assert.AreEqual( "Home Page", viewData["Title"] );
}

The above example is from the ASP.NET MVC Framework, and while it's just a sample from the testing framework this code is bad. Because when the right-hand side 'controller.Index() as ViewResult' returns null (the return type was not of type ViewResult) and this code should, because this expression implies that type does not have to be ViewResult. There is no error handling code and the result is the NullReferenceException, which at this point is a side effect of some other code that failed.

Do you know the null coalescing operator? It's about the most useless thing in the language, or is it?

(controller.Index() as ViewResult) ?? new ViewResult()

Now, if you really want the program to crash in the event of such a failure you should NOT do this, nor should you yield null in the case of a type test failure. But if you want to actually type cast and in the case of a problem fall-back to some instance, you can use the above snippet.

Think about it. The example is really stupid because it fails to capture that an error could (or could not occur) as well as that if it would, the wrong error would be reported. But this snippet, it can actually make sense of a failed type test and rely on any other code to provide an instance of some type that doesn't have to stop execution of your program.