galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Script.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
10#include "galaxy/core/ServiceLocator.hpp"
12
13#include "Script.hpp"
14
15namespace galaxy
16{
17 namespace components
18 {
20 : Serializable {}
21 {
22 }
23
24 Script::Script(const nlohmann::json& json)
25 : Serializable {}
26 {
27 deserialize(json);
28 }
29
31 {
32 this->m_file = std::move(s.m_file);
33 this->m_self = std::move(s.m_self);
34 this->m_update = std::move(s.m_update);
35 this->m_show_functions = s.m_show_functions;
36 this->m_show_userdata = s.m_show_userdata;
37 this->m_show_unknown = s.m_show_unknown;
38 }
39
41 {
42 if (this != &s)
43 {
44 this->m_file = std::move(s.m_file);
45 this->m_self = std::move(s.m_self);
46 this->m_update = std::move(s.m_update);
47 this->m_show_functions = s.m_show_functions;
48 this->m_show_userdata = s.m_show_userdata;
49 this->m_show_unknown = s.m_show_unknown;
50 }
51
52 return *this;
53 }
54
56 {
57 }
58
59 void Script::load(std::string_view file)
60 {
61 if (m_self.valid())
62 {
63 sol::function destruct = m_self["destruct"];
64 if (destruct.valid())
65 {
66 destruct(m_self);
67 }
68
69 m_self.abandon();
70 }
71
72 m_file = file;
73
74 if (!core::ServiceLocator<fs::VirtualFileSystem>::ref().exists(m_file))
75 {
76 GALAXY_LOG(GALAXY_ERROR, "Failed to find script '{0}'.", m_file);
77 m_file = {};
78 }
79 }
80
81 const std::string& Script::file() const
82 {
83 return m_file;
84 }
85
86 nlohmann::json Script::serialize()
87 {
88 nlohmann::json json = "{}"_json;
89 json["file"] = m_file;
90 json["show_functions"] = m_show_functions;
91 json["show_userdata"] = m_show_userdata;
92 json["show_unknown"] = m_show_unknown;
93
94 return json;
95 }
96
97 void Script::deserialize(const nlohmann::json& json)
98 {
99 load(json.at("file"));
100
101 if (json.contains("show_functions"))
102 {
103 m_show_functions = json.at("show_functions");
104 }
105
106 if (json.contains("show_userdata"))
107 {
108 m_show_userdata = json.at("show_userdata");
109 }
110
111 if (json.contains("show_unknown"))
112 {
113 m_show_unknown = json.at("show_unknown");
114 }
115 }
116 } // namespace components
117} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
High level abstraction of a lua script.
Definition Script.hpp:31
virtual ~Script()
Destructor.
Definition Script.cpp:55
bool m_show_userdata
For editor. Shows userdata types.
Definition Script.hpp:128
bool m_show_unknown
For editor. Show unknown type data.
Definition Script.hpp:133
const std::string & file() const
Get script file path.
Definition Script.cpp:81
Script & operator=(Script &&)
Move assignment operator.
Definition Script.cpp:40
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Script.cpp:97
void load(std::string_view file)
Load a script.
Definition Script.cpp:59
bool m_show_functions
For editor. Show functions.
Definition Script.hpp:123
nlohmann::json serialize() override
Serializes object.
Definition Script.cpp:86
sol::table m_self
The script object (table) returned by a lua script.
Definition Script.hpp:107
std::string m_file
Script file.
Definition Script.hpp:118
sol::function m_update
The update function belonging to the lua table.
Definition Script.hpp:112
Timer.hpp galaxy.
Definition Async.hpp:17