Need help extract mrg [明治東亰恋伽]

Extraction and unpacking of game archives and compression, encryption, obfuscation, decoding of unknown files
bam_bam
Posts: 31
Joined: Thu Apr 21, 2016 1:06 pm

Need help extract mrg [明治東亰恋伽]

Post by bam_bam »

Hi :)
I am not good at English, so I hope you look over some awkward expressions.

How can I unpack mrg file?

I know there is python tool already.
but It's not perfect and there is no repack function.

so I want a bms script for these file.
I will be very pleased if you help me.
Thanks to read.
Last edited by bam_bam on Thu Oct 04, 2018 12:47 pm, edited 1 time in total.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Need help extract mrg

Post by aluigi »

What's the name of the game?
bam_bam
Posts: 31
Joined: Thu Apr 21, 2016 1:06 pm

Re: Need help extract mrg

Post by bam_bam »

aluigi wrote:What's the name of the game?


oh, sorry.
A title is 明治東亰恋伽.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Need help extract mrg

Post by aluigi »

The HED file, which I expect contains information for extracting the data from the MRG file, is quite confusing.
In the MRG file I see only various "MZX0" strings.
What's the python script you are currently using?
bam_bam
Posts: 31
Joined: Thu Apr 21, 2016 1:06 pm

Re: Need help extract mrg

Post by bam_bam »

aluigi wrote:The HED file, which I expect contains information for extracting the data from the MRG file, is quite confusing.
In the MRG file I see only various "MZX0" strings.
What's the python script you are currently using?


https://github.com/Hintay/PS-HuneX_Tools

I always appreciate your help :)
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Need help extract mrg [明治東亰恋伽]

Post by aluigi »

Try the following:

Code: Select all

set MEMORY_FILE10 string "
// https://github.com/Hintay/PS-HuneX_Tools/blob/master/tools/mzx/decomp_mzx0.py
int mzx0_decompress(unsigned char *f, int fsz, unsigned char *dout, int exlen) {
    unsigned char   ringbuf[128];
    int ring_wpos = 0;
    int clear_count = 0;
    int offset = 0;
    int last1 = 0, last2 = 0;
    unsigned char   *max = f + fsz;
    int i, k;

    for(i = 0; i < sizeof(ringbuf); i++) ringbuf[i] = 0;

    while(offset < exlen) {
        if(f >= max)
            break;
        if(clear_count <= 0) {
            clear_count = 0x1000;
            last1 = last2 = 0;
        }
        int flags = *f++;

        clear_count -= ((flags & 0x03) == 2) ? 1 : ((flags / 4) + 1);

        if((flags & 0x03) == 0) {
            for(i = 0; i < ((flags / 4) + 1); i++) {
                if(offset >= exlen) break;
                dout[offset  ] = last1;
                dout[offset+1] = last2;
                offset += 2;
            }

        } else if((flags & 0x03) == 1) {
            if(f >= max)
                break;
            k = *f++;
            k = 2 * (k+1);
            for(i = 0; i < ((flags / 4) + 1); i++) {
                if(offset >= exlen) break;
                // read two previous bytes in sliding
                dout[offset] = dout[offset - k];
                offset += 1;
                dout[offset] = dout[offset - k];
                offset += 1;
            }
            last1 = dout[offset-2];
            last2 = dout[offset-1];

        } else if((flags & 0x03) == 2) {
            if(offset >= exlen) break;
            dout[offset  ] = last1 = ringbuf[2 * ((flags / 4))];
            dout[offset+1] = last2 = ringbuf[2 * ((flags / 4)) + 1];
            offset += 2;

        } else {
            for(i = 0; i < ((flags / 4) + 1); i++) {
                if(offset >= exlen) break;
                if(f >= max)
                    break;
                last1 = ringbuf[ring_wpos] = dout[offset] = *f++;
                ring_wpos += 1;
                offset += 1;
                last2 = ringbuf[ring_wpos] = dout[offset] = *f++;
                ring_wpos += 1;
                offset += 1;
                ring_wpos &= 0x7F;
            }
        }
    }
    return offset;
}
"
comtype calldll "MEMORY_FILE10 mzx0_decompress tcc RET #INPUT# #INPUT_SIZE# #OUTPUT# #OUTPUT_SIZE#"

#codepage 932
idstring "mrgd00"
get number_of_entries short
for i = 0 < number_of_entries
    get sector_offset short
    get offset short
    get sector_size_upper_boundary short
    get size short
    xmath OFFSET "(sector_offset * 0x800) + offset"
    putarray 0 i OFFSET
next i
savepos BASE_OFF

    get OFFSET asize
    math OFFSET - BASE_OFF
    putarray 0 i OFFSET
sortarray 0 1

# the first file contains the names
math END_NAMES = 0
for i = 1 < number_of_entries
    if END_NAMES == 0
        getdstring NAME 32
        if NAME == ""
            math END_NAMES = 1
        else
            string NAME + .
        endif
    else
        set NAME string ""
    endif
    getarray OFFSET 0 i
    math i + 1
    getarray SIZE   0 i
    math SIZE - OFFSET
    math OFFSET + BASE_OFF

    savepos TMP
    goto OFFSET
    getdstring SIGN 4
    if SIGN u== "MZX0"
        get XSIZE long
        math OFFSET + 8
        math SIZE   - 8
        clog NAME OFFSET SIZE XSIZE
    else
        log NAME OFFSET SIZE
    endif
    goto TMP
next

Basically I ignore the *size fields because they look confusing and difficult to calculate, the only downside is that you are probably going to have few (less than 8) padding bytes at the end of each file.

The names are chaotic because there are less names available than files, I used a work-around.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Need help extract mrg [明治東亰恋伽]

Post by aluigi »

I have updated the previous script for supporting the MZX compression (which will be natively available in the next quickbms).
bam_bam
Posts: 31
Joined: Thu Apr 21, 2016 1:06 pm

Re: Need help extract mrg [明治東亰恋伽]

Post by bam_bam »

aluigi wrote:I have updated the previous script for supporting the MZX compression (which will be natively available in the next quickbms).


It works well.
I don't know how to thank you.
I hope you always have a nice day!
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Need help extract mrg [明治東亰恋伽]

Post by aluigi »

Thanks to you for the new algorithm for quickbms :D
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Need help extract mrg [明治東亰恋伽]

Post by aluigi »

Script updated.
Files decompressed with previous versions may be wrong.
Nyanpasu
Posts: 8
Joined: Mon Apr 29, 2019 10:46 pm

Re: Need help extract mrg [明治東亰恋伽]

Post by Nyanpasu »

Thanks