Core/Boot: Refactor storage of boot-to-savestate data into a separate class.

This commit is contained in:
Admiral H. Curtiss 2021-11-20 19:38:09 +01:00
parent d5b917a6c2
commit 83ad84061e
No known key found for this signature in database
GPG key ID: F051B4C4044F33FB
9 changed files with 126 additions and 53 deletions

View file

@ -549,8 +549,7 @@ static float GetRenderSurfaceScale(JNIEnv* env)
} }
static void Run(JNIEnv* env, const std::vector<std::string>& paths, bool riivolution, static void Run(JNIEnv* env, const std::vector<std::string>& paths, bool riivolution,
const std::optional<std::string>& savestate_path = {}, BootSessionData boot_session_data = BootSessionData())
bool delete_savestate = false)
{ {
ASSERT(!paths.empty()); ASSERT(!paths.empty());
__android_log_print(ANDROID_LOG_INFO, DOLPHIN_TAG, "Running : %s", paths[0].c_str()); __android_log_print(ANDROID_LOG_INFO, DOLPHIN_TAG, "Running : %s", paths[0].c_str());
@ -561,9 +560,8 @@ static void Run(JNIEnv* env, const std::vector<std::string>& paths, bool riivolu
s_have_wm_user_stop = false; s_have_wm_user_stop = false;
std::unique_ptr<BootParameters> boot = BootParameters::GenerateFromFile(paths, savestate_path); std::unique_ptr<BootParameters> boot =
if (boot) BootParameters::GenerateFromFile(paths, std::move(boot_session_data));
boot->delete_savestate = delete_savestate;
if (riivolution && std::holds_alternative<BootParameters::Disc>(boot->parameters)) if (riivolution && std::holds_alternative<BootParameters::Disc>(boot->parameters))
{ {
@ -638,8 +636,10 @@ Java_org_dolphinemu_dolphinemu_NativeLibrary_Run___3Ljava_lang_String_2ZLjava_la
JNIEnv* env, jclass, jobjectArray jPaths, jboolean jRiivolution, jstring jSavestate, JNIEnv* env, jclass, jobjectArray jPaths, jboolean jRiivolution, jstring jSavestate,
jboolean jDeleteSavestate) jboolean jDeleteSavestate)
{ {
Run(env, JStringArrayToVector(env, jPaths), jRiivolution, GetJString(env, jSavestate), DeleteSavestateAfterBoot delete_state =
jDeleteSavestate); jDeleteSavestate ? DeleteSavestateAfterBoot::Yes : DeleteSavestateAfterBoot::No;
Run(env, JStringArrayToVector(env, jPaths), jRiivolution,
BootSessionData(GetJString(env, jSavestate), delete_state));
} }
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ChangeDisc(JNIEnv* env, jclass, JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ChangeDisc(JNIEnv* env, jclass,

View file

@ -112,22 +112,53 @@ static std::vector<std::string> ReadM3UFile(const std::string& m3u_path,
return result; return result;
} }
BootParameters::BootParameters(Parameters&& parameters_, BootSessionData::BootSessionData()
const std::optional<std::string>& savestate_path_)
: parameters(std::move(parameters_)), savestate_path(savestate_path_)
{ {
} }
std::unique_ptr<BootParameters> BootSessionData::BootSessionData(std::optional<std::string> savestate_path,
BootParameters::GenerateFromFile(std::string boot_path, DeleteSavestateAfterBoot delete_savestate)
const std::optional<std::string>& savestate_path) : m_savestate_path(std::move(savestate_path)), m_delete_savestate(delete_savestate)
{ {
return GenerateFromFile(std::vector<std::string>{std::move(boot_path)}, savestate_path);
} }
std::unique_ptr<BootParameters> BootSessionData::BootSessionData(BootSessionData&& other) = default;
BootParameters::GenerateFromFile(std::vector<std::string> paths,
const std::optional<std::string>& savestate_path) BootSessionData& BootSessionData::operator=(BootSessionData&& other) = default;
BootSessionData::~BootSessionData() = default;
const std::optional<std::string>& BootSessionData::GetSavestatePath() const
{
return m_savestate_path;
}
DeleteSavestateAfterBoot BootSessionData::GetDeleteSavestate() const
{
return m_delete_savestate;
}
void BootSessionData::SetSavestateData(std::optional<std::string> savestate_path,
DeleteSavestateAfterBoot delete_savestate)
{
m_savestate_path = std::move(savestate_path);
m_delete_savestate = delete_savestate;
}
BootParameters::BootParameters(Parameters&& parameters_, BootSessionData boot_session_data_)
: parameters(std::move(parameters_)), boot_session_data(std::move(boot_session_data_))
{
}
std::unique_ptr<BootParameters> BootParameters::GenerateFromFile(std::string boot_path,
BootSessionData boot_session_data_)
{
return GenerateFromFile(std::vector<std::string>{std::move(boot_path)},
std::move(boot_session_data_));
}
std::unique_ptr<BootParameters> BootParameters::GenerateFromFile(std::vector<std::string> paths,
BootSessionData boot_session_data_)
{ {
ASSERT(!paths.empty()); ASSERT(!paths.empty());
@ -176,21 +207,21 @@ BootParameters::GenerateFromFile(std::vector<std::string> paths,
if (disc) if (disc)
{ {
return std::make_unique<BootParameters>(Disc{std::move(path), std::move(disc), paths}, return std::make_unique<BootParameters>(Disc{std::move(path), std::move(disc), paths},
savestate_path); std::move(boot_session_data_));
} }
if (extension == ".elf") if (extension == ".elf")
{ {
auto elf_reader = std::make_unique<ElfReader>(path); auto elf_reader = std::make_unique<ElfReader>(path);
return std::make_unique<BootParameters>(Executable{std::move(path), std::move(elf_reader)}, return std::make_unique<BootParameters>(Executable{std::move(path), std::move(elf_reader)},
savestate_path); std::move(boot_session_data_));
} }
if (extension == ".dol") if (extension == ".dol")
{ {
auto dol_reader = std::make_unique<DolReader>(path); auto dol_reader = std::make_unique<DolReader>(path);
return std::make_unique<BootParameters>(Executable{std::move(path), std::move(dol_reader)}, return std::make_unique<BootParameters>(Executable{std::move(path), std::move(dol_reader)},
savestate_path); std::move(boot_session_data_));
} }
if (is_drive) if (is_drive)
@ -209,13 +240,13 @@ BootParameters::GenerateFromFile(std::vector<std::string> paths,
} }
if (extension == ".dff") if (extension == ".dff")
return std::make_unique<BootParameters>(DFF{std::move(path)}, savestate_path); return std::make_unique<BootParameters>(DFF{std::move(path)}, std::move(boot_session_data_));
if (extension == ".wad") if (extension == ".wad")
{ {
std::unique_ptr<DiscIO::VolumeWAD> wad = DiscIO::CreateWAD(std::move(path)); std::unique_ptr<DiscIO::VolumeWAD> wad = DiscIO::CreateWAD(std::move(path));
if (wad) if (wad)
return std::make_unique<BootParameters>(std::move(*wad), savestate_path); return std::make_unique<BootParameters>(std::move(*wad), std::move(boot_session_data_));
} }
if (extension == ".json") if (extension == ".json")
@ -223,7 +254,7 @@ BootParameters::GenerateFromFile(std::vector<std::string> paths,
auto descriptor = DiscIO::ParseGameModDescriptorFile(path); auto descriptor = DiscIO::ParseGameModDescriptorFile(path);
if (descriptor) if (descriptor)
{ {
auto boot_params = GenerateFromFile(descriptor->base_file, savestate_path); auto boot_params = GenerateFromFile(descriptor->base_file, std::move(boot_session_data_));
if (!boot_params) if (!boot_params)
{ {
PanicAlertFmtT("Could not recognize file {0}", descriptor->base_file); PanicAlertFmtT("Could not recognize file {0}", descriptor->base_file);

View file

@ -34,6 +34,34 @@ struct RegionSetting
class BootExecutableReader; class BootExecutableReader;
enum class DeleteSavestateAfterBoot : u8
{
No,
Yes
};
class BootSessionData
{
public:
BootSessionData();
BootSessionData(std::optional<std::string> savestate_path,
DeleteSavestateAfterBoot delete_savestate);
BootSessionData(const BootSessionData& other) = delete;
BootSessionData(BootSessionData&& other);
BootSessionData& operator=(const BootSessionData& other) = delete;
BootSessionData& operator=(BootSessionData&& other);
~BootSessionData();
const std::optional<std::string>& GetSavestatePath() const;
DeleteSavestateAfterBoot GetDeleteSavestate() const;
void SetSavestateData(std::optional<std::string> savestate_path,
DeleteSavestateAfterBoot delete_savestate);
private:
std::optional<std::string> m_savestate_path;
DeleteSavestateAfterBoot m_delete_savestate = DeleteSavestateAfterBoot::No;
};
struct BootParameters struct BootParameters
{ {
struct Disc struct Disc
@ -70,18 +98,17 @@ struct BootParameters
}; };
static std::unique_ptr<BootParameters> static std::unique_ptr<BootParameters>
GenerateFromFile(std::string boot_path, const std::optional<std::string>& savestate_path = {}); GenerateFromFile(std::string boot_path, BootSessionData boot_session_data_ = BootSessionData());
static std::unique_ptr<BootParameters> static std::unique_ptr<BootParameters>
GenerateFromFile(std::vector<std::string> paths, GenerateFromFile(std::vector<std::string> paths,
const std::optional<std::string>& savestate_path = {}); BootSessionData boot_session_data_ = BootSessionData());
using Parameters = std::variant<Disc, Executable, DiscIO::VolumeWAD, NANDTitle, IPL, DFF>; using Parameters = std::variant<Disc, Executable, DiscIO::VolumeWAD, NANDTitle, IPL, DFF>;
BootParameters(Parameters&& parameters_, const std::optional<std::string>& savestate_path_ = {}); BootParameters(Parameters&& parameters_, BootSessionData boot_session_data_ = BootSessionData());
Parameters parameters; Parameters parameters;
std::vector<DiscIO::Riivolution::Patch> riivolution_patches; std::vector<DiscIO::Riivolution::Patch> riivolution_patches;
std::optional<std::string> savestate_path; BootSessionData boot_session_data;
bool delete_savestate = false;
}; };
class CBoot class CBoot

View file

@ -450,7 +450,7 @@ bool BootCore(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)
std::make_unique<BootParameters>( std::make_unique<BootParameters>(
BootParameters::IPL{StartUp.m_region, BootParameters::IPL{StartUp.m_region,
std::move(std::get<BootParameters::Disc>(boot->parameters))}, std::move(std::get<BootParameters::Disc>(boot->parameters))},
boot->savestate_path), std::move(boot->boot_session_data)),
wsi); wsi);
} }
return Core::Init(std::move(boot), wsi); return Core::Init(std::move(boot), wsi);

View file

@ -479,8 +479,10 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
Keyboard::LoadConfig(); Keyboard::LoadConfig();
} }
const std::optional<std::string> savestate_path = boot->savestate_path; BootSessionData boot_session_data = std::move(boot->boot_session_data);
const bool delete_savestate = boot->delete_savestate; const std::optional<std::string>& savestate_path = boot_session_data.GetSavestatePath();
const bool delete_savestate =
boot_session_data.GetDeleteSavestate() == DeleteSavestateAfterBoot::Yes;
// Load and Init Wiimotes - only if we are booting in Wii mode // Load and Init Wiimotes - only if we are booting in Wii mode
bool init_wiimotes = false; bool init_wiimotes = false;

View file

@ -187,7 +187,8 @@ int main(int argc, char* argv[])
const std::list<std::string> paths_list = options.all("exec"); const std::list<std::string> paths_list = options.all("exec");
const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)), const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)),
std::make_move_iterator(std::end(paths_list))}; std::make_move_iterator(std::end(paths_list))};
boot = BootParameters::GenerateFromFile(paths, save_state_path); boot = BootParameters::GenerateFromFile(
paths, BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
game_specified = true; game_specified = true;
} }
else if (options.is_set("nand_title")) else if (options.is_set("nand_title"))
@ -204,7 +205,8 @@ int main(int argc, char* argv[])
} }
else if (args.size()) else if (args.size())
{ {
boot = BootParameters::GenerateFromFile(args.front(), save_state_path); boot = BootParameters::GenerateFromFile(
args.front(), BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
args.erase(args.begin()); args.erase(args.begin());
game_specified = true; game_specified = true;
} }

View file

@ -197,7 +197,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
const std::list<std::string> paths_list = options.all("exec"); const std::list<std::string> paths_list = options.all("exec");
const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)), const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)),
std::make_move_iterator(std::end(paths_list))}; std::make_move_iterator(std::end(paths_list))};
boot = BootParameters::GenerateFromFile(paths, save_state_path); boot = BootParameters::GenerateFromFile(
paths, BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
game_specified = true; game_specified = true;
} }
else if (options.is_set("nand_title")) else if (options.is_set("nand_title"))
@ -216,7 +217,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
} }
else if (!args.empty()) else if (!args.empty())
{ {
boot = BootParameters::GenerateFromFile(args.front(), save_state_path); boot = BootParameters::GenerateFromFile(
args.front(), BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
game_specified = true; game_specified = true;
} }

View file

@ -244,8 +244,13 @@ MainWindow::MainWindow(std::unique_ptr<BootParameters> boot_parameters,
if (!movie_path.empty()) if (!movie_path.empty())
{ {
if (Movie::PlayInput(movie_path, &m_pending_boot->savestate_path)) std::optional<std::string> savestate_path;
if (Movie::PlayInput(movie_path, &savestate_path))
{
m_pending_boot->boot_session_data.SetSavestateData(std::move(savestate_path),
DeleteSavestateAfterBoot::No);
emit RecordingStatusChanged(true); emit RecordingStatusChanged(true);
}
} }
} }
@ -768,14 +773,16 @@ void MainWindow::Play(const std::optional<std::string>& savestate_path)
std::shared_ptr<const UICommon::GameFile> selection = m_game_list->GetSelectedGame(); std::shared_ptr<const UICommon::GameFile> selection = m_game_list->GetSelectedGame();
if (selection) if (selection)
{ {
StartGame(selection->GetFilePath(), ScanForSecondDisc::Yes, savestate_path); StartGame(selection->GetFilePath(), ScanForSecondDisc::Yes,
std::make_unique<BootSessionData>(savestate_path, DeleteSavestateAfterBoot::No));
} }
else else
{ {
const QString default_path = QString::fromStdString(Config::Get(Config::MAIN_DEFAULT_ISO)); const QString default_path = QString::fromStdString(Config::Get(Config::MAIN_DEFAULT_ISO));
if (!default_path.isEmpty() && QFile::exists(default_path)) if (!default_path.isEmpty() && QFile::exists(default_path))
{ {
StartGame(default_path, ScanForSecondDisc::Yes, savestate_path); StartGame(default_path, ScanForSecondDisc::Yes,
std::make_unique<BootSessionData>(savestate_path, DeleteSavestateAfterBoot::No));
} }
else else
{ {
@ -978,7 +985,7 @@ void MainWindow::ScreenShot()
} }
void MainWindow::ScanForSecondDiscAndStartGame(const UICommon::GameFile& game, void MainWindow::ScanForSecondDiscAndStartGame(const UICommon::GameFile& game,
const std::optional<std::string>& savestate_path) std::unique_ptr<BootSessionData> boot_session_data)
{ {
auto second_game = m_game_list->FindSecondDisc(game); auto second_game = m_game_list->FindSecondDisc(game);
@ -986,35 +993,37 @@ void MainWindow::ScanForSecondDiscAndStartGame(const UICommon::GameFile& game,
if (second_game != nullptr) if (second_game != nullptr)
paths.push_back(second_game->GetFilePath()); paths.push_back(second_game->GetFilePath());
StartGame(paths, savestate_path); StartGame(paths, std::move(boot_session_data));
} }
void MainWindow::StartGame(const QString& path, ScanForSecondDisc scan, void MainWindow::StartGame(const QString& path, ScanForSecondDisc scan,
const std::optional<std::string>& savestate_path) std::unique_ptr<BootSessionData> boot_session_data)
{ {
StartGame(path.toStdString(), scan, savestate_path); StartGame(path.toStdString(), scan, std::move(boot_session_data));
} }
void MainWindow::StartGame(const std::string& path, ScanForSecondDisc scan, void MainWindow::StartGame(const std::string& path, ScanForSecondDisc scan,
const std::optional<std::string>& savestate_path) std::unique_ptr<BootSessionData> boot_session_data)
{ {
if (scan == ScanForSecondDisc::Yes) if (scan == ScanForSecondDisc::Yes)
{ {
std::shared_ptr<const UICommon::GameFile> game = m_game_list->FindGame(path); std::shared_ptr<const UICommon::GameFile> game = m_game_list->FindGame(path);
if (game != nullptr) if (game != nullptr)
{ {
ScanForSecondDiscAndStartGame(*game, savestate_path); ScanForSecondDiscAndStartGame(*game, std::move(boot_session_data));
return; return;
} }
} }
StartGame(BootParameters::GenerateFromFile(path, savestate_path)); StartGame(BootParameters::GenerateFromFile(
path, boot_session_data ? std::move(*boot_session_data) : BootSessionData()));
} }
void MainWindow::StartGame(const std::vector<std::string>& paths, void MainWindow::StartGame(const std::vector<std::string>& paths,
const std::optional<std::string>& savestate_path) std::unique_ptr<BootSessionData> boot_session_data)
{ {
StartGame(BootParameters::GenerateFromFile(paths, savestate_path)); StartGame(BootParameters::GenerateFromFile(
paths, boot_session_data ? std::move(*boot_session_data) : BootSessionData()));
} }
void MainWindow::StartGame(std::unique_ptr<BootParameters>&& parameters) void MainWindow::StartGame(std::unique_ptr<BootParameters>&& parameters)
@ -1818,8 +1827,7 @@ void MainWindow::ShowRiivolutionBootWidget(const UICommon::GameFile& game)
std::vector<std::string> paths = {game.GetFilePath()}; std::vector<std::string> paths = {game.GetFilePath()};
if (second_game != nullptr) if (second_game != nullptr)
paths.push_back(second_game->GetFilePath()); paths.push_back(second_game->GetFilePath());
std::unique_ptr<BootParameters> boot_params = std::unique_ptr<BootParameters> boot_params = BootParameters::GenerateFromFile(paths);
BootParameters::GenerateFromFile(paths, std::nullopt);
if (!boot_params) if (!boot_params)
return; return;
if (!std::holds_alternative<BootParameters::Disc>(boot_params->parameters)) if (!std::holds_alternative<BootParameters::Disc>(boot_params->parameters))

View file

@ -15,6 +15,7 @@ class QStackedWidget;
class QString; class QString;
class BreakpointWidget; class BreakpointWidget;
class BootSessionData;
struct BootParameters; struct BootParameters;
class CheatsManager; class CheatsManager;
class CodeWidget; class CodeWidget;
@ -132,13 +133,13 @@ private:
}; };
void ScanForSecondDiscAndStartGame(const UICommon::GameFile& game, void ScanForSecondDiscAndStartGame(const UICommon::GameFile& game,
const std::optional<std::string>& savestate_path = {}); std::unique_ptr<BootSessionData> boot_session_data = nullptr);
void StartGame(const QString& path, ScanForSecondDisc scan, void StartGame(const QString& path, ScanForSecondDisc scan,
const std::optional<std::string>& savestate_path = {}); std::unique_ptr<BootSessionData> boot_session_data = nullptr);
void StartGame(const std::string& path, ScanForSecondDisc scan, void StartGame(const std::string& path, ScanForSecondDisc scan,
const std::optional<std::string>& savestate_path = {}); std::unique_ptr<BootSessionData> boot_session_data = nullptr);
void StartGame(const std::vector<std::string>& paths, void StartGame(const std::vector<std::string>& paths,
const std::optional<std::string>& savestate_path = {}); std::unique_ptr<BootSessionData> boot_session_data = nullptr);
void StartGame(std::unique_ptr<BootParameters>&& parameters); void StartGame(std::unique_ptr<BootParameters>&& parameters);
void ShowRenderWidget(); void ShowRenderWidget();
void HideRenderWidget(bool reinit = true, bool is_exit = false); void HideRenderWidget(bool reinit = true, bool is_exit = false);