Capture current mission, AI, map and administration development

Include the current addon, terrain integrations, tools, regression tests and recorded development evidence.
This commit is contained in:
2026-10-13 22:52:53 +02:00
parent a951a96edc
commit 6a667d8deb
8104 changed files with 2124945 additions and 2061 deletions
@@ -0,0 +1,231 @@
// One operator-owned document. Legacy names identify sections internally only;
// custom paths used by isolated probes keep their original standalone behavior.
class ERR_ProfileConfig
{
static const string PATH = "$profile:Escapists.json";
static const string VOICE = "$profile:EscapistsWelcomeVoice.json";
static array<string> LegacyPaths()
{
return {ERR_Settings.PATH, ERR_ContentSettings.PATH, ERR_FactionSelection.PATH, ERR_TerrainPreference.PATH, ERR_ItemPreferences.PATH, VOICE};
}
static string Section(string path)
{
if (path == ERR_Settings.PATH) return "mission";
if (path == ERR_ContentSettings.PATH) return "mods";
if (path == ERR_FactionSelection.PATH) return "factions";
if (path == ERR_TerrainPreference.PATH) return "terrain";
if (path == ERR_ItemPreferences.PATH) return "items";
if (path == VOICE) return "voice";
return "";
}
static bool Text(string path, out string text)
{
FileHandle file = FileIO.OpenFile(path, FileMode.READ);
if (!file) return false;
int length = file.GetLength();
if (length < 2 || length > 8388608) { file.Close(); return false; }
file.Read(text, length); file.Close();
return text.Length() == length;
}
// The engine validates JSON syntax. This scan only separates root members,
// retaining unknown fields and dormant addon entries without reinterpreting them.
static bool Split(string text, map<string, string> members)
{
members.Clear(); text.TrimInPlace();
JsonLoadContext valid = new JsonLoadContext();
if (text.Length() < 2 || text.Substring(0, 1) != "{" || !valid.LoadFromString(text)) return false;
int depth, start = 1, colon = -1;
bool quoted, escaped;
string block;
for (int i = 0; i < text.Length(); i++)
{
if (i % 1024 == 0) block = text.Substring(i, Math.Min(1024, text.Length() - i));
string ch = block.Substring(i % 1024, 1);
if (quoted)
{
if (escaped) escaped = false;
else if (ch == "\\") escaped = true;
else if (ch == "\"") quoted = false;
continue;
}
if (ch == "\"") { quoted = true; continue; }
if (ch == "{" || ch == "[") depth++;
if (ch == "}" || ch == "]") depth--;
if (depth == 1 && ch == ":" && colon < 0) colon = i;
if ((depth == 1 && ch == ",") || (depth == 0 && ch == "}"))
{
if (colon < start)
{
string empty = text.Substring(start, i - start); empty.TrimInPlace();
return members.Count() == 0 && empty.IsEmpty() && i == text.Length() - 1;
}
JsonLoadContext keyJson = new JsonLoadContext();
string key;
if (!keyJson.LoadFromString("{\"key\":" + text.Substring(start, colon - start) + "}") || !keyJson.ReadValue("key", key) || members.Contains(key)) return false;
string value = text.Substring(colon + 1, i - colon - 1); value.TrimInPlace();
members.Set(key, value); start = i + 1; colon = -1;
}
}
return depth == 0 && !quoted;
}
static bool Document(string path, out map<string, string> members)
{
string text; members = new map<string, string>();
if (!Text(path, text) || !Split(text, members)) return false;
// Never downgrade or rewrite a configuration created by an unknown version.
return members.Contains("configVersion") && members.Get("configVersion") == "1";
}
static bool Valid(string section, string text)
{
JsonLoadContext json = new JsonLoadContext();
map<string, string> fields = new map<string, string>();
if (!Split(text, fields) || !json.LoadFromString(text)) return false;
map<string, int> numbers; map<string, string> roles; ResourceName scenario; array<string> records;
if (section == "mission") return ERR_Settings.Parse(json, numbers);
if (section == "mods") return ERR_ContentSettings.Parse(json, numbers);
if (section == "factions") return ERR_FactionSelection.Parse(json, roles);
if (section == "terrain") return ERR_TerrainPreference.Parse(json, scenario);
if (section == "items") return ERR_ItemPreferences.Parse(json, records);
string url;
return section == "voice" && json.ReadValue("welcomeUrl", url) && (url.IsEmpty() || ERR_WelcomeVoiceRules.URL(url));
}
static string Defaults(string section)
{
if (section == "mods") return "{}";
if (section == "factions") return "{\"escapists\":\"us\",\"enemy\":\"ussr\",\"independent\":\"fia\"}";
if (section == "terrain") return "{\"scenario\":\"\"}";
if (section == "items") return "{\"version\":1,\"records\":[\"active\"]}";
if (section == "voice") return "{\"welcomeUrl\":\"\"}";
JsonSaveContext json = new JsonSaveContext(); json.EnableTypeDiscriminator(false);
foreach (string key : ERR_Settings.Keys())
{
int value = ERR_Settings.AddedDefault(key);
if (key == "reportIntervalSeconds") value = 10;
if (key == "supportSquadLimit") value = 4;
if (key == "responseRadiusMetres") value = 2000;
if (key == "aiIntelChancePercent") value = 60;
if (key == "intelRevealChancePercent") value = 15;
if (key == "radioDefenceSeconds") value = 180;
json.WriteValue(key, value);
}
return json.SaveToString();
}
static string Encode(map<string, string> members)
{
string result = "{\n";
int index;
foreach (string key, string raw : members)
{
// Quote arbitrary root keys through the engine, including escaped characters.
JsonSaveContext name = new JsonSaveContext(); name.EnableTypeDiscriminator(false); name.WriteValue("key", key);
string pair = name.SaveToString(); int colon = pair.IndexOf(":");
string quotedKey = pair.Substring(colon + 1, pair.Length() - colon - 2); quotedKey.TrimInPlace();
if (index > 0) result += ",\n";
result += " " + quotedKey + ": " + raw; index++;
}
return result + "\n}\n";
}
static bool Same(map<string, string> a, map<string, string> b)
{
if (!a || !b || a.Count() != b.Count()) return false;
foreach (string key, string value : a) if (!b.Contains(key) || b.Get(key) != value) return false;
return true;
}
static bool Commit(map<string, string> members)
{
if (!Replication.IsServer()) return false;
FileHandle file = FileIO.OpenFile(PATH + ".tmp", FileMode.WRITE);
if (!file) return false;
file.Write(Encode(members)); file.Close();
map<string, string> checked, previous;
if (!Document(PATH + ".tmp", checked) || !Same(members, checked)) return false;
bool hadPrevious = Document(PATH, previous);
if (hadPrevious)
{
if (!FileIO.CopyFile(PATH, PATH + ".bak") || !Document(PATH + ".bak", checked) || !Same(previous, checked)) return false;
}
if (!FileIO.CopyFile(PATH + ".tmp", PATH) || !Document(PATH, checked) || !Same(members, checked))
{
if (FileIO.FileExists(PATH + ".bak")) FileIO.CopyFile(PATH + ".bak", PATH);
else if (!hadPrevious) FileIO.DeleteFile(PATH);
return false;
}
FileIO.DeleteFile(PATH + ".tmp");
return true;
}
static bool Ensure()
{
if (!Replication.IsServer()) return false;
// Existing unified files are authoritative; never reimport stale legacy data.
if (FileIO.FileExists(PATH) || FileIO.FileExists(PATH + ".bak"))
{
string existing; map<string, string> root = new map<string, string>();
if (Text(PATH, existing) && Split(existing, root) && (!root.Contains("configVersion") || root.Get("configVersion") != "1"))
{
Print("Reintroduced [config]: unsupported Escapists.json configVersion; file preserved, writes disabled", LogLevel.ERROR);
return false;
}
return true;
}
map<string, string> members = new map<string, string>(); members.Set("configVersion", "1");
foreach (string path : LegacyPaths())
{
string section = Section(path), raw;
if (!Text(path, raw) || !Valid(section, raw))
{
if (!Text(path + ".bak", raw) || !Valid(section, raw))
{
if (FileIO.FileExists(path) || FileIO.FileExists(path + ".bak"))
{
Print("Reintroduced [config]: cannot import " + path + "; repair it or its backup. Existing files preserved.", LogLevel.ERROR);
return false;
}
raw = Defaults(section);
}
}
raw.TrimInPlace(); members.Set(section, raw);
}
if (!Commit(members)) return false;
Print("Reintroduced [config]: using $profile:Escapists.json; legacy files retained but no longer used.");
return true;
}
static bool Load(string path, JsonLoadContext json)
{
string section = Section(path); bool backup;
if (section.IsEmpty() && path.EndsWith(".bak")) { section = Section(path.Substring(0, path.Length() - 4)); backup = true; }
if (section.IsEmpty()) return json.LoadFromFile(path);
if (!Ensure()) return false;
string source = PATH; if (backup) source += ".bak";
map<string, string> members;
if (!Document(source, members)) return false;
if (!members.Contains(section)) return json.LoadFromString(Defaults(section));
return json.LoadFromString(members.Get(section));
}
static bool Exists(string path)
{
string section = Section(path);
if (section.IsEmpty() && path.EndsWith(".bak")) section = Section(path.Substring(0, path.Length() - 4));
if (section.IsEmpty()) return FileIO.FileExists(path);
// A failed migration must make all owners read-only, not just the bad section.
if (!Ensure()) return true;
return FileIO.FileExists(PATH) || FileIO.FileExists(PATH + ".bak");
}
static bool Save(string path, JsonSaveContext json)
{
if (!Replication.IsServer() || !Ensure()) return false;
string section = Section(path), raw = json.SaveToString(); raw.TrimInPlace();
if (section.IsEmpty() || !Valid(section, raw)) return false;
map<string, string> members;
if (!Document(PATH, members) && !Document(PATH + ".bak", members)) return false;
// Keep unrecognized fields within the edited section too (for returning mods
// and newer optional settings). Explicitly written values replace old values.
map<string, string> fields = new map<string, string>();
map<string, string> changed = new map<string, string>();
if (members.Contains(section) && !Split(members.Get(section), fields)) return false;
if (!Split(raw, changed)) return false;
foreach (string key, string value : changed) fields.Set(key, value);
raw = Encode(fields); raw.TrimInPlace();
members.Set(section, raw);
return Commit(members);
}
}
@@ -0,0 +1,83 @@
"""Execute the actual shared-file transaction with injected disk failures.
Engine JSON/migration/menu checks are recorded separately in native evidence.
"""
from policy_harness import ROOT, body, run
p = ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_ProfileConfig.c'
method = body(p, 'static bool Commit(')
for before, after in [
('map<string, string>', 'Map'), ('FileHandle file', 'FileHandle* file'),
('file.', 'file->'), ('FileIO.', 'FileIO::'), ('FileMode.', 'FileMode::'),
('Replication.', 'Replication::')]:
method = method.replace(before, after)
run(r'''
#include <cassert>
#include <map>
#include <string>
using string=std::string;
using Map=std::map<string,string>;
const string PATH="Escapists.json";
std::map<string,string> files;
std::map<string,Map> documents;
int fault=0,serial=0;
namespace Replication {bool server=true;bool IsServer(){return server;}}
namespace FileMode {int WRITE=1;}
struct FileHandle {
string path;
void Write(string data){files[path]=fault==2?"CORRUPT":data;}
void Close(){}
};
namespace FileIO {
FileHandle* OpenFile(string path,int){static FileHandle h;h.path=path;return fault==1?nullptr:&h;}
bool FileExists(string path){return files.count(path);}
bool DeleteFile(string path){return files.erase(path);}
bool CopyFile(string from,string to){
if(!files.count(from))return false;
if(to==PATH+".bak"&&fault==3)return false;
files[to]=files[from];
if(to==PATH+".bak"&&fault==4)files[to]="CORRUPT";
if(from==PATH+".tmp"){
if(fault==5){files[to]="PARTIAL";return false;}
if(fault==6)files[to]="CORRUPT";
if(fault==7){files[to]="WRONG";documents["WRONG"]={{"mission","wrong"}};}
}
return true;
}
}
struct Store {
static string Encode(Map values){string id="doc"+std::to_string(++serial);documents[id]=values;return id;}
static bool Document(string path,Map& values){
if(!files.count(path)||!documents.count(files[path]))return false;
values=documents[files[path]];return true;
}
static bool Same(Map a,Map b){return a==b;}
''' + method + r'''
};
int main(){
Map old={{"mission","old"},{"items","presets"},{"voice","url"}}, next=old,read;
next["mission"]="new";
for(int injected:{1,2,5,6,7}){
files.clear();fault=injected;
assert(!Store::Commit(next)&&!files.count(PATH)); // failed first save stays absent
}
for(int injected=1;injected<=7;injected++){
files.clear();fault=0;assert(Store::Commit(old));
fault=injected;assert(!Store::Commit(next));
assert(Store::Document(PATH,read)&&read==old); // current valid config remains
fault=0;assert(Store::Commit(next));
assert(Store::Document(PATH,read)&&read==next);
}
for(int injected:{1,2,5,6,7}){
files.clear();files[PATH]="CORRUPT";files[PATH+".bak"]=Store::Encode(old);
fault=injected;assert(!Store::Commit(next));
assert(Store::Document(PATH+".bak",read)&&read==old); // only good backup not replaced
fault=0;assert(Store::Commit(next));
assert(Store::Document(PATH,read)&&read==next);
}
files.clear();fault=0;assert(Store::Commit(old));assert(Store::Commit(next));
assert(Store::Document(PATH+".bak",read)&&read==old);
assert(!files.count(PATH+".tmp"));
Replication::server=false;assert(!Store::Commit(old));
assert(Store::Document(PATH,read)&&read==next);
}
''')
print('PASS: shared config transaction rejects client writes, preserves first-save/prior/fallback state across open, write, backup, partial-copy, readback and mismatch failures')