AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LuxSelfSetup.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: self-setup on first start. A player unzips AMFP Enhanced into the game folder (no
3 * installer); on the first start the game writes its amfp_enhanced/ config from the install's own
4 * stock files, the same files tools/install_enhanced.py writes:
5 * main_init.cfg (pointing at our files), game.cfg (a copy), resources.cfg (our override folder
6 * first), default_user_keys_sdl2.cfg (hint / note on the D-pad), default_main_settings_sdl2.cfg
7 * (graphics defaults), default_user_settings.cfg (Modern preset), and copies of the stock
8 * language files next to ours. Stock files are only read, never changed.
9 * The text changes here are pure, unit tested in tests/cpp/test_self_setup.cpp.
10 */
11#ifndef LUX_SELF_SETUP_H
12#define LUX_SELF_SETUP_H
13
14#include <cctype>
15#include <string>
16
17static const char* const kLuxSetupDir = "amfp_enhanced";
18
19/** Replaces the quoted value of `Key = "..."` (any spacing); false if the key is missing. */
20inline bool LuxSetupSetCfgValue(std::string& asText, const std::string& asKey, const std::string& asValue)
21{
22 size_t lPos = 0;
23 while((lPos = asText.find(asKey, lPos)) != std::string::npos)
24 {
25 bool bWordStart = lPos == 0 || !(isalnum((unsigned char)asText[lPos - 1]) || asText[lPos - 1] == '_');
26 size_t i = lPos + asKey.size();
27 while(i < asText.size() && (asText[i] == ' ' || asText[i] == '\t')) ++i;
28 if(bWordStart && i < asText.size() && asText[i] == '=')
29 {
30 ++i;
31 while(i < asText.size() && (asText[i] == ' ' || asText[i] == '\t')) ++i;
32 if(i < asText.size() && asText[i] == '"')
33 {
34 size_t lEnd = asText.find('"', i + 1);
35 if(lEnd == std::string::npos) return false;
36 asText.replace(i + 1, lEnd - i - 1, asValue);
37 return true;
38 }
39 }
40 lPos += asKey.size();
41 }
42 return false;
43}
44
45/** main_init.cfg for the fork: our resources, game.cfg, key / settings defaults and language folders. */
46inline bool LuxSetupMainInit(std::string& asText)
47{
48 const std::string sDir = std::string(kLuxSetupDir) + "/";
49 const char* vKeys[][2] = {
50 { "Resources", "resources.cfg" }, { "Game", "game.cfg" },
51 { "DefaultMainSettingsSDL2", "default_main_settings_sdl2.cfg" },
52 { "DefaultUserKeysSDL2", "default_user_keys_sdl2.cfg" },
53 { "BaseLanguageFolder", "lang/" }, { "GameLanguageFolder", "lang_main/" },
54 };
55 for(size_t i = 0; i < sizeof(vKeys) / sizeof(vKeys[0]); ++i)
56 if(!LuxSetupSetCfgValue(asText, vKeys[i][0], sDir + vKeys[i][1])) return false;
57 LuxSetupSetCfgValue(asText, "DefaultUserSettings", sDir + "default_user_settings.cfg"); // optional key
58 return true;
59}
60
61/** The Secondary binding of <Action Primary="..." Secondary="..." />; false if the action is missing. */
62inline bool LuxSetupSetBinding(std::string& asText, const std::string& asAction, const std::string& asSecondary)
63{
64 size_t lTag = asText.find("<" + asAction + " ");
65 if(lTag == std::string::npos) return false;
66 size_t lClose = asText.find('>', lTag);
67 size_t lSec = asText.find("Secondary=\"", lTag);
68 if(lSec == std::string::npos || lSec > lClose) return false;
69 lSec += 11;
70 size_t lEnd = asText.find('"', lSec);
71 if(lEnd == std::string::npos) return false;
72 asText.replace(lSec, lEnd - lSec, asSecondary);
73 return true;
74}
75
76/** Latest hint on D-pad right, last note on D-pad left (stock: hint on L3, note unbound). */
77inline bool LuxSetupUserKeys(std::string& asText)
78{
79 return LuxSetupSetBinding(asText, "QuestLog", "GamepadHat.Hat 0.Right") &&
80 LuxSetupSetBinding(asText, "RecentText", "GamepadHat.Hat 0.Left");
81}
82
83/** Sets attr="value" on the first <Element ...> tag, adding the attribute (or the element). */
84inline void LuxSetupSetAttr(std::string& asText, const std::string& asElement, const std::string& asAttr, const std::string& asValue)
85{
86 size_t lTag = std::string::npos, lFrom = 0;
87 while((lFrom = asText.find("<" + asElement, lFrom)) != std::string::npos)
88 {
89 char c = lFrom + 1 + asElement.size() < asText.size() ? asText[lFrom + 1 + asElement.size()] : '>';
90 if(c == ' ' || c == '/' || c == '>' || c == '\t' || c == '\r' || c == '\n') { lTag = lFrom; break; }
91 lFrom += 1;
92 }
93 if(lTag == std::string::npos)
94 {
95 while(!asText.empty() && (asText[asText.size() - 1] == '\n' || asText[asText.size() - 1] == '\r')) asText.erase(asText.size() - 1);
96 asText += "\n<" + asElement + " " + asAttr + "=\"" + asValue + "\" />\n";
97 return;
98 }
99 size_t lEndTag = asText.find('>', lTag);
100 std::string sNeedle = " " + asAttr + "=\"";
101 size_t lAttr = asText.find(sNeedle, lTag);
102 if(lAttr != std::string::npos && lAttr < lEndTag)
103 {
104 size_t lVal = lAttr + sNeedle.size();
105 size_t lValEnd = asText.find('"', lVal);
106 asText.replace(lVal, lValEnd - lVal, asValue);
107 return;
108 }
109 asText.insert(lTag + 1 + asElement.size(), " " + asAttr + "=\"" + asValue + "\"");
110}
111
112/** default_main_settings: the graphics defaults of the Modern preset. */
113inline void LuxSetupMainSettings(std::string& asText)
114{
115 const char* v[][2] = { { "TextureFilter", "2" }, { "TextureAnisotropy", "16.000000" }, { "ShadowQuality", "2" },
116 { "ParallaxQuality", "1" }, { "SSAOSamples", "16" }, { "SmoothShadows", "true" }, { "HDShadows", "true" },
117 { "FogAmount", "0.350000" }, { "FogColor", "0.000000" } }; // = LuxPresets.h Modern
118 for(size_t i = 0; i < sizeof(v) / sizeof(v[0]); ++i) LuxSetupSetAttr(asText, "Graphics", v[i][0], v[i][1]);
119}
120
121/** default_user_settings: the Modern preset for new profiles (LuxPresets.h). */
122inline void LuxSetupUserSettings(std::string& asText)
123{
124 LuxSetupSetAttr(asText, "Input", "SmoothMouse", "false");
125 LuxSetupSetAttr(asText, "Game", "ScriptedMotion", "Reduced");
126 LuxSetupSetAttr(asText, "Game", "ShowControlsHud", "true");
127 LuxSetupSetAttr(asText, "Game", "ReduceHeadBob", "true");
128}
129
130/** resources.cfg: our override folder first, so our files win bare-name lookups. */
131inline bool LuxSetupResources(std::string& asText)
132{
133 if(asText.find("/amfp_enhanced/override") != std::string::npos) return true;
134 size_t lPos = asText.find("<Resources>");
135 if(lPos == std::string::npos) return false;
136 lPos += 11;
137 const char* sNl = asText.find("\r\n") != std::string::npos ? "\r\n" : "\n";
138 asText.insert(lPos, std::string(sNl) + " <Directory Path=\"/amfp_enhanced/override\" AddSubDirs=\"true\" />");
139 return true;
140}
141
142#endif
bool LuxSetupUserKeys(std::string &asText)
Latest hint on D-pad right, last note on D-pad left (stock: hint on L3, note unbound).
bool LuxSetupResources(std::string &asText)
resources.cfg: our override folder first, so our files win bare-name lookups.
static const char *const kLuxSetupDir
bool LuxSetupMainInit(std::string &asText)
main_init.cfg for the fork: our resources, game.cfg, key / settings defaults and language folders.
void LuxSetupSetAttr(std::string &asText, const std::string &asElement, const std::string &asAttr, const std::string &asValue)
Sets attr="value" on the first <Element ...> tag, adding the attribute (or the element).
void LuxSetupUserSettings(std::string &asText)
default_user_settings: the Modern preset for new profiles (LuxPresets.h).
bool LuxSetupSetBinding(std::string &asText, const std::string &asAction, const std::string &asSecondary)
The Secondary binding of <Action Primary="..." Secondary="..." />; false if the action is missing.
bool LuxSetupSetCfgValue(std::string &asText, const std::string &asKey, const std::string &asValue)
Replaces the quoted value of Key = "..." (any spacing); false if the key is missing.
void LuxSetupMainSettings(std::string &asText)
default_main_settings: the graphics defaults of the Modern preset.