fix a resman method and make saving work using SaveFileManager

svn-id: r9828
This commit is contained in:
Jonathan Gray 2003-08-23 14:33:57 +00:00
parent 5e0f61a8b2
commit 4df7dd406f
4 changed files with 51 additions and 60 deletions

View file

@ -489,7 +489,8 @@ uint32 resMan::Res_fetch_len( uint32 res ) //Tony27Jan96
{ {
//returns the total file length of a resource - i.e. all headers are included too //returns the total file length of a resource - i.e. all headers are included too
FILE *fh=0; //file pointer //FILE *fh=0; //file pointer
File fh;
uint16 parent_res_file; uint16 parent_res_file;
uint16 actual_res; uint16 actual_res;
uint32 len; uint32 len;
@ -503,17 +504,17 @@ uint32 resMan::Res_fetch_len( uint32 res ) //Tony27Jan96
// first we have to find the file via the res_conv_table // first we have to find the file via the res_conv_table
fh = fopen(resource_files[parent_res_file],"rb"); //open the cluster file // open the cluster file
if (fh==NULL) if (fh.open(resource_files[parent_res_file],g_sword2->getGameDataPath()) == false)
Con_fatal_error("Res_fetch_len cannot *OPEN* %s", resource_files[parent_res_file]); Con_fatal_error("Res_fetch_len cannot *OPEN* %s", resource_files[parent_res_file]);
fread( &table_offset, sizeof(char), sizeof(uint32), fh); //1st DWORD of a cluster is an offset to the look-up table fh.read( &table_offset, sizeof(uint32)); //1st DWORD of a cluster is an offset to the look-up table
fseek(fh, table_offset+(actual_res*8)+4, SEEK_SET); //2 dwords per resource + skip the position dword fh.seek(table_offset+(actual_res*8)+4, SEEK_SET); //2 dwords per resource + skip the position dword
//fread( &pos, sizeof(char), 4, fh); //get position of our resource within the cluster file //fread( &pos, sizeof(char), 4, fh); //get position of our resource within the cluster file
fread( &len, sizeof(char), 4, fh); //read the length fh.read( &len, 4); //read the length
return(len); return(len);

View file

@ -47,6 +47,7 @@
#include "save_rest.h" #include "save_rest.h"
#include "scroll.h" // for Set_scrolling() #include "scroll.h" // for Set_scrolling()
#include "sound.h" #include "sound.h"
#include "sword2.h"
#include "walker.h" #include "walker.h"
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -179,33 +180,27 @@ void FillSaveBuffer(mem *buffer, uint32 size, uint8 *desc)
uint32 SaveData(uint16 slotNo, uint8 *buffer, uint32 bufferSize) uint32 SaveData(uint16 slotNo, uint8 *buffer, uint32 bufferSize)
{ {
char saveFileName[MAX_FILENAME_LEN]; char saveFileName[MAX_FILENAME_LEN];
FILE *fp;
uint32 itemsWritten; uint32 itemsWritten;
SaveFile *out;
SaveFileManager *mgr = g_system->get_savefile_manager();
sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
//create saves directory just in case not there
scumm_mkdir("saves"); if (!(out = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), true)))
sprintf(saveFileName, "saves\\savegame.%.3d", slotNo); // construct filename
fp = fopen(saveFileName, "wb"); // attempt to open file for writing
if (fp==NULL)
{
return(SR_ERR_FILEOPEN); // error: couldn't open file return(SR_ERR_FILEOPEN); // error: couldn't open file
}
else
{ itemsWritten = out->write(buffer, bufferSize); // write the buffer
// itemsWritten = fwrite(sourceAddress, size, count, fp);
itemsWritten = fwrite(buffer, 1, bufferSize, fp); // write the buffer delete out;
fclose(fp); // close savegame file delete mgr;
if (itemsWritten == bufferSize) // if we successfully wrote it all
return(SR_OK); // buffer saved ok
else
return(SR_ERR_WRITEFAIL); // write failed for some reason (could be hard drive full)
if (itemsWritten == bufferSize) // if we successfully wrote it all
return(SR_OK); // buffer saved ok
else
return(SR_ERR_WRITEFAIL); // write failed for some reason (could be hard drive full)
}
} }
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -253,30 +248,26 @@ uint32 RestoreGame(uint16 slotNo) // (James05feb97)
uint32 RestoreData(uint16 slotNo, uint8 *buffer, uint32 bufferSize) uint32 RestoreData(uint16 slotNo, uint8 *buffer, uint32 bufferSize)
{ {
char saveFileName[MAX_FILENAME_LEN]; char saveFileName[MAX_FILENAME_LEN];
FILE *fp; SaveFile *in;
SaveFileManager *mgr = g_system->get_savefile_manager();
uint32 itemsRead; uint32 itemsRead;
sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
sprintf(saveFileName, "saves\\savegame.%.3d", slotNo); // construct filename
fp = fopen(saveFileName, "rb"); // attempt to open file for reading if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false)))
if (fp==NULL)
{
return(SR_ERR_FILEOPEN); // error: couldn't open file return(SR_ERR_FILEOPEN); // error: couldn't open file
}
else itemsRead = in->read(buffer, bufferSize); // read savegame into the buffer
{
// itemsRead = fread(destAddress, size, count, fp);
itemsRead = fread(buffer, 1, bufferSize, fp); // read savegame into the buffer
if (itemsRead == bufferSize) // if we successfully read it all if (itemsRead == bufferSize) // if we successfully read it all
{ {
fclose(fp); // close savegame file delete in;
delete mgr;
return(SR_OK); // file read ok return(SR_OK); // file read ok
} }
else // didn't read the expected amount of data for some reason else // didn't read the expected amount of data for some reason
{ {
/*
if (ferror(fp)) // if it was a genuine read error, before reaching the end of the file if (ferror(fp)) // if it was a genuine read error, before reaching the end of the file
{ {
fclose(fp); // close savegame file fclose(fp); // close savegame file
@ -284,11 +275,12 @@ uint32 RestoreData(uint16 slotNo, uint8 *buffer, uint32 bufferSize)
} }
else // we reached the end of the file before we filled the savegame buffer (ie. incompatible savegame file!) else // we reached the end of the file before we filled the savegame buffer (ie. incompatible savegame file!)
{ {
fclose(fp); // close savegame file */
delete in;
delete mgr;
return(SR_ERR_INCOMPATIBLE); // error: incompatible save-data - can't use! return(SR_ERR_INCOMPATIBLE); // error: incompatible save-data - can't use!
} //}
} }
}
} }
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -393,24 +385,20 @@ uint32 GetSaveDescription(uint16 slotNo, uint8 *description) // (James05feb97)
{ {
char saveFileName[MAX_FILENAME_LEN]; char saveFileName[MAX_FILENAME_LEN];
_savegameHeader dummy; _savegameHeader dummy;
FILE *fp; SaveFile *in;
SaveFileManager *mgr = g_system->get_savefile_manager();
sprintf(saveFileName, "saves\\savegame.%.3d", slotNo); // construct filename sprintf(saveFileName, "%s.%.3d", g_sword2->_game_name, slotNo); // construct filename
fp = fopen(saveFileName, "rb"); // attempt to open file for reading if (!(in = mgr->open_savefile(saveFileName, g_sword2->getSavePath(), false)))
return(SR_ERR_FILEOPEN); // error: couldn't open file
if (fp==NULL)
{ in->read(&dummy, sizeof(_savegameHeader)); // read header
return(SR_ERR_FILEOPEN); // error: couldn't open file delete in;
} delete mgr;
else sprintf((char*)description, dummy.description);
{ return(SR_OK);
// fread(destAddress, size, count, fp);
fread(&dummy, sizeof(_savegameHeader), 1, fp); // read header
fclose(fp);
sprintf((char*)description, dummy.description);
return(SR_OK);
}
} }
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------

View file

@ -106,6 +106,7 @@ Sword2State::Sword2State(GameDetector *detector, OSystem *syst)
g_sword2 = this; g_sword2 = this;
_features = detector->_game.features; _features = detector->_game.features;
_gameId = detector->_game.id; _gameId = detector->_game.id;
_game_name = strdup(detector->_gameFileName.c_str());
_bootParam = detector->_bootParam; _bootParam = detector->_bootParam;
// Setup mixer // Setup mixer

View file

@ -60,6 +60,7 @@ class Sword2State : public Engine {
GameDetector *_detector; GameDetector *_detector;
uint32 _features; uint32 _features;
byte _gameId; byte _gameId;
char *_game_name; // target name for saves
Sword2Sound *_sound; Sword2Sound *_sound;
OSystem::MutexRef _paletteMutex; // put in a gfx class? OSystem::MutexRef _paletteMutex; // put in a gfx class?
private: private: