/*
*
*    Copyright (C) 2003 Kent Hansen.
*
*    This file is part of Tile Molester.
*
*    Tile Molester is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    Tile Molester is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*/

package tm.filelistener;

/**
*
* Listener for Game Boy Advance (*.gba) files.
*
**/

public class GameBoyAdvanceFileListener extends TMFileListener {

    private static final int[] nintendoLogo = {
        0x24,0xFF,0xAE,0x51,0x69,0x9A,0xA2,0x21,0x3D,0x84,0x82,0x0A,0x84,0xE4,0x09,0xAD,
        0x11,0x24,0x8B,0x98,0xC0,0x81,0x7F,0x21,0xA3,0x52,0xBE,0x19,0x93,0x09,0xCE,0x20,
        0x10,0x46,0x4A,0x4A,0xF8,0x27,0x31,0xEC,0x58,0xC7,0xE8,0x33,0x82,0xE3,0xCE,0xBF,
        0x85,0xF4,0xDF,0x94,0xCE,0x4B,0x09,0xC1,0x94,0x56,0x8A,0xC0,0x13,0x72,0xA7,0xFC,
        0x9F,0x84,0x4D,0x73,0xA3,0xCA,0x9A,0x61,0x58,0x97,0xA3,0x27,0xFC,0x03,0x98,0x76,
        0x23,0x1D,0xC7,0x61,0x03,0x04,0xAE,0x56,0xBF,0x38,0x84,0x00,0x40,0xA7,0x0E,0xFD,
        0xFF,0x52,0xFE,0x03,0x6F,0x95,0x30,0xF1,0x97,0xFB,0xC0,0x85,0x60,0xD6,0x80,0x25,
        0xA9,0x63,0xBE,0x03,0x01,0x4E,0x38,0xE2,0xF9,0xA2,0x34,0xFF,0xBB,0x3E,0x03,0x44,
        0x78,0x00,0x90,0xCB,0x88,0x11,0x3A,0x94,0x65,0xC0,0x7C,0x63,0x87,0xF0,0x3C,0xAF,
        0xD6,0x25,0xE4,0x8B,0x38,0x0A,0xAC,0x72,0x21,0xD4,0xF8,0x07
    } ;

/**
*
* Detects if this is a Game Boy Advance file.
* The critera are that the extension is gba, that the Nintendo logo
* character data is correct and that the byte at 0xB2 equals 0x96.
*
**/

    public boolean doFormatDetect(final byte[] data, String extension) {
        // verify extension
        if (!extension.equals("gba")) {
            return false;
        }

        // verify Nintendo Logo Character Data
        for (int i=0; i<nintendoLogo.length; i++) {
            if ((data[0x004+i] & 0xFF) != nintendoLogo[i]) {
                return false;
            }
        }

        if ((data[0xB2] & 0xFF) != 0x96) {
            return false;
        }

        return true;
    }

/**
*
* Does nothing on file load.
*
**/

    public void fileLoaded(byte[] data, String extension) {
    }

/**
*
* Recalculates the complement check and checksum on file save.
*
**/

    public void fileSaving(byte[] data, String extension) {
        // update complement check
        int complementCheck = 0xE7;
        for(int i=0xA0; i<=0xBC; i++) {
            complementCheck -= data[i] & 0xFF;
        }
        data[0xBD] = (byte)(complementCheck & 0xFF);

        // update checksum
        int checkSum = 0;
        data[0xBE] = 0;
        data[0xBF] = 0;
        for (int i=0; i<data.length; i++) {
            checkSum += data[i] & 0xFF;
        }
        data[0xBE] = (byte)((checkSum >> 8) & 0xFF);
        data[0xBF] = (byte)(checkSum & 0xFF);
    }

}