1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #pragma once
- #include <nall/file.hpp>
- #include <nall/decode/zip.hpp>
- namespace nall::vfs {
- struct memory : file {
- ~memory() { delete[] _data; }
- static auto open(const void* data, uintmax size) -> shared_pointer<memory> {
- auto instance = shared_pointer<memory>{new memory};
- instance->_open((const uint8_t*)data, size);
- return instance;
- }
- static auto open(string location, bool decompress = false) -> shared_pointer<memory> {
- auto instance = shared_pointer<memory>{new memory};
- if(decompress && location.iendsWith(".zip")) {
- Decode::ZIP archive;
- if(archive.open(location) && archive.file.size() == 1) {
- auto memory = archive.extract(archive.file.first());
- instance->_open(memory.data(), memory.size());
- return instance;
- }
- }
- auto memory = nall::file::read(location);
- instance->_open(memory.data(), memory.size());
- return instance;
- }
- auto data() const -> const uint8_t* { return _data; }
- auto size() const -> uintmax override { return _size; }
- auto offset() const -> uintmax override { return _offset; }
- auto seek(intmax offset, index mode) -> void override {
- if(mode == index::absolute) _offset = (uintmax)offset;
- if(mode == index::relative) _offset += (intmax)offset;
- }
- auto read() -> uint8_t override {
- if(_offset >= _size) return 0x00;
- return _data[_offset++];
- }
- auto write(uint8_t data) -> void override {
- if(_offset >= _size) return;
- _data[_offset++] = data;
- }
- private:
- memory() = default;
- memory(const file&) = delete;
- auto operator=(const memory&) -> memory& = delete;
- auto _open(const uint8_t* data, uintmax size) -> void {
- _size = size;
- _data = new uint8_t[size];
- nall::memory::copy(_data, data, size);
- }
- uint8_t* _data = nullptr;
- uintmax _size = 0;
- uintmax _offset = 0;
- };
- }
|