using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
namespace TKS
{
///
/// 컨텐츠 모드 표(2026-11-17 컨텐츠 모드 기획 10판 · 개발 요청서 A-1) — SOT = 조직 레포 `공유/기획팀_산출물/삼국지슬링샷/tables/` →
/// 사본 `Assets/Resources/ContentModes/*.csv`(utf-8-sig · 시나리오 표와 같은 방식: 임포트 없이 실행 중에 읽는다 → 표만 바꾸면 다음 전투 · 화면부터 반영).
/// 값은 전부 표에서 읽는다(C45 · 코드에 수치 없음). 공통 설정 = content_modes_config(s_mode, s_key, s_value).
///
public static class ContentTables
{
public const string Root = "ContentModes/";
public const string Config = "content_modes_config";
static readonly Dictionary> cache = new Dictionary>();
static Dictionary cfg;
/// 읽기 오류(파일 없음 · 칸 수 · 숫자 아님) — 데이터 검사 테스트가 0 인지 본다.
public static readonly List Errors = new List();
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
Reload();
}
/// 다시 읽기(전투 · 화면을 열 때 부른다 — 표를 고친 뒤 바로 반영).
public static void Reload()
{
cache.Clear();
cfg = null;
Errors.Clear();
}
public static List Rows(string file)
{
List rows;
if (cache.TryGetValue(file, out rows)) return rows;
rows = new List();
var ta = Resources.Load(Root + file);
if (ta == null) Errors.Add("Resources/" + Root + file + ".csv 없음");
else
foreach (var d in StageTable.ReadCsv(ta.text, file + ".csv", Errors))
rows.Add(new ContentRow(file, d));
cache[file] = rows;
return rows;
}
/// 그 칸 값이 v 인 첫 줄(없으면 null).
public static ContentRow Find(string file, string col, string v)
{
foreach (var r in Rows(file))
if (r.S(col) == v) return r;
return null;
}
// ---------- 공통 설정(content_modes_config) ----------
static Dictionary CfgMap()
{
if (cfg != null) return cfg;
cfg = new Dictionary();
foreach (var r in Rows(Config)) cfg[r.S("s_mode") + "." + r.S("s_key")] = r.S("s_value");
return cfg;
}
public static bool Has(string mode, string key)
{
return CfgMap().ContainsKey(mode + "." + key);
}
public static string Str(string mode, string key)
{
string v;
if (CfgMap().TryGetValue(mode + "." + key, out v)) return v;
Missing(mode, key);
return "";
}
public static float Num(string mode, string key)
{
float v;
var s = Str(mode, key);
if (s.Length > 0 && TryNum(s, out v)) return v;
if (s.Length > 0) Errors.Add(Config + ".csv " + mode + "." + key + ": 숫자 아님 「" + s + "」");
return 0f;
}
public static int Int(string mode, string key)
{
return Mathf.RoundToInt(Num(mode, key));
}
/// 「a:1;b:2」 → { a:1, b:2 } (값이 숫자가 아닌 칸은 건너뜀).
public static Dictionary Map(string mode, string key)
{
return ParseMap(Str(mode, key));
}
/// 「a;b;c」 → [a, b, c].
public static List List(string mode, string key)
{
return SplitList(Str(mode, key));
}
/// 「1:5;6:6;8:7」 처럼 「그 단계부터 이 값」 계단 표에서 n 에 해당하는 값(없으면 def).
public static float Step(string mode, string key, int n, float def)
{
float best = def;
int at = int.MinValue;
foreach (var kv in Map(mode, key))
{
int k;
if (!int.TryParse(kv.Key, NumberStyles.Integer, CultureInfo.InvariantCulture, out k)) continue;
if (k <= n && k > at)
{
at = k;
best = kv.Value;
}
}
return best;
}
static readonly HashSet missing = new HashSet();
static void Missing(string mode, string key)
{
string k = mode + "." + key;
if (missing.Add(k)) Errors.Add(Config + ".csv 에 " + k + " 줄 없음");
}
// ---------- 해석 도우미 ----------
public static bool TryNum(string s, out float v)
{
return float.TryParse((s ?? "").Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out v);
}
public static Dictionary ParseMap(string s)
{
var res = new Dictionary();
foreach (var part in SplitList(s))
{
int i = part.LastIndexOf(':');
if (i <= 0) continue;
float v;
if (TryNum(part.Substring(i + 1), out v)) res[part.Substring(0, i).Trim()] = v;
}
return res;
}
///
/// 글자 속 숫자들(「{a} 피해 +50 % · 나머지 −20 %」 → [50, −20]) — 기획 표가 값을 설명 글로만 준 칸(훈련 조건 · 허수아비 체력)을 읽는다.
/// {n} {a} 같은 자리 표시는 건너뛴다. 「−」(유니코드 빼기)도 음수로 읽는다.
///
public static List Numbers(string s)
{
var res = new List();
if (string.IsNullOrEmpty(s)) return res;
int i = 0;
while (i < s.Length)
{
char c = s[i];
bool neg = (c == '-' || c == '−') && i + 1 < s.Length && char.IsDigit(s[i + 1]);
if (!char.IsDigit(c) && !neg)
{
i++;
continue;
}
int st = neg ? i + 1 : i;
int j = st;
while (j < s.Length && (char.IsDigit(s[j]) || s[j] == '.')) j++;
float v;
if (TryNum(s.Substring(st, j - st), out v)) res.Add(neg ? -v : v);
i = j;
}
return res;
}
public static List SplitList(string s)
{
var res = new List();
if (string.IsNullOrEmpty(s)) return res;
foreach (var p in s.Split(';'))
{
var t = p.Trim();
if (t.Length > 0) res.Add(t);
}
return res;
}
}
/// 표 1줄 — 칸 이름으로 읽는다(접두어 n_ 정수 · f_ 실수 · s_ 문자 · l_ 목록 · b_ 0/1 · e_ 낱말).
public class ContentRow
{
public readonly string File;
readonly Dictionary d;
public ContentRow(string file, Dictionary d)
{
File = file;
this.d = d;
}
public bool Has(string k)
{
return d.ContainsKey(k);
}
public string S(string k)
{
string v;
return d.TryGetValue(k, out v) ? v : "";
}
public float F(string k, float def = 0f)
{
float v;
var s = S(k);
return s.Length > 0 && ContentTables.TryNum(s, out v) ? v : def;
}
public int I(string k, int def = 0)
{
float v;
var s = S(k);
return s.Length > 0 && ContentTables.TryNum(s, out v) ? Mathf.RoundToInt(v) : def;
}
public bool B(string k)
{
var s = S(k);
return s == "1" || s == "true" || s == "TRUE";
}
public List L(string k)
{
return ContentTables.SplitList(S(k));
}
public List LF(string k)
{
var res = new List();
foreach (var p in L(k))
{
float v;
if (ContentTables.TryNum(p, out v)) res.Add(v);
}
return res;
}
/// 파일 줄 번호(오류 안내).
public string Line
{
get { return File + ".csv 줄 " + S("#"); }
}
}
}