RigsofRods
Soft-body Physics Simulation
ScriptUtils.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2013-2023 Petr Ohlidal
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 
22 #pragma once
23 
24 #ifdef USE_ANGELSCRIPT
25 
26 #include <angelscript.h>
27 #include "scriptdictionary/scriptdictionary.h"
28 #include "scriptarray/scriptarray.h"
29 #include "scriptbuilder/scriptbuilder.h"
30 
31 #include <list>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 namespace RoR {
37 
40 
41 template <typename T>
42 AngelScript::CScriptArray* VectorToScriptArray(const std::vector<T>& vec, const std::string& decl)
43 {
44  std::string arraydecl = fmt::format("array<{}>", decl);
45  AngelScript::asITypeInfo* typeinfo = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl(arraydecl.c_str());
46  AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(typeinfo, vec.size());
47 
48  for (AngelScript::asUINT i = 0; i < arr->GetSize(); i++)
49  {
50  // Set the value of each element
51  T tempval = vec[i];
52  arr->SetValue(i, &tempval);
53  }
54 
55  return arr;
56 }
57 
58 template <typename T, typename U>
59 AngelScript::CScriptArray* MapToScriptArray(std::map<T, U>& map, const std::string& decl)
60 {
61  std::string arraydecl = fmt::format("array<{}>", decl);
62  AngelScript::asITypeInfo* typeinfo = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl(arraydecl.c_str());
63  AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(typeinfo, map.size());
64 
65  for (auto itor = map.begin(); itor != map.end(); itor++)
66  {
67  // Set the value of each element
68  AngelScript::asUINT pos = static_cast<AngelScript::asUINT>(std::distance(map.begin(), itor));
69  arr->SetValue(pos, &itor->second);
70  }
71 
72  return arr;
73 }
74 
75 // Iterables:
76 
77 template <typename ItorT>
78 AngelScript::CScriptArray* IterableMapToScriptArray(ItorT begin, ItorT end, const std::string& decl)
79 {
80  std::string arraydecl = fmt::format("array<{}>", decl);
81  AngelScript::asITypeInfo* typeinfo = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl(arraydecl.c_str());
82  AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(typeinfo);
83 
84  for (auto itor = begin; itor != end; itor++)
85  {
86  arr->InsertLast(&itor->second);
87  }
88 
89  return arr;
90 }
91 
92 template <typename ItorT>
93 AngelScript::CScriptArray* IterableListToScriptArray(ItorT begin, ItorT end, const std::string& decl)
94 {
95  std::string arraydecl = fmt::format("array<{}>", decl);
96  AngelScript::asITypeInfo* typeinfo = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl(arraydecl.c_str());
97  AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(typeinfo);
98 
99  for (auto itor = begin; itor != end; itor++)
100  {
101  arr->InsertLast(&itor);
102  }
103 
104  return arr;
105 }
106 
107 template<typename T>
108 bool GetValueFromScriptDict(const std::string& log_msg, AngelScript::CScriptDictionary* dict, bool required, std::string const& key, const char* decl, T & out_value)
109 {
110  if (!dict)
111  {
112  // Dict is NULL
113  if (required)
114  {
115  App::GetScriptEngine()->SLOG(fmt::format("{}: ERROR, no parameters; '{}' is required.", log_msg, key));
116  }
117  return false;
118  }
119  auto itor = dict->find(key);
120  if (itor == dict->end())
121  {
122  // Key not found
123  if (required)
124  {
125  App::GetScriptEngine()->SLOG(fmt::format("{}: ERROR, required parameter '{}' not found.", log_msg, key));
126  }
127  return false;
128  }
129 
130  const int expected_typeid = App::GetScriptEngine()->getEngine()->GetTypeIdByDecl(decl);
131  const int actual_typeid = itor.GetTypeId();
132  if (actual_typeid != expected_typeid)
133  {
134  // Wrong type
135  if (required)
136  {
137  App::GetScriptEngine()->SLOG(fmt::format("{}: ERROR, required parameter '{}' must be a {}, instead got {}.",
138  log_msg, key, decl, App::GetScriptEngine()->getEngine()->GetTypeDeclaration(actual_typeid)));
139  }
140  return false;
141  }
142 
143  return itor.GetValue(&out_value, actual_typeid); // Error will be logged to Angelscript.log
144 }
145 
146 // Proof of concept: a read-only Dictionary view of a `std::map`-like container.
147 // Assumed to always use `std::string` for key.
148 // Not using RefCountingObject because this is only for use in the script, not C++
149 // Adopted from 'scriptdictionary.cpp' bundled with AngelScript SDK
150 template<typename T>
152 {
153 public:
154 
155  CReadonlyScriptDictView(const std::map<std::string, T>& map) : m_map(map), m_refcount(1) {}
156 
157  bool Exists(const std::string& key) const
158  {
159  return (m_map.find(key) != m_map.end());
160  }
161 
162  bool IsEmpty() const
163  {
164  if (m_map.size() == 0)
165  return true;
166 
167  return false;
168  }
169 
170  AngelScript::asUINT GetSize() const
171  {
172  return AngelScript::asUINT(m_map.size());
173  }
174 
175  T OpIndex(const std::string& key) const
176  {
177  auto it = m_map.find(key);
178  if (it != m_map.end())
179  return it->second;
180  else
181  return T{};
182  }
183 
184  AngelScript::CScriptArray* GetKeys() const
185  {
186  // Create the array object
187  AngelScript::CScriptArray* array = AngelScript::CScriptArray::Create(AngelScript::asGetActiveContext()->GetEngine()->GetTypeInfoByDecl("array<string>"), AngelScript::asUINT(m_map.size()));
188  long current = -1;
189  for (auto it = m_map.begin(); it != m_map.end(); it++)
190  {
191  current++;
192  *(std::string*)array->At(current) = it->first;
193  }
194 
195  return array;
196  }
197 
198  static void RegisterReadonlyScriptDictView(AngelScript::asIScriptEngine* engine, const char* decl, const char* value_decl)
199  {
200  using namespace AngelScript;
201 
202  int r;
203 
204  r = engine->RegisterObjectType(decl, sizeof(CReadonlyScriptDictView<T>), asOBJ_REF); ROR_ASSERT(r >= 0);
205  r = engine->RegisterObjectBehaviour(decl, asBEHAVE_ADDREF, "void f()", asMETHOD(CReadonlyScriptDictView<T>, AddRef), asCALL_THISCALL); ROR_ASSERT(r >= 0);
206  r = engine->RegisterObjectBehaviour(decl, asBEHAVE_RELEASE, "void f()", asMETHOD(CReadonlyScriptDictView <T>, Release), asCALL_THISCALL); ROR_ASSERT(r >= 0);
207 
208  r = engine->RegisterObjectMethod(decl, "bool exists(const string &in) const", asMETHOD(CReadonlyScriptDictView<T>, Exists), asCALL_THISCALL); ROR_ASSERT(r >= 0);
209  r = engine->RegisterObjectMethod(decl, "bool isEmpty() const", asMETHOD(CReadonlyScriptDictView<T>, IsEmpty), asCALL_THISCALL); ROR_ASSERT(r >= 0);
210  r = engine->RegisterObjectMethod(decl, "uint getSize() const", asMETHOD(CReadonlyScriptDictView<T>, GetSize), asCALL_THISCALL); ROR_ASSERT(r >= 0);
211  r = engine->RegisterObjectMethod(decl, "array<string> @getKeys() const", asMETHOD(CReadonlyScriptDictView<T>, GetKeys), asCALL_THISCALL); assert(r >= 0);
212 
213  std::string opIndexDecl = fmt::format("{}@ opIndex(const string &in)", value_decl);
214  r = engine->RegisterObjectMethod(decl, opIndexDecl.c_str(), asMETHOD(CReadonlyScriptDictView<T>, OpIndex), asCALL_THISCALL); ROR_ASSERT(r >= 0);
215  std::string opIndexConstDecl = fmt::format("const {}@ opIndex(const string &in) const", value_decl);
216  r = engine->RegisterObjectMethod(decl, opIndexConstDecl.c_str(), asMETHOD(CReadonlyScriptDictView<T>, OpIndex), asCALL_THISCALL); ROR_ASSERT(r >= 0);
217 
218  }
219 
220  // Angelscript refcounting
221  void AddRef() { m_refcount++; }
222  void Release() { m_refcount--; if (m_refcount == 0) { delete this; /* Comit suicide! This is legit in C++ and AngelScript relies on it. */ } }
223 private:
224  const std::map<std::string, T>& m_map;
226 };
227 
228 template <typename T>
230 {
231 public:
232 
233  CReadonlyScriptArrayView(const std::vector<T>& vec) : m_vec(vec), m_refcount(1) {}
234 
235  bool IsEmpty() const { return m_vec.empty(); }
236  unsigned GetSize() const { return m_vec.size(); }
237  T OpIndex(unsigned pos) { return m_vec.at(pos); }
238 
239  static void RegisterReadonlyScriptArrayView(AngelScript::asIScriptEngine* engine, const char* decl, const char* value_decl)
240  {
241  using namespace AngelScript;
242 
243  int r;
244 
245  r = engine->RegisterObjectType(decl, sizeof(CReadonlyScriptArrayView<T>), asOBJ_REF); ROR_ASSERT(r >= 0);
246  r = engine->RegisterObjectBehaviour(decl, asBEHAVE_ADDREF, "void f()", asMETHOD(CReadonlyScriptArrayView<T>, AddRef), asCALL_THISCALL); ROR_ASSERT(r >= 0);
247  r = engine->RegisterObjectBehaviour(decl, asBEHAVE_RELEASE, "void f()", asMETHOD(CReadonlyScriptArrayView <T>, Release), asCALL_THISCALL); ROR_ASSERT(r >= 0);
248 
249  r = engine->RegisterObjectMethod(decl, "bool isEmpty() const", asMETHOD(CReadonlyScriptArrayView<T>, IsEmpty), asCALL_THISCALL); ROR_ASSERT(r >= 0);
250  r = engine->RegisterObjectMethod(decl, "uint length() const", asMETHOD(CReadonlyScriptArrayView<T>, GetSize), asCALL_THISCALL); ROR_ASSERT(r >= 0);
251 
252  std::string opIndexDecl = fmt::format("{}@ opIndex(uint pos)", value_decl);
253  r = engine->RegisterObjectMethod(decl, opIndexDecl.c_str(), asMETHOD(CReadonlyScriptArrayView<T>, OpIndex), asCALL_THISCALL); ROR_ASSERT(r >= 0);
254  std::string opIndexConstDecl = fmt::format("const {}@ opIndex(uint pos) const", value_decl);
255  r = engine->RegisterObjectMethod(decl, opIndexConstDecl.c_str(), asMETHOD(CReadonlyScriptArrayView<T>, OpIndex), asCALL_THISCALL); ROR_ASSERT(r >= 0);
256 
257  }
258 
259  // Angelscript refcounting
260  void AddRef() { m_refcount++; }
261  void Release() { m_refcount--; if (m_refcount == 0) { delete this; /* Comit suicide! This is legit in C++ and AngelScript relies on it. */ } }
262 private:
263  const std::vector<T>& m_vec;
265 };
266 
267 // Example opCast behaviour
268 template<class A, class B>
270 {
271  // If the handle already is a null handle, then just return the null handle
272  if (!a) return 0;
273 
274  // Now try to dynamically cast the pointer to the wanted type
275  B* b = dynamic_cast<B*>(a);
276  if (b != 0)
277  {
278  // Since the cast was made, we need to increase the ref counter for the returned handle
279  b->AddRef();
280  }
281  return b;
282 }
283 
284 // Example opCast behaviour - for objects with asOBJ_NOCOUNT
285 template<class A, class B>
287 {
288  // If the handle already is a null handle, then just return the null handle
289  if (!a) return 0;
290 
291  // Now try to dynamically cast the pointer to the wanted type
292  return dynamic_cast<B*>(a);
293 }
294 
296 
297 } // namespace RoR
298 
299 #endif // USE_ANGELSCRIPT
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
RoR::CReadonlyScriptArrayView::m_refcount
int m_refcount
Definition: ScriptUtils.h:264
RoR::VectorToScriptArray
AngelScript::CScriptArray * VectorToScriptArray(const std::vector< T > &vec, const std::string &decl)
Definition: ScriptUtils.h:42
RoR::CReadonlyScriptArrayView::IsEmpty
bool IsEmpty() const
Definition: ScriptUtils.h:235
RoR::CReadonlyScriptArrayView::Release
void Release()
Definition: ScriptUtils.h:261
RoR::CReadonlyScriptArrayView::AddRef
void AddRef()
Definition: ScriptUtils.h:260
RoR::CReadonlyScriptDictView::Exists
bool Exists(const std::string &key) const
Definition: ScriptUtils.h:157
format
Truck file format(technical spec)
RoR::CReadonlyScriptDictView
Definition: ScriptUtils.h:151
RoR::CReadonlyScriptDictView::Release
void Release()
Definition: ScriptUtils.h:222
RoR::ScriptEngine::getEngine
AngelScript::asIScriptEngine * getEngine()
Definition: ScriptEngine.h:223
RoR::GetValueFromScriptDict
bool GetValueFromScriptDict(const std::string &log_msg, AngelScript::CScriptDictionary *dict, bool required, std::string const &key, const char *decl, T &out_value)
Definition: ScriptUtils.h:108
RoR::CReadonlyScriptDictView::RegisterReadonlyScriptDictView
static void RegisterReadonlyScriptDictView(AngelScript::asIScriptEngine *engine, const char *decl, const char *value_decl)
Definition: ScriptUtils.h:198
RoR::CReadonlyScriptDictView::m_map
const std::map< std::string, T > & m_map
Definition: ScriptUtils.h:224
RoR::IterableMapToScriptArray
AngelScript::CScriptArray * IterableMapToScriptArray(ItorT begin, ItorT end, const std::string &decl)
Definition: ScriptUtils.h:78
RoR::App::GetScriptEngine
ScriptEngine * GetScriptEngine()
Definition: Application.cpp:279
RoR::CReadonlyScriptArrayView::m_vec
const std::vector< T > & m_vec
Definition: ScriptUtils.h:263
RoR::IterableListToScriptArray
AngelScript::CScriptArray * IterableListToScriptArray(ItorT begin, ItorT end, const std::string &decl)
Definition: ScriptUtils.h:93
RoR::CReadonlyScriptDictView::IsEmpty
bool IsEmpty() const
Definition: ScriptUtils.h:162
RoR::ScriptRefCastNoCount
B * ScriptRefCastNoCount(A *a)
Definition: ScriptUtils.h:286
RoR::CReadonlyScriptArrayView::OpIndex
T OpIndex(unsigned pos)
Definition: ScriptUtils.h:237
RoR::MapToScriptArray
AngelScript::CScriptArray * MapToScriptArray(std::map< T, U > &map, const std::string &decl)
Definition: ScriptUtils.h:59
RoR::CReadonlyScriptDictView::GetSize
AngelScript::asUINT GetSize() const
Definition: ScriptUtils.h:170
RoR::ScriptRefCast
B * ScriptRefCast(A *a)
Definition: ScriptUtils.h:269
RoR::CReadonlyScriptDictView::OpIndex
T OpIndex(const std::string &key) const
Definition: ScriptUtils.h:175
RoR::CReadonlyScriptArrayView::RegisterReadonlyScriptArrayView
static void RegisterReadonlyScriptArrayView(AngelScript::asIScriptEngine *engine, const char *decl, const char *value_decl)
Definition: ScriptUtils.h:239
RoR::CReadonlyScriptDictView::AddRef
void AddRef()
Definition: ScriptUtils.h:221
RoR::CReadonlyScriptArrayView::GetSize
unsigned GetSize() const
Definition: ScriptUtils.h:236
RoR::CReadonlyScriptArrayView::CReadonlyScriptArrayView
CReadonlyScriptArrayView(const std::vector< T > &vec)
Definition: ScriptUtils.h:233
RoR::CReadonlyScriptDictView::m_refcount
int m_refcount
Definition: ScriptUtils.h:225
RoR::CReadonlyScriptDictView::CReadonlyScriptDictView
CReadonlyScriptDictView(const std::map< std::string, T > &map)
Definition: ScriptUtils.h:155
RoR::CReadonlyScriptArrayView
Definition: ScriptUtils.h:229
RoR
Definition: AppContext.h:36
RoR::CReadonlyScriptDictView::GetKeys
AngelScript::CScriptArray * GetKeys() const
Definition: ScriptUtils.h:184