RigsofRods  2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CacheSystemAngelscript.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2023 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "Actor.h"
23 #include "AngelScriptBindings.h"
24 #include "CacheSystem.h"
25 #include "ScriptEngine.h"
26 #include "ScriptUtils.h"
27 
28 #include <angelscript.h>
29 
30 using namespace AngelScript;
31 using namespace RoR;
32 
33 CScriptDictionary* CacheSystemQueryWrapper(CacheSystem* self, CScriptDictionary* dict)
34 {
35  CacheQuery query;
36  std::string log_msg = "modcache.query(): ";
37  std::string search_expr;
38  if (!GetValueFromScriptDict(log_msg, dict, /*required:*/true, "filter_type", "LoaderType", query.cqy_filter_type))
39  {
40  return nullptr;
41  }
42  int64_t i64_filter_category_id; // AngelScript's `Dictionary` converts all ints int `int64`
43  GetValueFromScriptDict(log_msg, dict, /*required:*/false, "filter_category_id", "int64", i64_filter_category_id);
44  query.cqy_filter_category_id = i64_filter_category_id;
45  GetValueFromScriptDict(log_msg, dict, /*required:*/false, "filter_guid", "string", query.cqy_filter_guid);
46  GetValueFromScriptDict(log_msg, dict, /*required:*/false, "search_expr", "string", search_expr);
47 
48  // FIXME: Copypasta of `GUI::MainSelector::UpdateSearchParams()`
49  if (search_expr.find(":") == std::string::npos)
50  {
51  query.cqy_search_method = CacheSearchMethod::FULLTEXT;
52  query.cqy_search_string = search_expr;
53  }
54  else
55  {
56  Ogre::StringVector v = Ogre::StringUtil::split(search_expr, ":");
57  if (v.size() < 2)
58  {
59  query.cqy_search_method = CacheSearchMethod::NONE;
60  query.cqy_search_string = "";
61  }
62  else if (v[0] == "guid")
63  {
64  query.cqy_search_method = CacheSearchMethod::GUID;
65  query.cqy_search_string = v[1];
66  }
67  else if (v[0] == "author")
68  {
69  query.cqy_search_method = CacheSearchMethod::AUTHORS;
70  query.cqy_search_string = v[1];
71  }
72  else if (v[0] == "wheels")
73  {
74  query.cqy_search_method = CacheSearchMethod::WHEELS;
75  query.cqy_search_string = v[1];
76  }
77  else if (v[0] == "file")
78  {
79  query.cqy_search_method = CacheSearchMethod::FILENAME;
80  query.cqy_search_string = v[1];
81  }
82  else
83  {
84  query.cqy_search_method = CacheSearchMethod::NONE;
85  query.cqy_search_string = "";
86  }
87  }
88  // END copypasta
89 
90  size_t results_count = self->Query(query);
91 
92  asITypeInfo* typeinfo_array_entries = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl("array<CacheEntryClass@>");
93  asITypeInfo* typeinfo_array_scores = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl("array<uint>");
94 
95  CScriptArray* results_entries = CScriptArray::Create(typeinfo_array_entries);
96  CScriptArray* results_scores = CScriptArray::Create(typeinfo_array_scores);
97  for (CacheQueryResult& result: query.cqy_results)
98  {
99  CacheEntry* entry_ptr = result.cqr_entry.GetRef();
100  results_entries->InsertLast(&entry_ptr);
101  results_scores->InsertLast(&result.cqr_score);
102  }
103 
104  CScriptDictionary* results_dict = CScriptDictionary::Create(App::GetScriptEngine()->getEngine());
105  results_dict->Set("count", (asINT64)results_count);
106  results_dict->Set("entries", results_entries, typeinfo_array_entries->GetTypeId());
107  results_dict->Set("scores", results_scores, typeinfo_array_scores->GetTypeId());
108 
109  return results_dict;
110 }
111 
112 void RoR::RegisterCacheSystem(asIScriptEngine *engine)
113 {
114  CacheEntry::RegisterRefCountingObject(engine, "CacheEntryClass");
115  CacheEntryPtr::RegisterRefCountingObjectPtr(engine, "CacheEntryClassPtr", "CacheEntryClass");
116 
117  int result;
118 
119  // enum LoaderType, which is a primary filter for modcache queries
120  result = engine->RegisterEnum("LoaderType"); ROR_ASSERT(result >= 0);
121  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_NONE", LT_None); ROR_ASSERT(result >= 0);
122  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_TERRAIN", LT_Terrain); ROR_ASSERT(result >= 0);
123  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_VEHICLE", LT_Vehicle); ROR_ASSERT(result >= 0);
124  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_TRUCK", LT_Truck); ROR_ASSERT(result >= 0);
125  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_CAR", LT_Car); ROR_ASSERT(result >= 0);
126  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_BOAT", LT_Boat); ROR_ASSERT(result >= 0);
127  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_AIRPLANE", LT_Airplane); ROR_ASSERT(result >= 0);
128  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_TRAILER", LT_Trailer); ROR_ASSERT(result >= 0);
129  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_TRAIN", LT_Train); ROR_ASSERT(result >= 0);
130  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_LOAD", LT_Load); ROR_ASSERT(result >= 0);
131  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_EXTENSION", LT_Extension); ROR_ASSERT(result >= 0);
132  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_SKIN", LT_Skin); ROR_ASSERT(result >= 0);
133  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_ALLBEAM", LT_AllBeam); ROR_ASSERT(result >= 0);
134  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_ADDONPART", LT_AddonPart); ROR_ASSERT(result >= 0);
135  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_TUNEUP", LT_Tuneup); ROR_ASSERT(result >= 0);
136  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_ASSETPACK", LT_AssetPack); ROR_ASSERT(result >= 0);
137  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_DASHBOARD", LT_DashBoard); ROR_ASSERT(result >= 0);
138  result = engine->RegisterEnumValue("LoaderType", "LOADER_TYPE_GADGET", LT_Gadget); ROR_ASSERT(result >= 0);
139 
140  // class CacheEntry, with read-only property access
141  // (Please maintain the same order as in 'CacheSystem.h' and 'doc/*/CacheEntryClass.h')
142 
143  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_fpath() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->fpath;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
144  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_fname() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->fname;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
145  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_fext() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->fext;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
146  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_dname() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->dname;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
147  result = engine->RegisterObjectMethod("CacheEntryClass", "int get_categoryid() const property", asFUNCTIONPR([](CacheEntry* self) -> int {return self->categoryid;}, (CacheEntry*), int), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
148  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_categoryname() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->categoryname;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
149  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_resource_bundle_type() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->resource_bundle_type;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
150  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_resource_bundle_path() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->resource_bundle_path;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
151  result = engine->RegisterObjectMethod("CacheEntryClass", "int get_number() const property", asFUNCTIONPR([](CacheEntry* self) -> int {return self->number;}, (CacheEntry*), int), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
152  result = engine->RegisterObjectMethod("CacheEntryClass", "bool get_deleted() const property", asFUNCTIONPR([](CacheEntry* self) -> bool {return self->deleted;}, (CacheEntry*), bool), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
153  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_filecachename() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->filecachename;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
154  result = engine->RegisterObjectMethod("CacheEntryClass", "const string& get_resource_group() const property", asFUNCTIONPR([](CacheEntry* self) -> const std::string& {return self->resource_group;}, (CacheEntry*), const std::string&), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
155 
156  // class CacheSystem (non-counted reference type)
157  result = engine->RegisterObjectType("CacheSystemClass", sizeof(CacheSystem), asOBJ_REF | asOBJ_NOCOUNT); ROR_ASSERT(result>=0);
158  result = engine->RegisterObjectMethod("CacheSystemClass", "CacheEntryClassPtr @findEntryByFilename(LoaderType, bool, const string &in)", asMETHOD(CacheSystem,FindEntryByFilename), asCALL_THISCALL); ROR_ASSERT(result>=0);
159  result = engine->RegisterObjectMethod("CacheSystemClass", "CacheEntryClassPtr @getEntryByNumber(int)", asMETHOD(CacheSystem,GetEntryByNumber), asCALL_THISCALL); ROR_ASSERT(result>=0);
160  result = engine->RegisterObjectMethod("CacheSystemClass", "dictionary@ query(dictionary@)", asFUNCTION(CacheSystemQueryWrapper), asCALL_CDECL_OBJFIRST); ROR_ASSERT(result>=0);
161 
162 }
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
RoR::CacheQueryResult::cqr_score
size_t cqr_score
Definition: CacheSystem.h:154
RoR::CacheQuery::cqy_filter_category_id
int cqy_filter_category_id
Definition: CacheSystem.h:172
RoR::LT_AssetPack
@ LT_AssetPack
Definition: Application.h:472
RoR::LT_AddonPart
@ LT_AddonPart
Definition: Application.h:470
AngelScriptBindings.h
RoR::RegisterCacheSystem
void RegisterCacheSystem(AngelScript::asIScriptEngine *engine)
defined in CacheSystemAngelscript.cpp
RoR::CacheQuery::cqy_search_method
CacheSearchMethod cqy_search_method
Definition: CacheSystem.h:175
RoR::LT_Skin
@ LT_Skin
Definition: Application.h:468
RoR::LT_Tuneup
@ LT_Tuneup
Definition: Application.h:471
RoR::CacheQuery
Definition: CacheSystem.h:167
RoR::ScriptEngine::getEngine
AngelScript::asIScriptEngine * getEngine()
Definition: ScriptEngine.h:291
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:109
RoR::LT_Load
@ LT_Load
Definition: Application.h:466
RoR::LT_Airplane
@ LT_Airplane
Definition: Application.h:463
RoR::LT_Car
@ LT_Car
Definition: Application.h:461
RoR::LT_Extension
@ LT_Extension
Definition: Application.h:467
RoR::CacheQueryResult::cqr_entry
CacheEntryPtr cqr_entry
Definition: CacheSystem.h:153
RoR::CacheSystem
A content database MOTIVATION: RoR users usually have A LOT of content installed.
Definition: CacheSystem.h:283
Actor.h
RoR::App::GetScriptEngine
ScriptEngine * GetScriptEngine()
Definition: Application.cpp:296
RoR::LT_Boat
@ LT_Boat
Definition: Application.h:462
RoR::LT_Truck
@ LT_Truck
Definition: Application.h:460
CacheSystem.h
A database of user-installed content alias 'mods' (vehicles, terrains...)
RoR::CacheQueryResult
Definition: CacheSystem.h:147
ScriptEngine.h
RoR::LT_Gadget
@ LT_Gadget
Definition: Application.h:474
RoR::LT_Vehicle
@ LT_Vehicle
Definition: Application.h:459
RefCountingObjectPtr::GetRef
T * GetRef()
Definition: RefCountingObjectPtr.h:50
RoR::LT_Trailer
@ LT_Trailer
Definition: Application.h:464
RoR::LT_DashBoard
@ LT_DashBoard
Definition: Application.h:473
RoR::CacheQuery::cqy_search_string
std::string cqy_search_string
Definition: CacheSystem.h:176
RoR::CacheEntry
Definition: CacheSystem.h:55
RoR::LT_Terrain
@ LT_Terrain
Definition: Application.h:458
RoR::CacheQuery::cqy_filter_type
RoR::LoaderType cqy_filter_type
Definition: CacheSystem.h:171
RoR::LT_None
@ LT_None
Definition: Application.h:457
RoR::LT_AllBeam
@ LT_AllBeam
Definition: Application.h:469
RoR::CacheQuery::cqy_filter_guid
std::string cqy_filter_guid
Exact match (case-insensitive); leave empty to disable.
Definition: CacheSystem.h:173
ScriptUtils.h
RoR
Definition: AppContext.h:36
RoR::CacheQuery::cqy_results
std::vector< CacheQueryResult > cqy_results
Definition: CacheSystem.h:181
RoR::LT_Train
@ LT_Train
Definition: Application.h:465
CacheSystemQueryWrapper
CScriptDictionary * CacheSystemQueryWrapper(CacheSystem *self, CScriptDictionary *dict)
Definition: CacheSystemAngelscript.cpp:33