-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssetBundle.cpp
More file actions
174 lines (151 loc) · 4.73 KB
/
Copy pathAssetBundle.cpp
File metadata and controls
174 lines (151 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "AssetBundle.h"
#include "AssetExporter.h"
#include "AssetImporter.h"
#include "Logger.h"
namespace sh::core
{
AssetBundle::AssetBundle()
{
static_assert(sizeof(Header) == 8);
static_assert(sizeof(AssetEntry) == 48);
}
AssetBundle::AssetBundle(AssetBundle&& other) noexcept :
header(other.header),
bundlePath(std::move(other.bundlePath)),
assetEntries(std::move(other.assetEntries)),
bundleData(std::move(other.bundleData)),
nextAssetDataOffset(other.nextAssetDataOffset),
bundleStream(std::move(other.bundleStream))
{
}
SH_CORE_API void AssetBundle::Clear()
{
bundleStream.clear();
bundleStream.close();
assetEntries.clear();
bundleData.clear();
nextAssetDataOffset = 0;
}
SH_CORE_API auto AssetBundle::AddAsset(const Asset& asset, bool bCompress) -> bool
{
if (assetEntries.find(asset.GetAssetUUID()) != assetEntries.end())
return false;
std::size_t lastOffset = bundleData.size();
std::vector<uint8_t> assetData = AssetExporter::SaveToMemory(asset, bCompress);
AssetEntry entry{};
entry.uuid = asset.GetAssetUUID().GetRawData();
std::memcpy(entry.type, asset.GetType(), sizeof(entry.type));
entry.dataSize = assetData.size();
entry.dataOffset = lastOffset;
entry.bCompressed = bCompress;
bundleData.resize(bundleData.size() + assetData.size());
std::memcpy(bundleData.data() + lastOffset, assetData.data(), assetData.size());
assetEntries.insert_or_assign(asset.GetAssetUUID(), entry);
return true;
}
SH_CORE_API auto AssetBundle::HasAsset(const UUID& uuid) const -> bool
{
return assetEntries.find(uuid) != assetEntries.end();
}
SH_CORE_API auto AssetBundle::IsLoaded() const -> bool
{
return bundleStream.is_open();
}
SH_CORE_API auto AssetBundle::GetAllAssetUUIDs() const -> std::vector<UUID>
{
std::vector<UUID> uuids;
uuids.reserve(assetEntries.size());
for (const auto& pair : assetEntries)
uuids.push_back(pair.first);
return uuids;
}
SH_CORE_API auto AssetBundle::SaveBundle(const std::filesystem::path& path) const -> bool
{
bundleStream.clear();
if (bundleStream.is_open())
bundleStream.close();
bundleStream.open(path, std::ios::binary | std::ios::out | std::ios::trunc);
if (!bundleStream.is_open())
{
SH_ERROR_FORMAT(u8"Could not open file for writing: {}", path.u8string());
return false;
}
Header header{};
header.version = VERSION;
header.numAssets = static_cast<uint32_t>(assetEntries.size());
bundleStream.write(reinterpret_cast<const char*>(&header), sizeof(Header));
for (const auto& pair : assetEntries)
{
const AssetEntry& entry = pair.second;
bundleStream.write(reinterpret_cast<const char*>(&entry), sizeof(AssetEntry));
}
if (!bundleData.empty())
{
bundleStream.write(reinterpret_cast<const char*>(bundleData.data()), bundleData.size());
}
bundleStream.close();
return true;
}
SH_CORE_API auto AssetBundle::LoadBundle(const std::filesystem::path& path) -> bool
{
bundleStream.clear();
if (!bundleStream.is_open())
{
bundleStream.open(path, std::ios::binary | std::ios::in);
if (!bundleStream.is_open())
{
SH_ERROR_FORMAT("Could not open file for reading: {}", path.u8string());
return false;
}
}
else
{
Clear();
return LoadBundle(path);
}
assetEntries.clear();
bundleStream.read(reinterpret_cast<char*>(&header), sizeof(Header));
if (header.version != VERSION)
{
SH_WARN_FORMAT("AssetBundle version mismatch. Expected {}, found {}. Loading might fail.", VERSION, header.version);
}
for (uint32_t i = 0; i < header.numAssets; ++i)
{
AssetEntry entry{};
bundleStream.read(reinterpret_cast<char*>(&entry), sizeof(AssetEntry));
assetEntries.insert_or_assign(UUID{ entry.uuid }, entry);
}
bundlePath = path;
return true;
}
SH_CORE_API auto AssetBundle::LoadAsset(const core::UUID& uuid) -> std::unique_ptr<Asset>
{
assert(!bundlePath.empty());
bundleStream.clear();
if (!bundleStream.is_open())
{
SH_ERROR("Call LoadBundle() first");
return nullptr;
}
auto it = assetEntries.find(uuid);
if (it == assetEntries.end())
{
SH_ERROR_FORMAT("Asset({}) is not exist in bundle", uuid.ToString());
return nullptr;
}
const AssetEntry& entry = it->second;
auto dataSectionOffset = sizeof(Header) + assetEntries.size() * sizeof(AssetEntry);
bundleStream.seekg(dataSectionOffset + static_cast<std::streampos>(entry.dataOffset));
std::vector<uint8_t> data(entry.dataSize);
bundleStream.read(reinterpret_cast<char*>(data.data()), entry.dataSize);
return AssetImporter::LoadFromMemory(data);
}
SH_CORE_API auto AssetBundle::GetVersion() const -> uint32_t
{
return header.version;
}
SH_CORE_API auto AssetBundle::GetAssetEntries() const -> const std::unordered_map<UUID, AssetEntry>&
{
return assetEntries;
}
}//namespace