Deduplicate code in four places. Comments by: @elexis Differential Revision: https://code.wildfiregames.com/D5261 This was SVN commit r28085.