using LitJson; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; public class Series { public string Name; public string Url; public int Version; string SeriesPath { get { return $"{CUtil.DataSavePath}data/{Name.Replace("/", "")}/"; } } public List BoxLst = new List(); public List Tags = new List(); public List Rare = new List(); Dictionary Cards = new Dictionary(); public Dictionary GetCards() { return Cards; } public Series(string name, string url, int time) { Name = name; Url = url; Version = time; } public Series(string name, string url) { Name = name; Url = url; Version = 0; } public void SetVersion(int version) { Version = version; } public void AddCard(Card card) { Cards[card.Num] = card; if (!BoxLst.Contains(card.Box)) BoxLst.Add(card.Box); for(int i = 0; i < card.Tag.Length; i++) { if (!Tags.Contains(card.Tag[i])) Tags.Add(card.Tag[i]); } if (!Rare.Contains(card.Rare)) Rare.Add(card.Rare); } public string ToJson() { string json = $"\"Name\":\"{Name}\", \"Url\":\"{Url}\", \"Version\":{Version}"; return "{" + json + "},"; } string ToCardLstJson() { string json = ""; foreach (var item in Cards) { json += item.Value.ToJson() + ",\n"; } json = json.Remove(json.LastIndexOf(",")); return $"[\n{json}\n]"; } public void SaveCardLst() { if (!Directory.Exists($"{SeriesPath}")) Directory.CreateDirectory($"{SeriesPath}"); string path = $"{SeriesPath}/Cards.json"; string json = ToCardLstJson(); FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs); sw.Write(json); sw.Close(); fs.Close(); } /// /// 加载卡片列表 /// public void LoadCardsFromJson() { if (!Directory.Exists($"{SeriesPath}")) Directory.CreateDirectory($"{SeriesPath}"); string path = $"{SeriesPath}/Cards.json"; string json = null; if (!File.Exists(path)) { //尝试读取本地的 TextAsset tx = Resources.Load($"data/{Name.Replace("/", "")}/Cards.json"); if (tx == null) return; else json = tx.text; } else //读取存档 json = File.ReadAllText(path); LoadFromJsonText(json); } public void LoadFromJsonText(string json) { JsonData data = JsonMapper.ToObject(json); for (int i = 0; i < data.Count; i++) { Card card = new Card(data[i]); card.CheckImgFile(); AddCard(card); } } }