AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LuxPreload.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: what to preload while playing. A low-priority thread reads the next map's files
3 * into the OS file cache (LuxPreloader.cpp), so the first visit loads from memory, not disk.
4 * Pure parsing, unit tested in tests/cpp/test_preload.cpp.
5 */
6#ifndef LUX_PRELOAD_H
7#define LUX_PRELOAD_H
8
9#include <string>
10#include <vector>
11
12inline std::string LuxPreloadLower(std::string s)
13{
14 for(size_t i = 0; i < s.size(); ++i) if(s[i] >= 'A' && s[i] <= 'Z') s[i] = (char)(s[i] - 'A' + 'a');
15 return s;
16}
17
18/** "E:/svn/maps/02_Mansion_02" -> "02_mansion_02.map" */
19inline std::string LuxPreloadMapName(const std::string& asPath)
20{
21 size_t lSlash = asPath.find_last_of("/\\");
22 std::string s = lSlash == std::string::npos ? asPath : asPath.substr(lSlash + 1);
23 s = LuxPreloadLower(s);
24 if(s.size() < 4 || s.compare(s.size() - 4, 4, ".map") != 0) s += ".map";
25 return s;
26}
27
28inline void LuxPreloadAddUnique(std::vector<std::string>& avList, const std::string& asX)
29{
30 for(size_t i = 0; i < avList.size(); ++i) if(avList[i] == asX) return;
31 avList.push_back(asX);
32}
33
34/** Maps reachable from here: the script's ChangeMap("...") calls and level doors' MapFile values. */
35inline std::vector<std::string> LuxPreloadNextMaps(const std::string& asScript, const std::string& asMapXml, const std::string& asCurrentMap)
36{
37 std::vector<std::string> vOut;
38 std::string sCurrent = LuxPreloadMapName(asCurrentMap);
39 const char* vKeys[2] = { "ChangeMap(\"", "Name=\"MapFile\" Value=\"" };
40 const std::string* vTexts[2] = { &asScript, &asMapXml };
41 for(int k = 0; k < 2; ++k)
42 {
43 const std::string& s = *vTexts[k];
44 std::string sKey = vKeys[k];
45 for(size_t i = s.find(sKey); i != std::string::npos; i = s.find(sKey, i + 1))
46 {
47 size_t a = i + sKey.size(), b = s.find('"', a);
48 if(b == std::string::npos || b == a) continue;
49 std::string sMap = LuxPreloadMapName(s.substr(a, b - a));
50 if(sMap != sCurrent && sMap != ".map") LuxPreloadAddUnique(vOut, sMap);
51 }
52 }
53 return vOut;
54}
55
56/** Folders of the files a map uses (its FileIndex: static objects, entities, decals), in order. */
57inline std::vector<std::string> LuxPreloadIndexFolders(const std::string& asMapXml)
58{
59 std::vector<std::string> vOut;
60 std::string sKey = "<File Id=\"";
61 for(size_t i = asMapXml.find(sKey); i != std::string::npos; i = asMapXml.find(sKey, i + 1))
62 {
63 size_t p = asMapXml.find("Path=\"", i);
64 size_t lEnd = asMapXml.find('>', i);
65 if(p == std::string::npos || p > lEnd) continue;
66 p += 6;
67 size_t q = asMapXml.find('"', p);
68 if(q == std::string::npos) continue;
69 std::string sPath = asMapXml.substr(p, q - p);
70 size_t lSlash = sPath.find_last_of("/\\");
71 if(lSlash == std::string::npos) continue;
72 LuxPreloadAddUnique(vOut, sPath.substr(0, lSlash + 1));
73 }
74 return vOut;
75}
76
77/** Files worth warming in those folders (models, materials, textures, entity and animation data). */
78inline bool LuxPreloadWanted(const std::string& asFile)
79{
80 std::string s = LuxPreloadLower(asFile);
81 const char* vExt[] = { ".msh", ".mat", ".dds", ".ent", ".anm", ".tga", ".png", ".jpg" };
82 for(int i = 0; i < 8; ++i)
83 {
84 size_t n = std::string(vExt[i]).size();
85 if(s.size() > n && s.compare(s.size() - n, n, vExt[i]) == 0) return true;
86 }
87 return false;
88}
89
90#endif
std::string LuxPreloadMapName(const std::string &asPath)
"E:/svn/maps/02_Mansion_02" -> "02_mansion_02.map"
Definition LuxPreload.h:19
std::vector< std::string > LuxPreloadNextMaps(const std::string &asScript, const std::string &asMapXml, const std::string &asCurrentMap)
Maps reachable from here: the script's ChangeMap("...") calls and level doors' MapFile values.
Definition LuxPreload.h:35
std::string LuxPreloadLower(std::string s)
Definition LuxPreload.h:12
void LuxPreloadAddUnique(std::vector< std::string > &avList, const std::string &asX)
Definition LuxPreload.h:28
bool LuxPreloadWanted(const std::string &asFile)
Files worth warming in those folders (models, materials, textures, entity and animation data).
Definition LuxPreload.h:78
std::vector< std::string > LuxPreloadIndexFolders(const std::string &asMapXml)
Folders of the files a map uses (its FileIndex: static objects, entities, decals),...
Definition LuxPreload.h:57