-
Notifications
You must be signed in to change notification settings - Fork 0
/
durangoimagercontroller.cpp
133 lines (113 loc) · 4.38 KB
/
durangoimagercontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* SPDX-License-Identifier: LGPL v3.0
* Copyright (C) 2023 Durango Computer Team (durangoretro.com)
*/
#include "durangoimagercontroller.h"
#include "durangorom.h"
#include <cstring>
#include <fstream>
DurangoImagerController::DurangoImagerController()
{
this->RomList = new std::vector<DurangoRom*>();
this->destinationFile= "";
this->addEmptySpace=false;
this->emptyspace=0;
}
std::string DurangoImagerController::addRomFile(std::string path){
fprintf(stderr,"Adding Path %s\n", path.c_str());
DurangoRom * durangoROM= DurangoRom::readDurangoROMFile(path);
this->RomList->push_back(durangoROM);
return durangoROM->getName();
}
void DurangoImagerController::removeRomFile(int index){
this->RomList->erase(std::next(this->RomList->begin(),index));
}
DurangoImagerController::~DurangoImagerController(){
delete RomList;
}
void DurangoImagerController::storeDestinationPath(std::string path){
this->destinationFile=path;
}
bool DurangoImagerController::hastDestination(){
return !this->destinationFile.empty();
}
void DurangoImagerController::setEmptyEndSpace(bool emptySpace){
this->addEmptySpace=emptySpace;
}
void DurangoImagerController::setEmptySpaceSize(long emptySpaceSize){
this->emptyspace=emptySpaceSize;
}
emptySpaceStruct DurangoImagerController::createEmptySpace(size_t emptySpaceSize){
fprintf(stderr,"Creating Empty Space...Size: %lu\n",emptySpaceSize);
emptySpaceStruct space;
durangoHeaderInfo headerInfo;
strncpy(headerInfo.filename,"FreeSpace",9);
headerInfo.signature=DurangoSignature::DL;
DurangoHeader header(headerInfo);
char* emptySpaceContent=new char[emptySpaceSize];
//Generate Content
char * headerContent = header.generateHeader();
//TODO: Implement Signature Converter
headerContent[SIGNATURE_POS]='d';
headerContent[SIGNATURE_POS+1]='L';
//fill content
std::fill_n(emptySpaceContent,emptySpaceSize,0xFF);
//Copy Header
memcpy(emptySpaceContent,headerContent,HEADER_SIZE);
space.emptySpaceContent=emptySpaceContent;
space.emptySpaceSize=emptySpaceSize;
return space;
}
std::vector<std::string> DurangoImagerController::openExistingVolume(std::string path){
//Read Volume File
fprintf(stderr,"Reading Volume File...%s\n",path.c_str());
std::ifstream * volumeFile = new std::ifstream(path.c_str(),std::ios::binary);
volumeFile->seekg(0,volumeFile->end);
size_t volumeSize = volumeFile->tellg();
volumeFile->seekg(0,volumeFile->beg);
char * volumeContent = new char[volumeSize];
volumeFile->read(volumeContent,volumeSize);
fprintf(stderr,"Readed Volume File...%s\n",path.c_str());
volumeFile->close();
//get Each Rom
std::vector<unsigned long> romPositions;
std::vector<std::string> romNames;
for(unsigned long i=0;i<volumeSize-1;i++){
//if found a Durango Executable
if(volumeContent[i]=='d' && volumeContent[i+1]=='X'){
romPositions.push_back(i-1);
}
//Empty Space
if(volumeContent[i]=='d' && volumeContent[i+1]=='L'){
romPositions.push_back(i-1);
}
//TODO: Other File Types
}
romPositions.push_back(volumeSize);
for(size_t i=0;i<romPositions.size()-1;i++){
unsigned long initrompos = romPositions.data()[i];
unsigned long endrompos = romPositions.data()[i+1]-1;
unsigned long romSize = endrompos-initrompos;
char * romContent = new char[romSize];
memcpy(romContent,volumeContent+initrompos,romSize);
//Read and create Rom
DurangoRom * rom = DurangoRom::readDurangoROMFile(romContent,romSize);
RomList->push_back(rom);
romNames.push_back(rom->getName());
}
return romNames;
}
void DurangoImagerController::createVolume(){
fprintf(stderr,"Creaing Volume File...%s\n",this->destinationFile.c_str());
std::ofstream * durangoVolume = new std::ofstream(this->destinationFile.c_str(),std::ios::binary|std::ios::trunc);
for(unsigned int i=0;i<this->RomList->size();i++){
DurangoRom * currentRom = this->RomList->data()[i];
durangoVolume->write(currentRom->getRomContent(),currentRom->getRomSize());
}
// create empty Space (If needed)
if(addEmptySpace){
emptySpaceStruct emptyspc= createEmptySpace(emptyspace);
durangoVolume->write(emptyspc.emptySpaceContent,emptyspc.emptySpaceSize);
}
durangoVolume->close();
}