// Detect It Easy: detection rule file
// Author: Kae <TG@kaens>

meta("format", "RenderWare collision data (.COL)"); // Criterion Software/Rockstar Games

function detect() {
    var hksz = 0, lst = [], c = 0, bad = '';
    for (p = 0; p < X.Sz(); c++, p += hksz) {
        if (!X.c("'COLL'", p) || X.U8(p - 1) || p + 8 > X.Sz())
            if (!X.c("'COLL'", --p)) break; else bad = bad.addIfNone('!badchunk'/*+'@'+Hex(p+1-hksz)*/);
        // In GTA:SA, peds.col has 3 chunks that are 1 byte shorter than declared!
        // They seem to be linked to the unusual entity name length.
        hksz = 8 + X.U32(p + 4, _LE);
        if (p + hksz > X.Sz()) return;
        var entity = X.SA(p + 8, 8); if (!entity.length) break;
        //_l2r('col',p,entity+':'+charStat(X.readBytes(p+8,8,true)));
        if (charStat(entity, 1).indexOf('allasc') < 0) return;
        lst.push(X.SA(p + 8, 8))
    }

    if (!c) return;
    if (bad.length) sVersion = sVersion.appendS('malformed' + bad, '/');
    if (X.isVerbose()) {
        sOption(addEllipsis(lst.join(',')));
        sOption(outSz(p), 'sz:');
    }

    bDetected = true;

    return result();
}