From 68a1f2464545eb2eb4fe3cc88f4b8f7b599518ef Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Mon, 7 Oct 2024 15:14:30 +0800 Subject: [PATCH] complete csv save module --- Example/UsingTest/Script/EGSaveTest.cs | 11 +++- .../EGFramework/Module/SaveTools/EGCsvSave.cs | 59 +++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index 113a315..5594152 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -23,8 +23,14 @@ namespace EGFramework.Examples.Test{ EGCsvSave csvSave = new EGCsvSave(); csvSave.InitSaveFile("SaveData/TestCsv.csv"); - Customer testData = csvSave.GetData("",1); - GD.Print("Name = "+testData.Name +" || ID = "+testData.Id); + // Customer testData = csvSave.GetData("",1); + // GD.Print("Name = "+testData.Name +" || ID = "+testData.Id); + Customer testData = new Customer(){ + Id = 1008, + Name = "AddDataDefault", + IsActive = true + }; + csvSave.SetData("",testData,2); // GD.Print(typeof(Customer)); // Type type = typeof(Customer); @@ -67,6 +73,7 @@ namespace EGFramework.Examples.Test{ [CsvParam("Name")] public string Name { get; set; } public string[] Phones { get; set; } + [CsvParam("是否启用")] public bool IsActive { get; set; } } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs index 7072b12..0538205 100644 --- a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs @@ -1,11 +1,11 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; -using Godot; namespace EGFramework { @@ -15,7 +15,10 @@ namespace EGFramework private string DefaultPath { set; get; } private List CsvDataBlock { get; set; } private Dictionary CsvDataHeader = new Dictionary(); + public IOCContainer TypeDataContainer = new IOCContainer(); private string ReadText { set; get; } + + public void InitSaveFile(string path) { ReadDataBlock(path); @@ -62,7 +65,6 @@ namespace EGFramework writeText += lineText + "\n"; } writeText = writeText.Remove(writeText.Length-1,1); - byte[] data = StringEncoding.GetBytes(writeText); fileStream.Write(data,0,data.Length); fileStream.Close(); @@ -108,8 +110,31 @@ namespace EGFramework public void SetData(string dataKey, TData data, object id) { - throw new NotImplementedException(); + bool IsAdd = false; + int dataID = 0; + if(id.GetType()==typeof(int)){ + dataID = (int)id; + }else if(int.TryParse(id.ToString() ,out dataID)){ + throw new Exception("Id cannot be convert to int!"); + } + if(dataID>=CsvDataBlock.Count() || dataID < 0){ + IsAdd = true; + } + string[] csvSet = new string[CsvDataHeader.Keys.Count()]; + foreach(PropertyInfo property in data.GetType().GetProperties()){ + CsvParamAttribute csvParam = property.GetCustomAttribute(); + if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){ + csvSet[CsvDataHeader[csvParam._name]] = property.GetValue(data).ToString(); + } + } + if(IsAdd){ + CsvDataBlock.Add(csvSet); + }else{ + CsvDataBlock[dataID] = csvSet; + } + this.WriteDataBlock(DefaultPath); } + public TData GetData(string dataKey, object id) where TData : new() { TData data = new TData(); @@ -137,12 +162,36 @@ namespace EGFramework } public IEnumerable GetAll(string dataKey) where TData : new() { - throw new NotImplementedException(); + // throw new NotImplementedException(); + List DataList = new List(); + for (int dataID = 0; dataID < CsvDataBlock.Count(); dataID++){ + foreach(PropertyInfo property in typeof(TData).GetProperties()){ + CsvParamAttribute csvParam = property.GetCustomAttribute(); + if(csvParam != null && CsvDataHeader.ContainsKey(csvParam._name)){ + string valueStr = CsvDataBlock[dataID][CsvDataHeader[csvParam._name]]; + TData data = new TData(); + if(property.PropertyType==typeof(string)){ + property.SetValue(data,valueStr); + }else{ + property.SetValue(data,Convert.ChangeType(valueStr,property.PropertyType)); + } + DataList.Add(data); + } + } + } + TypeDataContainer.Register(DataList); + return DataList; } public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() { - throw new NotImplementedException(); + List sourceList; + if(TypeDataContainer.self.ContainsKey(typeof(List))){ + sourceList = TypeDataContainer.Get>(); + }else{ + sourceList = (List)GetAll(dataKey); + } + return sourceList.Where(expression.Compile()); } }