Generalize Template Haskell nonsense

It's now a more generic KeyedEnum thingy.

Also rework some protocol system stuff.