Clear message instead of crash when data/ or the save is not readable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-26 21:15:13 +02:00
co-authored by Claude Opus 5.5
parent f439f13268
commit 48f76e97cc
3 changed files with 67 additions and 7 deletions
+36 -6
View File
@@ -73,19 +73,30 @@ public sealed class GameServer
public bool LoadSave()
{
saveFiles = new List<KeyValuePair<string, byte[]>>();
Directory.CreateDirectory(savePath);
foreach (string name in SaveFiles)
// Erst alles lesen, dann übernehmen: schlägt "reload-save" fehl, bleibt der bisherige Spielstand aktiv
var loaded = new List<KeyValuePair<string, byte[]>>();
try
{
string file = Path.Combine(savePath, name);
if (File.Exists(file)) saveFiles.Add(new(name, File.ReadAllBytes(file)));
Directory.CreateDirectory(savePath);
foreach (string name in SaveFiles)
{
string file = Path.Combine(savePath, name);
if (File.Exists(file)) loaded.Add(new(name, File.ReadAllBytes(file)));
}
}
if (saveFiles.All(f => f.Key != "defaultES2File.txt"))
catch (Exception e) when (e is UnauthorizedAccessException or IOException)
{
Log("Cannot read the save game: " + e.Message);
Log(PermissionHint());
return false;
}
if (loaded.All(f => f.Key != "defaultES2File.txt"))
{
Log("No save game found in " + savePath);
Log("Copy your My Summer Car save files there, or start the server with --import-save.");
return false;
}
saveFiles = loaded;
Log("Loaded save game (" + saveFiles.Count + " files, " + saveFiles.Sum(f => f.Value.Length) / 1024 + " KB)");
return true;
}
@@ -507,6 +518,25 @@ public sealed class GameServer
}
}
/// <summary>
/// Hinweis bei fehlenden Dateirechten (Linux/Docker: Dateien gehören einem anderen Benutzer,
/// z.B. root oder per SFTP mit anderem Konto hochgeladen).
/// </summary>
public static string PermissionHint()
{
string uid = "?";
try
{
string line = File.ReadLines("/proc/self/status").FirstOrDefault(l => l.StartsWith("Uid:"));
if (line != null) uid = line.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries)[1];
}
catch { } // nicht Linux
return uid == "?"
? "Check the file permissions of the data folder."
: $"The server runs as user id {uid}. Fix on the host: sudo chown -R {uid}:{uid} data " +
"(or set MSCO_UID / MSCO_GID in .env to your own id: `id -u`) - see README \"Permission denied\".";
}
void SaveWorld()
{
try