Unravel Engine C++ Reference
Loading...
Searching...
No Matches
assets_bindings.cpp
Go to the documentation of this file.
2#include "script_bindings.h"
4
5#include "../script_interop.h"
6
10#include <engine/ecs/prefab.h>
14#include <graphics/texture.h>
15
16namespace unravel
17{
18namespace
19{
20
21struct dotnet_asset
22{
23 virtual auto get_asset_uuid(const hpp::uuid& uid) const -> hpp::uuid = 0;
24 virtual auto get_asset_uuid(const std::string& key) const -> hpp::uuid = 0;
25};
26
27template<typename T>
28struct dotnet_asset_impl : dotnet_asset
29{
30 auto get_asset_uuid(const hpp::uuid& uid) const -> hpp::uuid override
31 {
32 auto& ctx = engine::context();
33 auto& am = ctx.get_cached<asset_manager>();
34
35 auto asset = am.get_asset<T>(uid);
36 return asset.uid();
37 }
38
39 auto get_asset_uuid(const std::string& key) const -> hpp::uuid override
40 {
41 auto& ctx = engine::context();
42 auto& am = ctx.get_cached<asset_manager>();
43
44 auto asset = am.get_asset<T>(key);
45 return asset.uid();
46 }
47};
48
49auto get_dotnet_asset(size_t type_hash) -> const dotnet_asset*
50{
51 // clang-format off
52 static std::map<size_t, std::shared_ptr<dotnet_asset>> reg =
53 {
54 {dotnet::type::get_hash("Unravel.Core.Texture"), std::make_shared<dotnet_asset_impl<gfx::texture>>()},
55 {dotnet::type::get_hash("Unravel.Core.Material"), std::make_shared<dotnet_asset_impl<material>>()},
56 {dotnet::type::get_hash("Unravel.Core.Mesh"), std::make_shared<dotnet_asset_impl<mesh>>()},
57 {dotnet::type::get_hash("Unravel.Core.AnimationClip"), std::make_shared<dotnet_asset_impl<animation_clip>>()},
58 {dotnet::type::get_hash("Unravel.Core.Prefab"), std::make_shared<dotnet_asset_impl<prefab>>()},
59 {dotnet::type::get_hash("Unravel.Core.Scene"), std::make_shared<dotnet_asset_impl<scene_prefab>>()},
60 {dotnet::type::get_hash("Unravel.Core.PhysicsMaterial"), std::make_shared<dotnet_asset_impl<physics_material>>()},
61 {dotnet::type::get_hash("Unravel.Core.AudioClip"), std::make_shared<dotnet_asset_impl<audio_clip>>()},
62 {dotnet::type::get_hash("Unravel.Core.Font"), std::make_shared<dotnet_asset_impl<font>>()}
63 };
64 // clang-format on
65
66 auto it = reg.find(type_hash);
67 if(it != reg.end())
68 {
69 return it->second.get();
70 }
71 static const dotnet_asset* empty{};
72 return empty;
73};
74
75auto internal_m2n_get_asset_by_uuid(const hpp::uuid& uid, const dotnet::type& type) -> hpp::uuid
76{
77 if(auto asset = get_dotnet_asset(type.get_hash()))
78 {
79 return asset->get_asset_uuid(uid);
80 }
81
82 return {};
83}
84
85auto internal_m2n_get_asset_by_key(const std::string& key, const dotnet::type& type) -> hpp::uuid
86{
87 if(auto asset = get_dotnet_asset(type.get_hash()))
88 {
89 return asset->get_asset_uuid(key);
90 }
91
92 return {};
93}
94
95auto internal_m2n_get_material_properties(const hpp::uuid& uid) -> dotnetpp_backend::managed_interface::material_properties
96{
97 using converter = dotnet::managed_interface::converter;
98
99 auto& ctx = engine::context();
100 auto& am = ctx.get_cached<asset_manager>();
101
103 auto asset = am.get_asset<material>(uid);
104 if(!asset)
105 {
106 return props;
107 }
108 auto material = asset.get();
109
111}
112
113auto internal_m2n_audio_clip_get_length(const hpp::uuid& uid) -> float
114{
115 auto& ctx = engine::context();
116 auto& am = ctx.get_cached<asset_manager>();
117
118 auto asset = am.get_asset<audio_clip>(uid);
119
120 if(asset.is_valid())
121 {
122 if(auto clip = asset.get())
123 {
124 float secs = clip->get_info().duration.count();
125 return secs;
126 }
127 }
128
129 return 0.0f;
130}
131
132auto internal_m2n_animation_clip_get_length(const hpp::uuid& uid) -> float
133{
134 auto& ctx = engine::context();
135 auto& am = ctx.get_cached<asset_manager>();
136
137 auto asset = am.get_asset<animation_clip>(uid);
138
139 if(asset.is_valid())
140 {
141 if(auto clip = asset.get())
142 {
143 return clip->duration.count();
144 }
145 }
146
147 return 0.0f;
148}
149
150auto internal_m2n_animation_clip_get_name(const hpp::uuid& uid) -> std::string
151{
152 auto& ctx = engine::context();
153 auto& am = ctx.get_cached<asset_manager>();
154
155 auto asset = am.get_asset<animation_clip>(uid);
156
157 if(asset.is_valid())
158 {
159 if(auto clip = asset.get())
160 {
161 return clip->name;
162 }
163 }
164
165 return {};
166}
167
168} // namespace
169
171{
172 APPLOG_TRACE("{}", __func__);
173 {
174 auto reg = dotnet::internal_call_registry("Unravel.Core.Assets");
175 reg.add_internal_call("internal_m2n_get_asset_by_uuid", dotnet_internal_call(internal_m2n_get_asset_by_uuid));
176 reg.add_internal_call("internal_m2n_get_asset_by_key", dotnet_internal_call(internal_m2n_get_asset_by_key));
177 reg.add_internal_call("internal_m2n_get_material_properties",
178 dotnet_internal_call(internal_m2n_get_material_properties));
179
180 }
181 {
182 auto reg = dotnet::internal_call_registry("Unravel.Core.AudioClip");
183 reg.add_internal_call("internal_m2n_audio_clip_get_length", dotnet_internal_call(internal_m2n_audio_clip_get_length));
184
185 }
186 {
187 auto reg = dotnet::internal_call_registry("Unravel.Core.AnimationClip");
188 reg.add_internal_call("internal_m2n_animation_clip_get_length", dotnet_internal_call(internal_m2n_animation_clip_get_length));
189 reg.add_internal_call("internal_m2n_animation_clip_get_name", dotnet_internal_call(internal_m2n_animation_clip_get_name));
190 }
191}
192
193} // namespace unravel
#define APPLOG_TRACE(...)
Definition logging.h:17
texture_job_type type
void register_assets_script_bindings()
auto get_material_properties(const material::sptr &material) -> dotnetpp_backend::managed_interface::material_properties
static auto context() -> rtti::context &
Definition engine.cpp:111