// Detect It Easy: detection rule file
// Author: LinXP

meta("format", "");

function detect() {
    if (Binary.getSize() >= 9) {

        if (Binary.compare("'XNB'")) {
            bDetected = true;
            sName = "Microsoft XNA Game Studio Binary Package (.XNB)"
            var nXNA_Ver = Binary.read_uint8(0x4);
            var fFlag = Binary.read_uint8(0x5);
            switch (nXNA_Ver) {
                case 3:
                    sVersion = "v3.0";
                    break;
                case 4:
                    sVersion = "v3.1";
                    break;
                case 5:
                    sVersion = "v4.0";
                    break;
                default:
                    sOption("UNKNOWN");
                    break;
            }


            /* XNA Studio platforms: 'w' = Windows, 'm' = Windows Phone 7, 'x' = X360
             * MonoGame extensions: 'i' = iOS, 'a' = Android, 'X' = MacOSX, 'P' = PS4, 'S' = Switch, etc */
            switch (Binary.read_uint8(0x3)) {
                case 0x77:
                    sOption("Windows");
                    break;
                case 0x78:
                    sOption("Xbox 360");
                    break;
                case 0x6D:
                    sOption("Windows Phone 7");
                    break;
                case 0x69:
                    sOption("iOS");
                    break;
                case 0x61:
                    sOption("Android");
                    break;
                case 0x58:
                    sOption("MacOSX");
                    break;
                case 0x50:
                    sOption("PS4");
                    break;
                case 0x53:
                    sOption("Switch");
                    break;
                default:
                    sOption("UNKNOWN");
                    break;
            }

            if (Binary.isVerbose()) {
                if ((fFlag & 0x01) >> 0) { sOption("hiDef") };
                if ((fFlag & 0x80) >> 2) { sOption("compressed") };
                if (!Binary.getSize() == Binary.read_uint32(0x5)) { sOption("wrong size!") };
            }
        } else if (Binary.compare("'WBND'")) {
            bDetected = true;
            sName = "Microsoft XACT Wave Bank (.XWB)"
        } else if (Binary.compare("'SDBK'")) {
            bDetected = true;
            sName = "Microsoft XACT Sound Bank (.XSB)"
        } else if (Binary.compare("'XGSF'")) {
            bDetected = true;
            sName = "Microsoft XACT Global Settings File (.XGS)"
        }
    }

    return result();
}