libeditwl
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
nfs_NitroFs.hpp
1
2#pragma once
3#include <twl/fs/fs_File.hpp>
4#include <twl/fs/fs_FileFormat.hpp>
5
6namespace twl::fmt::nfs {
7
8 struct NitroFile {
9 std::string name;
10 u16 file_id;
11 fs::BufferFile inner_file;
12
13 inline void Dispose() {
14 this->inner_file.Dispose();
15 }
16 };
17
19 std::string name;
20 std::vector<NitroDirectory> dirs;
21 std::vector<NitroFile> files;
22 };
23
24 struct NitroFileSystem;
25
28 u32 start;
29 u16 first_file_id;
30 u16 parent_id;
31 };
32
34 u32 file_start;
35 u32 file_end;
36 };
37
38 static constexpr u16 RootDirectoryId = 0xF000;
39 static constexpr size_t MaxEntryNameLength = 128;
40
41 size_t fat_data_offset;
42 size_t fnt_data_offset;
43 NitroDirectory root_dir;
44 std::vector<NitroFile> ext_files;
45
46 Result ReadFrom(fs::File &rf, const size_t file_data_offset, const size_t fat_data_offset, const size_t fnt_data_offset);
47 Result WriteTo(fs::BufferReaderWriter &out_fnt_data, fs::BufferReaderWriter &out_file_data, std::vector<NitroFileSystem::DirectoryNameTableEntry> &out_fnt, std::vector<NitroFileSystem::FileAllocationTableEntry> &out_fat, const size_t file_end_align);
48
49 void Dispose();
50 };
51
52 class NitroFileSystemFile : public fs::File {
53 public:
54 NitroFile *file_ref;
55
56 public:
57 NitroFileSystemFile() : File(), file_ref(nullptr) {}
58 NitroFileSystemFile(NitroFile *file_ref) : File(), file_ref(file_ref) {}
59
60 Result CreateById(NitroFileSystem &nitro_fs, const u32 file_id);
61 Result CreateByPath(NitroFileSystem &nitro_fs, const std::string &path);
62
63 inline bool IsValid() {
64 return this->file_ref != nullptr;
65 }
66
67 Result OpenImpl(const fs::FileMode mode) override;
68
69 inline Result GetSizeImpl(size_t &out_size) override {
70 TWL_R_TRY(this->file_ref->inner_file.GetSizeImpl(out_size));
71 TWL_R_SUCCEED();
72 }
73
74 inline Result SetOffsetImpl(const size_t offset, const fs::Whence whence) override {
75 TWL_R_TRY(this->file_ref->inner_file.SetOffsetImpl(offset, whence));
76 TWL_R_SUCCEED();
77 }
78
79 inline Result GetOffsetImpl(size_t &out_offset) override {
80 TWL_R_TRY(this->file_ref->inner_file.GetOffsetImpl(out_offset));
81 TWL_R_SUCCEED();
82 }
83
84 inline Result ReadBufferImpl(void *read_buf, const size_t read_size) override {
85 TWL_R_TRY(this->file_ref->inner_file.ReadBufferImpl(read_buf, read_size));
86 TWL_R_SUCCEED();
87 }
88
89 inline Result WriteBufferImpl(const void *write_buf, const size_t write_size) override {
90 TWL_R_TRY(this->file_ref->inner_file.WriteBufferImpl(write_buf, write_size));
91 TWL_R_SUCCEED();
92 }
93
94 inline Result CloseImpl() override {
95 TWL_R_TRY(this->file_ref->inner_file.CloseImpl());
96 TWL_R_SUCCEED();
97 }
98 };
99
101 protected:
102 NitroFileSystem nitro_fs;
103
104 public:
105 inline Result CreateFileById(NitroFileSystemFile &file, const u32 file_id) {
106 TWL_R_TRY(file.CreateById(this->nitro_fs, file_id));
107 TWL_R_SUCCEED();
108 }
109
110 inline Result CreateFileByPath(NitroFileSystemFile &file, const std::string &path) {
111 TWL_R_TRY(file.CreateByPath(this->nitro_fs, path));
112 TWL_R_SUCCEED();
113 }
114 };
115
116}
Definition nfs_NitroFs.hpp:52
Definition nfs_NitroFs.hpp:100
Definition fs_File.hpp:327
Definition fs_File.hpp:166
Definition fs_File.hpp:232
Definition twl_Include.hpp:62
Definition nfs_NitroFs.hpp:18
Definition nfs_NitroFs.hpp:26
Definition nfs_NitroFs.hpp:8