Ejemplo n.º 1
0
void DialogMakeRom::on_buttonMake_clicked()
{
    std::FILE *rom,*mapdata;
    rom=fopenQ(ui->editRom->text(),"rb+");
    if(rom==0){
        QMessageBox msgBox;
        msgBox.setText("Failed to open the ROM.");
        msgBox.setIcon(QMessageBox::Icon::Critical);
        msgBox.exec();
        return;
    }
    mapdata=fopenQ(ui->editSrcFile->text(),"rb");
    if(mapdata==0){
        QMessageBox msgBox;
        msgBox.setText("Failed to open the mapdata file.");
        msgBox.setIcon(QMessageBox::Icon::Critical);
        msgBox.exec();
        return;
    }
    u32 len;
    std::fseek(mapdata,0,SEEK_END);
    len=std::ftell(mapdata);
    std::fseek(mapdata,0,SEEK_SET);
    std::unique_ptr<u8[]> buf(new u8[len]);
    std::fread(buf.get(),len,1,mapdata);
    std::fclose(mapdata);

    u16 fileId=nitroGetSubFileId(rom,"rom/map01/mapdata");
    assert(fileId!=0xFFFF);
    u32 dp,dl;
    dp=nitroGetSubFileOffset(rom,fileId,&dl);
    if(dl>=len){
        std::fseek(rom,dp,SEEK_SET);
        std::fwrite(buf.get(),len,1,rom);
    }else{
        ROM_HEADER rom_header;
        std::fseek(rom,0,SEEK_SET);
        std::fread(&rom_header,sizeof(rom_header),1,rom);
        dp=rom_header.ROMSize;
        rom_header.ROMSize+=len;
        rom_header.CRC16=nitroCrc16(&rom_header,offsetof(ROM_HEADER,headerCRC16));
        std::fseek(rom,0,SEEK_SET);
        std::fwrite(&rom_header,sizeof(rom_header),1,rom);
        nitroSetSubFileOffset(rom,fileId,dp,len);
        std::fseek(rom,dp,SEEK_SET);
        std::fwrite(buf.get(),len,1,rom);
    }



    std::fclose(rom);

    QMessageBox msgBox;
    msgBox.setText("Success!");
    msgBox.exec();
    return;
}
Ejemplo n.º 2
0
void MainWindow::on_actionExtract_triggered(){
    QString fileName=QFileDialog::getOpenFileName(this, tr("Select ROM"),
        "",
        tr("ROM File(*.nds *.bin);;Any files(*.*)"));
    if(fileName==QString::null)return;
    std::FILE* rom=fopenQ(fileName,"rb");
    if(rom==nullptr){
        QMessageBox msgBox;
        msgBox.setText(tr("Failed to open the ROM."));
        msgBox.setIcon(QMessageBox::Icon::Critical);
        msgBox.exec();
        return;
    }

    fileName=QFileDialog::getSaveFileName(this, tr("Save mapdata to..."),
        "",
        tr("mapdata File(*.bin)"));
    if(fileName==QString::null)return;
    std::FILE* mapdataFile=fopenQ(fileName,"wb");
    if(mapdataFile==nullptr){
        QMessageBox msgBox;
        msgBox.setText(tr("Failed to open mapdata file."));
        msgBox.setIcon(QMessageBox::Icon::Critical);
        msgBox.exec();
        std::fclose(rom);
        return;
    }

    u16 id=nitroGetSubFileId(rom,"rom/map01/mapdata");
    u32 off,len;
    off=nitroGetSubFileOffset(rom,id,&len);
    std::unique_ptr<u8[]> buf(new u8[len]);
    std::fseek(rom,off,SEEK_SET);
    std::fread(buf.get(),len,1,rom);
    std::fwrite(buf.get(),len,1,mapdataFile);
    std::fclose(rom);
    std::fclose(mapdataFile);

    QMessageBox msgBox;
    msgBox.setText(tr("Succeeded to extract mapdata from ROM."));
    msgBox.exec();

    openMapdata(fileName);
}