class UnicodeFontMutator extends Mutator;

function PostBeginPlay()
{
	local GameStats stats;
	stats = Spawn(class'UnicodeFontMutator.UFGameStats');
	Level.Game.GameStats=stats;
    //Level.Game.GameStatsClass="UnicodeFontMutator.UFGameStats"; //do NOT uncomment

    Super.PostBeginPlay();
}

static function int LookupTable(int raw)
{
	local int i;
	local int CP1252Table[0x20];

	i = 0;
	CP1252Table[i++] = 0x20AC;
	CP1252Table[i++] = 0;
	CP1252Table[i++] = 0x201A;
	CP1252Table[i++] = 0x192;
	CP1252Table[i++] = 0x201E;
	CP1252Table[i++] = 0x2026;
	CP1252Table[i++] = 0x2020;
	CP1252Table[i++] = 0x2021;
	CP1252Table[i++] = 0x2C6;
	CP1252Table[i++] = 0x2030;
	CP1252Table[i++] = 0x160;
	CP1252Table[i++] = 0x2039;
	CP1252Table[i++] = 0x152;
	CP1252Table[i++] = 0;
	CP1252Table[i++] = 0x17D;
	CP1252Table[i++] = 0;
	//
	CP1252Table[i++] = 0;
	CP1252Table[i++] = 0x2018;
	CP1252Table[i++] = 0x2019;
	CP1252Table[i++] = 0x201C;
	CP1252Table[i++] = 0x201D;
	CP1252Table[i++] = 0x2022;
	CP1252Table[i++] = 0x2013;
	CP1252Table[i++] = 0x2014;
	CP1252Table[i++] = 0x2DC;
	CP1252Table[i++] = 0x2122;
	CP1252Table[i++] = 0x161;
	CP1252Table[i++] = 0x203A;
	CP1252Table[i++] = 0x153;
	CP1252Table[i++] = 0;
	CP1252Table[i++] = 0x17E;
	CP1252Table[i++] = 0x178;

	if (raw == 0)
	{
		return 0;
	}
	for(i = 0; i < 0x20; i++)
	{
		if (CP1252Table[i] == raw)
		{
			return 0x80+i;
		}
	}
	return raw;
}

static function string Junk2Unicode(string a)
{
	local int i;
	local int c;
	local int d;
	local int e;
	local string u;

	u = "";
	for(i = 0; i < Len(a); i++)
	{
		c = LookupTable(Asc(Mid(a, i, 1)));
		if(c < 0x80) //not junk
		{
			u $= Chr(c);
			continue;
		}

		if((c >> 5 == 6)&&(Len(a) > i+1)) //2-byte char?
		{
			d = LookupTable(Asc(Mid(a, ++i, 1)));
			if(d >> 6 == 2)
			{
				u $= Chr( ((c & 0x1F)<<6)|(d ^ 0x80) ); //insane hack
			}
			else
			{
				u $= Chr(c); //map CP1252 char
				i--;
			}
			continue;
		}

		if((c >> 4 == 14)&&(Len(a) > i+2)) //3-byte char?
		{
			d = LookupTable(Asc(Mid(a, ++i, 1)));
			e = LookupTable(Asc(Mid(a, ++i, 1)));
			if((d >> 6 == 2)&&(e >> 6 == 2))
			{
				u $= Chr( ((c & 0x0F)<<12)|((d ^ 0x80) << 6)|(e ^ 0x80) );
			}
			else
			{
				Chr(c);
				i -= 2;
			}
			continue;
		}
		//good enough for the demo lol. give up.
		u $= Chr(c);

	}
	return u;
}

defaultproperties
{
     bAddToServerPackages=True
     GroupName="KFUnicodeFontMutator"
     FriendlyName="Unicode Support - Player names fix"
     Description="Fix player names so they display properly in Unicode."
}
