AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LuxGameRoot.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: the game folder the Windows exe works from. The 64-bit exe and its DLLs live in
3 * <game>/amfp_enhanced/bin, apart from the original game's 32-bit DLLs of the same names.
4 * The root launcher (aamfp_enhanced.exe, Steam, shortcuts) starts it with the game folder as
5 * working directory; when it is started from bin directly, it switches there itself.
6 * Pure (paths as strings), unit tested in tests/cpp/test_game_root.cpp.
7 */
8#ifndef LUX_GAME_ROOT_H
9#define LUX_GAME_ROOT_H
10
11#include <string>
12
13/** The folder of a file path ("C:/a/b/c.exe" -> "C:/a/b"); either slash; "" if none. */
14template<class tStr>
15inline tStr LuxParentDir(const tStr& asPath)
16{
17 size_t lPos = asPath.find_last_of(typename tStr::value_type('/')) ;
18 size_t lPos2 = asPath.find_last_of(typename tStr::value_type('\\'));
19 if(lPos == tStr::npos || (lPos2 != tStr::npos && lPos2 > lPos)) lPos = lPos2;
20 return lPos == tStr::npos ? tStr() : asPath.substr(0, lPos);
21}
22
23/** Last path component, compared case-insensitively against an ASCII name. */
24template<class tStr>
25inline bool LuxDirNameIs(const tStr& asDir, const char* asName)
26{
27 size_t lStart = asDir.find_last_of(typename tStr::value_type('/'));
28 size_t lStart2 = asDir.find_last_of(typename tStr::value_type('\\'));
29 if(lStart == tStr::npos || (lStart2 != tStr::npos && lStart2 > lStart)) lStart = lStart2;
30 lStart = lStart == tStr::npos ? 0 : lStart + 1;
31 size_t n = 0;
32 for(; asName[n]; ++n)
33 {
34 if(lStart + n >= asDir.size()) return false;
35 typename tStr::value_type c = asDir[lStart + n];
36 if(c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
37 if(c != (typename tStr::value_type)asName[n]) return false;
38 }
39 return lStart + n == asDir.size();
40}
41
42/** Game folder for an exe at <game>/amfp_enhanced/bin/<exe>; "" when the exe is elsewhere. */
43template<class tStr>
44inline tStr LuxGameRootFromExe(const tStr& asExePath)
45{
46 tStr sBin = LuxParentDir(asExePath);
47 if(LuxDirNameIs(sBin, "bin") == false) return tStr();
48 tStr sEnhanced = LuxParentDir(sBin);
49 if(LuxDirNameIs(sEnhanced, "amfp_enhanced") == false) return tStr();
50 return LuxParentDir(sEnhanced);
51}
52
53#endif
bool LuxDirNameIs(const tStr &asDir, const char *asName)
Last path component, compared case-insensitively against an ASCII name.
Definition LuxGameRoot.h:25
tStr LuxGameRootFromExe(const tStr &asExePath)
Game folder for an exe at <game>/amfp_enhanced/bin/<exe>; "" when the exe is elsewhere.
Definition LuxGameRoot.h:44
tStr LuxParentDir(const tStr &asPath)
The folder of a file path ("C:/a/b/c.exe" -> "C:/a/b"); either slash; "" if none.
Definition LuxGameRoot.h:15