Skip to content

Commit

Permalink
Tuning: added addonpart_filename directive (optional whitelist)
Browse files Browse the repository at this point in the history
The modder can add any number of `addonpart_filename <foo.truck>` directives (case-insensitive). If none are specified, any filename goes. Matching by GUID is still the primary method, this only helps in cases where the GUID is shared by too many trucks (not uncommon).

This commit also fixes the GUID filtering being case-sensitive in all cases (trucks, skins, addonparts), even though the search string is always case-insensitive.

Cache file format was set to dummy value; will be properly bumped when #3151 is merged.
  • Loading branch information
ohlidalp committed May 28, 2024
1 parent a0910b7 commit 16ce345
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions source/main/gui/panels/GUI_TopMenubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,7 @@ void TopMenubar::RefreshTuningMenu()
CacheQuery query_addonparts;
query_addonparts.cqy_filter_type = LT_AddonPart;
query_addonparts.cqy_filter_guid = current_actor->getUsedActorEntry()->guid;
query_addonparts.cqy_filter_target_filename = current_actor->getTruckFileName(); // Addonparts without any filenames listed will just pass.
App::GetCacheSystem()->Query(query_addonparts);
for (CacheQueryResult& res: query_addonparts.cqy_results)
{
Expand Down
43 changes: 41 additions & 2 deletions source/main/resources/CacheSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ void CacheSystem::ImportEntryFromJson(rapidjson::Value& j_entry, CacheEntryPtr &
{
out_entry->addonpart_guids.insert(j_addonguid.GetString());
}

// Addon part suggested mod filenames
for (rapidjson::Value& j_addonfname: j_entry["addonpart_filenames"].GetArray())
{
out_entry->addonpart_filenames.insert(j_addonfname.GetString());
}
}

CacheValidity CacheSystem::LoadCacheFileJson()
Expand Down Expand Up @@ -609,6 +615,13 @@ void CacheSystem::ExportEntryToJson(rapidjson::Value& j_entries, rapidjson::Docu
}
j_entry.AddMember("addonpart_guids", j_addonguids, j_doc.GetAllocator());

rapidjson::Value j_addonfnames(rapidjson::kArrayType);
for (std::string const & ag: entry->addonpart_filenames)
{
j_addonfnames.PushBack(rapidjson::StringRef(ag.c_str()), j_doc.GetAllocator());
}
j_entry.AddMember("addonpart_filenames", j_addonfnames, j_doc.GetAllocator());

// Add entry to list
j_entries.PushBack(j_entry, j_doc.GetAllocator());
}
Expand Down Expand Up @@ -900,6 +913,7 @@ void CacheSystem::FillTruckDetailInfo(CacheEntryPtr& entry, Ogre::DataStreamPtr
if (def->root_module->guid.size() > 0)
{
entry->guid = def->root_module->guid[def->root_module->guid.size() - 1].guid;
Ogre::StringUtil::toLowerCase(entry->guid);
}
entry->fileformatversion = 0;
if (def->root_module->fileformatversion.size() > 0)
Expand Down Expand Up @@ -1142,6 +1156,8 @@ void CacheSystem::FillSkinDetailInfo(CacheEntryPtr &entry, std::shared_ptr<SkinD
entry->description = skin_def->description;
entry->categoryid = -1;
entry->skin_def = skin_def; // Needed to generate preview image

Ogre::StringUtil::toLowerCase(entry->guid);
}

void CacheSystem::FillAddonPartDetailInfo(CacheEntryPtr &entry, Ogre::DataStreamPtr ds)
Expand All @@ -1163,7 +1179,15 @@ void CacheSystem::FillAddonPartDetailInfo(CacheEntryPtr &entry, Ogre::DataStream
}
else if (ctx->isTokKeyword() && ctx->getTokKeyword() == "addonpart_guid")
{
entry->addonpart_guids.insert(ctx->getTokString(1));
std::string guid = ctx->getTokString(1);
Ogre::StringUtil::toLowerCase(guid);
entry->addonpart_guids.insert(guid);
}
else if (ctx->isTokKeyword() && ctx->getTokKeyword() == "addonpart_filename")
{
std::string fname = ctx->getTokString(1);
Ogre::StringUtil::toLowerCase(fname);
entry->addonpart_filenames.insert(fname);
}

ctx->seekNextLine();
Expand Down Expand Up @@ -1217,6 +1241,8 @@ void CacheSystem::FillTuneupDetailInfo(CacheEntryPtr &entry, TuneupDefPtr& tuneu
entry->description = tuneup_def->description;
entry->categoryid = tuneup_def->category_id;
entry->tuneup_def = tuneup_def; // Needed to generate preview image

Ogre::StringUtil::toLowerCase(entry->guid);
}

void CacheSystem::LoadAssetPack(CacheEntryPtr& target_entry, Ogre::String const & assetpack_filename)
Expand Down Expand Up @@ -1917,6 +1943,8 @@ void CacheSystem::DeleteProject(CacheEntryPtr& entry)
size_t CacheSystem::Query(CacheQuery& query)
{
Ogre::StringUtil::toLowerCase(query.cqy_search_string);
Ogre::StringUtil::toLowerCase(query.cqy_filter_guid);
Ogre::StringUtil::toLowerCase(query.cqy_filter_target_filename);
std::time_t cur_time = std::time(nullptr);
for (CacheEntryPtr& entry: m_entries)
{
Expand All @@ -1931,6 +1959,17 @@ size_t CacheSystem::Query(CacheQuery& query)
}
}

// Filter by target filename (currently only `addonpart_filenames`); pass items which have no target filenames listed.
if (query.cqy_filter_target_filename != "")
{
if (entry->fext == "addonpart"
&& entry->addonpart_filenames.size() > 0
&& entry->addonpart_filenames.count(query.cqy_filter_target_filename) == 0)
{
continue;
}
}

// Filter by entry type
bool add = false;
if (entry->fext == "terrn2")
Expand Down Expand Up @@ -2017,7 +2056,7 @@ size_t CacheSystem::Query(CacheQuery& query)
match = this->Match(score, entry->fname, query.cqy_search_string, 100);
break;

default: // CacheSearchMethod::NONE
default: // CacheSearchMethod::
match = true;
break;
};
Expand Down
14 changes: 9 additions & 5 deletions source/main/resources/CacheSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <set>

#define CACHE_FILE "mods.cache"
#define CACHE_FILE_FORMAT 13
#define CACHE_FILE_FORMAT 3151 // TBD bump to 14 when merging pull request #3151
#define CACHE_FILE_FRESHNESS 86400 // 60*60*24 = one day

namespace RoR {
Expand Down Expand Up @@ -74,8 +74,7 @@ class CacheEntry: public RefCountingObject<CacheEntry>

std::time_t addtimestamp; //!< timestamp when this file was added to the cache
Ogre::String uniqueid; //!< file's unique id
Ogre::String guid; //!< global unique id. Type "addonpart" leaves this empty.
std::set<std::string> addonpart_guids; //!< GUIDs of all vehicles this addonpart is used in. Empty for non-addonpart entries.
Ogre::String guid; //!< global unique id. Type "addonpart" leaves this empty and uses `addonpart_guids`.
int version; //!< file's version

std::string resource_bundle_type; //!< Archive type recognized by OGRE resource system: 'FileSystem' or 'Zip'
Expand All @@ -95,6 +94,10 @@ class CacheEntry: public RefCountingObject<CacheEntry>
RoR::TuneupDefPtr addonpart_data_only; //!< Cached addonpart data (dummy tuneup), only used for evaluating conflicts, see `AddonPartUtility::RecordAddonpartConflicts()`
// TBD: Make Terrn2Def a RefcountingObjectPtr<> and cache it here too.

// following all ADDONPART detail information:
std::set<std::string> addonpart_guids; //!< GUIDs of all vehicles this addonpart is used with.
std::set<std::string> addonpart_filenames; //!< File names of all vehicles this addonpart is used with. If empty, any filename goes.

// following all TRUCK detail information:
Ogre::String description;
Ogre::String tags;
Expand Down Expand Up @@ -165,7 +168,7 @@ struct CacheQueryResult

enum class CacheSearchMethod // Always case-insensitive
{
NONE, //!< No searching
NONE, //!< Ignore the search string and find all
FULLTEXT, //!< Partial match in: name, filename, description, author name/mail
GUID, //!< Partial match in: guid
AUTHORS, //!< Partial match in: author name/email
Expand All @@ -177,7 +180,8 @@ struct CacheQuery
{
RoR::LoaderType cqy_filter_type = RoR::LoaderType::LT_None;
int cqy_filter_category_id = CacheCategoryId::CID_All;
std::string cqy_filter_guid; //!< Exact match; leave empty to disable
std::string cqy_filter_guid; //!< Exact match (case-insensitive); leave empty to disable
std::string cqy_filter_target_filename; //!< Exact match (case-insensitive); leave empty to disable (currently only used with addonparts)
CacheSearchMethod cqy_search_method = CacheSearchMethod::NONE;
std::string cqy_search_string;

Expand Down

0 comments on commit 16ce345

Please sign in to comment.