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"
12#include "ship/core/Component.h"
13#ifdef ENABLE_SCRIPTING
15#endif
16
17namespace Ship {
18struct File;
19class Archive;
20class ResourceManager;
21class Keystore;
22
23#ifdef ENABLE_SCRIPTING
30using UntrustedArchiveHandler = std::function<bool(Archive& archive, KeystoreEntry& key)>;
31#endif
32
51class ArchiveManager : public Component {
52 public:
53 explicit ArchiveManager(std::shared_ptr<ResourceManager> resourceManager = nullptr,
54 std::shared_ptr<Keystore> keystore = nullptr);
55
60 void Init(const std::vector<std::string>& archivePaths);
61
67 void Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validGameVersions);
69
75 std::shared_ptr<Archive> AddArchive(const std::string& archivePath);
76
82 std::shared_ptr<Archive> AddArchive(std::shared_ptr<Archive> archive);
83
85 std::shared_ptr<std::vector<std::shared_ptr<Archive>>> GetArchives();
86
91 void SetArchives(std::shared_ptr<std::vector<std::shared_ptr<Archive>>> archives);
92
98 size_t RemoveArchive(std::shared_ptr<Archive> archive);
99
105 size_t RemoveArchive(const std::string& path);
106
112 std::shared_ptr<File> LoadFile(const std::string& filePath);
113
119 std::shared_ptr<File> LoadFile(uint64_t hash);
120
128 bool WriteFile(std::shared_ptr<Archive> archive, const std::string& filename, const std::vector<uint8_t>& data);
129
135 bool HasFile(const std::string& filePath);
136
142 bool HasFile(uint64_t hash);
143
149 std::shared_ptr<Archive> GetArchiveFromFile(const std::string& filePath);
150
156 std::shared_ptr<std::vector<std::string>> ListFiles(const std::string& searchMask = "");
157
164 std::shared_ptr<std::vector<std::string>> ListFiles(const std::list<std::string>& includes,
165 const std::list<std::string>& excludes);
166
172 std::shared_ptr<std::vector<std::string>> ListDirectories(const std::string& searchMask = "");
173
178 std::vector<uint32_t> GetGameVersions();
179
185 const std::string* HashToString(uint64_t hash) const;
186
192 const char* HashToCString(uint64_t hash) const;
193
198 bool IsGameVersionValid(uint32_t gameVersion);
199
200#ifdef ENABLE_SCRIPTING
206
209#endif
210
211 protected:
217 static std::vector<std::string> GetArchiveListInPaths(const std::vector<std::string>& archivePaths);
218
220 void AddGameVersion(uint32_t newGameVersion);
221
224
225 private:
226 std::vector<std::shared_ptr<Archive>> mArchives;
227 std::vector<uint32_t> mGameVersions;
228 std::unordered_set<uint32_t> mValidGameVersions;
229 std::unordered_map<uint64_t, std::string> mHashes;
230 std::unordered_set<std::string> mDirectories;
231 std::unordered_map<uint64_t, std::shared_ptr<Archive>> mFileToArchive;
232 std::shared_ptr<ResourceManager> mResourceManager;
233 std::shared_ptr<Keystore> mKeystore;
234#ifdef ENABLE_SCRIPTING
235 UntrustedArchiveHandler mUntrustedArchiveHandler;
236#endif
237};
238} // namespace Ship
Manages a prioritised collection of mounted Archive objects.
Definition ArchiveManager.h:51
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.
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.
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
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:30
A single named key stored in the Keystore.
Definition Keystore.h:24