-
Notifications
You must be signed in to change notification settings - Fork 5
/
object_formlist.cpp
268 lines (239 loc) · 10.4 KB
/
object_formlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "object_formlist.h"
namespace FORMLIST
{
struct line_content create_patch_instruction(const std::string& line)
{
line_content l;
// extract objects
std::regex objects_regex("filterByFormLists\\s*=([^:]+)", regex::icase);
std::smatch objects_match;
std::regex_search(line, objects_match, objects_regex);
std::vector<std::string> objects;
if (objects_match.empty() || objects_match[1].str().empty()) {
//empty
} else {
std::string objects_str = objects_match[1];
std::regex objects_list_regex("[^,]+[ ]*[|][ ]*[a-zA-Z0-9]{1,8}", regex::icase);
std::sregex_iterator objects_iterator(objects_str.begin(), objects_str.end(), objects_list_regex);
std::sregex_iterator objects_end;
while (objects_iterator != objects_end) {
std::string tempVar = (*objects_iterator)[0].str();
tempVar.erase(tempVar.begin(), std::find_if_not(tempVar.begin(), tempVar.end(), ::isspace));
tempVar.erase(std::find_if_not(tempVar.rbegin(), tempVar.rend(), ::isspace).base(), tempVar.end());
//logger::info(FMT_STRING("Race: {}"), race);
if (tempVar != "none") {
objects.push_back(tempVar);
}
++objects_iterator;
}
l.objects = objects;
}
// extract formsToAdd
std::regex formsToAdd_regex("formsToAdd\\s*=([^:]+)", regex::icase);
std::smatch formsToAdd_match;
std::regex_search(line, formsToAdd_match, formsToAdd_regex);
std::vector<std::string> formsToAdd;
if (formsToAdd_match.empty() || formsToAdd_match[1].str().empty()) {
//empty
} else {
std::string formsToAdd_str = formsToAdd_match[1];
std::regex formsToAdd_list_regex("[^,]+[ ]*[|][ ]*[a-zA-Z0-9]{1,8}", regex::icase);
std::sregex_iterator formsToAdd_iterator(formsToAdd_str.begin(), formsToAdd_str.end(), formsToAdd_list_regex);
std::sregex_iterator formsToAdd_end;
while (formsToAdd_iterator != formsToAdd_end) {
std::string keywordToAdd = (*formsToAdd_iterator)[0].str();
keywordToAdd.erase(keywordToAdd.begin(), std::find_if_not(keywordToAdd.begin(), keywordToAdd.end(), ::isspace));
keywordToAdd.erase(std::find_if_not(keywordToAdd.rbegin(), keywordToAdd.rend(), ::isspace).base(), keywordToAdd.end());
if (keywordToAdd != "none") {
//logger::info(FMT_STRING("formsToAdd: {}"), keywordToAdd);
formsToAdd.push_back(keywordToAdd);
}
++formsToAdd_iterator;
}
l.objectsAdd = formsToAdd;
}
std::regex formListReplace_regex("formsToReplace\\s*=([^:]+)", regex::icase);
std::smatch formListReplace_match;
std::regex_search(line, formListReplace_match, formListReplace_regex);
std::vector<std::string> formListReplace;
if (formListReplace_match.empty() || formListReplace_match[1].str().empty()) {
//empty
} else {
std::string formListReplace_str = formListReplace_match[1];
std::regex pattern("([^,]+[ ]*[|][ ]*[a-zA-Z0-9]{1,8})\\s*=\\s*([^,]+[ ]*[|][ ]*[a-zA-Z0-9]{1,8})", regex::icase);
std::smatch match;
auto begin = std::sregex_iterator(formListReplace_str.begin(), formListReplace_str.end(), pattern);
auto end = std::sregex_iterator();
for (std::sregex_iterator i = begin; i != end; ++i) {
std::smatch match = *i;
l.formsToReplace.push_back(match[1]);
l.formsToReplaceWith.push_back(match[2]);
//logger::debug(FMT_STRING("Match: {} {}"), match[1].str(), match[2].str());
}
}
// extract formsToRemove
std::regex formsToRemove_regex("formsToRemove\\s*=([^:]+)", regex::icase);
std::smatch formsToRemove_match;
std::regex_search(line, formsToRemove_match, formsToRemove_regex);
std::vector<std::string> formsToRemove;
if (formsToRemove_match.empty() || formsToRemove_match[1].str().empty()) {
//empty
} else {
std::string formsToRemove_str = formsToRemove_match[1];
std::regex formsToRemove_list_regex("[^,]+[ ]*[|][ ]*[a-zA-Z0-9]{1,8}", regex::icase);
std::sregex_iterator formsToRemove_iterator(formsToRemove_str.begin(), formsToRemove_str.end(), formsToRemove_list_regex);
std::sregex_iterator formsToRemove_end;
while (formsToRemove_iterator != formsToRemove_end) {
std::string keywordToRemove = (*formsToRemove_iterator)[0].str();
keywordToRemove.erase(keywordToRemove.begin(), std::find_if_not(keywordToRemove.begin(), keywordToRemove.end(), ::isspace));
keywordToRemove.erase(std::find_if_not(keywordToRemove.rbegin(), keywordToRemove.rend(), ::isspace).base(), keywordToRemove.end());
if (keywordToRemove != "none") {
//logger::info(FMT_STRING("formsToRemove: {}"), keywordToRemove);
formsToRemove.push_back(keywordToRemove);
}
++formsToRemove_iterator;
}
l.objectsRemove = formsToRemove;
}
return l;
}
void process_patch_instructions(const std::list<line_content>& tokens)
{
logger::debug("processing patch instructions");
const auto dataHandler = RE::TESDataHandler::GetSingleton();
RE::BSTArray<RE::BGSListForm*> objectArray = dataHandler->GetFormArray<RE::BGSListForm>();
for (const auto& line : tokens) {
for (const auto& curobj : objectArray) {
bool found = false;
//logger::debug(FMT_STRING("formlist found. {:08X}"), curobj->formID);
if (!line.objects.empty()) {
//logger::info("npc not empty");
for (const auto& objectstring : line.objects) {
RE::TESForm* currentform = nullptr;
RE::BGSListForm* object = nullptr;
std::string string_form = objectstring;
currentform = GetFormFromIdentifier(string_form);
if (currentform && currentform->formType == RE::ENUM_FORM_ID::kFLST) {
object = (RE::BGSListForm*)currentform;
if (curobj->formID == object->formID) {
found = true;
//logger::info("NPC found.");
break;
}
}
}
}
if (line.objects.empty()) {
found = true;
}
if (found && !line.objectsRemove.empty()) {
for (size_t i = 0; i < line.objectsRemove.size(); i++) {
RE::TESForm* currentform = nullptr;
std::string string_form = line.objectsRemove[i];
currentform = GetFormFromIdentifier(string_form);
if (currentform ) {
//logger::debug(FMT_STRING("formlist formid: {:08X} with count({}) "), curobj->formID, curobj->arrayOfForms.size());
curobj->arrayOfForms.erase(std::remove_if(curobj->arrayOfForms.begin(), curobj->arrayOfForms.end(), [&](const RE::TESForm* x) {
return x->formID == currentform->formID;
}),
curobj->arrayOfForms.end());
logger::debug(FMT_STRING("formlist formid: {:08X} with count({}) removed form {:08X} "), curobj->formID, curobj->arrayOfForms.size(), currentform->formID);
}
}
}
if (found && !line.objectsAdd.empty()) {
for (size_t i = 0; i < line.objectsAdd.size(); i++) {
RE::TESForm* currentform = nullptr;
std::string string_form = line.objectsAdd[i];
currentform = GetFormFromIdentifier(string_form);
if (currentform ) {
//logger::debug(FMT_STRING("formlist formid: {:08X} with count({}) added form {:08X} "), curobj->formID, curobj->arrayOfForms.size(), currentform->formID);
curobj->arrayOfForms.push_back(currentform);
logger::debug(FMT_STRING("formlist formid: {:08X} with count({}) added form {:08X} "), curobj->formID, curobj->arrayOfForms.size(), currentform->formID);
}
}
}
if (found && !line.formsToReplace.empty()) {
for (uint32_t i = 0; i < line.formsToReplace.size(); i++) {
RE::TESForm* currentform = nullptr;
RE::TESForm* currentform2 = nullptr;
std::string string_form = line.formsToReplace[i];
std::string string_form2 = line.formsToReplaceWith[i];
currentform = GetFormFromIdentifier(string_form);
currentform2 = GetFormFromIdentifier(string_form2);
if (currentform && currentform2) {
// Loop through all elements in arrayOfForms and replace matches
for (auto& form : curobj->arrayOfForms) {
if (form && form->formID == currentform->formID) {
form = currentform2; // Replace the matching form
}
}
}
}
}
if (found) {
}
}
}
}
void* readConfig(const std::string& folder)
{
char skipChar = '/';
std::string extension = ".ini";
DIR* dir;
struct dirent* ent;
std::list<std::string> directories{ folder };
std::string currentFolder;
while (!directories.empty()) {
currentFolder = directories.front();
directories.pop_front();
if ((dir = opendir(currentFolder.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
std::string fullPath = currentFolder + "\\" + ent->d_name;
struct _stat st;
if (_stat(fullPath.c_str(), &st) == 0 && (_S_IFDIR & st.st_mode)) {
directories.push_back(fullPath);
} else {
std::string fileName = ent->d_name;
size_t pos = fileName.find(extension);
if (pos != std::string::npos) {
fileName = fileName.substr(0, pos);
const char* modname = fileName.c_str();
if ((strstr(modname, ".esp") != nullptr || strstr(modname, ".esl") != nullptr || strstr(modname, ".esm") != nullptr)) {
if (!IsPluginInstalled(modname)) {
logger::info("************************************************************");
logger::info(FMT_STRING("{} not found or is not a valid plugin file, skipping config file {}."), modname, fullPath);
logger::info("************************************************************");
continue;
}
}
logger::info("************************************************************");
logger::info(FMT_STRING("Processing config file {}... "), fullPath.c_str());
logger::info("************************************************************");
std::string line;
std::ifstream infile;
std::list<line_content> tokens;
infile.open(fullPath);
while (std::getline(infile, line)) {
if (line[0] == skipChar) {
continue;
}
if (line.empty()) {
continue;
}
tokens.push_back(create_patch_instruction(line));
}
infile.close();
process_patch_instructions(tokens);
}
}
}
}
closedir(dir);
} else {
logger::info(FMT_STRING("Couldn't open directory {}."), currentFolder.c_str());
}
}
}
}