metaforce/Runtime/CDvdFile.hpp

67 lines
2.3 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
2015-08-23 06:42:29 +00:00
#include <atomic>
2016-03-07 03:12:32 +00:00
#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
2019-04-07 05:14:48 +00:00
#include <unordered_map>
2016-03-07 03:12:32 +00:00
#include "Runtime/GCNTypes.hpp"
#include "Runtime/RetroTypes.hpp"
#include <athena/FileReader.hpp>
2021-04-10 08:42:06 +00:00
namespace metaforce {
2015-08-23 06:42:29 +00:00
2018-12-08 05:30:43 +00:00
enum class ESeekOrigin { Begin = 0, Cur = 1, End = 2 };
2015-08-23 06:42:29 +00:00
struct DVDFileInfo;
class IDvdRequest;
2015-08-23 06:42:29 +00:00
2018-12-08 05:30:43 +00:00
class CDvdFile {
friend class CResLoader;
friend class CFileDvdRequest;
static hecl::ProjectPath m_DvdRoot;
2019-03-09 08:58:27 +00:00
static std::unordered_map<std::string, std::string> m_caseInsensitiveMap;
2018-12-08 05:30:43 +00:00
static std::thread m_WorkerThread;
static std::mutex m_WorkerMutex;
static std::condition_variable m_WorkerCV;
static std::mutex m_WaitMutex;
static std::atomic_bool m_WorkerRun;
static std::vector<std::shared_ptr<IDvdRequest>> m_RequestQueue;
static void WorkerProc();
std::string x18_path;
std::shared_ptr<athena::io::FileReader> m_reader;
2016-03-07 03:12:32 +00:00
2019-03-09 08:58:27 +00:00
static hecl::ProjectPath ResolvePath(std::string_view path);
static void RecursiveBuildCaseInsensitiveMap(const hecl::ProjectPath& path, std::string::size_type prefixLen);
2015-08-23 06:42:29 +00:00
public:
2018-12-08 05:30:43 +00:00
static void Initialize(const hecl::ProjectPath& path);
static void Shutdown();
CDvdFile(std::string_view path)
: x18_path(path)
2019-03-09 08:58:27 +00:00
, m_reader(std::make_shared<athena::io::FileReader>(ResolvePath(path).getAbsolutePath())) {}
2018-12-08 05:30:43 +00:00
operator bool() const { return m_reader->isOpen(); }
void UpdateFilePos(int pos) { m_reader->seek(pos, athena::SeekOrigin::Begin); }
2019-03-09 08:58:27 +00:00
static bool FileExists(std::string_view path) { return ResolvePath(path).isFile(); }
2018-12-08 05:30:43 +00:00
void CloseFile() { m_reader->close(); }
std::shared_ptr<IDvdRequest> AsyncSeekRead(void* buf, u32 len, ESeekOrigin whence, int off,
std::function<void(u32)>&& cb = {});
u32 SyncSeekRead(void* buf, u32 len, ESeekOrigin whence, int offset) {
m_reader->seek(offset, athena::SeekOrigin(whence));
return m_reader->readBytesToBuf(buf, len);
}
std::shared_ptr<IDvdRequest> AsyncRead(void* buf, u32 len, std::function<void(u32)>&& cb = {}) {
return AsyncSeekRead(buf, len, ESeekOrigin::Cur, 0, std::move(cb));
}
u32 SyncRead(void* buf, u32 len) { return m_reader->readBytesToBuf(buf, len); }
u64 Length() const { return m_reader->length(); }
2018-12-08 05:30:43 +00:00
std::string_view GetPath() const { return x18_path; }
2015-08-23 06:42:29 +00:00
};
2021-04-10 08:42:06 +00:00
} // namespace metaforce