metaforce/Runtime/CDvdFile.hpp

80 lines
2.1 KiB
C++
Raw Normal View History

2018-10-06 20:42:33 -07:00
#pragma once
2015-08-22 23:42:29 -07:00
#include "RetroTypes.hpp"
#include "athena/FileReader.hpp"
2015-08-22 23:42:29 -07:00
2016-03-06 19:12:32 -08:00
#include <thread>
#include <mutex>
#include <condition_variable>
2016-03-04 15:04:53 -08:00
namespace urde
2015-08-22 23:42:29 -07:00
{
2015-11-20 17:16:07 -08:00
enum class ESeekOrigin
2015-08-22 23:42:29 -07:00
{
2015-11-20 17:16:07 -08:00
Begin = 0,
Cur = 1,
End = 2
2015-08-22 23:42:29 -07:00
};
struct DVDFileInfo;
class IDvdRequest;
2015-08-22 23:42:29 -07:00
class CDvdFile
{
2015-08-23 16:58:07 -07:00
friend class CResLoader;
2016-03-06 19:12:32 -08:00
friend class CFileDvdRequest;
static hecl::ProjectPath m_DvdRoot;
static std::thread m_WorkerThread;
static std::mutex m_WorkerMutex;
static std::condition_variable m_WorkerCV;
static std::mutex m_WaitMutex;
2018-06-01 17:03:31 -07:00
static std::atomic_bool m_WorkerRun;
2016-03-06 19:12:32 -08:00
static std::vector<std::shared_ptr<IDvdRequest>> m_RequestQueue;
static void WorkerProc();
std::string x18_path;
2016-12-13 17:10:17 -08:00
std::shared_ptr<athena::io::FileReader> m_reader;
2016-03-06 19:12:32 -08:00
2015-08-22 23:42:29 -07:00
public:
2016-03-06 19:12:32 -08:00
static void Initialize(const hecl::ProjectPath& path);
static void Shutdown();
2017-11-12 22:19:18 -08:00
CDvdFile(std::string_view path)
2016-12-13 17:10:17 -08:00
: x18_path(path),
m_reader(std::make_shared<athena::io::FileReader>(
hecl::ProjectPath(m_DvdRoot, path).getAbsolutePath())) {}
operator bool() const { return m_reader->isOpen(); }
2016-03-06 19:12:32 -08:00
void UpdateFilePos(int pos)
{
2016-12-13 17:10:17 -08:00
m_reader->seek(pos, athena::SeekOrigin::Begin);
2016-03-06 19:12:32 -08:00
}
2017-11-12 22:19:18 -08:00
static bool FileExists(std::string_view path)
2016-03-06 19:12:32 -08:00
{
2016-09-18 16:47:48 -07:00
return hecl::ProjectPath(m_DvdRoot, path).isFile();
2016-03-06 19:12:32 -08:00
}
void CloseFile()
{
2016-12-13 17:10:17 -08:00
m_reader->close();
2016-03-06 19:12:32 -08:00
}
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)
2016-03-06 19:12:32 -08:00
{
2016-12-13 17:10:17 -08:00
m_reader->seek(offset, athena::SeekOrigin(whence));
return m_reader->readBytesToBuf(buf, len);
2016-03-06 19:12:32 -08:00
}
std::shared_ptr<IDvdRequest> AsyncRead(void* buf, u32 len, std::function<void(u32)>&& cb = {})
2016-03-06 19:12:32 -08:00
{
return AsyncSeekRead(buf, len, ESeekOrigin::Cur, 0, std::move(cb));
2016-03-06 19:12:32 -08:00
}
u32 SyncRead(void* buf, u32 len)
2016-03-06 19:12:32 -08:00
{
return m_reader->readBytesToBuf(buf, len);
2016-03-06 19:12:32 -08:00
}
2016-12-13 17:10:17 -08:00
u64 Length() {return m_reader->length();}
2017-11-12 22:19:18 -08:00
std::string_view GetPath() const { return x18_path; }
2015-08-22 23:42:29 -07:00
};
}