diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index f1bc5b9..754c672 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -14,12 +14,16 @@ namespace EGFramework.Examples.Test{ // GD.Print(ProjectSettings.GlobalizePath("user://SaveData/Default.json")); // GD.Print(Path.GetDirectoryName(ProjectSettings.GlobalizePath("res://SaveData/Default.json"))); // TestLiteDB(); - string CardPath1 = "SaveData/CardData1.json".GetGodotResPath(); - this.EGSave().LoadObjectFile(CardPath1); - // this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" }); - // this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" }); - Customer customer = this.EGSave().GetObject(CardPath1,"Customer3"); - GD.Print("ReadName is "+customer.Name); + // string CardPath1 = "SaveData/CardData1.json".GetGodotResPath(); + // this.EGSave().LoadObjectFile(CardPath1); + // // this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" }); + // // this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" }); + // Customer customer = this.EGSave().GetObject(CardPath1,"Customer3"); + // GD.Print("ReadName is "+customer.Name); + EGCsvSave csvSave = new EGCsvSave(); + csvSave.InitSaveFile("SaveData/TestCsv.csv"); + string[] group = {"7","MyTestData"}; + csvSave.WriteLine(group); } public void TestSqlite(){ diff --git a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs index 98704e9..1ac9153 100644 --- a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs @@ -12,21 +12,23 @@ namespace EGFramework { public Encoding StringEncoding { set; get; } = Encoding.UTF8; private string DefaultPath { set; get; } - private IEnumerable CsvDataBlock { get; set; } + private List CsvDataBlock { get; set; } private string[] CsvDataHeader; private string ReadText { set; get; } public void InitSaveFile(string path) { - + ReadDataBlock(path); } public void ReadDataBlock(string path){ + DefaultPath = path; try { FileStream fileStream = new FileStream(path,FileMode.Open); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); fileStream.Close(); + fileStream.Dispose(); ReadText = StringEncoding.GetString(buffer); } catch (System.Exception e) @@ -38,7 +40,41 @@ namespace EGFramework CsvDataBlock = GetCSVDataBlockFromText(ReadText,out CsvDataHeader); } } - public IEnumerable GetCSVDataBlockFromText(string text,out string[] header){ + + public void WriteDataBlock(string path){ + try + { + FileStream fileStream = File.Create(path); + string writeText = ""; + string headerText = ""; + foreach(string headers in CsvDataHeader){ + headerText+=headers + ","; + } + headerText = headerText.Remove(headerText.Length-1,1); + writeText = headerText + "\n"; + foreach(string[] lineData in CsvDataBlock){ + string lineText = ""; + foreach(string singleData in lineData){ + lineText += singleData + ","; + } + lineText = lineText.Remove(lineText.Length-1,1); + writeText += lineText + "\n"; + } + writeText = writeText.Remove(writeText.Length-1,1); + + byte[] data = StringEncoding.GetBytes(writeText); + fileStream.Write(data,0,data.Length); + fileStream.Close(); + fileStream.Dispose(); + } + catch (System.Exception e) + { + Godot.GD.Print("e:" + e); + throw; + } + } + + public List GetCSVDataBlockFromText(string text,out string[] header){ List csvBlock = new List(); string[] lineData = text.Split('\n'); header = lineData[0].Split(','); @@ -51,6 +87,19 @@ namespace EGFramework return csvBlock; } + public string[] ReadLine(int id){ + if(CsvDataBlock.Count()>0){ + return CsvDataBlock[id]; + }else{ + return null; + } + } + + public void WriteLine(string[] lineData){ + CsvDataBlock.Add(lineData); + this.WriteDataBlock(DefaultPath); + } + public void SetData(string dataKey, TData data, object id) { throw new NotImplementedException(); @@ -63,7 +112,7 @@ namespace EGFramework { throw new NotImplementedException(); } - + public IEnumerable FindData(string dataKey, Expression> expression) where TData : new() { throw new NotImplementedException();