// File: OpenFodder[P]SOS_Unpacker/SOS_Unpacker.cpp
// Function: main
int main(int argc, char* argv[]) {
    std::cout << "SOS Unpacker\n";
    std::cout << "https://github.com/OpenFodder\n\n";

    if (CAPSInit()) {
        std::cout << "CAPSImg.dll not found\n";
    } else {

        system("mkdir out");

        for (auto& extension : { ".raw", ".ipf" } ) {
            std::cout << "Finding " << extension << " files\n";
            auto Files = local_DirectoryList(".", extension);

            if (!Files.size())
                std::cout << "No " << extension << " files found\n";

            //local_DirectoryList
            for (auto& File : Files) {
                std::cout << "\nLoading " << File << "\n";
                auto Tracks = GetSOSTracks(File);

                if (!Tracks.size()) {
                    std::cout << "No Tracks found in file: " << File << "\n";
                    continue;
                }


                std::cout << "Extracting to .\\out\\...\n";
                ExtractFiles(Tracks, "out");
            }
        }
    }

    std::cout << "Press enter to finish\n";
    std::cin.get();

    CAPSExit();
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0xc180(%rip), %rdi      # 0x26fc0
leaq 0x35d5(%rip), %rsi      # 0x1e41c
callq 0x4220
movq 0xc16d(%rip), %rdi      # 0x26fc0
leaq 0x35d0(%rip), %rsi      # 0x1e42a
callq 0x4220
callq 0x6b70
cmpl $0x0, %eax
je 0x1ae81
movq 0xc150(%rip), %rdi      # 0x26fc0
leaq 0x35d3(%rip), %rsi      # 0x1e44a
callq 0x4220
jmp 0x1b34b
leaq 0x35d9(%rip), %rdi      # 0x1e461
callq 0x41b0
leaq 0x35d7(%rip), %rax      # 0x1e46b
movq %rax, -0x38(%rbp)
leaq 0x35d1(%rip), %rax      # 0x1e470
movq %rax, -0x30(%rbp)
leaq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x2, -0x20(%rbp)
leaq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1b720
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1b740
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x1b349
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0xc0ce(%rip), %rdi      # 0x26fc0
leaq 0x357c(%rip), %rsi      # 0x1e475
callq 0x4220
movq %rax, %rdi
movq -0x50(%rbp), %rax
movq (%rax), %rsi
callq 0x4220
movq %rax, %rdi
leaq 0x3567(%rip), %rsi      # 0x1e47e
callq 0x4220
leaq -0x89(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x4430
movq -0x150(%rbp), %rdx
leaq 0x336c(%rip), %rsi      # 0x1e2a9
leaq -0x88(%rbp), %rdi
callq 0x4320
jmp 0x1af4b
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x160(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x4430
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x4320
jmp 0x1af88
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq 0x1d580
jmp 0x1afa1
leaq -0xc0(%rbp), %rdi
callq 0x4488
leaq -0xc1(%rbp), %rdi
callq 0x4260
leaq -0x88(%rbp), %rdi
callq 0x4488
leaq -0x89(%rbp), %rdi
callq 0x4260
leaq -0x68(%rbp), %rdi
callq 0x1b780
cmpq $0x0, %rax
jne 0x1b0bc
movq 0xbfd5(%rip), %rdi      # 0x26fc0
leaq 0x3494(%rip), %rsi      # 0x1e486
callq 0x4220
movq %rax, -0x168(%rbp)
jmp 0x1b000
movq -0x168(%rbp), %rdi
movq -0x50(%rbp), %rax
movq (%rax), %rsi
callq 0x4220
movq %rax, -0x170(%rbp)
jmp 0x1b01c
movq -0x170(%rbp), %rdi
leaq 0x3460(%rip), %rsi      # 0x1e48a
callq 0x4220
jmp 0x1b031
jmp 0x1b0bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1b094
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1b07c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x4488
leaq -0xc1(%rbp), %rdi
callq 0x4260
leaq -0x88(%rbp), %rdi
callq 0x4488
leaq -0x89(%rbp), %rdi
callq 0x4260
jmp 0x1b37a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1b33e
leaq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x1b7a0
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
callq 0x1b7d0
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x1b800
testb $0x1, %al
jne 0x1b109
jmp 0x1b324
leaq -0xd8(%rbp), %rdi
callq 0x1b840
movq %rax, -0xe8(%rbp)
movq 0xbe9d(%rip), %rdi      # 0x26fc0
leaq 0x336e(%rip), %rsi      # 0x1e498
callq 0x4220
movq %rax, -0x178(%rbp)
jmp 0x1b138
movq -0x178(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x41e0
movq %rax, -0x180(%rbp)
jmp 0x1b154
movq -0x180(%rbp), %rdi
leaq 0x32b5(%rip), %rsi      # 0x1e417
callq 0x4220
jmp 0x1b169
movq -0xe8(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0x1a160
jmp 0x1b17e
leaq -0x100(%rbp), %rdi
callq 0x1b780
cmpq $0x0, %rax
jne 0x1b203
movq 0xbe29(%rip), %rdi      # 0x26fc0
leaq 0x3304(%rip), %rsi      # 0x1e4a2
callq 0x4220
movq %rax, -0x188(%rbp)
jmp 0x1b1ac
movq -0x188(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x41e0
movq %rax, -0x190(%rbp)
jmp 0x1b1c8
movq -0x190(%rbp), %rdi
leaq 0x3241(%rip), %rsi      # 0x1e417
callq 0x4220
jmp 0x1b1dd
movl $0x5, -0x104(%rbp)
jmp 0x1b29f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1b316
movq 0xbdb6(%rip), %rdi      # 0x26fc0
leaq 0x32ab(%rip), %rsi      # 0x1e4bc
callq 0x4220
jmp 0x1b218
leaq -0x120(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x1b860
jmp 0x1b22d
leaq -0x141(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x4430
movq -0x198(%rbp), %rdx
leaq 0x3219(%rip), %rsi      # 0x1e467
leaq -0x140(%rbp), %rdi
callq 0x4320
jmp 0x1b25c
leaq -0x120(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x1a850
jmp 0x1b271
leaq -0x140(%rbp), %rdi
callq 0x4488
leaq -0x141(%rbp), %rdi
callq 0x4260
leaq -0x120(%rbp), %rdi
callq 0x1b550
movl $0x0, -0x104(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x1b550
movl -0x104(%rbp), %eax
testl %eax, %eax
je 0x1b2b9
jmp 0x1b2b7
jmp 0x1b2bb
jmp 0x1b2bb
leaq -0xd8(%rbp), %rdi
callq 0x1b950
jmp 0x1b0ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x1b2fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x4488
leaq -0x141(%rbp), %rdi
callq 0x4260
leaq -0x120(%rbp), %rdi
callq 0x1b550
leaq -0x100(%rbp), %rdi
callq 0x1b550
jmp 0x1b33e
leaq -0x68(%rbp), %rdi
callq 0x1b550
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
jmp 0x1aed5
leaq -0x68(%rbp), %rdi
callq 0x1b550
jmp 0x1b37a
jmp 0x1b34b
movq 0xbc6e(%rip), %rdi      # 0x26fc0
leaq 0x317c(%rip), %rsi      # 0x1e4d5
callq 0x4220
movq 0xbc73(%rip), %rdi      # 0x26fd8
callq 0x42b0
callq 0x6b80
xorl %eax, %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x4420
nopw %cs:(%rax,%rax)