* code function for relocating the savefile.

* don't bitch at the user when trying to load a savestate file that doesn't exist.
* grey out 'load state' items if there is no file.
This commit is contained in:
StapleButter 2018-10-23 22:54:09 +02:00
parent f60ac42466
commit 1edf2aed3b
3 changed files with 58 additions and 12 deletions

View File

@ -36,7 +36,7 @@ namespace NDSCart_SRAM
u8* SRAM;
u32 SRAMLength;
char SRAMPath[256];
char SRAMPath[1024];
void (*WriteFunc)(u8 val, bool islast);
@ -148,8 +148,8 @@ void LoadSave(const char* path)
Discover_AddrLength = 0x7FFFFFFF;
Discover_LikelySize = 0;
strncpy(SRAMPath, path, 255);
SRAMPath[255] = '\0';
strncpy(SRAMPath, path, 1023);
SRAMPath[1023] = '\0';
FILE* f = melon_fopen(path, "rb");
if (f)
@ -196,6 +196,22 @@ void LoadSave(const char* path)
StatusReg = 0x00;
}
void RelocateSave(const char* path)
{
strncpy(SRAMPath, path, 1023);
SRAMPath[1023] = '\0';
FILE* f = melon_fopen(path, "wb");
if (!f)
{
printf("NDSCart_SRAM::RelocateSave: failed to create new file. fuck\n");
return;
}
fwrite(SRAM, SRAMLength, 1, f);
fclose(f);
}
u8 Read()
{
return Data;
@ -1127,6 +1143,12 @@ bool LoadROM(const char* path, const char* sram, bool direct)
return true;
}
void RelocateSave(const char* path)
{
// herp derp
NDSCart_SRAM::RelocateSave(path);
}
void ReadROM(u32 addr, u32 len, u32 offset)
{
if (!CartInserted) return;

View File

@ -45,6 +45,7 @@ void Reset();
void DoSavestate(Savestate* file);
bool LoadROM(const char* path, const char* sram, bool direct);
void RelocateSave(const char* path);
void WriteROMCnt(u32 val);
u32 ReadROMData();

View File

@ -24,6 +24,7 @@
#include "libui/ui.h"
#include "../types.h"
#include "../melon_fopen.h"
#include "../version.h"
#include "../Config.h"
@ -58,6 +59,8 @@ uiMenuItem* MenuItem_SaveState;
uiMenuItem* MenuItem_LoadState;
uiMenuItem* MenuItem_UndoStateLoad;
uiMenuItem* MenuItem_LoadStateSlot[8];
uiMenuItem* MenuItem_Pause;
uiMenuItem* MenuItem_Reset;
uiMenuItem* MenuItem_Stop;
@ -104,9 +107,19 @@ void SetupScreenRects(int width, int height);
void SaveState(int slot);
void LoadState(int slot);
void UndoStateLoad();
void GetSavestateName(int slot, char* filename, int len);
bool FileExists(char* name)
{
FILE* f = melon_fopen(name, "rb");
if (!f) return false;
fclose(f);
return true;
}
void UpdateWindowTitle(void* data)
{
uiWindowSetTitle(MainWindow, (const char*)data);
@ -751,6 +764,14 @@ void Run()
uiMenuItemEnable(MenuItem_LoadState);
uiMenuItemEnable(MenuItem_UndoStateLoad);
for (int i = 0; i < 8; i++)
{
char ssfile[1024];
GetSavestateName(i+1, ssfile, 1024);
if (FileExists(ssfile)) uiMenuItemEnable(MenuItem_LoadStateSlot[i]);
else uiMenuItemDisable(MenuItem_LoadStateSlot[i]);
}
uiMenuItemEnable(MenuItem_Pause);
uiMenuItemEnable(MenuItem_Reset);
uiMenuItemEnable(MenuItem_Stop);
@ -867,6 +888,12 @@ void LoadState(int slot)
uiFreeText(file);
}
if (!FileExists(filename))
{
EmuRunning = prevstatus;
return;
}
// backup
Savestate* backup = new Savestate("timewarp.mln", true);
NDS::DoSavestate(backup);
@ -926,6 +953,9 @@ void SaveState(int slot)
{
NDS::DoSavestate(state);
delete state;
if (slot > 0)
uiMenuItemEnable(MenuItem_LoadStateSlot[slot-1]);
}
EmuRunning = prevstatus;
@ -1216,15 +1246,6 @@ void ApplyNewSettings()
}
bool _fileexists(char* name)
{
FILE* f = fopen(name, "rb");
if (!f) return false;
fclose(f);
return true;
}
int main(int argc, char** argv)
{
srand(time(NULL));
@ -1313,6 +1334,8 @@ int main(int argc, char** argv)
uiMenuItem* ssitem = uiMenuAppendItem(submenu, name);
uiMenuItemOnClicked(ssitem, OnLoadState, (void*)&kSavestateNum[i]);
if (i < 8) MenuItem_LoadStateSlot[i] = ssitem;
}
MenuItem_LoadState = uiMenuAppendSubmenu(menu, submenu);