Limo
A simple mod manager
Loading...
Searching...
No Matches
autotag.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "pathutils.h"
9#include "progressnode.h"
10#include "tag.h"
11#include "tagconditionnode.h"
12#include <filesystem>
13#include <json/json.h>
14#include <optional>
15#include <string>
16#include <vector>
17
18
23class AutoTag : public Tag
24{
25public:
34 AutoTag(const std::string& name,
35 const std::string& expression,
36 const std::vector<TagCondition>& conditions);
42 AutoTag(const Json::Value& json);
43
51 template<typename View>
52 void reapplyMods(const std::map<int, std::vector<std::pair<std::string, std::string>>>& files,
53 const View& mods,
54 std::optional<ProgressNode*> progress_node = {})
55 {
56 mods_.clear();
57 for(int mod : mods)
58 {
59 if(evaluator_.evaluate(files.at(mod)))
60 mods_.push_back(mod);
61 if(progress_node)
62 (*progress_node)->advance();
63 }
64 }
65
72 template<typename View>
73 void reapplyMods(const std::filesystem::path& staging_dir,
74 const View& mods,
75 std::optional<ProgressNode*> progress_node = {})
76 {
77 reapplyMods(readModFiles(staging_dir, mods), mods, progress_node);
78 }
79
86 template<typename View>
87 void updateMods(const std::map<int, std::vector<std::pair<std::string, std::string>>>& files,
88 const View& mods,
89 std::optional<ProgressNode*> progress_node = {})
90 {
91 for(int mod : mods)
92 {
93 auto iter = std::ranges::find(mods_, mod);
94 if(iter != mods_.end())
95 mods_.erase(iter);
96 if(evaluator_.evaluate(files.at(mod)))
97 mods_.push_back(mod);
98 if(progress_node)
99 (*progress_node)->advance();
100 }
101 }
102
109 template<typename View>
110 void updateMods(const std::filesystem::path& staging_dir,
111 const View& mods,
112 std::optional<ProgressNode*> progress_node = {})
113 {
114 updateMods(readModFiles(staging_dir, mods), mods, progress_node);
115 }
116
121 void setEvaluator(const std::string& expression, const std::vector<TagCondition>& conditions);
126 Json::Value toJson() const;
132 bool operator==(const std::string& name) const;
137 std::string getExpression() const;
142 std::vector<TagCondition> getConditions() const;
147 int getNumConditions() const;
157 template<typename View>
158 static std::map<int, std::vector<std::pair<std::string, std::string>>> readModFiles(
159 const std::filesystem::path& staging_dir,
160 View mods,
161 std::optional<ProgressNode*> progress_node = {})
162 {
163 std::map<int, std::vector<std::pair<std::string, std::string>>> files;
164 for(int mod : mods)
165 {
166 files[mod] = {};
167 const std::filesystem::path mod_path = staging_dir / std::to_string(mod);
168 for(const auto& dir_entry : std::filesystem::recursive_directory_iterator(mod_path))
169 {
170 std::string path = path_utils::getRelativePath(dir_entry.path(), mod_path);
171 if(path.front() == '/')
172 path.erase(0, 1);
173 files[mod].emplace_back(path, dir_entry.path().filename().string());
174 }
175 if(progress_node)
176 (*progress_node)->advance();
177 }
178 return files;
179 }
180
181private:
183 std::string expression_;
185 std::vector<TagCondition> conditions_;
191};
void reapplyMods(const std::filesystem::path &staging_dir, const View &mods, std::optional< ProgressNode * > progress_node={})
Removes this tag from all mods, then applies it to all given mods which fulfill its conditions.
Definition autotag.h:73
Json::Value toJson() const
Serializes this tag to a json object.
Definition autotag.cpp:71
void updateMods(const std::filesystem::path &staging_dir, const View &mods, std::optional< ProgressNode * > progress_node={})
Reevaluates if the given mods should have this tag. Adds/ removes the tag from all given mods when ne...
Definition autotag.h:110
void reapplyMods(const std::map< int, std::vector< std::pair< std::string, std::string > > > &files, const View &mods, std::optional< ProgressNode * > progress_node={})
Removes this tag from all mods, then applies it to all given mods which fulfill its conditions.
Definition autotag.h:52
static std::map< int, std::vector< std::pair< std::string, std::string > > > readModFiles(const std::filesystem::path &staging_dir, View mods, std::optional< ProgressNode * > progress_node={})
Recursively iterates over all files for all mods with given ids and creates a a map of mod ids to a v...
Definition autotag.h:158
void setEvaluator(const std::string &expression, const std::vector< TagCondition > &conditions)
Changes the conditions and expression used by this tag.
Definition autotag.cpp:63
void updateMods(const std::map< int, std::vector< std::pair< std::string, std::string > > > &files, const View &mods, std::optional< ProgressNode * > progress_node={})
Reevaluates if the given mods should have this tag. Adds/ removes the tag from all given mods when ne...
Definition autotag.h:87
bool operator==(const std::string &name) const
Compares this tag by name to the given name.
Definition autotag.cpp:92
std::vector< TagCondition > conditions_
Conditions used by the TagConditionNode.
Definition autotag.h:185
std::string expression_
Expression used by the TagConditionNode.
Definition autotag.h:183
TagConditionNode evaluator_
This tag is applied to a mod if this nodes evaluate function returns true for the mods installation d...
Definition autotag.h:190
std::vector< TagCondition > getConditions() const
Getter for this tags conditions.
Definition autotag.cpp:102
int getNumConditions() const
Returns the number of conditions for this tag.
Definition autotag.cpp:107
AutoTag(const std::string &name, const std::string &expression, const std::vector< TagCondition > &conditions)
Constructor.
Definition autotag.cpp:10
std::string getExpression() const
Getter for this tags expression.
Definition autotag.cpp:97
Represents a node in a tree used to model a boolean expression for evaluating if the files in a direc...
Definition tagconditionnode.h:19
bool evaluate(const std::vector< std::pair< std::string, std::string > > &files) const
Checks if files in the given vector satisfy the boolean expression modeled by this tree node.
Definition tagconditionnode.cpp:80
Abstract base class for a tag assigned to a set of mods.
Definition tag.h:17
std::vector< int > mods_
Contains ids of all mods to which this tag has been added.
Definition tag.h:56
char path[256]
Path to which to extract the file.
Definition lspakfilelistentry.h:1
Header for the path_utils namespace.
Header for the ProgressNode class.
Header for the Tag class.
Header for the TagConditionNode class.