Browse Source

csv readline and writeline

master
jkpete 2 months ago
parent
commit
7fde85cffd
  1. 16
      Example/UsingTest/Script/EGSaveTest.cs
  2. 55
      addons/EGFramework/Module/SaveTools/EGCsvSave.cs

16
Example/UsingTest/Script/EGSaveTest.cs

@ -14,12 +14,16 @@ namespace EGFramework.Examples.Test{ @@ -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<EGJsonSave>(CardPath1);
// this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" });
// this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" });
Customer customer = this.EGSave().GetObject<Customer>(CardPath1,"Customer3");
GD.Print("ReadName is "+customer.Name);
// string CardPath1 = "SaveData/CardData1.json".GetGodotResPath();
// this.EGSave().LoadObjectFile<EGJsonSave>(CardPath1);
// // this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" });
// // this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" });
// Customer customer = this.EGSave().GetObject<Customer>(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(){

55
addons/EGFramework/Module/SaveTools/EGCsvSave.cs

@ -12,21 +12,23 @@ namespace EGFramework @@ -12,21 +12,23 @@ namespace EGFramework
{
public Encoding StringEncoding { set; get; } = Encoding.UTF8;
private string DefaultPath { set; get; }
private IEnumerable<string[]> CsvDataBlock { get; set; }
private List<string[]> 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 @@ -38,7 +40,41 @@ namespace EGFramework
CsvDataBlock = GetCSVDataBlockFromText(ReadText,out CsvDataHeader);
}
}
public IEnumerable<string[]> 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<string[]> GetCSVDataBlockFromText(string text,out string[] header){
List<string[]> csvBlock = new List<string[]>();
string[] lineData = text.Split('\n');
header = lineData[0].Split(',');
@ -51,6 +87,19 @@ namespace EGFramework @@ -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<TData>(string dataKey, TData data, object id)
{
throw new NotImplementedException();

Loading…
Cancel
Save