libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
ArchiveManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <memory>
5#include <vector>
6#include <list>
7#include <unordered_map>
8#include <unordered_set>
9#include <stdint.h>
10#include <functional>
11#include "ship/resource/File.h"
13#include "ship/core/Component.h"
14#ifdef ENABLE_SCRIPTING
16#endif
17
18namespace Ship {
19struct File;
20class Archive;
21class ResourceManager;
22class Keystore;
23
24#ifdef ENABLE_SCRIPTING
31using UntrustedArchiveHandler = std::function<bool(Archive& archive, KeystoreEntry& key)>;
32#endif
33
52class ArchiveManager : public Component {
53 public:
54 explicit ArchiveManager(std::shared_ptr<ResourceManager> resourceManager = nullptr,
55 std::shared_ptr<Keystore> keystore = nullptr);
56
61 void Init(const std::vector<std::string>& archivePaths);
62
68 void Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validGameVersions);
70
76 std::shared_ptr<Archive> AddArchive(const std::string& archivePath);
77
83 std::shared_ptr<Archive> AddArchive(std::shared_ptr<Archive> archive);
84
86 std::shared_ptr<std::vector<std::shared_ptr<Archive>>> GetArchives();
87
92 void SetArchives(std::shared_ptr<std::vector<std::shared_ptr<Archive>>> archives);
93
99 size_t RemoveArchive(std::shared_ptr<Archive> archive);
100
106 size_t RemoveArchive(const std::string& path);
107
113 std::shared_ptr<File> LoadFile(const std::string& filePath);
114
120 std::shared_ptr<File> LoadFile(uint64_t hash);
121
129 bool WriteFile(std::shared_ptr<Archive> archive, const std::string& filename, const std::vector<uint8_t>& data);
130
136 bool HasFile(const std::string& filePath);
137
143 bool HasFile(uint64_t hash);
144
150 std::shared_ptr<Archive> GetArchiveFromFile(const std::string& filePath);
151
158 int32_t GetFilePriority(const ResourceIdentifier& identifier);
159
165 std::shared_ptr<std::vector<std::string>> ListFiles(const std::string& searchMask = "");
166
173 std::shared_ptr<std::vector<std::string>> ListFiles(const std::list<std::string>& includes,
174 const std::list<std::string>& excludes);
175
181 std::shared_ptr<std::vector<std::string>> ListDirectories(const std::string& searchMask = "");
182
187 std::vector<uint32_t> GetGameVersions();
188
194 const std::string* HashToString(uint64_t hash) const;
195
201 const char* HashToCString(uint64_t hash) const;
202
207 bool IsGameVersionValid(uint32_t gameVersion);
208
209#ifdef ENABLE_SCRIPTING
215
218#endif
219
220 protected:
226 static std::vector<std::string> GetArchiveListInPaths(const std::vector<std::string>& archivePaths);
227
229 void AddGameVersion(uint32_t newGameVersion);
230
233
240 void AddFileToVfs(uint64_t hash, const std::string& filePath, const std::shared_ptr<Archive>& archive);
241
242 private:
243 std::vector<std::shared_ptr<Archive>> mArchives;
244 std::vector<uint32_t> mGameVersions;
245 std::unordered_set<uint32_t> mValidGameVersions;
246 std::unordered_map<uint64_t, std::string> mHashes;
247 std::unordered_set<std::string> mDirectories;
248 std::unordered_map<uint64_t, std::shared_ptr<Archive>> mFileToArchive;
249 std::shared_ptr<ResourceManager> mResourceManager;
250 std::shared_ptr<Keystore> mKeystore;
251#ifdef ENABLE_SCRIPTING
252 UntrustedArchiveHandler mUntrustedArchiveHandler;
253#endif
254};
255} // namespace Ship
Manages a prioritised collection of mounted Archive objects.
Definition ArchiveManager.h:52
std::shared_ptr< Archive > AddArchive(std::shared_ptr< Archive > archive)
Adds a pre-constructed Archive object to the managed collection.
bool HasFile(const std::string &filePath)
Checks whether any mounted archive contains a file at the given path.
std::shared_ptr< std::vector< std::string > > ListFiles(const std::string &searchMask="")
Lists virtual paths of all files matching the given search mask across all archives.
UntrustedArchiveHandler GetUntrustedArchiveHandler() const
Returns the current untrusted-archive handler.
std::shared_ptr< std::vector< std::shared_ptr< Archive > > > GetArchives()
Returns all currently mounted archives in priority order (last = highest priority).
bool HasFile(uint64_t hash)
Checks whether any mounted archive contains a file with the given hash.
void SetUntrustedArchiveHandler(const UntrustedArchiveHandler &handler)
Sets the callback invoked when an untrusted (unsigned/unknown-key) archive is added.
std::shared_ptr< File > LoadFile(uint64_t hash)
Loads raw file bytes by 64-bit hash from the highest-priority matching archive.
static std::vector< std::string > GetArchiveListInPaths(const std::vector< std::string > &archivePaths)
Scans the given filesystem paths and returns a flat list of archive file paths.
const std::string * HashToString(uint64_t hash) const
Looks up the virtual path string for a given 64-bit hash.
void Init(const std::vector< std::string > &archivePaths, const std::unordered_set< uint32_t > &validGameVersions)
Discovers and mounts archives, filtering by acceptable game versions.
bool WriteFile(std::shared_ptr< Archive > archive, const std::string &filename, const std::vector< uint8_t > &data)
Writes raw data into a specific archive.
void AddGameVersion(uint32_t newGameVersion)
Adds a game-version value to the internal version set.
void Init(const std::vector< std::string > &archivePaths)
Discovers and mounts all archives found under the given paths.
std::shared_ptr< std::vector< std::string > > ListDirectories(const std::string &searchMask="")
Lists virtual directory paths matching the given search mask across all archives.
size_t RemoveArchive(std::shared_ptr< Archive > archive)
Removes a specific archive from the managed collection.
int32_t GetFilePriority(const ResourceIdentifier &identifier)
Load-order priority of the archive that owns the given file (higher = higher priority; the last-loade...
std::shared_ptr< Archive > AddArchive(const std::string &archivePath)
Opens and adds an archive by filesystem path.
size_t RemoveArchive(const std::string &path)
Removes the archive at the given filesystem path.
std::shared_ptr< Archive > GetArchiveFromFile(const std::string &filePath)
Returns the highest-priority archive that contains the given file.
const char * HashToCString(uint64_t hash) const
Looks up the virtual path C-string for a given 64-bit hash.
void SetArchives(std::shared_ptr< std::vector< std::shared_ptr< Archive > > > archives)
Replaces the entire archive list with a new collection.
std::vector< uint32_t > GetGameVersions()
Returns the union of all game-version values found across mounted archives.
void AddFileToVfs(uint64_t hash, const std::string &filePath, const std::shared_ptr< Archive > &archive)
Records a file in the hash-to-path and path-to-archive lookup tables.
std::shared_ptr< File > LoadFile(const std::string &filePath)
Loads raw file bytes from the highest-priority archive that contains the path.
void ResetVirtualFileSystem()
Rebuilds the hash-to-path and path-to-archive lookup tables from the current archive list.
ArchiveManager(std::shared_ptr< ResourceManager > resourceManager=nullptr, std::shared_ptr< Keystore > keystore=nullptr)
std::shared_ptr< std::vector< std::string > > ListFiles(const std::list< std::string > &includes, const std::list< std::string > &excludes)
Lists virtual paths matching include/exclude glob masks across all archives.
bool IsGameVersionValid(uint32_t gameVersion)
Returns true if the given game version is in the set of valid versions.
Abstract base class representing a mounted archive (OTR, O2R, or folder).
Definition Archive.h:61
A named Part with a parent/child hierarchy and optional thread safety.
Definition Component.h:34
Uniquely identifies a cached resource by path/hash, owner, and source archive.
Definition ResourceIdentifier.h:15
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
std::function< bool(Archive &archive, KeystoreEntry &key)> UntrustedArchiveHandler
Callback invoked when an archive without a trusted key is added.
Definition ArchiveManager.h:31
A single named key stored in the Keystore.
Definition Keystore.h:24