flycast/core/nullDC.cpp

227 lines
4.9 KiB
C++
Raw Normal View History

2021-07-05 17:44:08 +00:00
#ifndef LIBRETRO
#include "types.h"
#include "emulator.h"
2013-12-19 17:10:14 +00:00
#include "hw/mem/_vmem.h"
#include "cfg/cfg.h"
#include "cfg/option.h"
#include "log/LogManager.h"
2021-07-05 17:44:08 +00:00
#include "rend/gui.h"
#include "oslib/oslib.h"
#include "debug/gdb_server.h"
2021-07-05 17:44:08 +00:00
#include "archive/rzip.h"
#include "rend/mainui.h"
#include "input/gamepad_device.h"
2021-10-03 16:34:27 +00:00
#include "lua/lua.h"
#include "stdclass.h"
#include "serialize.h"
int flycast_init(int argc, char* argv[])
2013-12-19 17:10:14 +00:00
{
#if defined(TEST_AUTOMATION)
setbuf(stdout, 0);
setbuf(stderr, 0);
settings.aica.muteAudio = true;
#endif
2013-12-19 17:10:14 +00:00
if (!_vmem_reserve())
{
ERROR_LOG(VMEM, "Failed to alloc mem");
2013-12-19 17:10:14 +00:00
return -1;
}
if (ParseCommandLine(argc, argv))
2013-12-19 17:10:14 +00:00
{
return 69;
2013-12-19 17:10:14 +00:00
}
config::Settings::instance().reset();
LogManager::Shutdown();
if (!cfgOpen())
2013-12-19 17:10:14 +00:00
{
LogManager::Init();
NOTICE_LOG(BOOT, "Config directory is not set. Starting onboarding");
gui_open_onboarding();
2013-12-19 17:10:14 +00:00
}
else
{
LogManager::Init();
config::Settings::instance().load(false);
}
gui_init();
os_CreateWindow();
os_SetupInput();
if(config::GDB)
debugger::init(config::GDBPort);
2021-10-03 16:34:27 +00:00
lua::init();
if(config::ProfilerEnabled)
LogManager::GetInstance()->SetEnable(LogTypes::PROFILER, true);
return 0;
}
void dc_exit()
2018-09-02 13:49:23 +00:00
{
emu.stop();
mainui_stop();
2018-09-02 13:49:23 +00:00
}
2013-12-19 17:10:14 +00:00
void SaveSettings()
{
config::Settings::instance().save();
GamepadDevice::SaveMaplePorts();
#ifdef __ANDROID__
void SaveAndroidSettings();
SaveAndroidSettings();
#endif
2019-02-06 18:57:13 +00:00
}
void flycast_term()
2018-09-20 20:41:10 +00:00
{
gui_cancel_load();
2021-10-03 16:34:27 +00:00
lua::term();
emu.term();
gui_term();
os_TermInput();
2018-09-20 20:41:10 +00:00
}
2021-06-03 11:22:40 +00:00
void dc_savestate(int index)
2018-09-02 13:49:23 +00:00
{
Serializer ser;
dc_serialize(ser);
2018-09-02 13:49:23 +00:00
void *data = malloc(ser.size());
2021-07-05 17:44:08 +00:00
if (data == nullptr)
2018-09-02 13:49:23 +00:00
{
WARN_LOG(SAVESTATE, "Failed to save state - could not malloc %d bytes", (int)ser.size());
gui_display_notification("Save state failed - memory full", 2000);
return;
2018-09-02 13:49:23 +00:00
}
ser = Serializer(data, ser.size());
dc_serialize(ser);
2018-09-02 13:49:23 +00:00
2021-07-05 17:44:08 +00:00
std::string filename = hostfs::getSavestatePath(index, true);
2020-12-26 08:58:53 +00:00
#if 0
FILE *f = nowide::fopen(filename.c_str(), "wb") ;
2018-09-02 13:49:23 +00:00
if ( f == NULL )
{
WARN_LOG(SAVESTATE, "Failed to save state - could not open %s for writing", filename.c_str()) ;
gui_display_notification("Cannot open save file", 2000);
2021-07-05 17:44:08 +00:00
free(data);
return;
2018-09-02 13:49:23 +00:00
}
std::fwrite(data, 1, ser.size(), f) ;
std::fclose(f);
2020-12-26 08:58:53 +00:00
#else
RZipFile zipFile;
if (!zipFile.Open(filename, true))
{
WARN_LOG(SAVESTATE, "Failed to save state - could not open %s for writing", filename.c_str());
gui_display_notification("Cannot open save file", 2000);
2021-07-05 17:44:08 +00:00
free(data);
2020-12-26 08:58:53 +00:00
return;
}
if (zipFile.Write(data, ser.size()) != ser.size())
2020-12-26 08:58:53 +00:00
{
WARN_LOG(SAVESTATE, "Failed to save state - error writing %s", filename.c_str());
gui_display_notification("Error saving state", 2000);
zipFile.Close();
2021-07-05 17:44:08 +00:00
free(data);
2020-12-26 08:58:53 +00:00
return;
}
zipFile.Close();
#endif
2018-09-02 13:49:23 +00:00
2021-07-05 17:44:08 +00:00
free(data);
INFO_LOG(SAVESTATE, "Saved state to %s size %d", filename.c_str(), (int)ser.size()) ;
gui_display_notification("State saved", 1000);
2018-09-02 13:49:23 +00:00
}
2021-06-03 11:22:40 +00:00
void dc_loadstate(int index)
2018-09-02 13:49:23 +00:00
{
2020-12-26 08:58:53 +00:00
u32 total_size = 0;
FILE *f = nullptr;
2018-09-02 13:49:23 +00:00
emu.stop();
2018-09-02 13:49:23 +00:00
2021-07-05 17:44:08 +00:00
std::string filename = hostfs::getSavestatePath(index, false);
2020-12-26 08:58:53 +00:00
RZipFile zipFile;
if (zipFile.Open(filename, false))
2018-09-02 13:49:23 +00:00
{
2020-12-26 08:58:53 +00:00
total_size = (u32)zipFile.Size();
2021-10-21 10:03:04 +00:00
if (index == -1 && config::GGPOEnable)
{
f = zipFile.rawFile();
long pos = std::ftell(f);
MD5Sum().add(f)
.getDigest(settings.network.md5.savestate);
std::fseek(f, pos, SEEK_SET);
f = nullptr;
}
2018-09-02 13:49:23 +00:00
}
2020-12-26 08:58:53 +00:00
else
{
f = nowide::fopen(filename.c_str(), "rb") ;
2020-12-26 08:58:53 +00:00
if ( f == NULL )
{
WARN_LOG(SAVESTATE, "Failed to load state - could not open %s for reading", filename.c_str()) ;
gui_display_notification("Save state not found", 2000);
return;
}
if (index == -1 && config::GGPOEnable)
MD5Sum().add(f)
.getDigest(settings.network.md5.savestate);
std::fseek(f, 0, SEEK_END);
total_size = (u32)std::ftell(f);
std::fseek(f, 0, SEEK_SET);
2020-12-26 08:58:53 +00:00
}
void *data = malloc(total_size);
2018-09-02 13:49:23 +00:00
if ( data == NULL )
{
WARN_LOG(SAVESTATE, "Failed to load state - could not malloc %d bytes", total_size) ;
gui_display_notification("Failed to load state - memory full", 2000);
2020-12-26 08:58:53 +00:00
if (f != nullptr)
std::fclose(f);
2020-12-26 08:58:53 +00:00
else
zipFile.Close();
return;
2018-09-02 13:49:23 +00:00
}
2020-12-26 08:58:53 +00:00
size_t read_size;
if (f == nullptr)
{
read_size = zipFile.Read(data, total_size);
zipFile.Close();
}
else
{
read_size = fread(data, 1, total_size, f) ;
std::fclose(f);
2020-12-26 08:58:53 +00:00
}
if (read_size != total_size)
{
WARN_LOG(SAVESTATE, "Failed to load state - I/O error");
gui_display_notification("Failed to load state - I/O error", 2000);
2021-07-05 17:44:08 +00:00
free(data);
return;
}
2018-09-02 13:49:23 +00:00
try {
Deserializer deser(data, total_size);
dc_loadstate(deser);
if (deser.size() != total_size)
WARN_LOG(SAVESTATE, "Savestate size %d but only %d bytes used", total_size, (int)deser.size());
} catch (const Deserializer::Exception& e) {
ERROR_LOG(SAVESTATE, "%s", e.what());
}
2018-10-29 14:11:34 +00:00
2021-07-05 17:44:08 +00:00
free(data);
EventManager::event(Event::LoadState);
INFO_LOG(SAVESTATE, "Loaded state from %s size %d", filename.c_str(), total_size) ;
2018-09-02 13:49:23 +00:00
}
2021-07-05 17:44:08 +00:00
#endif