using System.Collections.Generic; using System.Globalization; using UnityEngine; namespace TKS.OutGame.Net { /// /// 서버 접속 설정. 값 = 표 `Resources/Net/server.csv`(key,value) — 주소·시간 값을 코드에 적지 않는다. /// 이 PC 에서만 켜고 끄기 = PlayerPrefs(에디터 메뉴 「TKS/Server」) · 표의 enabled 보다 우선한다. /// 메모리 세이브(자동 테스트 · 검증 캡처)에서는 항상 꺼진다 — 서버 연결 테스트만 ForceForTest 로 켠다. /// public static class ServerConfig { public const string ResourcePath = "Net/server"; /// 이 PC 덮어쓰기: 0 = 끔 · 1 = 켬 · 키 없음 = 표 값. public const string PrefEnabled = "tks.server.enabled"; /// 이 PC 덮어쓰기: 서버 주소(비면 표 값). public const string PrefUrl = "tks.server.url"; static Dictionary table; static bool forced; static string forcedUrl; static int forcedGame; [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] static void ResetStatics() { table = null; forced = false; forcedUrl = null; } static Dictionary Table { get { if (table != null) return table; table = new Dictionary(); var asset = Resources.Load(ResourcePath); if (asset == null) { Debug.LogError("[TKS] 서버 설정 표가 없습니다: Resources/" + ResourcePath + ".csv"); return table; } var lines = asset.text.Replace("\r", "").Split('\n'); for (int i = 1; i < lines.Length; i++) // 0 = 머리줄 { var line = lines[i].Trim('', ' '); int comma = line.IndexOf(','); if (comma <= 0) continue; table[line.Substring(0, comma).Trim()] = line.Substring(comma + 1).Trim(); } return table; } } static string Get(string key) { string v; if (Table.TryGetValue(key, out v)) return v; Debug.LogError("[TKS] 서버 설정 표에 키 「" + key + "」 가 없습니다"); return ""; } static float Num(string key) { float f; return float.TryParse(Get(key), NumberStyles.Float, CultureInfo.InvariantCulture, out f) ? f : 0f; } /// 서버 연결을 쓰도록 설정됐는가(표 또는 이 PC 덮어쓰기). public static bool Enabled { get { if (forced) return true; if (PlayerPrefs.HasKey(PrefEnabled)) return PlayerPrefs.GetInt(PrefEnabled) == 1; return Get("enabled") == "1"; } } /// 이번 실행이 서버에 붙는가 — 메모리 세이브에서는 붙지 않는다(테스트 강제 제외). public static bool Active { get { return forced || (Enabled && !SaveStore.MemoryOnly); } } public static bool Forced { get { return forced; } } public static string BaseUrl { get { string url = forced ? forcedUrl : PlayerPrefs.GetString(PrefUrl, ""); if (string.IsNullOrEmpty(url)) url = Get("base_url"); return url.TrimEnd('/'); } } public static int GameId { get { return forced && forcedGame > 0 ? forcedGame : Mathf.RoundToInt(Num("game_id")); } } /// 요청 1건 제한 시간(초). public static int TimeoutSec { get { return Mathf.Max(1, Mathf.RoundToInt(Num("timeout_sec"))); } } /// 연결 실패 시 알림 없이 다시 보내는 횟수. public static int Retry { get { return Mathf.Max(0, Mathf.RoundToInt(Num("retry"))); } } /// 보낼 것이 없어진 뒤 서버 상태로 맞추기까지 기다리는 시간(초). public static float ReconcileDelay { get { return Mathf.Max(0f, Num("reconcile_delay_sec")); } } /// 응답을 기다려야 하는 요청이 이 시간(초)을 넘기면 「기록하는 중」 안내를 띄운다. public static float WaitNotice { get { return Mathf.Max(0f, Num("wait_notice_sec")); } } /// 서버 모드에서 쓰는 기기 사본 파일 이름 — 기존 save.json 을 덮어쓰지 않게 따로 둔다. public static string CacheFile { get { return Get("cache_file"); } } /// 서버 연결 테스트 전용 — 메모리 세이브에서도 주어진 서버에 붙는다. 끝나면 ClearForce. public static void ForceForTest(string url, int gameId) { forced = true; forcedUrl = url; forcedGame = gameId; } public static void ClearForce() { forced = false; forcedUrl = null; } } }