DEV9: Separate HddCreate code from Wx

Also disable auto generating an image on boot if one is not found
This commit is contained in:
TheLastRar 2022-03-22 21:54:42 +00:00 committed by refractionpcsx2
parent 5db9a738c5
commit e1fb84d6a3
9 changed files with 197 additions and 89 deletions

View File

@ -401,9 +401,11 @@ if(NOT PCSX2_CORE)
list(APPEND pcsx2DEV9Sources
DEV9/ConfigUI.cpp
DEV9/DEV9Config.cpp
DEV9/ATA/HddCreateWx.cpp
)
list(APPEND pcsx2DEV9Sources
DEV9/DEV9Config.h
DEV9/ATA/HddCreateWx.h
)
endif()

View File

@ -17,7 +17,9 @@
#include "ATA.h"
#include "DEV9/DEV9.h"
#include "HddCreate.h"
#ifndef PCSX2_CORE
#include "HddCreateWx.h"
#endif
ATA::ATA()
{
@ -36,13 +38,17 @@ int ATA::Open(fs::path hddPath)
//Open File
if (!fs::exists(hddPath))
{
HddCreate hddCreator;
#ifndef PCSX2_CORE
HddCreateWx hddCreator;
hddCreator.filePath = hddPath;
hddCreator.neededSize = ((u64)EmuConfig.DEV9.HddSizeSectors) * 512;
hddCreator.Start();
if (hddCreator.errored)
return -1;
#else
return -1;
#endif
}
hddImage = fs::fstream(hddPath, std::ios::in | std::ios::out | std::ios::binary);

View File

@ -16,71 +16,14 @@
#include "PrecompiledHeader.h"
#include <fstream>
#include <fmt/format.h>
#include "HddCreate.h"
void HddCreate::Start()
{
#ifndef PCSX2_CORE
//This can be called from the EE Core thread
//ensure that UI creation/deletaion is done on main thread
if (!wxIsMainThread())
{
wxTheApp->CallAfter([&] { Start(); });
//Block until done
std::unique_lock competedLock(completedMutex);
completedCV.wait(competedLock, [&] { return completed; });
return;
}
#endif
int reqMiB = (neededSize + ((1024 * 1024) - 1)) / (1024 * 1024);
#ifndef PCSX2_CORE
//This creates a modeless dialog
progressDialog = new wxProgressDialog(_("Creating HDD file"), _("Creating HDD file"), reqMiB, nullptr, wxPD_APP_MODAL | wxPD_AUTO_HIDE | wxPD_CAN_ABORT | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME);
#endif
fileThread = std::thread(&HddCreate::WriteImage, this, filePath, neededSize);
//This code was written for a modal dialog, however wxProgressDialog is modeless only
//The idea was block here in a ShowModal() call, and have the worker thread update the UI
//via CallAfter()
//Instead, loop here to update UI
wxString msg;
int currentSize;
while ((currentSize = written.load()) != reqMiB && !errored.load())
{
msg.Printf(_("%i / %i MiB"), written.load(), reqMiB);
#ifndef PCSX2_CORE
if (!progressDialog->Update(currentSize, msg))
canceled.store(true);
#endif
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
fileThread.join();
if (errored.load())
{
#ifndef PCSX2_CORE
wxMessageDialog dialog(nullptr, _("Failed to create HDD file"), _("Info"), wxOK);
dialog.ShowModal();
#endif
}
#ifndef PCSX2_CORE
delete progressDialog;
#endif
//Signal calling thread to resume
{
std::lock_guard ioSignallock(completedMutex);
completed = true;
}
completedCV.notify_all();
Init();
WriteImage(filePath, neededSize);
Cleanup();
}
void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
@ -90,6 +33,7 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
if (fs::exists(hddPath))
{
errored.store(true);
SetError();
return;
}
@ -98,6 +42,7 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
if (newImage.fail())
{
errored.store(true);
SetError();
return;
}
@ -111,6 +56,7 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
{
newImage.close();
fs::remove(filePath);
errored.store(true);
SetError();
return;
}
@ -132,6 +78,7 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
{
newImage.close();
fs::remove(filePath);
errored.store(true);
SetError();
return;
}
@ -145,6 +92,7 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
{
newImage.close();
fs::remove(filePath);
errored.store(true);
SetError();
return;
}
@ -154,12 +102,13 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastUpdate).count() >= 100 || (iMiB + 1) == reqMiB)
{
lastUpdate = now;
SetFileProgress(iMiB + 1);
SetFileProgress(newImage.tellp());
}
if (canceled.load())
{
newImage.close();
fs::remove(filePath);
errored.store(true);
SetError();
return;
}
@ -168,12 +117,17 @@ void HddCreate::WriteImage(fs::path hddPath, u64 reqSizeBytes)
newImage.close();
}
void HddCreate::SetFileProgress(int currentSize)
void HddCreate::SetFileProgress(u64 currentSize)
{
written.store(currentSize);
Console.WriteLn(fmt::format("{} / {} Bytes", currentSize, neededSize).c_str());
}
void HddCreate::SetError()
{
errored.store(true);
Console.WriteLn("Failed to create HDD file");
}
void HddCreate::SetCanceled()
{
canceled.store(true);
}

View File

@ -15,14 +15,7 @@
#pragma once
#ifndef PCSX2_CORE
#include <wx/progdlg.h>
#endif
#include <string>
#include <thread>
#include <atomic>
#include <condition_variable>
#include <chrono>
class HddCreate
@ -34,26 +27,24 @@ public:
std::atomic_bool errored{false};
private:
#ifndef PCSX2_CORE
wxProgressDialog* progressDialog;
#endif
std::atomic_int written{0};
std::thread fileThread;
std::atomic_bool canceled{false};
std::mutex completedMutex;
std::condition_variable completedCV;
bool completed = false;
std::chrono::steady_clock::time_point lastUpdate;
public:
HddCreate(){};
void Start();
virtual ~HddCreate(){};
protected:
virtual void Init(){};
virtual void Cleanup(){};
virtual void SetFileProgress(u64 currentSize);
virtual void SetError();
void SetCanceled();
private:
void SetFileProgress(int currentSize);
void SetError();
void WriteImage(fs::path hddPath, u64 reqSizeBytes);
};

View File

@ -0,0 +1,102 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "HddCreateWx.h"
void HddCreateWx::Init()
{
//This can be called from the EE Core thread
//ensure that UI creation/deletaion is done on main thread
//Also block calling thread untill ui is ready
if (!wxIsMainThread())
{
dialogReady = false;
wxTheApp->CallAfter([&] { Init(); });
//Block until done
std::unique_lock loadLock(dialogMutex);
dialogCV.wait(loadLock, [&] { return dialogReady; });
return;
}
reqMiB = (neededSize + ((1024 * 1024) - 1)) / (1024 * 1024);
//This creates a modeless dialog
progressDialog = new wxProgressDialog(_("Creating HDD file"), _("Creating HDD file"), reqMiB, nullptr, wxPD_APP_MODAL | wxPD_AUTO_HIDE | wxPD_CAN_ABORT | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME);
{
std::lock_guard dialogLock1(dialogMutex);
dialogReady = true;
}
dialogCV.notify_all();
}
void HddCreateWx::SetFileProgress(u64 currentSize)
{
if (!wxIsMainThread())
{
wxTheApp->CallAfter([&, currentSize] { SetFileProgress(currentSize); });
return;
}
wxString msg;
const int writtenMB = (currentSize + ((1024 * 1024) - 1)) / (1024 * 1024);
msg.Printf(_("%i / %i MiB"), writtenMB, reqMiB);
if (!progressDialog->Update(writtenMB, msg))
SetCanceled();
}
void HddCreateWx::SetError()
{
if (!wxIsMainThread())
{
dialogReady = false;
wxTheApp->CallAfter([&] { SetError(); });
//Block until done
std::unique_lock loadLock(dialogMutex);
dialogCV.wait(loadLock, [&] { return dialogReady; });
return;
}
wxMessageDialog dialog(nullptr, _("Failed to create HDD file"), _("Info"), wxOK);
dialog.ShowModal();
{
std::lock_guard dialogLock1(dialogMutex);
dialogReady = true;
}
dialogCV.notify_all();
}
void HddCreateWx::Cleanup()
{
if (!wxIsMainThread())
{
dialogReady = false;
wxTheApp->CallAfter([&] { Cleanup(); });
//Block until done
std::unique_lock loadLock(dialogMutex);
dialogCV.wait(loadLock, [&] { return dialogReady; });
return;
}
delete progressDialog;
{
std::lock_guard dialogLock1(dialogMutex);
dialogReady = true;
}
dialogCV.notify_all();
}

View File

@ -0,0 +1,45 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2020 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <wx/progdlg.h>
#include <mutex>
#include <condition_variable>
#include "DEV9/ATA/HddCreate.h"
class HddCreateWx : public HddCreate
{
public:
HddCreateWx(){};
virtual ~HddCreateWx(){};
private:
wxProgressDialog* progressDialog;
std::mutex dialogMutex;
std::condition_variable dialogCV;
bool dialogReady = false;
int reqMiB;
protected:
virtual void Init();
virtual void Cleanup();
virtual void SetFileProgress(u64 currentSize);
virtual void SetError();
};

View File

@ -38,7 +38,7 @@
#include "Win32/tap.h"
#endif
#include "ATA/HddCreate.h"
#include "ATA/HddCreateWx.h"
using PacketReader::IP::IP_Address;
@ -397,7 +397,7 @@ void DEV9configure()
if (g_Conf->EmuOptions.DEV9.HddEnable && !fs::exists(hddPath))
{
HddCreate hddCreator;
HddCreateWx hddCreator;
hddCreator.filePath = hddPath;
hddCreator.neededSize = ((u64)g_Conf->EmuOptions.DEV9.HddSizeSectors) * 512;
hddCreator.Start();

View File

@ -285,6 +285,7 @@
<ClCompile Include="DEV9\ATA\ATA_State.cpp" />
<ClCompile Include="DEV9\ATA\ATA_Transfer.cpp" />
<ClCompile Include="DEV9\ATA\HddCreate.cpp" />
<ClCompile Include="DEV9\ATA\HddCreateWx.cpp" />
<ClCompile Include="DEV9\ConfigUI.cpp" />
<ClCompile Include="DEV9\DEV9Config.cpp" />
<ClCompile Include="DEV9\DEV9.cpp" />
@ -731,6 +732,7 @@
<ClInclude Include="DebugTools\SymbolMap.h" />
<ClInclude Include="DEV9\ATA\ATA.h" />
<ClInclude Include="DEV9\ATA\HddCreate.h" />
<ClInclude Include="DEV9\ATA\HddCreateWx.h" />
<ClInclude Include="DEV9\DEV9Config.h" />
<ClInclude Include="DEV9\DEV9.h" />
<ClInclude Include="DEV9\InternalServers\DHCP_Server.h" />

View File

@ -1199,6 +1199,9 @@
<ClCompile Include="DEV9\ATA\HddCreate.cpp">
<Filter>System\Ps2\DEV9\ATA</Filter>
</ClCompile>
<ClCompile Include="DEV9\ATA\HddCreateWx.cpp">
<Filter>System\Ps2\DEV9\ATA</Filter>
</ClCompile>
<ClCompile Include="DEV9\ConfigUI.cpp">
<Filter>System\Ps2\DEV9</Filter>
</ClCompile>
@ -2266,6 +2269,9 @@
<ClInclude Include="DEV9\ATA\HddCreate.h">
<Filter>System\Ps2\DEV9\ATA</Filter>
</ClInclude>
<ClInclude Include="DEV9\ATA\HddCreateWx.h">
<Filter>System\Ps2\DEV9\ATA</Filter>
</ClInclude>
<ClInclude Include="DEV9\DEV9Config.h">
<Filter>System\Ps2\DEV9</Filter>
</ClInclude>