Newer
Older
EARS-LINUX / program / AppMode.cs
using System;
using System.IO;

public static class AppMode
{
    /// false = current single-map mode, true = legacy 7-region mode.
    /// Set once in Program.Main from the --legacy argument, before any UI exists.
    public static bool Legacy = false;

    public static string CasesCsv
    {
        get { return SoundWindow.AppFile(Legacy ? "cases_old.csv" : "cases.csv"); }
    }

    // Legacy looks in its own folder first and falls back to the shared one,
    // so files only need to exist once.
    private static string MapDir   { get { return Legacy ? "map_old"   : "map";   } }
    private static string SoundDir { get { return Legacy ? "sound_old" : "sound"; } }

    /// CSV value -> absolute path. Returns null for "none"/empty.
    public static string ResolveMap(string csvValue)   { return Resolve(csvValue, MapDir,   "map");   }
    public static string ResolveSound(string csvValue) { return Resolve(csvValue, SoundDir, "sound"); }

    private static string Resolve(string csvValue, string dir, string fallbackDir)
    {
        if (string.IsNullOrWhiteSpace(csvValue)) return null;
        if (csvValue.Trim().Equals("none", StringComparison.OrdinalIgnoreCase)) return null;

        string file = Path.GetFileName(csvValue.Trim().Replace('\\', '/'));   // Python CSVs may use Windows paths
        string path = SoundWindow.AppFile(Path.Combine(dir, file));

        if (!File.Exists(path) && dir != fallbackDir)       // shared asset fallback
        {
            string shared = SoundWindow.AppFile(Path.Combine(fallbackDir, file));
            if (File.Exists(shared)) return shared;
        }

        // Linux is case-sensitive; the Python data came from Windows.
        if (!File.Exists(path))
        {
            string ci = FindIgnoreCase(dir, file) ?? FindIgnoreCase(fallbackDir, file);
            if (ci != null) return ci;
        }

        return path;    // may not exist; callers log the missing file
    }

    private static string FindIgnoreCase(string dir, string file)
    {
        string full = SoundWindow.AppFile(dir);
        if (!Directory.Exists(full)) return null;
        foreach (string f in Directory.GetFiles(full))
            if (string.Equals(Path.GetFileName(f), file, StringComparison.OrdinalIgnoreCase))
                return f;
        return null;
    }
}