Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
30using namespace AngelScript;
31using namespace RoR;
32
33CScriptDictionary* 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
112void 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}
#define ROR_ASSERT(_EXPR)
Definition Application.h:40
A database of user-installed content alias 'mods' (vehicles, terrains...)
CScriptDictionary * CacheSystemQueryWrapper(CacheSystem *self, CScriptDictionary *dict)
static void RegisterRefCountingObjectPtr(AS_NAMESPACE_QUALIFIER asIScriptEngine *engine, const char *handle_name, const char *obj_name)
Definition CacheSystem.h:56
CacheEntryID_t number
Sequential number, assigned internally, used by Selector-GUI.
Definition CacheSystem.h:64
Ogre::String fname
filename
Definition CacheSystem.h:67
Ogre::String fext
file's extension
Definition CacheSystem.h:69
Ogre::String fpath
filepath relative to the .zip file
Definition CacheSystem.h:66
int categoryid
category id
Definition CacheSystem.h:72
Ogre::String dname
name parsed from the file
Definition CacheSystem.h:70
Ogre::String resource_group
Resource group of the loaded bundle. Empty if not loaded yet.
Definition CacheSystem.h:89
std::string resource_bundle_type
Archive type recognized by OGRE resource system: 'FileSystem' or 'Zip'.
Definition CacheSystem.h:80
std::string resource_bundle_path
Path of ZIP or directory which contains the media. Shared between CacheEntries, loaded only once.
Definition CacheSystem.h:81
bool deleted
is this mod deleted?
Definition CacheSystem.h:84
Ogre::String categoryname
category name
Definition CacheSystem.h:73
Ogre::String filecachename
preview image filename
Definition CacheSystem.h:87
A content database MOTIVATION: RoR users usually have A LOT of content installed.
size_t Query(CacheQuery &query)
AngelScript::asIScriptEngine * getEngine()
void RegisterCacheSystem(AngelScript::asIScriptEngine *engine)
defined in CacheSystemAngelscript.cpp
bool GetValueFromScriptDict(const std::string &log_msg, AngelScript::CScriptDictionary *dict, bool required, std::string const &key, const char *decl, T &out_value)
ScriptEngine * GetScriptEngine()
@ LT_Airplane
@ LT_Tuneup
@ LT_AssetPack
@ LT_Boat
@ LT_DashBoard
@ LT_Train
@ LT_AddonPart
@ LT_Skin
@ LT_Truck
@ LT_Gadget
@ LT_Car
@ LT_Load
@ LT_Terrain
@ LT_Trailer
@ LT_Vehicle
@ LT_None
@ LT_Extension
@ LT_AllBeam
std::string cqy_filter_guid
Exact match (case-insensitive); leave empty to disable.
std::string cqy_search_string
CacheSearchMethod cqy_search_method
std::vector< CacheQueryResult > cqy_results
RoR::LoaderType cqy_filter_type