2016-03-07 03:12:32 +00:00
|
|
|
#include "CDvdFile.hpp"
|
|
|
|
#include "CDvdRequest.hpp"
|
2017-11-19 07:10:54 +00:00
|
|
|
#include "CStopwatch.hpp"
|
2016-03-07 03:12:32 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace urde {
|
2016-03-07 03:12:32 +00:00
|
|
|
|
|
|
|
hecl::ProjectPath CDvdFile::m_DvdRoot;
|
2019-03-09 08:58:27 +00:00
|
|
|
std::unordered_map<std::string, std::string> CDvdFile::m_caseInsensitiveMap;
|
2016-03-07 03:12:32 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
class CFileDvdRequest : public IDvdRequest {
|
|
|
|
std::shared_ptr<athena::io::FileReader> m_reader;
|
|
|
|
void* m_buf;
|
|
|
|
u32 m_len;
|
|
|
|
ESeekOrigin m_whence;
|
|
|
|
int m_offset;
|
|
|
|
std::atomic_bool m_cancel = {false};
|
|
|
|
std::atomic_bool m_complete = {false};
|
|
|
|
std::function<void(u32)> m_callback;
|
|
|
|
|
2016-03-07 03:12:32 +00:00
|
|
|
public:
|
2018-12-08 05:30:43 +00:00
|
|
|
~CFileDvdRequest() { PostCancelRequest(); }
|
2016-03-07 03:12:32 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void WaitUntilComplete() {
|
|
|
|
while (!m_complete.load() && !m_cancel.load()) {
|
|
|
|
std::unique_lock<std::mutex> lk(CDvdFile::m_WaitMutex);
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
bool IsComplete() { return m_complete.load(); }
|
|
|
|
void PostCancelRequest() {
|
|
|
|
std::unique_lock<std::mutex> waitlk(CDvdFile::m_WaitMutex);
|
|
|
|
m_cancel.store(true);
|
|
|
|
}
|
2016-03-07 03:12:32 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
EMediaType GetMediaType() const { return EMediaType::File; }
|
2016-03-07 03:12:32 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CFileDvdRequest(CDvdFile& file, void* buf, u32 len, ESeekOrigin whence, int off, std::function<void(u32)>&& cb)
|
|
|
|
: m_reader(file.m_reader), m_buf(buf), m_len(len), m_whence(whence), m_offset(off), m_callback(std::move(cb)) {}
|
2016-03-07 03:12:32 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void DoRequest() {
|
|
|
|
if (m_cancel.load())
|
|
|
|
return;
|
|
|
|
u32 readLen;
|
|
|
|
if (m_whence == ESeekOrigin::Cur && m_offset == 0) {
|
|
|
|
readLen = m_reader->readBytesToBuf(m_buf, m_len);
|
|
|
|
} else {
|
|
|
|
m_reader->seek(m_offset, athena::SeekOrigin(m_whence));
|
|
|
|
readLen = m_reader->readBytesToBuf(m_buf, m_len);
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
if (m_callback)
|
|
|
|
m_callback(readLen);
|
|
|
|
m_complete.store(true);
|
|
|
|
}
|
2016-03-07 03:12:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::thread CDvdFile::m_WorkerThread;
|
|
|
|
std::mutex CDvdFile::m_WorkerMutex;
|
|
|
|
std::condition_variable CDvdFile::m_WorkerCV;
|
2016-03-07 22:48:54 +00:00
|
|
|
std::mutex CDvdFile::m_WaitMutex;
|
2018-06-02 00:03:31 +00:00
|
|
|
std::atomic_bool CDvdFile::m_WorkerRun = {false};
|
2016-03-07 03:12:32 +00:00
|
|
|
std::vector<std::shared_ptr<IDvdRequest>> CDvdFile::m_RequestQueue;
|
2018-12-08 05:30:43 +00:00
|
|
|
void CDvdFile::WorkerProc() {
|
|
|
|
logvisor::RegisterThreadName("CDvdFile");
|
|
|
|
while (m_WorkerRun.load()) {
|
|
|
|
std::unique_lock<std::mutex> lk(CDvdFile::m_WorkerMutex);
|
|
|
|
while (CDvdFile::m_RequestQueue.size()) {
|
|
|
|
std::vector<std::shared_ptr<IDvdRequest>> swapQueue;
|
|
|
|
swapQueue.swap(CDvdFile::m_RequestQueue);
|
|
|
|
lk.unlock();
|
|
|
|
std::unique_lock<std::mutex> waitlk(CDvdFile::m_WaitMutex);
|
|
|
|
for (std::shared_ptr<IDvdRequest>& req : swapQueue) {
|
|
|
|
CFileDvdRequest& concreteReq = static_cast<CFileDvdRequest&>(*req);
|
|
|
|
concreteReq.DoRequest();
|
|
|
|
}
|
|
|
|
waitlk.unlock();
|
|
|
|
swapQueue.clear();
|
|
|
|
lk.lock();
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
if (!m_WorkerRun.load())
|
|
|
|
break;
|
|
|
|
m_WorkerCV.wait(lk);
|
|
|
|
}
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
std::shared_ptr<IDvdRequest> CDvdFile::AsyncSeekRead(void* buf, u32 len, ESeekOrigin whence, int off,
|
|
|
|
std::function<void(u32)>&& cb) {
|
|
|
|
std::shared_ptr<IDvdRequest> ret = std::make_shared<CFileDvdRequest>(*this, buf, len, whence, off, std::move(cb));
|
|
|
|
std::unique_lock<std::mutex> lk(CDvdFile::m_WorkerMutex);
|
|
|
|
m_RequestQueue.emplace_back(ret);
|
|
|
|
lk.unlock();
|
|
|
|
m_WorkerCV.notify_one();
|
|
|
|
return ret;
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 08:58:27 +00:00
|
|
|
hecl::ProjectPath CDvdFile::ResolvePath(std::string_view path) {
|
|
|
|
auto start = path.begin();
|
|
|
|
while (*start == '/') ++start;
|
|
|
|
std::string lowerChStr(start, path.end());
|
|
|
|
std::transform(lowerChStr.begin(), lowerChStr.end(), lowerChStr.begin(), ::tolower);
|
|
|
|
auto search = m_caseInsensitiveMap.find(lowerChStr);
|
|
|
|
if (search == m_caseInsensitiveMap.end())
|
|
|
|
return {};
|
|
|
|
return hecl::ProjectPath(m_DvdRoot, search->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDvdFile::RecursiveBuildCaseInsensitiveMap(const hecl::ProjectPath& path, std::string::size_type prefixLen) {
|
|
|
|
for (const auto& p : path.enumerateDir()) {
|
|
|
|
if (p.m_isDir) {
|
|
|
|
RecursiveBuildCaseInsensitiveMap(hecl::ProjectPath(path, p.m_name), prefixLen);
|
|
|
|
} else {
|
|
|
|
hecl::ProjectPath ch(path, p.m_name);
|
|
|
|
std::string chStr(ch.getAbsolutePathUTF8().begin() + prefixLen, ch.getAbsolutePathUTF8().end());
|
|
|
|
std::string lowerChStr(chStr);
|
|
|
|
std::transform(lowerChStr.begin(), lowerChStr.end(), lowerChStr.begin(), ::tolower);
|
|
|
|
m_caseInsensitiveMap[lowerChStr] = chStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CDvdFile::Initialize(const hecl::ProjectPath& path) {
|
|
|
|
m_DvdRoot = path;
|
2019-03-09 08:58:27 +00:00
|
|
|
RecursiveBuildCaseInsensitiveMap(path, path.getAbsolutePathUTF8().length() + 1);
|
2018-12-08 05:30:43 +00:00
|
|
|
if (m_WorkerRun.load())
|
|
|
|
return;
|
|
|
|
m_WorkerRun.store(true);
|
|
|
|
m_WorkerThread = std::thread(WorkerProc);
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CDvdFile::Shutdown() {
|
|
|
|
if (!m_WorkerRun.load())
|
|
|
|
return;
|
|
|
|
m_WorkerRun.store(false);
|
|
|
|
m_WorkerCV.notify_one();
|
|
|
|
if (m_WorkerThread.joinable())
|
|
|
|
m_WorkerThread.join();
|
|
|
|
m_RequestQueue.clear();
|
2016-03-07 03:12:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace urde
|