using System; using System.Collections.Generic; using System.IO; using Script.UI; using UnityEngine; using ZXL.ID; namespace Script { public class TitleInfo { public string title; public string subject; } public class FileManager : SingleManager { private string dataPath; private string titlePath; private TitleInfo titleInfo; public TitleInfo GetTitle => titleInfo; // cache private string cachePath; public FileManager() { // data dataPath = Application.streamingAssetsPath + $"/TXT/data{GenerateGlobalID.GenerateIntID()}.txt"; if (!File.Exists(dataPath)) { var fileStream = File.Create(dataPath); fileStream.Dispose(); } // title titlePath = Application.streamingAssetsPath + "/title.txt"; var text = File.ReadAllLines(titlePath); titleInfo = new TitleInfo() { title = text[0], subject = text[1] }; // cache cachePath = Application.streamingAssetsPath + "/Cache/cache.txt"; if (!File.Exists(cachePath)) { var fileStream = File.Create(cachePath); fileStream.Dispose(); } // cacheBPath = Application.streamingAssetsPath + "/Cache/cache.txt"; // if (!File.Exists(cacheBPath)) // { // var fileStream = File.Create(cacheBPath); // fileStream.Dispose(); // } // // cacheCPath = Application.streamingAssetsPath + "/Cache/cache.txt"; // if (!File.Exists(cacheCPath)) // { // var fileStream = File.Create(cacheCPath); // fileStream.Dispose(); // } // // cacheDPath = Application.streamingAssetsPath + "/Cache/cache.txt"; // if (!File.Exists(cacheDPath)) // { // var fileStream = File.Create(cacheDPath); // fileStream.Dispose(); // } Dictionary> dic = new Dictionary>(); var cache = File.ReadAllLines(cachePath); foreach (var s in cache) { var strings = s.Split(":"); var questionBankType = Enum.Parse(strings[0]); if (dic.TryGetValue(questionBankType, out var list)) list.Add(strings[1]); else dic.Add(questionBankType, new List() { strings[1] }); } GlobalManager.Instance.questionIDs = dic; } public void SavePlayerData(string content) { var text = File.ReadAllText(dataPath); File.WriteAllText(dataPath, text + "\n" + content); } /// /// 保存已抽题的题目id /// /// /// public void SaveAnsweredData(QuestionBankType questionBankType, string content) { var text = File.ReadAllText(cachePath); var s = questionBankType.ToString() + ":" + content; if (text == "") File.WriteAllText(cachePath, s); else File.WriteAllText(cachePath, text + "\n" + s); } public void ClearAnsweredData() { File.WriteAllText(cachePath, ""); } public enum QuestionBankType { A, B, C, D } } }