int build_anim(char *anim_filename, char *scene_name, char *group_name)
{
	int ret;
	FILE *f;
	size_t transfersize;
	char anim_path[256];

	memset(anim_path, 0, sizeof(anim_path));
	snprintf(anim_path, sizeof(anim_path)-1, "%s%canim%c%s", darc_dirpath, PATH_SEPERATOR, PATH_SEPERATOR, anim_filename);

	f = fopen(anim_path, "wb");
	if(f==NULL)
	{
		printf("Failed to open the following filepath for writing: %s\n", anim_path);
		return 2;
	}

	memset(filebuf, 0, filebuf_size);

	layout_header = (bclyt_header*)filebuf;//.bclan has the exact same header as .bclyt besides the magicnum value, so just reuse the same structure.

	putle32(layout_header->magicnum, 0x4e414c43);
	putle16(layout_header->bom, 0xfeff);
	putle16(layout_header->headerlen, 0x14);
	putle32(layout_header->revision, 0x2020000);

	layout_pos = sizeof(bclyt_header);

	ret = build_animsections(scene_name, group_name);
	if(ret)
	{
		fclose(f);
		return ret;
	}

	putle32(layout_header->filesize, layout_pos);

	transfersize = fwrite(filebuf, 1, layout_pos, f);
	fclose(f);
	if(transfersize != layout_pos)
	{
		printf("Failed to write the anim data to the file.\n");
		return 4;
	}

	return 0;
}
int build_layout(char *layout_filename, char *texture_filename, int screenid)
{
	int ret;
	FILE *f;
	size_t transfersize;
	char layout_path[256];

	memset(layout_path, 0, sizeof(layout_path));
	snprintf(layout_path, sizeof(layout_path)-1, "%s%cblyt%c%s", darc_dirpath, PATH_SEPERATOR, PATH_SEPERATOR, layout_filename);

	f = fopen(layout_path, "wb");
	if(f==NULL)
	{
		printf("Failed to open the following filepath for writing: %s\n", layout_path);
		return 2;
	}

	memset(filebuf, 0, filebuf_size);

	layout_header = (bclyt_header*)filebuf;

	putle32(layout_header->magicnum, 0x54594c43);
	putle16(layout_header->bom, 0xfeff);
	putle16(layout_header->headerlen, 0x14);
	putle32(layout_header->revision, 0x2020000);

	layout_pos = sizeof(bclyt_header);

	ret = build_layoutsections(texture_filename, screenid);
	if(ret)
	{
		fclose(f);
		return ret;
	}

	putle32(layout_header->filesize, layout_pos);

	transfersize = fwrite(filebuf, 1, layout_pos, f);
	fclose(f);
	if(transfersize != layout_pos)
	{
		printf("Failed to write the layout data to the file.\n");
		return 4;
	}

	return 0;
}
int anim_buildsection_pai1(char *mat_name, char *rootpane_name)
{
	u32 size;
	u32 pos;
	u8 section_data[0x44];

	memset(section_data, 0, sizeof(section_data));

	pos = 0;

	putle32(&section_data[pos], 15);//Time related.
	pos+=4;

	putle16(&section_data[pos], 0x0);//Unknown
	pos+=2;

	putle16(&section_data[pos], 0x2);//num_entries
	pos+=2;

	putle32(&section_data[pos], 0x14);//offset_entries (from magicnum)
	pos+=4;

	//Each entry is a u32 offset, relative to the above magicnum.

	putle32(&section_data[pos], 0x1c);//Offset to the actual entry data for mat_name, relative to the start of the pat1 magicnum.
	pos+=4;

	putle32(&section_data[pos], 0x28+0x18);//Offset to the actual entry data for the root pane, relative to the start of the pat1 magicnum.
	pos+=4;

	//Start of the entry for mat_name.
	strncpy((char*)&section_data[pos], mat_name, 0x13);
	pos+= 0x14;

	putle32(&section_data[pos], 0x0);//Total animation chunks.
	pos+=4;

	//Start of the entry for rootpane.
	strncpy((char*)&section_data[pos], rootpane_name, 0x13);
	pos+= 0x14;

	putle32(&section_data[pos], 0x0);//Total animation chunks.
	pos+=4;

	return layout_addsection(0x31696170, section_data, 0x44);
}
static ssize_t end_write(int fd, void *d, size_t len)
/* write an array of shorts in little-endian format */
{
    unsigned char buf[BUFSIZ];
    short *data = (short *)d;

    size_t n;
    for (n = 0; n < (size_t)(len/2); n++)
	putle16(buf, n*2, data[n]); 
    return write(fd, (char*)buf, len);
}