-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathServerResourceList.cpp
375 lines (304 loc) · 9.88 KB
/
ServerResourceList.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "StdInc.h"
#include "ServerResourceList.h"
#include "CoreConsole.h"
#include "Resource.h"
#include "ResourceManager.h"
#include "ResourceMetaDataComponent.h"
#include "ManifestVersion.h"
#include "VFSManager.h"
#include "ServerInstanceBase.h"
#include "ServerInstanceBaseRef.h"
#include <boost/algorithm/string.hpp>
#include <skyr/percent_encode.hpp>
#include <skyr/url.hpp>
#include <filesystem>
#include <queue>
namespace fx::resources
{
fwRefContainer<ServerResourceList> ServerResourceList::Create()
{
return new ServerResourceList();
}
void ServerResourceList::AttachToObject(fx::ResourceManager* object)
{
m_manager = object;
}
void ServerResourceList::ScanResources(const std::string& resourceRoot, ScanResult* outResult /* = nullptr */)
{
// TEMPORARY convar for chat build swaparound
static auto useChatVar = m_manager->GetComponent<fx::ServerInstanceBaseRef>()->Get()->AddVariable<bool>("resources_useSystemChat", ConVar_None, false);
bool isSystemResourceRoot = resourceRoot.find("/system_resources/") != std::string::npos;
m_currentResult = outResult;
auto resourceRootPath = std::filesystem::u8path(resourceRoot).lexically_normal();
std::queue<std::string> pathsToIterate;
pathsToIterate.push(resourceRoot);
std::vector<pplx::task<fwRefContainer<fx::Resource>>> tasks;
// save scanned resource names so we don't scan them twice
std::map<std::string, std::string> scannedNow;
std::set<std::string> updatedNow;
size_t newResources = 0;
size_t updatedResources = 0;
size_t reloadedResources = 0;
while (!pathsToIterate.empty())
{
std::string thisPath = pathsToIterate.front();
pathsToIterate.pop();
auto vfsDevice = vfs::GetDevice(thisPath);
vfs::FindData findData;
auto handle = vfsDevice->FindFirst(thisPath, &findData);
if (handle != INVALID_DEVICE_HANDLE)
{
do
{
if (findData.name == "." || findData.name == "..")
{
continue;
}
// common things to skip
if (findData.name == ".git")
{
continue;
}
// part of TEMPORARY chat hack
if (findData.name == "chat")
{
// if resources_useSystemChat is true, we want to only use chat if system resource
// same goes for opposite
if (useChatVar->GetValue() != isSystemResourceRoot)
{
continue;
}
}
if (findData.attributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::string resPath(thisPath + "/" + findData.name);
// is this a category?
if (findData.name[0] == '[' && findData.name[findData.name.size() - 1] == ']')
{
pathsToIterate.push(resPath);
// check if it has a fxmanifest/__resource
if (vfsDevice->GetAttributes(resPath + "/fxmanifest.lua") != -1 || vfsDevice->GetAttributes(resPath + "/__resource.lua") != -1)
{
AddError(ScanMessageType::Warning, findData.name.substr(1, findData.name.length() - 2), "category_manifest", { findData.name });
}
}
// it's a resource
else if (scannedNow.find(findData.name) == scannedNow.end())
{
const auto& resourceName = findData.name;
// ignore hidden folders
if (resourceName[0] == '.')
{
continue;
}
scannedNow.emplace(resourceName, resPath);
auto oldRes = m_manager->GetResource(resourceName, false);
auto oldScanData = m_scanData.find(resourceName);
// did the path change? if so, unload the old resource
if (oldRes.GetRef() && oldScanData != m_scanData.end() && oldScanData->second != resPath)
{
{
std::unique_lock _(m_resourcesByComponentMutex);
// remove from by-component lists
for (auto& list : m_resourcesByComponent)
{
list.second.erase(resourceName);
}
}
// unmount relative device
vfs::Unmount(fmt::sprintf("@%s/", resourceName));
// stop and remove resource
oldRes->Stop();
m_manager->RemoveResource(oldRes);
// undo ptr
oldRes = {};
}
if (oldRes.GetRef())
{
auto metaDataComponent = oldRes->GetComponent<fx::ResourceMetaDataComponent>();
// filter function to remove _extra entries (Lua table ordering determinism)
auto filterMetadata = [](auto&& metadataIn)
{
for (auto it = metadataIn.begin(); it != metadataIn.end();)
{
if (boost::algorithm::ends_with(it->first, "_extra"))
{
it = metadataIn.erase(it);
}
else
{
++it;
}
}
return std::move(metadataIn);
};
// save the old metadata for comparison
auto oldMetaData = filterMetadata(metaDataComponent->GetAllEntries());
// load new metadata
metaDataComponent->LoadMetaData(resPath);
// compare differences
auto newMetaData = filterMetadata(metaDataComponent->GetAllEntries());
bool different = (newMetaData.size() != oldMetaData.size()) || (newMetaData != oldMetaData);
// if different, track as updated
if (different)
{
updatedNow.insert(resourceName);
updatedResources++;
}
// track resource as reloaded
reloadedResources++;
}
else
{
console::DPrintf("resources", "Found new resource %s in %s\n", resourceName, resPath);
newResources++;
updatedNow.insert(resourceName);
auto path = std::filesystem::u8path(resPath);
// get parent path components
std::error_code ec;
std::vector<std::string> components;
auto relPath = std::filesystem::relative(path, resourceRootPath, ec);
if (!ec)
{
for (const auto& component : relPath)
{
auto name = component.filename().u8string();
if (name[0] == '[' && name[name.size() - 1] == ']')
{
components.push_back(name);
}
}
}
m_scanData[resourceName] = resPath;
skyr::url_record record;
record.scheme = "file";
skyr::url url{ std::move(record) };
url.set_pathname(*skyr::percent_encode(resPath, skyr::encode_set::path));
url.set_hash(*skyr::percent_encode(resourceName, skyr::encode_set::fragment));
auto task = m_manager->AddResource(url.href())
.then([this, components = std::move(components)](fwRefContainer<fx::Resource> resource)
{
if (resource.GetRef())
{
std::unique_lock _(m_resourcesByComponentMutex);
for (const auto& component : components)
{
m_resourcesByComponent[component].insert(resource->GetName());
}
}
return resource;
});
tasks.push_back(task);
}
}
else // scannedNow already contained the resource
{
auto scannedEntry = scannedNow.find(findData.name);
AddError(ScanMessageType::Warning, findData.name, "duplicate_resource", { resPath, scannedEntry->second });
}
}
} while (vfsDevice->FindNext(handle, &findData));
vfsDevice->FindClose(handle);
}
}
auto combinedTask = pplx::when_all(tasks.begin(), tasks.end());
combinedTask.wait();
if (outResult)
{
for (const auto& resource : combinedTask.get())
{
if (resource.GetRef())
{
outResult->resources.push_back(resource);
}
}
}
// check for outdated
m_manager->ForAllResources([this, &updatedNow](const fwRefContainer<fx::Resource>& resource)
{
auto md = resource->GetComponent<fx::ResourceMetaDataComponent>();
auto fxV2 = md->IsManifestVersionBetween("adamant", "");
auto fxV1 = md->IsManifestVersionBetween(ManifestVersion{ "44febabe-d386-4d18-afbe-5e627f4af937" }.guid, guid_t{ 0 });
if (!fxV2 || !*fxV2)
{
if (!fxV1 || !*fxV1)
{
if (!md->GlobEntriesVector("client_script").empty())
{
auto resourceName = resource->GetName();
// only alert if a resource updated this iteration
if (updatedNow.find(resourceName) != updatedNow.end())
{
AddError(ScanMessageType::Warning, resourceName, "outdated_manifest", {});
}
}
}
}
});
if (outResult)
{
outResult->newResources += newResources;
outResult->reloadedResources += reloadedResources;
outResult->updatedResources += updatedResources;
}
m_currentResult = nullptr;
}
void ServerResourceList::AddError(ScanMessageType type, const std::string& resource, const std::string& identifier, std::vector<std::string>&& args)
{
if (m_currentResult)
{
std::unique_lock _(m_resultMutex);
ScanMessage message;
message.type = type;
message.resource = resource;
message.identifier = identifier;
message.args = std::move(args);
m_currentResult->messages.push_back(std::move(message));
}
}
std::set<std::string> ServerResourceList::FindByPathComponent(const std::string& pathComponent)
{
std::shared_lock _(m_resourcesByComponentMutex);
auto category = m_resourcesByComponent.find(pathComponent);
if (category == m_resourcesByComponent.end())
{
return {};
}
return category->second;
}
std::string ScanMessage::Format() const
{
if (identifier == "outdated_manifest")
{
return fmt::sprintf("%s has an outdated manifest (__resource.lua instead of fxmanifest.lua)", resource);
}
else if (identifier == "duplicate_resource")
{
auto cleanPath = [](const std::string& resPath)
{
try
{
return std::filesystem::u8path(resPath).lexically_normal().u8string();
}
catch (...)
{
return resPath;
}
};
return fmt::sprintf("%s exists in more than one place (%s is used, the duplicate is %s)", resource, cleanPath(args[1]), cleanPath(args[0]));
}
else if (identifier == "load_failed")
{
return fmt::sprintf("%s failed to load: %s", resource, args[0]);
}
else if (identifier == "no_manifest")
{
return fmt::sprintf("%s does not have a resource manifest (fxmanifest.lua)", resource);
}
else if (identifier == "category_manifest")
{
return fmt::sprintf("%s is a category, but has a resource manifest (if this is meant to be a resource, don't use [] around the name)", args[0]);
}
return fmt::sprintf("[%s]", identifier);
}
}