2018-09-19 18:57:39 -07:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-09-23 18:50:16 -07:00
|
|
|
#include <algorithm>
|
2018-09-19 18:57:39 -07:00
|
|
|
#include <memory>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
|
|
|
class StaticVfsFile : public VfsFile {
|
|
|
|
public:
|
2018-09-25 14:56:14 -07:00
|
|
|
explicit StaticVfsFile(u8 value, std::size_t size = 0, std::string name = "",
|
2018-09-25 14:26:09 -07:00
|
|
|
VirtualDir parent = nullptr)
|
|
|
|
: value{value}, size{size}, name{std::move(name)}, parent{std::move(parent)} {}
|
2018-09-19 18:57:39 -07:00
|
|
|
|
|
|
|
std::string GetName() const override {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:56:14 -07:00
|
|
|
std::size_t GetSize() const override {
|
2018-09-19 18:57:39 -07:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:56:14 -07:00
|
|
|
bool Resize(std::size_t new_size) override {
|
2018-09-19 18:57:39 -07:00
|
|
|
size = new_size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:31:58 -08:00
|
|
|
VirtualDir GetContainingDirectory() const override {
|
2018-09-19 18:57:39 -07:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWritable() const override {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReadable() const override {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:56:14 -07:00
|
|
|
std::size_t Read(u8* data, std::size_t length, std::size_t offset) const override {
|
2018-09-19 18:57:39 -07:00
|
|
|
const auto read = std::min(length, size - offset);
|
|
|
|
std::fill(data, data + read, value);
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:56:14 -07:00
|
|
|
std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override {
|
2018-09-19 18:57:39 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-29 21:03:25 -07:00
|
|
|
std::optional<u8> ReadByte(std::size_t offset) const override {
|
2020-09-22 14:31:53 -07:00
|
|
|
if (offset >= size) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2018-09-19 18:57:39 -07:00
|
|
|
}
|
|
|
|
|
2018-09-25 14:56:14 -07:00
|
|
|
std::vector<u8> ReadBytes(std::size_t length, std::size_t offset) const override {
|
2018-09-19 18:57:39 -07:00
|
|
|
const auto read = std::min(length, size - offset);
|
|
|
|
return std::vector<u8>(read, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Rename(std::string_view new_name) override {
|
|
|
|
name = new_name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-25 14:26:09 -07:00
|
|
|
u8 value;
|
2018-09-25 14:56:14 -07:00
|
|
|
std::size_t size;
|
2018-09-19 18:57:39 -07:00
|
|
|
std::string name;
|
|
|
|
VirtualDir parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace FileSys
|