display-name: "Games/Secret Agent"
meta: {"Working": "All tiles, images, sprites and audio", "Thanks to": "Frenkel Smeijers"}

bit-stride: 8

if (file.name like "*1.gfx")
{
	folder: "Background Tiles"

	loop(16)
	{
		xor-start: 0
		encrypted [8064] "SecretAgentXOR"
		{
			unsigned8 tile-count
			unsigned8 width-in-bytes
			unsigned8 height

			image
			{
				format: "A1I4"
				width: width-in-bytes * 8
				tiles-across: 4
			}
		}
	}
}

if (file.name like "*2.gfx")
{
	loop(3)
	{
		xor-start: 0
		encrypted [2048] "SecretAgentXOR"
		{
			unsigned8 tile-count
			unsigned8 width-in-bytes
			unsigned8 height

			image
			{
				format: "A1I4"
				width: width-in-bytes * 8
				tiles-across: 26
			}
		}
	}
}


if (file.name like "*3.gfx")
{
	folder: "Levels"

	file[file.size / 2016] : i
	{
		offset: i * 2016
		size: 2016
		name: "Level " + i

		interpret-as "sa-level"
	}
}

file-format sa-level
{
	loop while(file.remaining-bytes > 0)
	{
		xor-start: 0
		encrypted [42] "SecretAgentXORLevel"
		{
			text
		}			
	}
}

if (file.name like ["*.crd", "*.ttl", "*.end", "*.apo"])
{
	folder: "Fullscreen Images"
	interpret-as "image-PCX"
}

if (file.name like "*.snd")
{
	folder: "Sound"
	file[12]: i
	{
		folder: ""
		name: "Sound " + i
		offset: i * 610
		size: 610

		interpret-as "sa-sound"
	}
}


file-format sa-sound
{
	xor-start: offset
	encrypted[610] "SecretAgentXOR"
	{
		at 600
		{
			unsigned16 priority
			unsigned16 unknown0
			unsigned16 vibrate
			unsigned32 unknown1
		}

		compressed[600] "CC-sound"
		{
			sound
			{
				channels: 1
				samples-per-second: 44100
				bits-per-sample: 16
			}
		}
	}
}

function SecretAgentXOR
{
	XORData: [0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x31, 0x39, 0x39, 0x31,
        	  0x20, 0x50, 0x65, 0x64, 0x65, 0x72, 0x20, 0x4A, 0x75, 0x6E, 0x67, 0x63, 0x6B, 0x00]

	counter: xor-start % 28

	loop while(file.remaining-bytes > 0)
	{
		read unsigned8 encryptedByte

		//reverse the byte
		encryptedByte = ((encryptedByte & 0x0F) << 4) | ((encryptedByte & 0xF0) >> 4)
		encryptedByte = ((encryptedByte & 0x33) << 2) | ((encryptedByte & 0xCC) >> 2)
		encryptedByte = ((encryptedByte & 0x55) << 1) | ((encryptedByte & 0xAA) >> 1)
		encryptedByte = encryptedByte ^ XORData[counter % 28]

        	write unsigned8 encryptedByte

		counter = counter + 1
	}
}

function SecretAgentXORLevel
{
	XORData: [0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x31, 0x39, 0x39, 0x31,
        	  0x20, 0x50, 0x65, 0x64, 0x65, 0x72, 0x20, 0x4A, 0x75, 0x6E, 0x67, 0x63, 0x6B, 0x00,
        	  0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x31, 0x39, 0x39, 0x00]

	counter: xor-start % 42

	loop while(file.remaining-bytes > 0)
	{
		read unsigned8 encryptedByte

		//reverse the byte
		encryptedByte = ((encryptedByte & 0x0F) << 4) | ((encryptedByte & 0xF0) >> 4)
		encryptedByte = ((encryptedByte & 0x33) << 2) | ((encryptedByte & 0xCC) >> 2)
		encryptedByte = ((encryptedByte & 0x55) << 1) | ((encryptedByte & 0xAA) >> 1)
		encryptedByte = encryptedByte ^ XORData[counter % 42]

        	write unsigned8 encryptedByte

		counter = counter + 1
	}
}