Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
CacheSystemAngelscriptWrappers.h
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
27
28#pragma once
29
30#include "Actor.h"
31#include "CacheSystem.h"
32#include "ScriptEngine.h"
33#include "ScriptUtils.h"
34
35#include <angelscript.h>
36
38
39using namespace AngelScript;
40using namespace RoR;
41
42// class CacheEntry - read-only property access
43// (Please maintain the same order as in 'CacheSystem.h' and 'doc/*/CacheEntryClass.h')
44static const std::string& CacheEntry_get_fpath(CacheEntry* self) { return self->fpath; }
45static const std::string& CacheEntry_get_fname(CacheEntry* self) { return self->fname; }
46static const std::string& CacheEntry_get_fext(CacheEntry* self) { return self->fext; }
47static const std::string& CacheEntry_get_dname(CacheEntry* self) { return self->dname; }
48static int CacheEntry_get_categoryid(CacheEntry* self) { return self->categoryid; }
49static const std::string& CacheEntry_get_categoryname(CacheEntry* self) { return self->categoryname; }
50static const std::string& CacheEntry_get_resource_bundle_type(CacheEntry* self) { return self->resource_bundle_type; }
51static const std::string& CacheEntry_get_resource_bundle_path(CacheEntry* self) { return self->resource_bundle_path; }
52static int CacheEntry_get_number(CacheEntry* self) { return self->number; }
53static bool CacheEntry_get_deleted(CacheEntry* self) { return self->deleted; }
54static const std::string& CacheEntry_get_filecachename(CacheEntry* self) { return self->filecachename; }
55static const std::string& CacheEntry_get_resource_group(CacheEntry* self) { return self->resource_group; }
56
57// class CacheSystem
58static CScriptDictionary* CacheSystemQueryWrapper(CacheSystem* self, CScriptDictionary* dict)
59{
60 CacheQuery query;
61 std::string log_msg = "modcache.query(): ";
62 std::string search_expr;
63 if (!GetValueFromScriptDict(log_msg, dict, /*required:*/true, "filter_type", "LoaderType", query.cqy_filter_type))
64 {
65 return nullptr;
66 }
67 int64_t i64_filter_category_id; // AngelScript's `Dictionary` converts all ints int `int64`
68 GetValueFromScriptDict(log_msg, dict, /*required:*/false, "filter_category_id", "int64", i64_filter_category_id);
69 query.cqy_filter_category_id = i64_filter_category_id;
70 GetValueFromScriptDict(log_msg, dict, /*required:*/false, "filter_guid", "string", query.cqy_filter_guid);
71 GetValueFromScriptDict(log_msg, dict, /*required:*/false, "search_expr", "string", search_expr);
72
73 // FIXME: Copypasta of `GUI::MainSelector::UpdateSearchParams()`
74 if (search_expr.find(":") == std::string::npos)
75 {
76 query.cqy_search_method = CacheSearchMethod::FULLTEXT;
77 query.cqy_search_string = search_expr;
78 }
79 else
80 {
81 Ogre::StringVector v = Ogre::StringUtil::split(search_expr, ":");
82 if (v.size() < 2)
83 {
84 query.cqy_search_method = CacheSearchMethod::NONE;
85 query.cqy_search_string = "";
86 }
87 else if (v[0] == "guid")
88 {
89 query.cqy_search_method = CacheSearchMethod::GUID;
90 query.cqy_search_string = v[1];
91 }
92 else if (v[0] == "author")
93 {
94 query.cqy_search_method = CacheSearchMethod::AUTHORS;
95 query.cqy_search_string = v[1];
96 }
97 else if (v[0] == "wheels")
98 {
99 query.cqy_search_method = CacheSearchMethod::WHEELS;
100 query.cqy_search_string = v[1];
101 }
102 else if (v[0] == "file")
103 {
104 query.cqy_search_method = CacheSearchMethod::FILENAME;
105 query.cqy_search_string = v[1];
106 }
107 else
108 {
109 query.cqy_search_method = CacheSearchMethod::NONE;
110 query.cqy_search_string = "";
111 }
112 }
113 // END copypasta
114
115 size_t results_count = self->Query(query);
116
117 asITypeInfo* typeinfo_array_entries = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl("array<CacheEntryClass@>");
118 asITypeInfo* typeinfo_array_scores = App::GetScriptEngine()->getEngine()->GetTypeInfoByDecl("array<uint>");
119
120 CScriptArray* results_entries = CScriptArray::Create(typeinfo_array_entries);
121 CScriptArray* results_scores = CScriptArray::Create(typeinfo_array_scores);
122 for (CacheQueryResult& result: query.cqy_results)
123 {
124 CacheEntry* entry_ptr = result.cqr_entry.GetRef();
125 results_entries->InsertLast(&entry_ptr);
126 results_scores->InsertLast(&result.cqr_score);
127 }
128
129 CScriptDictionary* results_dict = CScriptDictionary::Create(App::GetScriptEngine()->getEngine());
130 results_dict->Set("count", (asINT64)results_count);
131 results_dict->Set("entries", results_entries, typeinfo_array_entries->GetTypeId());
132 results_dict->Set("scores", results_scores, typeinfo_array_scores->GetTypeId());
133
134 return results_dict;
135}
136
137} // namespace CacheSystemAngelscriptWrappers
A database of user-installed content alias 'mods' (vehicles, terrains...)
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()
bool GetValueFromScriptDict(const std::string &log_msg, AngelScript::CScriptDictionary *dict, bool required, std::string const &key, const char *decl, T &out_value)
static int CacheEntry_get_number(CacheEntry *self)
static const std::string & CacheEntry_get_filecachename(CacheEntry *self)
static const std::string & CacheEntry_get_resource_bundle_type(CacheEntry *self)
static bool CacheEntry_get_deleted(CacheEntry *self)
static const std::string & CacheEntry_get_fpath(CacheEntry *self)
static const std::string & CacheEntry_get_fname(CacheEntry *self)
static const std::string & CacheEntry_get_categoryname(CacheEntry *self)
static CScriptDictionary * CacheSystemQueryWrapper(CacheSystem *self, CScriptDictionary *dict)
static int CacheEntry_get_categoryid(CacheEntry *self)
static const std::string & CacheEntry_get_fext(CacheEntry *self)
static const std::string & CacheEntry_get_dname(CacheEntry *self)
static const std::string & CacheEntry_get_resource_bundle_path(CacheEntry *self)
static const std::string & CacheEntry_get_resource_group(CacheEntry *self)
ScriptEngine * GetScriptEngine()
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