From 6d771ac5d1f7a22bd0a06e8f328c40f9d7e52b99 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Tue, 22 Oct 2024 18:19:32 +0800 Subject: [PATCH] fixed egsave manual --- Example/UsingTest/Script/EGSaveTest.cs | 20 ++- addons/EGFramework/Manual/EGSave.md | 156 ++++++++++++++++-- .../EGFramework/Module/SaveTools/EGCsvSave.cs | 8 + .../Module/SaveTools/EGJsonSave.cs | 7 + addons/EGFramework/Module/SaveTools/EGSave.cs | 85 +++++++++- .../Module/SaveTools/SaveToolsInterface.cs | 1 + 6 files changed, 247 insertions(+), 30 deletions(-) diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index 2556a2f..7941de6 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -26,9 +26,9 @@ namespace EGFramework.Examples.Test{ // GD.Print(ProjectSettings.GlobalizePath("res://SaveData/Default.json")); // 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); + + // string Path2 = "Data1.json".GetGodotResPath(); + // this.EGSave().LoadObjectFile(Path2); // // this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" }); // // this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" }); @@ -36,10 +36,10 @@ namespace EGFramework.Examples.Test{ // string CardPath1 = "Card1"; // FileAccess fileAccess = FileAccess.Open("res://SaveData/TestCsv.csv", FileAccess.ModeFlags.Read); // GD.Print(fileAccess.GetAsText()); - // FileAccess testFile = FileAccess.Open("res://SaveData/CardData1.json", FileAccess.ModeFlags.Read); - // this.EGSave().ReadObject(CardPath1,testFile.GetAsText()); - // Customer customer = this.EGSave().GetObject(CardPath1,"Customer3"); - // GD.Print("ReadName is "+customer.Name); + FileAccess testJson = FileAccess.Open("res://TestJson.json", FileAccess.ModeFlags.Read); + this.EGSave().ReadObject("TestJson",testJson.GetAsText()); + Customer customer = this.EGSave().GetObject("TestJson","Customer3"); + GD.Print("ReadName is "+customer.Name); // FileAccess testCSV = FileAccess.Open("res://SaveData/TestCSV.json", FileAccess.ModeFlags.Read); @@ -54,9 +54,11 @@ namespace EGFramework.Examples.Test{ // IsActive = true // }; // csvSave.SetData("",testData,2) - + // string Path1 = "Test.csv".GetGodotResPath(); + // this.EGSave().LoadDataFile(Path1); + // FileAccess testCsv = FileAccess.Open("res://SaveData/TestCsv.csv", FileAccess.ModeFlags.Read); - // this.EGSave().ReadData(CardPath1,testCsv.GetAsText()); + // this.EGSave().ReadData("TestCsv",testCsv.GetAsText()); // IEnumerable allResult = this.EGSave().GetAllData(CardPath1,""); // GD.Print("Get result " + allResult.Count()); // foreach(Customer customer in allResult){ diff --git a/addons/EGFramework/Manual/EGSave.md b/addons/EGFramework/Manual/EGSave.md index 98a55f9..7946f88 100644 --- a/addons/EGFramework/Manual/EGSave.md +++ b/addons/EGFramework/Manual/EGSave.md @@ -2,9 +2,25 @@ --- -SaveTools使用了两种数据格式,一种是表式存储,一种是对象存储,统一使用key-value方式存储数据,通过唯一key值锁定对应的数据。 +SaveTools使用了两种数据格式,一种是表式存储,一种是对象存储,统一使用key-value方式存储数据,通过唯一key值锁定对应的数据。如果是只读数据,则需指定唯一标识key,读写数据,这个key值则为文件路径(唯一)。 -在使用该库时,一定要保证该数据被加载。 +在使用该库时,一定要保证该数据被加载。可以重复读取数据文件,如果外部进行了修改,新读取的数据会覆盖掉原来的数据。 + +需要在对应Godot的Node类下实现接口 `IEGFramework` + +下面演示一个打开Res文件夹的示例: + +```csharp +public partial class EGSaveTest : Node,IEGFramework{ + public override void _Ready() + { + base._Ready(); + this.EGSave().OpenResPath(); + } +} +``` + +数据工具类开发状态如下: 只读&非只读数据: @@ -32,25 +48,37 @@ SaveTools使用了两种数据格式,一种是表式存储,一种是对象 --- +# EGSave + + + ## 属性 暂无 + + ## 方法 -| 方法名 | 简介 | -| ----------------------------------------------------------------- | ------------------ | -| void LoadDataFile(string path) | 加载数据文件(需要路径) | -| void ReadData(string key,string data) | 读取数据(需获取string原始值) | -| void LoadObjectFile(string path) | 加载对象文件(需要路径) | -| void ReadObject(string key,string data) | 读取对象(需获取string原始值) | -| void SetObject(string path,string objectKey,TObject obj) | 设置对象(写入文件) | -| TObject GetObject(string path,string key) | 获取对象(读取文件) | -| void SetData(string path,string dataKey,TData data,int id) | 设置数据(写入文件) | -| TData GetData(string path,string key,int id) | 获取单个数据(读取文件) | -| IEnumerable GetAllData(string path,string key) | 获取全部数据(读取文件) | -| OpenResPath() | 打开Res文件目录 | -| OpenUserPath() | 打开User文件目录 | +| 方法名 | 简介 | +| ----------------------------------------------------------------- | ---------------- | +| void LoadDataFile(string path) | 加载数据文件(需要路径) | +| void ReadData(string key,string data) | 读取数据(需获取字符串原始值) | +| void ReadData(string key,byte[] data) | 读取数据(需获取字节流原始值) | +| void LoadObjectFile(string path) | 加载对象文件(需要路径) | +| void ReadObject(string key,string data) | 读取对象(需获取字符串原始值) | +| void ReadObject(string key,byte[] data) | 读取对象(需获取字节流原始值) | +| void Unload(string keyOrPath) | 卸载数据 | +| List GetKeys() | 获取所有加载过的key或者路径值 | +| void SetObject(string path,string objectKey,TObject obj) | 设置对象(写入文件) | +| TObject GetObject(string path,string key) | 获取对象(读取文件) | +| void SetData(string path,string dataKey,TData data,int id) | 设置数据(写入文件) | +| TData GetData(string path,string key,int id) | 获取单个数据(读取文件) | +| IEnumerable GetAllData(string path,string key) | 获取全部数据(读取文件) | +| OpenResPath() | 打开Res文件目录 | +| OpenUserPath() | 打开User文件目录 | + + ## 扩展方法 @@ -60,12 +88,110 @@ SaveTools使用了两种数据格式,一种是表式存储,一种是对象 | [string].GetGodotResPath(this string path) | 转为res文件下的相对路径 | | [string].GetGodotUserPath(this string path) | 转为User文件下的相对路径 | + + ## 属性说明 暂无 + + ## 方法说明 +### LoadDataFile(string path) + +从路径中加载数据文件,因为是可读写,所以必须指定对应的文件路径位置,如果不存在该文件则会新建对应数据文件。 + +- T:指代任何一个数据存储工具类 + +- Path:文件的相对路径&绝对路径 + +```csharp +string Path1 = "Data/Test1.csv".GetGodotResPath(); +this.EGSave().LoadDataFile(Path1); +``` + + + +### ReadData(string key,string data) + +### ReadData(string key,byte[] data) + +从文本数据中读取数据,并且赋予对应的key值方便索引到该数据位置上。由于是只读数据,不会创建文件,也不会存在路径信息。 + +- T:指代任何一个数据工具类 + +- key:数据的索引,用于定位数据内容。 + +- data:原始数据,字符串格式或者byte[]字节流。 + +```csharp +FileAccess testCsv = FileAccess.Open("res://TestCsv.csv", FileAccess.ModeFlags.Read); +this.EGSave().ReadData("TestCsv",testCsv.GetAsText()); +``` + + + +### LoadObjectFile(string path) + +从路径中加载对象文件,因为是可读写,所以必须指定对应的文件路径位置,如果不存在该文件则会新建对应数据文件。 + +- T:指代任何一个对象存储工具类 + +- Path:文件的相对路径&绝对路径 + +```csharp +string Path2 = "Data1.json".GetGodotResPath(); +this.EGSave().LoadObjectFile(Path2); +``` + + + +### ReadObject(string key,string data) + +### ReadObject(string key,byte[] data) + +从文本数据中读取对象,并且赋予对应的key值方便索引到该数据位置上。由于是只读对象,不会创建文件,也不会存在路径信息。 + +- T:指代任何一个对象工具类 + +- key:对象的索引,用于定位对象内容。 + +- data:原始数据,字符串格式或者byte[]字节流。 + +```csharp +FileAccess testJson = FileAccess.Open("res://TestJson.json", FileAccess.ModeFlags.Read); +this.EGSave().ReadObject("TestJson",testJson.GetAsText()); +``` + + + +### Unload(string keyOrPath) + +卸载已读取或者加载的数据,并删除对应key值下的数据,此方法执行后,不再能获取对应的数据,也不能写入文件,需要重新加载。 + +- keyOrPath:key值或者路径值 + +```csharp +this.EGSave().Unload("TestCsv"); +``` + + + +### List GetKeys() + +获取所有加载过的key值或者路径值。 + +```csharp +List keys = this.EGSave().GetKeys(); +``` + + + + + + + # 接口说明 --- diff --git a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs index 1079a16..7c45202 100644 --- a/addons/EGFramework/Module/SaveTools/EGCsvSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGCsvSave.cs @@ -33,6 +33,14 @@ namespace EGFramework } } + public void InitReadOnly(byte[] data) + { + ReadText = StringEncoding.GetString(data); + if(ReadText != null || ReadText != ""){ + CsvDataBlock = GetCSVDataBlockFromText(ReadText,out CsvDataHeader); + } + } + public void ReadDataBlock(string path){ DefaultPath = path; try diff --git a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs index 1294173..f137aa5 100644 --- a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs @@ -11,6 +11,7 @@ namespace EGFramework public class EGJsonSave : IEGSave,IEGSaveReadOnly,IEGSaveObject { public bool IsReadOnly { get; set; } + public Encoding StringEncoding { set; get; } = Encoding.UTF8; private string DefaultPath { set; get; } private JObject _SaveObject; private JObject SaveObject{ @@ -49,6 +50,12 @@ namespace EGFramework IsReadOnly = true; } + public void InitReadOnly(byte[] data) + { + _SaveObject = JObject.Parse(StringEncoding.GetString(data)); + IsReadOnly = true; + } + public void SetObject(string objectKey,TObject obj) { if(IsReadOnly){ diff --git a/addons/EGFramework/Module/SaveTools/EGSave.cs b/addons/EGFramework/Module/SaveTools/EGSave.cs index f17a878..4273ff5 100644 --- a/addons/EGFramework/Module/SaveTools/EGSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGSave.cs @@ -39,25 +39,94 @@ namespace EGFramework public void LoadDataFile(string path) where TSaveData:IEGSaveData,IEGSave,new(){ TSaveData saveData = new TSaveData(); saveData.InitSaveFile(path); - DataBaseFiles.Add(path,saveData); + if(!DataBaseFiles.ContainsKey(path)){ + DataBaseFiles.Add(path,saveData); + }else{ + DataBaseFiles[path] = saveData; + } } - public void ReadData(string key,string data)where TReadOnlyData:IEGSaveDataReadOnly,IEGSaveReadOnly,new(){ + public void ReadData(string key,string data) where TReadOnlyData:IEGSaveDataReadOnly,IEGSaveReadOnly,new(){ TReadOnlyData readOnlyData = new TReadOnlyData(); readOnlyData.InitReadOnly(data); - DataBaseReadOnly.Add(key,readOnlyData); + if(!DataBaseReadOnly.ContainsKey(key)){ + DataBaseReadOnly.Add(key,readOnlyData); + }else{ + DataBaseReadOnly[key] = readOnlyData; + } + } + + public void ReadData(string key,byte[] data) where TReadOnlyData:IEGSaveDataReadOnly,IEGSaveReadOnly,new(){ + TReadOnlyData readOnlyData = new TReadOnlyData(); + readOnlyData.InitReadOnly(data); + if(!DataBaseReadOnly.ContainsKey(key)){ + DataBaseReadOnly.Add(key,readOnlyData); + }else{ + DataBaseReadOnly[key] = readOnlyData; + } } public void LoadObjectFile(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){ TSaveObject saveObject = new TSaveObject(); saveObject.InitSaveFile(path); - ObjectFiles.Add(path, saveObject); + if(!ObjectFiles.ContainsKey(path)){ + ObjectFiles.Add(path, saveObject); + }else{ + ObjectFiles[path] = saveObject; + } + + } + + public void ReadObject(string key,string data) where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){ + TReadOnlyObject readOnlyObject = new TReadOnlyObject(); + readOnlyObject.InitReadOnly(data); + if(!ObjectReadOnly.ContainsKey(key)){ + ObjectReadOnly.Add(key,readOnlyObject); + }else{ + ObjectReadOnly[key] = readOnlyObject; + } } - public void ReadObject(string key,string data)where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){ + public void ReadObject(string key,byte[] data) where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){ TReadOnlyObject readOnlyObject = new TReadOnlyObject(); readOnlyObject.InitReadOnly(data); - ObjectReadOnly.Add(key,readOnlyObject); + if(!ObjectReadOnly.ContainsKey(key)){ + ObjectReadOnly.Add(key,readOnlyObject); + }else{ + ObjectReadOnly[key] = readOnlyObject; + } + + } + + public void Unload(string keyOrPath){ + if(DataBaseReadOnly.ContainsKey(keyOrPath)){ + DataBaseReadOnly.Remove(keyOrPath); + }else if(ObjectReadOnly.ContainsKey(keyOrPath)){ + ObjectReadOnly.Remove(keyOrPath); + }else if(DataBaseFiles.ContainsKey(keyOrPath)){ + DataBaseFiles.Remove(keyOrPath); + }else if(ObjectFiles.ContainsKey(keyOrPath)){ + ObjectFiles.Remove(keyOrPath); + }else{ + throw new Exception("Key is not found!"); + } + } + + public List GetKeys(){ + List keys = new List(); + foreach(string key in DataBaseReadOnly.Keys){ + keys.Add(key); + } + foreach(string key in ObjectReadOnly.Keys){ + keys.Add(key); + } + foreach(string key in DataBaseFiles.Keys){ + keys.Add(key); + } + foreach(string key in ObjectFiles.Keys){ + keys.Add(key); + } + return keys; } public void SetObject(string path,string objectKey,TObject obj){ @@ -143,6 +212,10 @@ namespace EGFramework public static string GetGodotUserPath(this string path){ return ProjectSettings.GlobalizePath("user://"+path); } + + public static string GetDirectoryName(this string path){ + return Path.GetDirectoryName(path); + } } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs index 8a58ffa..80b1c54 100644 --- a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs +++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs @@ -12,6 +12,7 @@ namespace EGFramework public interface IEGSaveReadOnly{ void InitReadOnly(string data); + void InitReadOnly(byte[] data); } public interface IEGSaveObjectReadOnly{