2019-09-14 10:28:47 +00:00
|
|
|
#include "host_interface.h"
|
|
|
|
#include "YBaseLib/ByteStream.h"
|
2019-09-14 11:54:46 +00:00
|
|
|
#include "YBaseLib/Log.h"
|
2019-10-10 16:20:21 +00:00
|
|
|
#include "common/audio_stream.h"
|
2019-09-14 10:28:47 +00:00
|
|
|
#include "system.h"
|
2019-09-14 11:54:46 +00:00
|
|
|
Log_SetChannel(HostInterface);
|
2019-09-14 10:28:47 +00:00
|
|
|
|
|
|
|
HostInterface::HostInterface() = default;
|
|
|
|
|
|
|
|
HostInterface::~HostInterface() = default;
|
|
|
|
|
2019-09-30 04:22:30 +00:00
|
|
|
bool HostInterface::InitializeSystem(const char* filename, const char* exp1_filename)
|
2019-09-14 11:54:46 +00:00
|
|
|
{
|
2019-10-04 05:00:32 +00:00
|
|
|
Settings settings;
|
2019-10-27 06:45:23 +00:00
|
|
|
settings.memory_card_a_filename = "memory_card_a.mcd";
|
2019-10-04 05:00:32 +00:00
|
|
|
|
|
|
|
m_system = std::make_unique<System>(this, settings);
|
2019-09-14 11:54:46 +00:00
|
|
|
if (!m_system->Initialize())
|
|
|
|
{
|
|
|
|
m_system.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_system->Reset();
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
const StaticString filename_str(filename);
|
2019-10-04 03:38:49 +00:00
|
|
|
if (filename_str.EndsWith(".psexe", false) || filename_str.EndsWith(".exe", false))
|
2019-09-14 11:54:46 +00:00
|
|
|
{
|
2019-09-20 10:14:00 +00:00
|
|
|
Log_InfoPrintf("Sideloading EXE file '%s'", filename);
|
2019-09-14 11:54:46 +00:00
|
|
|
if (!m_system->LoadEXE(filename))
|
|
|
|
{
|
2019-09-20 10:14:00 +00:00
|
|
|
Log_ErrorPrintf("Failed to load EXE file '%s'", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("Inserting CDROM from image file '%s'", filename);
|
|
|
|
if (!m_system->InsertMedia(filename))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to insert media '%s'", filename);
|
2019-09-14 11:54:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 15:28:00 +00:00
|
|
|
if (exp1_filename)
|
|
|
|
m_system->SetExpansionROM(exp1_filename);
|
|
|
|
|
2019-09-14 11:54:46 +00:00
|
|
|
// Resume execution.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:28:47 +00:00
|
|
|
bool HostInterface::LoadState(const char* filename)
|
|
|
|
{
|
|
|
|
ByteStream* stream;
|
|
|
|
if (!ByteStream_OpenFileStream(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED, &stream))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ReportMessage(SmallString::FromFormat("Loading state from %s...", filename));
|
|
|
|
|
|
|
|
const bool result = m_system->LoadState(stream);
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
ReportMessage(SmallString::FromFormat("Loading state from %s failed. Resetting.", filename));
|
|
|
|
m_system->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->Release();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HostInterface::SaveState(const char* filename)
|
|
|
|
{
|
|
|
|
ByteStream* stream;
|
|
|
|
if (!ByteStream_OpenFileStream(filename,
|
|
|
|
BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE | BYTESTREAM_OPEN_TRUNCATE |
|
|
|
|
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED,
|
|
|
|
&stream))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool result = m_system->SaveState(stream);
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
ReportMessage(SmallString::FromFormat("Saving state to %s failed.", filename));
|
|
|
|
stream->Discard();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReportMessage(SmallString::FromFormat("State saved to %s.", filename));
|
|
|
|
stream->Commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->Release();
|
|
|
|
return result;
|
|
|
|
}
|