metaforce/Runtime/CDvdFile.hpp

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