libER 0.1.4.2
ELDEN RING API library
Loading...
Searching...
No Matches
file.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <coresystem/task.hpp>
11#include <detail/optref.hpp>
12#include <detail/preprocessor.hpp>
14#include <fd4/resource.hpp>
17
18#include <atomic>
19#include <filesystem>
20#include <memory>
21#include <string>
22
23namespace from {
24namespace CS {
32#define LIBER_RESOURCE_REPOSITORY(NAME) NAME,
34#include <coresystem/file/resource_repositories.inl>
36#undef LIBER_RESOURCE_REPOSITORY
37};
38
45class CSFile {
46public:
47 FD4_SINGLETON_CLASS(CSFile);
48
49 virtual ~CSFile() LIBER_INTERFACE_ONLY;
50
57 return this->file_repository;
58 }
59
60private:
61 FD4::FD4ResRep* file_repository;
62};
63
71public:
72 FD4_SINGLETON_CLASS(CSFD4MoWwisebankRepository);
73
74 virtual ~CSFD4MoWwisebankRepository() LIBER_INTERFACE_ONLY;
75};
76} // namespace CS
77
91public:
105 enum file_request_type : char {
106 LOAD = 0,
107 RELOAD = 1,
108 UNLOAD = 2
109 };
110
129 file_request(const std::filesystem::path& file, file_request_type type)
130 : path(file), type(type) {
131 this->init_request();
132 }
133
152 file_request(std::filesystem::path&& file, file_request_type type)
153 : path(file), type(type) {
154 this->init_request();
155 }
156
157 file_request(const file_request&) = delete;
158 file_request(file_request&&) noexcept = delete;
159
160 file_request& operator=(const file_request&) = delete;
161 file_request& operator=(file_request&&) noexcept = delete;
162
163 ~file_request() noexcept {
164 this->is_ready.test_and_set();
165 this->is_ready.notify_all();
166 }
167
179 bool ready() const noexcept {
180 return is_ready.test();
181 }
182
198 this->is_ready.wait(false);
199 return this->check();
200 }
201
212 return this->file_cap;
213 }
214
219 using loader_type = FD4::FD4FileCap* (*)(CS::CSFile*, const wchar_t*,
220 void*, void*, void*, void*);
221
222private:
223 LIBERAPI void init_request();
224
225 std::filesystem::path path;
227 bool is_vfs_path = false;
228 bool unload_file = false;
229 enum class file_extension_type : char {
230 ANY = 0,
231 BNK = 1,
232 WEM = 2
233 } file_extension = file_extension_type::ANY;
234 loader_type loader = nullptr;
235 FD4::FD4FileCap* file_cap = nullptr;
236 mutable std::atomic_flag is_ready = ATOMIC_FLAG_INIT;
237 struct file_request_task : public CS::CSEzTask {
238 file_request_task(file_request& request) : request(request) {}
239 LIBERAPI void eztask_execute(FD4::FD4TaskData*) override;
240 file_request& request;
241 };
243 from::make_unique<file_request_task>(*this);
244};
245
259public:
268 const std::wstring& resource,
269 const std::filesystem::path& source = std::filesystem::path{})
270 : repository(repository), resource(resource),
271 file(!source.empty() ? new file_request(source, file_request::LOAD)
272 : nullptr) {
273 this->task->register_task(CS::CSTaskGroup::FileStepUpdate_End);
274 }
275
284 std::wstring&& resource,
285 const std::filesystem::path& source = std::filesystem::path{})
286 : repository(repository), resource(resource),
287 file(!source.empty() ? new file_request(source, file_request::LOAD)
288 : nullptr) {
289 this->task->register_task(CS::CSTaskGroup::FileStepUpdate_End);
290 }
291
292 resource_request(const resource_request&) = delete;
293 resource_request(resource_request&&) noexcept = delete;
294
295 resource_request& operator=(const resource_request&) = delete;
296 resource_request& operator=(resource_request&&) noexcept = delete;
297
298 ~resource_request() noexcept {
299 this->is_ready.test_and_set();
300 this->is_ready.notify_all();
301 }
302
314 bool ready() const noexcept {
315 return is_ready.test();
316 }
317
333 this->is_ready.wait(false);
334 return this->check();
335 }
336
347 if (!this->res_cap)
348 return std::nullopt;
349 return *this->res_cap;
350 }
351
352private:
353 CS::CSResourceRepository repository;
354 std::wstring resource;
355 std::unique_ptr<file_request> file;
356 FD4::FD4ResCap* res_cap = nullptr;
357 mutable std::atomic_flag is_ready = ATOMIC_FLAG_INIT;
358 struct resource_request_task : public CS::CSEzTask {
359 resource_request_task(resource_request& request) : request(request) {}
360 LIBERAPI void eztask_execute(FD4::FD4TaskData*) override;
361 resource_request& request;
362 };
364 from::make_unique<resource_request_task>(*this);
365};
366} // namespace from
Inherit from this minimal task interface to create a custom task.
Definition task.hpp:51
File repository for Wwise banks.
Definition file.hpp:70
A singleton responsible for file management.
Definition file.hpp:45
FD4::FD4ResRep * get_file_repository() noexcept
Get the file repository.
Definition file.hpp:56
Abstract file capsule class.
Definition resource.hpp:239
The base of any resource, which can also be a file or a repository.
Definition resource.hpp:120
Abstract resource repository class.
Definition resource.hpp:316
Asynchronous file loading using the file capsule system.
Definition file.hpp:90
file_request(const std::filesystem::path &file, file_request_type type)
Construct a new file request.
Definition file.hpp:129
bool ready() const noexcept
Check if the file request is ready.
Definition file.hpp:179
FD4::FD4FileCap *(*)(CS::CSFile *, const wchar_t *, void *, void *, void *, void *) loader_type
File capsule loader callback signature, for internal use.
Definition file.hpp:220
liber::optref< FD4::FD4FileCap > check() const noexcept
Get the request result whether it had finished or not.
Definition file.hpp:211
file_request(std::filesystem::path &&file, file_request_type type)
Construct a new file request (path move constructor).
Definition file.hpp:152
liber::optref< FD4::FD4FileCap > get() const noexcept
Block until the file request is ready and get the result.
Definition file.hpp:197
file_request_type
Supported request types.
Definition file.hpp:105
Asynchronous resource loading using the resource capsule system.
Definition file.hpp:258
liber::optref< FD4::FD4ResCap > check() const noexcept
Get the request result whether it had finished or not.
Definition file.hpp:346
bool ready() const noexcept
Check if the resource request is ready.
Definition file.hpp:314
liber::optref< FD4::FD4ResCap > get() const noexcept
Block until the resource request is ready and get the result.
Definition file.hpp:332
resource_request(CS::CSResourceRepository repository, std::wstring &&resource, const std::filesystem::path &source=std::filesystem::path{})
Construct a new resource request (string move constructor).
Definition file.hpp:283
resource_request(CS::CSResourceRepository repository, const std::wstring &resource, const std::filesystem::path &source=std::filesystem::path{})
Construct a new resource request.
Definition file.hpp:267
CSResourceRepository
All resource repositories supported by libER.
Definition file.hpp:31
from::shared_ptr based on std::shared_ptr
from::unique_ptr based on std::unique_ptr
std::unique_ptr< T, from::delay_delete< T, AllocatorTag > > unique_ptr
std::unique_ptr with from::allocator and from::delay_delete.
Definition from_unique_ptr.hpp:22
Optional references based on std::optional.
FD4_SINGLETON_CLASS implementation.
A std::optional<std::reference_wrapper<T>> wrapper for lvalue references.
Definition optref.hpp:31
Namespace CS task interface.