92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace Hk4eGm.Services;
|
|
|
|
public sealed record Entry(string Id, string Name, string Category)
|
|
{
|
|
public string Label => $"{Id} {Name}";
|
|
public override string ToString() => Label;
|
|
}
|
|
|
|
public sealed class Table
|
|
{
|
|
public string Name { get; }
|
|
public List<Entry> Entries { get; } = new();
|
|
public List<string> Categories { get; } = new() { "全部" };
|
|
|
|
public Table(string name) => Name = name;
|
|
|
|
public IEnumerable<Entry> Filtered(string category)
|
|
=> string.IsNullOrEmpty(category) || category == "全部"
|
|
? Entries
|
|
: Entries.Where(e => e.Category == category);
|
|
|
|
public IEnumerable<Entry> Search(string keyword, int limit = 300)
|
|
{
|
|
var kw = (keyword ?? "").Trim().ToLowerInvariant();
|
|
IEnumerable<Entry> pool = Entries;
|
|
if (kw.Length == 0) return pool.Take(limit);
|
|
if (kw.All(char.IsDigit)) return pool.Where(e => e.Id.StartsWith(kw)).Take(limit);
|
|
return pool.Where(e => e.Name.ToLowerInvariant().Contains(kw) || e.Id.Contains(kw)).Take(limit);
|
|
}
|
|
|
|
public Entry? ById(string id) => Entries.FirstOrDefault(e => e.Id == id);
|
|
}
|
|
|
|
public sealed class DataStore
|
|
{
|
|
private readonly Dictionary<string, Table> _cache = new(StringComparer.OrdinalIgnoreCase);
|
|
private readonly string _dir;
|
|
|
|
public DataStore()
|
|
{
|
|
var baseDir = AppContext.BaseDirectory;
|
|
var candidate = Path.Combine(baseDir, "data");
|
|
_dir = Directory.Exists(candidate) ? candidate : Path.Combine(baseDir, "..", "..", "..", "..", "data");
|
|
}
|
|
|
|
public Table Get(string name)
|
|
{
|
|
if (_cache.TryGetValue(name, out var cached)) return cached;
|
|
var table = new Table(name);
|
|
var path = Path.Combine(_dir, name + ".txt");
|
|
if (File.Exists(path))
|
|
{
|
|
var category = "";
|
|
foreach (var raw in File.ReadAllLines(path, Encoding.UTF8))
|
|
{
|
|
var line = raw.TrimEnd('\r');
|
|
if (line.Trim().Length == 0) continue;
|
|
if (line.TrimStart().StartsWith("//"))
|
|
{
|
|
category = line.TrimStart()[2..].Trim();
|
|
if (category.Length > 0 && !table.Categories.Contains(category)) table.Categories.Add(category);
|
|
continue;
|
|
}
|
|
var idx = line.IndexOf(':');
|
|
if (idx <= 0) continue;
|
|
var id = line[..idx].Trim();
|
|
if (id.Length == 0 || !id.All(char.IsDigit)) continue;
|
|
var name2 = line[(idx + 1)..].Trim();
|
|
table.Entries.Add(new Entry(id, name2, category));
|
|
}
|
|
}
|
|
_cache[name] = table;
|
|
return table;
|
|
}
|
|
|
|
public Table Item => Get("Item");
|
|
public Table Weapon => Get("Weapon");
|
|
public Table Avatar => Get("Avatar");
|
|
public Table Artifact => Get("Artifact");
|
|
public Table ArtifactCat => Get("ArtifactCat");
|
|
public Table ArtifactMain => Get("ArtifactMainAttribution");
|
|
public Table ArtifactSub => Get("ArtifactSubAttribution");
|
|
public Table Monster => Get("Monster");
|
|
public Table Gadget => Get("Gadget");
|
|
public Table Quest => Get("Quest");
|
|
public Table Dungeon => Get("Dungeon");
|
|
public Table Activity => Get("Activity");
|
|
}
|