forked from codeurzebs/fivem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamingProgress.cpp
More file actions
258 lines (204 loc) · 5.92 KB
/
Copy pathStreamingProgress.cpp
File metadata and controls
258 lines (204 loc) · 5.92 KB
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
#include <StdInc.h>
#include <CoreConsole.h>
#include <Hooking.h>
#include <Streaming.h>
#include <nutsnbolts.h>
#include <StatusText.h>
#include <StreamingEvents.h>
#include <shared_mutex>
#include <mmsystem.h>
#include <ResourceCacheDeviceV2.h>
#include <VFSManager.h>
#include <ICoreGameInit.h>
struct StreamingDownloadProgress
{
size_t downloadDone;
size_t downloadTotal;
};
static std::shared_mutex g_mutex;
static std::map<std::string, StreamingDownloadProgress> g_downloadProgress;
static std::map<std::string, size_t> g_downloadList;
static std::set<std::string> g_downloadDone;
static std::map<std::string, std::string> g_nameMap;
static uint32_t g_lastDownloadTime;
static void StreamingProgress_OnDownload(const std::string& fileName, size_t done, size_t total)
{
std::unique_lock<std::shared_mutex> lock(g_mutex);
auto tgtName = fileName;
if (fileName.find("cache_nb:/") == 0)
{
tgtName = "cache:/" + fileName.substr(10);
}
else if (fileName.find("compcache_nb:/") == 0)
{
tgtName = "compcache:/" + fileName.substr(14);
}
g_downloadProgress[tgtName] = { done, total };
if (done == total)
{
if (g_downloadList.find(tgtName) != g_downloadList.end())
{
g_downloadDone.insert(tgtName);
}
}
}
#include <atHashMap.h>
struct strRequestInfo
{
uint64_t pad[3];
};
static void StreamingProgress_Update()
{
// process requests
auto streaming = streaming::Manager::GetInstance();
int thisRequests = 0;
std::set<std::string> foundNow;
std::vector<uint32_t> loadingOrRequestedList;
loadingOrRequestedList.reserve(64);
// gather loading/requested list rather than looping over every index
atMap<uint32_t, bool>* loadingList = (atMap<uint32_t, bool>*)((char*)streaming + 152); // see rage::strStreamingInfoManager::SetObjectToLoading
loadingList->ForAllEntriesWithHash([&loadingOrRequestedList](uint32_t idx, bool*)
{
loadingOrRequestedList.push_back(idx);
});
// request list
atMap<uint32_t, strRequestInfo>* requestList = (atMap<uint32_t, strRequestInfo>*)((char*)streaming + 64); // see rage::strInfoStatus::AddRequest
requestList->ForAllEntriesWithHash([&loadingOrRequestedList](uint32_t idx, strRequestInfo*)
{
loadingOrRequestedList.push_back(idx);
});
// loop over loading/requested list
for (uint32_t idx : loadingOrRequestedList)
{
auto data = &streaming->Entries[idx];
auto flags = data->flags & 3;
if (flags != 2 && flags != 3)
{
continue;
}
// try getting streaming data
StreamingPackfileEntry* spf = streaming::GetStreamingPackfileForEntry(data);
if (spf)
{
char nameBuffer[256];
rage::fiCollection* collection = nullptr;
if (spf)
{
collection = reinterpret_cast<rage::fiCollection*>(spf->packfile);
}
if (!collection && streaming::IsRawHandle(data->handle))
{
collection = streaming::GetRawStreamerByIndex(streaming::GetCollectionIndex(data->handle));
}
if (collection)
{
// is this a networked packfile?
if (!spf->isHdd)
{
strcpy(nameBuffer, "CfxRequest");
collection->GetEntryNameToBuffer(streaming::GetEntryIndex(data->handle), nameBuffer, 255);
bool isCache = false;
std::string fileName;
char readBuffer[2048];
if (strncmp(nameBuffer, "cache:/", 7) == 0)
{
fileName = std::string("cache_nb:/") + &nameBuffer[7];
isCache = true;
}
if (strncmp(nameBuffer, "compcache:/", 11) == 0)
{
fileName = std::string("compcache_nb:/") + &nameBuffer[11];
isCache = true;
}
if (isCache)
{
auto device = vfs::GetDevice(fileName);
fwRefContainer<resources::ResourceCacheDeviceV2> resDevice(device);
if (!resDevice->ExistsOnDisk(fileName))
{
std::unique_lock<std::shared_mutex> lock(g_mutex);
if (g_downloadList.find(nameBuffer) == g_downloadList.end())
{
g_downloadList.insert({ nameBuffer, resDevice->GetLength(fileName) });
}
foundNow.insert(nameBuffer);
thisRequests++;
}
}
}
}
}
}
// remove any entries that are no longer requested
for (auto it = g_downloadList.begin(); it != g_downloadList.end(); )
{
// skip if it's actually done
if (g_downloadDone.find(it->first) != g_downloadDone.end())
{
it++;
continue;
}
// if not found this frame, remove it
if (foundNow.find(it->first) == foundNow.end())
{
std::unique_lock<std::shared_mutex> lock(g_mutex);
it = g_downloadList.erase(it);
}
else
{
it++;
}
}
if (thisRequests > 0)
{
g_lastDownloadTime = timeGetTime();
}
if ((timeGetTime() - g_lastDownloadTime) < 2000)
{
std::shared_lock<std::shared_mutex> lock(g_mutex);
size_t downloadDone = 0;
size_t downloadSize = 0;
for (auto& downloadPair : g_downloadList)
{
downloadDone += g_downloadProgress[downloadPair.first].downloadDone;
downloadSize += downloadPair.second;
}
std::string str = fmt::sprintf("Downloading assets (%d of %d)... (%.2f/%.2f MB)", std::min(g_downloadDone.size(), g_downloadList.size()), g_downloadList.size(), std::min(downloadDone, downloadSize) / 1024.0 / 1024.0, downloadSize / 1024.0 / 1024.0);
ActivateStatusText(str.c_str(), 5, 3);
}
else
{
DeactivateStatusText(3);
g_downloadList.clear();
g_downloadDone.clear();
}
}
static InitFunction initFunction([]()
{
// TODO: KillNetwork handler
fx::OnCacheDownloadStatus.Connect([](const std::string& fileName, size_t done, size_t total)
{
StreamingProgress_OnDownload(fileName, done, total);
});
OnMainGameFrame.Connect([]()
{
static ConVar<bool> useStreamingProgress("game_showStreamingProgress", ConVar_Archive | ConVar_UserPref, false);
if (useStreamingProgress.GetValue())
{
StreamingProgress_Update();
}
else if (Instance<ICoreGameInit>::Get()->HasVariable("networkInited"))
{
DeactivateStatusText(3);
g_downloadList.clear();
g_downloadDone.clear();
}
});
OnLookAliveFrame.Connect([]()
{
if (!Instance<ICoreGameInit>::Get()->HasVariable("networkInited"))
{
StreamingProgress_Update();
}
});
});