Unravel Engine C++ Reference
Loading...
Searching...
No Matches
core_runtime_bindings.cpp
Go to the documentation of this file.
2#include "script_bindings.h"
3
4#include "../script_interop.h"
5
6#include <engine/events.h>
7#include <engine/play_mode.h>
10#include <seq/seq.h>
12#include <uuid/uuid.h>
13
14namespace unravel
15{
16namespace
17{
18
19void internal_m2n_log_trace(const std::string& message, const std::string& func, const std::string& file, int line)
20{
21 APPLOG_TRACE_LOC(file.c_str(), line, func.c_str(), message);
22}
23
24void internal_m2n_log_info(const std::string& message, const std::string& func, const std::string& file, int line)
25{
26 APPLOG_INFO_LOC(file.c_str(), line, func.c_str(), message);
27}
28
29void internal_m2n_log_warning(const std::string& message, const std::string& func, const std::string& file, int line)
30{
31 APPLOG_WARNING_LOC(file.c_str(), line, func.c_str(), message);
32}
33
34void internal_m2n_log_error(const std::string& message, const std::string& func, const std::string& file, int line)
35{
36 APPLOG_ERROR_LOC(file.c_str(), line, func.c_str(), message);
37}
38
39//-------------------------------------------------------------------------
40
41void internal_m2n_application_quit()
42{
43 auto delay = seq::delay(0ms);
44 delay.on_end.connect(
45 []()
46 {
47 auto& ctx = engine::context();
48 auto& ev = ctx.get_cached<events>();
49 ctx.get_cached<play_mode>().set_active(ctx, false);
50 });
51
52 seq::queue(delay, "script");
53}
54
55void internal_m2n_set_time_scale(float scale)
56{
57 auto& ctx = engine::context();
58 auto& sim = ctx.get_cached<simulation>();
59 sim.set_time_scale(scale);
60}
61
62void internal_m2n_profiler_add_record(const std::string& name, float time_ms)
63{
64 auto profiler = get_app_profiler();
65 if(profiler)
66 {
67 profiler->add_record(name, time_ms);
68 }
69}
70
71auto m2n_test_uuid(const hpp::uuid& uid) -> hpp::uuid
72{
73 APPLOG_INFO("{}:: From C# {}", __func__, hpp::to_string(uid));
74
75 auto newuid = generate_uuid();
76 APPLOG_INFO("{}:: New C++ {}", __func__, hpp::to_string(newuid));
77
78 return newuid;
79}
80
81auto internal_m2n_layers_layer_to_name(int layer) -> const std::string&
82{
83 auto& ctx = engine::context();
84 auto& csettings = ctx.get<settings>();
85
86 if(layer >= csettings.layer.layers.size())
87 {
88 dotnet::raise_exception("System", "Exception", fmt::format("Layer index {} is out of bounds.", layer));
89
90 static const std::string empty;
91 return empty;
92 }
93 return csettings.layer.layers[layer];
94}
95
96auto internal_m2n_layers_name_to_layer(const std::string& name) -> int
97{
98 auto& ctx = engine::context();
99 auto& csettings = ctx.get<settings>();
100
101 auto it = std::find(csettings.layer.layers.begin(), csettings.layer.layers.end(), name);
102 if(it != csettings.layer.layers.end())
103 {
104 return static_cast<int>(std::distance(csettings.layer.layers.begin(), it));
105 }
106
107 return -1;
108}
109
110} // namespace
111
113{
114 APPLOG_TRACE("{}", __func__);
115
116 {
117 auto reg = dotnet::internal_call_registry("Unravel.Core.Log");
118 reg.add_internal_call("internal_m2n_log_trace", dotnet_internal_call(internal_m2n_log_trace));
119 reg.add_internal_call("internal_m2n_log_info", dotnet_internal_call(internal_m2n_log_info));
120 reg.add_internal_call("internal_m2n_log_warning", dotnet_internal_call(internal_m2n_log_warning));
121 reg.add_internal_call("internal_m2n_log_error", dotnet_internal_call(internal_m2n_log_error));
122
123 }
124 {
125 auto reg = dotnet::internal_call_registry("Unravel.Core.Tests");
126 reg.add_internal_call("m2n_test_uuid", dotnet_internal_call(m2n_test_uuid));
127
128 }
129 {
130 auto reg = dotnet::internal_call_registry("Unravel.Core.LayerMask");
131 reg.add_internal_call("internal_m2n_layers_layer_to_name", dotnet_internal_call(internal_m2n_layers_layer_to_name));
132 reg.add_internal_call("internal_m2n_layers_name_to_layer", dotnet_internal_call(internal_m2n_layers_name_to_layer));
133
134 }
135 {
136 auto reg = dotnet::internal_call_registry("Unravel.Core.Application");
137 reg.add_internal_call("internal_m2n_application_quit", dotnet_internal_call(internal_m2n_application_quit));
138
139 }
140 {
141 auto reg = dotnet::internal_call_registry("Unravel.Core.Time");
142 reg.add_internal_call("internal_m2n_set_time_scale", dotnet_internal_call(internal_m2n_set_time_scale));
143
144 }
145 {
146 auto reg = dotnet::internal_call_registry("Unravel.Core.Profiler");
147 reg.add_internal_call("internal_m2n_profiler_add_record", dotnet_internal_call(internal_m2n_profiler_add_record));
148
149 }
150 {
151 auto reg = dotnet::internal_call_registry("Unravel.Core.GCMonitor");
152 reg.add_internal_call("internal_m2n_get_dotnet_heap_size", dotnet_internal_call(dotnet::gc_get_heap_size));
153 reg.add_internal_call("internal_m2n_get_dotnet_used_size", dotnet_internal_call(dotnet::gc_get_used_size));
154 }
155}
156
157} // namespace unravel
std::string name
Definition hub.cpp:33
#define APPLOG_INFO(...)
Definition logging.h:18
#define APPLOG_INFO_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
Definition logging.h:34
#define APPLOG_WARNING_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
Definition logging.h:36
#define APPLOG_ERROR_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
Definition logging.h:38
#define APPLOG_TRACE_LOC(FILE_LOC, LINE_LOC, FUNC_LOC,...)
Definition logging.h:32
#define APPLOG_TRACE(...)
Definition logging.h:17
auto queue(seq_action action, const seq_scope_policy &scope_policy, hpp::source_location location) -> seq_id_t
Queues a new action.
Definition seq.cpp:14
auto delay(const duration_t &duration, const sentinel_t &sentinel) -> seq_action
Creates a delay action.
Definition seq_core.cpp:182
auto get_app_profiler() -> performance_profiler *
Definition profiler.cpp:99
auto generate_uuid() -> hpp::uuid
Definition uuid.cpp:25
void register_core_runtime_script_bindings()
std::vector< float > scale
emitter_sim_state sim
static auto context() -> rtti::context &
Definition engine.cpp:111