using Game.Config; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using UnityEngine; public abstract class GameData { public int ID; public abstract string FlieName(); } public abstract class GameData : GameData where T : GameData { protected static Dictionary configDatas = new Dictionary(); protected static string[] fields; protected static string[] types; public override string FlieName() { return ""; } protected static void Onload(CsvReader csvReader) { configDatas.Clear(); string[][] datas = csvReader.RowDatas; fields = csvReader.Fields(); types = csvReader.Types(); int length = datas.Length; bool IsHasID = csvReader.HasField("ID"); for (int i = csvReader.StartLine; i < length; i++) { T data = ExcelParser.Serializable(fields, datas[i]); //Debug.Log(data.FlieName() + data.ID); if (!IsHasID) { data.ID = configDatas.Count; } configDatas.Add(data.ID, data); } } public static T GetData(int id) { T data = null; if (configDatas.ContainsKey(id)) data = configDatas[id]; return data; } public static T[] Where(Func func) { return configDatas.Values.Where(func).ToArray(); } public static T Find(Func func) { T data = configDatas.Values.Where(func).FirstOrDefault(); return data; } public static void Foreach( Action action) { List datas = AllData(); int length = datas.Count; for (int i = 0; i < length; i++) { action?.Invoke(datas[i]); } } public static void Clear(bool isClear = true) { configDatas.Clear(); if (isClear) { fields = null; types = null; } } public static List AllData() { return configDatas.Values.ToList(); } public void Delete() { if (configDatas.ContainsKey(ID)) { configDatas.Remove(ID); } } public static void AddNewData(T data) { int id = GetNextIndex(); data.ID = id; configDatas.Add(id,data); } public static string WriteToFile(string path = null) { GameDataFormatInfo info = new GameDataFormatInfo(fields, types); Type type = typeof(T); T obj = Activator.CreateInstance(); CsvWriter csvWriter = new CsvWriter(path, obj.FlieName(), configDatas.Values.ToList(), info); if (!string.IsNullOrEmpty(path)) { csvWriter.Write(); } return csvWriter.Data; } public static GameDataFormatInfo GetFormatInfo() { return new GameDataFormatInfo(fields, types); } private static int GetNextIndex() { int max = 0; if (configDatas.Count > 0) { max = configDatas.Keys.OrderBy(x => x).Max(); } return max + 1; } } public enum ConfigMode { OnAsset, OnMemory, } public class GameDataFormatInfo { public string[] Fileds; public string[] Types; public GameDataFormatInfo(string[] fs, string[] ts) { this.Fileds = fs; this.Types = ts; } } //public class GameDatabase : Singleton //{ // private Dictionary formatInfos; // private ConfigMode configMode; // private string bundleName; // //public void Init(string bundleName, ConfigMode mode = ConfigMode.OnMemory) // //{ // // configMode = mode; // // formatInfos = new Dictionary(); // // this.bundleName = bundleName; // // LoadDatabase(new ConfigPathInfo().Paths.ToArray()); // //} // //public void LoadDatabase(string[] paths) // //{ // // if (configMode == ConfigMode.OnAsset || paths == null || paths.Length == 0) // // return; // // ResourcesManager resmgr = ResourcesManager.Instance; // // int length = paths.Length; // // for (int i = 0; i < length; i++) // // { // // Debug.Log(paths[i]); // // int index = i; // // resmgr.LoadAssetNoDependencisAsync(paths[index], bundleName, txt => // // { // // string name = FileHelper.GetName(paths[index]); // // CsvReader csvReader = new CsvReader(name, txt.bytes); // // Type type = Type.GetType(FileHelper.GetConfigClassFullName(name)); // // MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); // // methodInfo?.Invoke(null, new object[] { csvReader }); // // formatInfos.Add(name, new GameDataFormatInfo(csvReader.Fields(), csvReader.Types())); // // }); // // } // // //resmgr.UnLoad(bundleName); // //} // public static void LoadDataBaseFormMemory(byte[] data) where T: GameData // { // Type type = typeof(T); // CsvReader csvReader = new CsvReader(type.Name, data); // MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); // methodInfo?.Invoke(null, new object[] { csvReader }); // } // public GameDataFormatInfo GetGameDataFormatInfo(string name) // { // GameDataFormatInfo ret = null; // if (formatInfos.ContainsKey(name)) // ret = formatInfos[name]; // return ret; // } // private Dictionary GetConfigDatas(CsvReader reader) // { // Dictionary gameDatas = new Dictionary(); // string[][] datas = reader.RowDatas; // string[] fields = reader.Fields(); // int length = datas.Length; // for (int i = 2; i < length; i++) // { // GameData gameData = ExcelParser.Serializable(Type.GetType($"Game.Config.{reader.Name}"), fields, datas[i]); // gameDatas.Add(gameData.ID, gameData); // } // return gameDatas; // } //}