« Cold off the presses! | Main | More Q&A from the comments »

Swizzled

The past week, I've been working on a patch for Civ 3 to fix bugs that were reported on the released version. The patch is in testing now and will probably be ready in a few weeks, depending on how the QA process goes.

I was able to fix pretty much all of the reported crashing bugs as well as compatibility issues with play-by-e-mail (PBEM) games and some third-party mods (like Rye's of Civilization). I had to break network compatibility with the current Mac version as a side effect of that. The PBEM games can occasionally contain embedded network data chunks, so Mac users participating in a game that has PC players would typically crash or act oddly when this data was encountered.

Byte-swapping the network data was something I was going to have to do eventually for the Universal Binary builds, so this gave me the perfect excuse to get that done. It ended up being more of a chore than I would have liked though. Byte-swapping the data in Civ3 was a relative breeze compared to sorting out the byte-swapping issues in our DirectPlay8 peer-to-peer code. As it turns out, our DP8 code had to stay in big-endian format or older Mac versions would crash when an updated game advertised itself on the network. This led me down a headache-inducing path whereby I had to swizzle the bytes for our DirectPlay8 stuff so that it stayed in big-endian, and swizzle the bytes in the Civ3 data payloads so that it stayed little-endian. I never thought I'd have to write code to byte-swap Unicode character streams, but now I have.

In the end, I can say unequivocally that I don't care for network code. :-)

Comments

Byte-swapping Unicode character streams shouldn't be that hard -- they're just 16-bit ints, so it's the same as swapping an array of shorts. I've had to do this lots and lots of times, unfortunately.

"Byte-swapping Unicode character streams shouldn't be that hard -- they're just 16-bit ints, so it's the same as swapping an array of shorts."

I may be using the wrong terminology so let's pretend I said Unicode character strings instead. :) But you're right - in this case it was a matter of byte-swapping an array of 32-bit values. (We gave up on 16-bit wchar values years ago, and luckily for us, this hasn't been a problem for most of our game ports.)

The main oddity for me is that, over the course of several years, I've byte-swapped every bizarre type of data under the sun but never Unicode values. I was starting to fantasize that it'd never happen. I guess this is what happens when you think about these things too much.

Post a comment