Implement RealVfsFile (computer fs backend)
This commit is contained in:
@@ -31,6 +31,10 @@ add_library(core STATIC
|
||||
file_sys/storage.h
|
||||
file_sys/vfs.cpp
|
||||
file_sys/vfs.h
|
||||
file_sys/vfs_offset.cpp
|
||||
file_sys/vfs_offset.h
|
||||
file_sys/vfs_real.cpp
|
||||
file_sys/vfs_real.h
|
||||
frontend/emu_window.cpp
|
||||
frontend/emu_window.h
|
||||
frontend/framebuffer_layout.cpp
|
||||
|
||||
@@ -28,18 +28,22 @@ u64 VfsFile::ReplaceBytes(const std::vector<u8>& data) {
|
||||
return WriteBytes(data, 0);
|
||||
}
|
||||
|
||||
boost::optional<VfsFile> VfsDirectory::GetFile(const std::string& name) {
|
||||
VfsDirectory::operator bool() {
|
||||
return IsGood();
|
||||
}
|
||||
|
||||
std::shared_ptr<VfsFile> VfsDirectory::GetFile(const std::string& name) {
|
||||
auto files = GetFiles();
|
||||
auto iter = std::find_if(files.begin(), files.end(),
|
||||
[&name](auto file1) { return name == file1.GetName(); });
|
||||
return iter == files.end() ? boost::none : boost::make_optional(*iter);
|
||||
return iter == files.end() ? nullptr : std::move(*iter);
|
||||
}
|
||||
|
||||
boost::optional<VfsDirectory> VfsDirectory::GetSubdirectory(const std::string& name) {
|
||||
std::shared_ptr<VfsDirectory> VfsDirectory::GetSubdirectory(const std::string& name) {
|
||||
auto subs = GetSubdirectories();
|
||||
auto iter = std::find_if(subs.begin(), subs.end(),
|
||||
[&name](auto file1) { return name == file1.GetName(); });
|
||||
return iter == subs.end() ? boost::none : boost::make_optional(*iter);
|
||||
return iter == subs.end() ? nullptr : std::move(*iter);
|
||||
}
|
||||
|
||||
u64 VfsDirectory::GetSize() {
|
||||
@@ -54,10 +58,14 @@ u64 VfsDirectory::GetSize() {
|
||||
|
||||
bool VfsDirectory::Copy(const std::string& src, const std::string& dest) {
|
||||
auto f1 = CreateFile(src), f2 = CreateFile(dest);
|
||||
if (f1 == boost::none || f2 == boost::none)
|
||||
if (f1 == nullptr || f2 == nullptr)
|
||||
return false;
|
||||
|
||||
return f2->ReplaceBytes(f1->ReadAllBytes()) == f1->GetSize();
|
||||
}
|
||||
|
||||
VfsFile::~VfsFile() {}
|
||||
|
||||
VfsDirectory::~VfsDirectory() {}
|
||||
|
||||
} // namespace FileSys
|
||||
|
||||
@@ -21,7 +21,7 @@ struct VfsFile : NonCopyable {
|
||||
virtual std::string GetName() = 0;
|
||||
virtual u64 GetSize() = 0;
|
||||
virtual bool Resize(u64 new_size) = 0;
|
||||
virtual boost::optional<VfsDirectory> GetContainingDirectory() = 0;
|
||||
virtual std::shared_ptr<VfsDirectory> GetContainingDirectory() = 0;
|
||||
|
||||
virtual bool IsWritable() = 0;
|
||||
virtual bool IsReadable() = 0;
|
||||
@@ -65,14 +65,21 @@ struct VfsFile : NonCopyable {
|
||||
virtual u64 ReplaceBytes(const std::vector<u8>& data);
|
||||
|
||||
virtual bool Rename(const std::string& name) = 0;
|
||||
|
||||
~VfsFile();
|
||||
};
|
||||
|
||||
struct VfsDirectory : NonCopyable {
|
||||
virtual std::vector<VfsFile> GetFiles() = 0;
|
||||
virtual boost::optional<VfsFile> GetFile(const std::string& name);
|
||||
virtual bool IsReady() = 0;
|
||||
virtual bool IsGood() = 0;
|
||||
virtual operator bool();
|
||||
virtual void ResetState() = 0;
|
||||
|
||||
virtual std::vector<VfsDirectory> GetSubdirectories() = 0;
|
||||
virtual boost::optional<VfsDirectory> GetSubdirectory(const std::string& name);
|
||||
virtual std::vector<std::shared_ptr<VfsFile>> GetFiles() = 0;
|
||||
virtual std::shared_ptr<VfsFile> GetFile(const std::string& name);
|
||||
|
||||
virtual std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() = 0;
|
||||
virtual std::shared_ptr<VfsDirectory> GetSubdirectory(const std::string& name);
|
||||
|
||||
virtual bool IsWritable() = 0;
|
||||
virtual bool IsReadable() = 0;
|
||||
@@ -81,10 +88,10 @@ struct VfsDirectory : NonCopyable {
|
||||
|
||||
virtual std::string GetName() = 0;
|
||||
virtual u64 GetSize();
|
||||
virtual boost::optional<VfsDirectory> GetParentDirectory() = 0;
|
||||
virtual std::shared_ptr<VfsDirectory> GetParentDirectory() = 0;
|
||||
|
||||
virtual boost::optional<VfsDirectory> CreateSubdirectory(const std::string& name) = 0;
|
||||
virtual boost::optional<VfsFile> CreateFile(const std::string& name) = 0;
|
||||
virtual std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) = 0;
|
||||
virtual std::shared_ptr<VfsFile> CreateFile(const std::string& name) = 0;
|
||||
|
||||
virtual bool DeleteSubdirectory(const std::string& name) = 0;
|
||||
virtual bool DeleteFile(const std::string& name) = 0;
|
||||
@@ -92,5 +99,7 @@ struct VfsDirectory : NonCopyable {
|
||||
virtual bool Rename(const std::string& name) = 0;
|
||||
|
||||
virtual bool Copy(const std::string& src, const std::string& dest);
|
||||
|
||||
~VfsDirectory();
|
||||
};
|
||||
} // namespace FileSys
|
||||
|
||||
5
src/core/file_sys/vfs_offset.cpp
Normal file
5
src/core/file_sys/vfs_offset.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
namespace FileSys {} // namespace FileSys
|
||||
7
src/core/file_sys/vfs_offset.h
Normal file
7
src/core/file_sys/vfs_offset.h
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace FileSys {} // namespace FileSys
|
||||
72
src/core/file_sys/vfs_real.cpp
Normal file
72
src/core/file_sys/vfs_real.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "core/file_sys/vfs_real.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
RealVfsFile::RealVfsFile(const std::string& path, const char openmode[])
|
||||
: backing(path, openmode), path(path), mode(openmode) {}
|
||||
|
||||
bool RealVfsFile::IsReady() {
|
||||
return backing.IsOpen();
|
||||
}
|
||||
|
||||
bool RealVfsFile::IsGood() {
|
||||
return backing.IsGood();
|
||||
}
|
||||
|
||||
void RealVfsFile::ResetState() {
|
||||
backing.Clear();
|
||||
}
|
||||
|
||||
std::string RealVfsFile::GetName() {
|
||||
std::string part_path, name, extention;
|
||||
Common::SplitPath(path, &part_path, &name, &extention);
|
||||
return name + "." + extention;
|
||||
}
|
||||
|
||||
u64 RealVfsFile::GetSize() {
|
||||
return backing.GetSize();
|
||||
}
|
||||
|
||||
bool RealVfsFile::Resize(u64 new_size) {
|
||||
return backing.Resize(new_size);
|
||||
}
|
||||
|
||||
std::shared_ptr<VfsDirectory> RealVfsFile::GetContainingDirectory() {
|
||||
std::string part_path, name, extention;
|
||||
Common::SplitPath(path, &part_path, &name, &extention);
|
||||
return std::make_unique<RealVfsDirectory>(part_path);
|
||||
}
|
||||
|
||||
bool RealVfsFile::IsWritable() {
|
||||
return mode.find('w') != std::string::npos;
|
||||
}
|
||||
|
||||
bool RealVfsFile::IsReadable() {
|
||||
return mode.find('r') != std::string::npos;
|
||||
}
|
||||
|
||||
std::vector<u8> RealVfsFile::ReadBytes(u64 offset, u64 length) {
|
||||
backing.Seek(offset, SEEK_SET);
|
||||
std::vector<u8> out(length);
|
||||
backing.ReadBytes(out.data(), length);
|
||||
return out;
|
||||
}
|
||||
|
||||
u64 RealVfsFile::WriteBytes(const std::vector<u8>& data, u64 offset) {
|
||||
backing.Seek(offset, SEEK_SET);
|
||||
return backing.WriteBytes(data.data(), data.size());
|
||||
}
|
||||
|
||||
bool RealVfsFile::Rename(const std::string& name) {
|
||||
auto out = FileUtil::Rename(GetName(), name);
|
||||
std::string part_path, o_name, extention;
|
||||
Common::SplitPath(path, &part_path, &o_name, &extention);
|
||||
backing = FileUtil::IOFile(part_path + name, mode.c_str());
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace FileSys
|
||||
55
src/core/file_sys/vfs_real.h
Normal file
55
src/core/file_sys/vfs_real.h
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
#include "common/file_util.h"
|
||||
#include "core\file_sys\vfs.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
struct RealVfsFile : public VfsFile {
|
||||
explicit RealVfsFile(const std::string& name, const char openmode[]);
|
||||
|
||||
bool IsReady() override;
|
||||
bool IsGood() override;
|
||||
void ResetState() override;
|
||||
std::string GetName() override;
|
||||
u64 GetSize() override;
|
||||
bool Resize(u64 new_size) override;
|
||||
std::shared_ptr<VfsDirectory> GetContainingDirectory() override;
|
||||
bool IsWritable() override;
|
||||
bool IsReadable() override;
|
||||
std::vector<u8> ReadBytes(u64 offset, u64 length) override;
|
||||
u64 WriteBytes(const std::vector<u8>& data, u64 offset) override;
|
||||
bool Rename(const std::string& name) override;
|
||||
|
||||
private:
|
||||
FileUtil::IOFile backing;
|
||||
std::string path;
|
||||
std::string mode;
|
||||
};
|
||||
|
||||
struct RealVfsDirectory : public VfsDirectory {
|
||||
explicit RealVfsDirectory(const std::string& name);
|
||||
|
||||
bool IsReady() override;
|
||||
bool IsGood() override;
|
||||
void ResetState() override;
|
||||
std::vector<std::shared_ptr<VfsFile>> GetFiles() override;
|
||||
std::vector<std::shared_ptr<VfsDirectory>> GetSubdirectories() override;
|
||||
bool IsWritable() override;
|
||||
bool IsReadable() override;
|
||||
bool IsRoot() override;
|
||||
std::string GetName() override;
|
||||
u64 GetSize() override;
|
||||
std::shared_ptr<VfsDirectory> GetParentDirectory() override;
|
||||
std::shared_ptr<VfsDirectory> CreateSubdirectory(const std::string& name) override;
|
||||
std::shared_ptr<VfsFile> CreateFile(const std::string& name) override;
|
||||
bool DeleteSubdirectory(const std::string& name) override;
|
||||
bool DeleteFile(const std::string& name) override;
|
||||
bool Rename(const std::string& name) override;
|
||||
bool Copy(const std::string& src, const std::string& dest) override;
|
||||
};
|
||||
|
||||
} // namespace FileSys
|
||||
Reference in New Issue
Block a user