display-name: "Games/Shadow Knights"
meta: {"Working": ["All graphics", "Levels", "Audio"], "Thanks to": "Andy Durdin"}

if (file.name == "egatiles.nin")
{
	image
	{
		format: "I4"
		width: 16
		height: 16
		data-size: file.size
		tiles-across: 13
		bit-stride: width * height
	}
}

if (file.name == "shadow.exe")
{
	file[1]
	{
		name: "sounds"
		offset: 0x1046A
		size: 0x11B8
		interpret-as "archive-SND_DOS"
	}
}


level-object-images: {
	0x0001: "s_thug.nin\\0",
	0x0002: "s_archer.nin\\0",
	0x0003: "s_bats.nin\\8",
	0x0004: "s_dogs.nin\\0",
	0x0005: "s_skel.nin\\0",
	0x0006: "s_dragon.nin\\0",
	0x0007: "s_demon.nin\\0",
	0x0008: "s_ninja.nin\\72",
	0x0009: "s_skel.nin\\0",
	0x000A: "s_thug.nin\\0",
	0x000B: "s_ninja.nin\\84",
	0x000C: "s_ninja.nin\\76",
	0x000D: "s_ninja.nin\\80",
	0x000E: "s_bats.nin\\0",
	0x00FF: "s_ninja.nin\\12",
	"*": "debug"
}

if (file.name like "level??.nin")
{
	unsigned32 decompressed-size

	magic-number: 0xFEFE

	level 
	{
		grid-x: 16
		grid-y: 16

		unsigned16 width
		unsigned16 height
		unsigned16 level-plane-count //always 2
		unsigned32 zero //always 0
		unsigned32 ted-variables
		unsigned16 level-plane-size
		data(16) unknown
	
		compressed[file.remaining-bytes] "KeenRLE" 
		{
			level-layer
			{
				data-type: "unsigned16"
				layer-image: "egatiles.nin"
				order: 0
			}

			if (!(file.name like "level80.nin"))
			{
				at level-plane-size:

				level-layer
				{
					layer-name: "Objects"
					ignore-tile: 0
					data-type: "unsigned16"
					//layer-images-prefix: level-image-table[file.name] + "\\"
					layer-images: level-object-images
					//debug: true
					//layer-image: "debug"
					order: 1
				}
			}
		}
	}
}

if (file.name like "endpic.nin")
{
	unsigned32 decompressed-size-plus-2
	compressed[file.size - 4] "SWRLE"
	{
		unsigned16 width-div-8
		unsigned16 height
		image
		{
			format: "I4"
			width: width-div-8 * 8
			bit-stride: width * height
		}
	}
}

if (file.name like "egaplane.nin")
{
	plane-size: file.size / 4
	image
	{
		format: "I4"
		width: 8
		height: 8
		bit-stride: width * height
		plane-offset: [0, plane-size * 1, plane-size * 2, plane-size * 3]
		data-size: file.size
		per-image-offset: 8
		tiles-across: 5
	}
}

sprite-data: {
	"s_ninja.nin": {"count": 86, "offset": 0 },
	"s_thug.nin": {"count": 16, "offset": 86},
	"s_archer.nin": {"count": 12, "offset": 102},
	"s_bats.nin": {"count": 16, "offset": 114},
	"s_dogs.nin": {"count": 8, "offset": 130},
	"s_skel.nin": {"count": 12, "offset": 138},
	"s_demon.nin": {"count": 12, "offset": 150},
	"s_dragon.nin": {"count": 18, "offset": 162}
}

file-format shadow-sprite-internal
{
	original-name: file.name

	header-offset: (sprite-data[file.name].offset * 128) + 64

	file [sprite-data[file.name].count]: i
	{
		offset: 0
		size: file.size
		name: "" + i
		sprite-index: i
		interpret-as "shadow-sprite-image-internal"	
	}
}

file-format shadow-sprite-image-internal
{
	unsigned32 decompressed-size
	compressed[file.size - 4] "SWRLE"
	{
		unsigned16 plane-size
	
		header-offset: (sprite-data[original-name].offset * 128) + 64

		from "egahead.nin" at (header-offset + (sprite-index * 128)) 
		{
			unsigned16 _width
			width: _width * 8
			unsigned16 height
			unsigned16 _offset_helper
			unsigned16 offset	
			unsigned32 hitbox-ul
			unsigned32 hitbox-br
			fixed-string(12) name		
		}

		at (offset * 16) + 2 + _offset_helper:

		image
		{
			format: "I4A1"
			bit-stride: width * height
			alpha-mask: 1
			plane-offset: [0, plane-size * 1, plane-size * 2, plane-size * 3, plane-size * 4]
		}
	}
}

if (file.name like "s_*.nin")
{	
	folder: "Sprites"
	interpret-as-internally "shadow-sprite-internal"

	unsigned32 decompressed-size
	compressed[file.size - 4] "SWRLE"
	{
		unsigned16 plane-size
	
		header-offset: (sprite-data[file.name].offset * 128) + 64

		loop (sprite-data[file.name].count): i
		{
			from "egahead.nin" at (header-offset + (i * 128)) 
			{
				unsigned16 _width
				width: _width * 8
				unsigned16 height
				unsigned16 _offset_helper
				unsigned16 offset	
				unsigned32 hitbox-ul
				unsigned32 hitbox-br
				fixed-string(12) name		
			}

			at (offset * 16) + 2 + _offset_helper:

			image
			{
				format: "I4A1"
				bit-stride: width * height
				alpha-mask: 1
				plane-offset: [0, plane-size * 1, plane-size * 2, plane-size * 3, plane-size * 4]
			}
		}
	}
}

function SWRLE
{	
	loop while (file.remaining-bytes > 0)
	{		
		read unsigned8 token

		if (token == 0xFE)
		{
			read unsigned8 count
			read unsigned8 value

			loop (count)
			{
				write unsigned8 value
			}			
		}
		else
		{
			write unsigned8 token			
		}
	}
}