Browse Source

csv readline and writeline

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

16
Example/UsingTest/Script/EGSaveTest.cs

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

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

@ -12,21 +12,23 @@ namespace EGFramework
{ {
public Encoding StringEncoding { set; get; } = Encoding.UTF8; public Encoding StringEncoding { set; get; } = Encoding.UTF8;
private string DefaultPath { set; get; } private string DefaultPath { set; get; }
private IEnumerable<string[]> CsvDataBlock { get; set; } private List<string[]> CsvDataBlock { get; set; }
private string[] CsvDataHeader; private string[] CsvDataHeader;
private string ReadText { set; get; } private string ReadText { set; get; }
public void InitSaveFile(string path) public void InitSaveFile(string path)
{ {
ReadDataBlock(path);
} }
public void ReadDataBlock(string path){ public void ReadDataBlock(string path){
DefaultPath = path;
try try
{ {
FileStream fileStream = new FileStream(path,FileMode.Open); FileStream fileStream = new FileStream(path,FileMode.Open);
byte[] buffer = new byte[fileStream.Length]; byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length); fileStream.Read(buffer, 0, (int)fileStream.Length);
fileStream.Close(); fileStream.Close();
fileStream.Dispose();
ReadText = StringEncoding.GetString(buffer); ReadText = StringEncoding.GetString(buffer);
} }
catch (System.Exception e) catch (System.Exception e)
@ -38,7 +40,41 @@ namespace EGFramework
CsvDataBlock = GetCSVDataBlockFromText(ReadText,out CsvDataHeader); 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[]>(); List<string[]> csvBlock = new List<string[]>();
string[] lineData = text.Split('\n'); string[] lineData = text.Split('\n');
header = lineData[0].Split(','); header = lineData[0].Split(',');
@ -51,6 +87,19 @@ namespace EGFramework
return csvBlock; 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) public void SetData<TData>(string dataKey, TData data, object id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -63,7 +112,7 @@ namespace EGFramework
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IEnumerable<TData> FindData<TData>(string dataKey, Expression<Func<TData, bool>> expression) where TData : new() public IEnumerable<TData> FindData<TData>(string dataKey, Expression<Func<TData, bool>> expression) where TData : new()
{ {
throw new NotImplementedException(); throw new NotImplementedException();

Loading…
Cancel
Save