00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025
00026 #include <wx/wxprec.h>
00027
00028 #ifndef WX_PRECOMP
00029 #include <wx/wx.h>
00030 #endif
00031
00032 #include <fstream>
00033 #include <cstring>
00034
00035 #include "SRAMFile.hh"
00036
00037 using namespace emuWorks;
00038
00039 SRAMFile::SRAMFile(wxString &filename) {
00040 current = -1;
00041 load(filename);
00042 }
00043
00044 SRAMFile::~SRAMFile() {
00045 delete file;
00046 delete data;
00047 delete games[0];
00048 delete games[1];
00049 delete games[2];
00050 }
00051
00052 bool SRAMFile::isModified() {
00053 if (current == -1) {
00054 return false;
00055 }
00056
00057 for (int game = 0; game < 3; game++) {
00058 if (games[game]->isModified()) {
00059 return true;
00060 }
00061 }
00062
00063 return false;
00064 }
00065
00066 SaveSlot *SRAMFile::getCurrentGame() {
00067 if (current == -1) {
00068 return 0;
00069 }
00070
00071 return games[current];
00072 }
00073
00074 bool SRAMFile::setCurrentGame(unsigned int current) {
00075 if (current > 2) {
00076 return false;
00077 }
00078
00079 if (!isValidGame(current)) {
00080 return false;
00081 }
00082
00083 this->current = current;
00084 return true;
00085 }
00086
00087 bool SRAMFile::isValidGame(int game) {
00088 return games[game]->isValid();
00089 }
00090
00091 bool SRAMFile::save() {
00092 return save(*file);
00093 }
00094
00095 bool SRAMFile::save(wxString &filename) {
00096 for (int game = 0; game < 3; game++) {
00097 if (isValidGame(game)) {
00098 memcpy((data + GAME_OFFSET + (game * GAME_SIZE)),
00099 games[game]->nvram,
00100 GAME_SIZE);
00101 }
00102 }
00103
00104 std::ofstream out(filename.mb_str(), std::ios::binary | std::ios::out);
00105
00106 if (!out) {
00107 wxMessageBox(wxT("Unable to open the SRAM file."),
00108 wxT("File Open Error"), wxOK | wxICON_ERROR);
00109
00110 return false;
00111 }
00112
00113 out.write(data, SRAM_SIZE);
00114
00115 if (out.rdstate() & std::ios::failbit) {
00116 wxMessageBox(wxT("Unable to write to the SRAM file."),
00117 wxT("File I/O error"), wxOK | wxICON_ERROR);
00118
00119 out.close();
00120 return false;
00121 }
00122
00123 out.close();
00124
00125 games[0]->setModified(false);
00126 games[1]->setModified(false);
00127 games[2]->setModified(false);
00128
00129 return true;
00130 }
00131
00132 void SRAMFile::load(wxString &filename) {
00133 std::ifstream in(filename.mb_str(), std::ios::in | std::ios::binary);
00134
00135 if (!in) {
00136 wxMessageBox(wxT("Unable to open the SRAM file."),
00137 wxT("File Open Error"), wxOK | wxICON_ERROR);
00138 return;
00139 }
00140
00141 data = new char[SRAM_SIZE];
00142 in.read(data, SRAM_SIZE);
00143
00144 if (in.rdstate() & std::ios::failbit) {
00145 wxMessageBox(wxT("Unable to read the SRAM file."),
00146 wxT("File I/O Error"), wxOK | wxICON_ERROR);
00147
00148 in.close();
00149 delete data;
00150
00151 return;
00152 }
00153
00154 in.close();
00155
00156 for (int slot = 2; slot >= 0; slot--) {
00157 games[slot] = new SaveSlot(data + GAME_OFFSET + (slot * GAME_SIZE));
00158
00159 if (games[slot]->isValid()) {
00160 current = slot;
00161 }
00162 }
00163
00164 file = new wxString(filename);
00165
00166 if (current != -1) {
00167 wxString bakfile = filename + ".bak";
00168 std::ofstream out(bakfile.mb_str(), std::ios::out | std::ios::binary);
00169
00170 if (out) {
00171 out.write(data, SRAM_SIZE);
00172 out.close();
00173 }
00174 }
00175 }