NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
AssetRegistry.cpp
Go to the documentation of this file.
1
#include "
Engine/Assets/AssetRegistry.h
"
2
#include <fstream>
3
#include <iomanip>
4
#include <algorithm>
5
#include <random>
6
#include <sstream>
7
#include <cctype>
8
9
namespace
NoJob
10
{
11
namespace
12
{
13
std::filesystem::path MetaPath(
const
std::filesystem::path& asset)
14
{
15
return
std::filesystem::path(asset.string() +
".meta"
);
16
}
17
18
AssetHandle
NewGuid()
19
{
20
std::random_device rd;
21
std::mt19937_64 gen(
22
(
static_cast<
std::uint64_t
>
(rd()) << 32) ^ rd());
23
std::uniform_int_distribution<std::uint64_t> dist;
24
AssetHandle
h =
InvalidAssetHandle
;
25
while
(h ==
InvalidAssetHandle
) h = dist(gen);
26
return
h;
27
}
28
29
AssetHandle
ReadMeta(
const
std::filesystem::path& asset)
30
{
31
std::ifstream in(MetaPath(asset));
32
if
(!in)
return
InvalidAssetHandle
;
33
std::string magic;
34
AssetHandle
handle =
InvalidAssetHandle
;
35
in >> magic >> handle;
36
return
magic ==
"NOJOB_META"
? handle :
InvalidAssetHandle
;
37
}
38
39
void
WriteMeta(
const
std::filesystem::path& asset,
AssetHandle
handle)
40
{
41
std::ofstream out(MetaPath(asset), std::ios::trunc);
42
if
(out) out <<
"NOJOB_META "
<< handle <<
"\n"
;
43
}
44
}
45
46
AssetRegistry::AssetRegistry
(std::filesystem::path root)
47
: m_Root(std::filesystem::absolute(std::move(root)).lexically_normal()) {}
48
49
std::string AssetRegistry::Normalize(
const
std::filesystem::path& p)
50
{
51
return
std::filesystem::absolute(p).lexically_normal().generic_string();
52
}
53
54
AssetHandle
AssetRegistry::StableHandle(
const
std::filesystem::path& p)
55
{
56
std::uint64_t h=1469598103934665603ull;
57
for
(
unsigned
char
c:Normalize(p)){h^=c;h*=1099511628211ull;}
58
return
h==0?1:h;
59
}
60
61
AssetType
AssetRegistry::TypeFromExtension
(
const
std::filesystem::path& p)
62
{
63
auto
e=p.extension().string();
64
std::transform(e.begin(),e.end(),e.begin(),[](
unsigned
char
c){return char(std::tolower(c));});
65
if
(e==
".png"
||e==
".jpg"
||e==
".jpeg"
||e==
".bmp"
||e==
".tga"
||e==
".hdr"
)
return
AssetType::Texture2D
;
66
if
(e==
".obj"
||e==
".fbx"
||e==
".gltf"
||e==
".glb"
||e==
".dae"
||e==
".stl"
||e==
".ply"
||e==
".3ds"
||e==
".blend"
)
return
AssetType::Mesh
;
67
if
(e==
".nojobmat"
)
return
AssetType::Material
;
68
if
(e==
".nojobscene"
)
return
AssetType::Scene
;
69
if
(e==
".nojobprefab"
)
return
AssetType::Prefab
;
70
if
(e==
".cpp"
||e==
".h"
||e==
".hpp"
)
return
AssetType::Script
;
71
if
(e==
".wav"
||e==
".mp3"
||e==
".flac"
)
return
AssetType::Audio
;
72
return
AssetType::Unknown
;
73
}
74
75
AssetHandle
AssetRegistry::Register
(
const
std::filesystem::path& path,
AssetType
type)
76
{
77
const
auto
normalized=std::filesystem::absolute(path).lexically_normal();
78
const
auto
key=Normalize(normalized);
79
if
(
auto
it=m_ByPath.find(key);it!=m_ByPath.end())
return
it->second;
80
81
AssetHandle
handle=ReadMeta(normalized);
82
if
(handle==
InvalidAssetHandle
|| m_ByHandle.contains(handle))
83
{
84
do
{ handle=NewGuid(); }
while
(m_ByHandle.contains(handle));
85
WriteMeta(normalized,handle);
86
}
87
88
AssetMetadata
md;
89
md.
Handle
=handle; md.
Path
=normalized; md.
Name
=normalized.stem().string();
90
md.
Type
=type==
AssetType::Unknown
?
TypeFromExtension
(normalized):type;
91
std::error_code ec;
92
if
(std::filesystem::is_regular_file(normalized,ec))
93
md.
FileSize
=std::filesystem::file_size(normalized,ec);
94
auto
ft=std::filesystem::last_write_time(normalized,ec);
95
if
(!ec) md.
LastWriteTime
=(std::uint64_t)ft.time_since_epoch().count();
96
97
m_ByHandle[handle]=md; m_ByPath[key]=handle;
98
return
handle;
99
}
100
101
void
AssetRegistry::Scan
()
102
{
103
std::filesystem::create_directories(m_Root);
104
m_ByHandle.clear(); m_ByPath.clear();
105
for
(
auto
const
& e:std::filesystem::recursive_directory_iterator(
106
m_Root,std::filesystem::directory_options::skip_permission_denied))
107
{
108
if
(!e.is_regular_file())
continue
;
109
const
auto
& p=e.path();
110
if
(p.filename()==
"AssetRegistry.nojob"
111
|| p.extension()==
".meta"
112
|| p.extension()==
".source"
)
continue
;
113
Register
(p);
114
}
115
Save
();
116
}
117
118
bool
AssetRegistry::Save
()
const
119
{
120
std::ofstream out(m_Root/
"AssetRegistry.nojob"
);
if
(!out)
return
false
;
121
out<<
"NOJOB_ASSET_REGISTRY 2\n"
;
122
for
(
auto
& [h,m]:m_ByHandle)
123
out<<h<<
' '
<<int(m.Type)<<
' '
<<std::quoted(m.Path.generic_string())<<
' '
<<std::quoted(m.Name)<<
'\n'
;
124
return
true
;
125
}
126
127
bool
AssetRegistry::Load
()
128
{
129
std::ifstream in(m_Root/
"AssetRegistry.nojob"
);
if
(!in)
return
false
;
130
std::string header;std::getline(in,header);
if
(header.rfind(
"NOJOB_ASSET_REGISTRY"
,0)!=0)
return
false
;
131
m_ByHandle.clear();m_ByPath.clear();
132
AssetHandle
h;
int
ty;std::string path,name;
133
while
(in>>h>>ty>>std::quoted(path)>>std::quoted(name)){
134
AssetMetadata
md;md.
Handle
=h;md.
Type
=
AssetType
(ty);md.
Path
=path;md.
Name
=name;
135
if
(std::filesystem::exists(md.
Path
)){
136
m_ByHandle[h]=md;m_ByPath[Normalize(md.
Path
)]=h;
137
}
138
}
139
return
true
;
140
}
141
142
AssetHandle
AssetRegistry::FindHandle
(
const
std::filesystem::path& p)
const
143
{
auto
it=m_ByPath.find(Normalize(p));
return
it==m_ByPath.end()?
InvalidAssetHandle
:it->second;}
144
const
AssetMetadata
*
AssetRegistry::Find
(
AssetHandle
h)
const
145
{
auto
it=m_ByHandle.find(h);
return
it==m_ByHandle.end()?
nullptr
:&it->second;}
146
const
AssetMetadata
*
AssetRegistry::Find
(
const
std::filesystem::path& p)
const
147
{
auto
h=
FindHandle
(p);
return
Find
(h);}
148
std::vector<AssetMetadata>
AssetRegistry::GetAll
()
const
149
{std::vector<AssetMetadata> v;v.reserve(m_ByHandle.size());
for
(
auto
&[_,m]:m_ByHandle)v.push_back(m);
return
v;}
150
}
AssetRegistry.h
AssetRegistry declarations for the Asset Management subsystem.
NoJob::AssetRegistry::Load
bool Load()
Definition
AssetRegistry.cpp:127
NoJob::AssetRegistry::Register
AssetHandle Register(const std::filesystem::path &path, AssetType type=AssetType::Unknown)
Definition
AssetRegistry.cpp:75
NoJob::AssetRegistry::AssetRegistry
AssetRegistry(std::filesystem::path root="Assets")
Definition
AssetRegistry.cpp:46
NoJob::AssetRegistry::TypeFromExtension
static AssetType TypeFromExtension(const std::filesystem::path &path)
Definition
AssetRegistry.cpp:61
NoJob::AssetRegistry::Find
const AssetMetadata * Find(AssetHandle handle) const
Definition
AssetRegistry.cpp:144
NoJob::AssetRegistry::Save
bool Save() const
Definition
AssetRegistry.cpp:118
NoJob::AssetRegistry::FindHandle
AssetHandle FindHandle(const std::filesystem::path &path) const
Definition
AssetRegistry.cpp:142
NoJob::AssetRegistry::Scan
void Scan()
Definition
AssetRegistry.cpp:101
NoJob::AssetRegistry::GetAll
std::vector< AssetMetadata > GetAll() const
Definition
AssetRegistry.cpp:148
NoJob
Definition
BTActionNode.h:13
NoJob::AssetHandle
std::uint64_t AssetHandle
Definition
Asset.h:13
NoJob::AssetType
AssetType
Definition
Asset.h:17
NoJob::AssetType::Mesh
@ Mesh
Definition
Asset.h:18
NoJob::AssetType::Unknown
@ Unknown
Definition
Asset.h:18
NoJob::AssetType::Scene
@ Scene
Definition
Asset.h:18
NoJob::AssetType::Texture2D
@ Texture2D
Definition
Asset.h:18
NoJob::AssetType::Audio
@ Audio
Definition
Asset.h:18
NoJob::AssetType::Material
@ Material
Definition
Asset.h:18
NoJob::AssetType::Script
@ Script
Definition
Asset.h:18
NoJob::AssetType::Prefab
@ Prefab
Definition
Asset.h:18
NoJob::InvalidAssetHandle
constexpr AssetHandle InvalidAssetHandle
Definition
Asset.h:14
NoJob::AssetMetadata
Definition
Asset.h:22
NoJob::AssetMetadata::LastWriteTime
std::uint64_t LastWriteTime
Definition
Asset.h:28
NoJob::AssetMetadata::Handle
AssetHandle Handle
Definition
Asset.h:23
NoJob::AssetMetadata::FileSize
std::uint64_t FileSize
Definition
Asset.h:27
NoJob::AssetMetadata::Path
std::filesystem::path Path
Definition
Asset.h:25
NoJob::AssetMetadata::Type
AssetType Type
Definition
Asset.h:24
NoJob::AssetMetadata::Name
std::string Name
Definition
Asset.h:26
Engine
Assets
AssetRegistry.cpp
Generated by
1.18.0