From 2ee8c167314785c3e6cc6275bfdc4b2ee5547980 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Mon, 30 Sep 2024 19:16:58 +0800 Subject: [PATCH] rebuild egsave code --- .../Gateway/Script/View/ViewBacNetGateway.cs | 6 +- .../Script/View/ViewBacnetHttpServer.cs | 6 +- .../Gateway/Script/View/ViewModbusGateway.cs | 6 +- Example/Gateway/Script/View/ViewTcpGateway.cs | 6 +- Example/ModbusDebugTool/Script/ViewEdit.cs | 2 +- Example/ModbusDebugTool/Script/ViewMenu.cs | 2 +- .../ModbusDebugTool/Script/ViewModbusItem.cs | 2 +- .../ModbusDebugTool/Script/ViewSettings.cs | 4 +- Example/UsingTest/Script/EGSaveTest.cs | 10 +- .../Module/SaveTools/EGByteSave.cs | 22 +-- .../Module/SaveTools/{EGTable.cs => EGCsv.cs} | 2 +- .../Module/SaveTools/EGJsonSave.cs | 67 ++++++++ addons/EGFramework/Module/SaveTools/EGSave.cs | 156 +++++++----------- .../Module/SaveTools/SaveToolsInterface.cs | 23 +++ 14 files changed, 190 insertions(+), 124 deletions(-) rename addons/EGFramework/Module/SaveTools/{EGTable.cs => EGCsv.cs} (84%) create mode 100644 addons/EGFramework/Module/SaveTools/EGJsonSave.cs create mode 100644 addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs diff --git a/Example/Gateway/Script/View/ViewBacNetGateway.cs b/Example/Gateway/Script/View/ViewBacNetGateway.cs index d591291..4ae304e 100644 --- a/Example/Gateway/Script/View/ViewBacNetGateway.cs +++ b/Example/Gateway/Script/View/ViewBacNetGateway.cs @@ -20,16 +20,16 @@ namespace EGFramework.Examples.Gateway{ public void InitGateway() { - if(this.EGSave().GetDataByFile() == null){ + if(this.EGSave().GetObjectFromJson() == null){ DataBacnetGatewaySetting = new DataBacnetGatewaySetting(){ MqttHost = "192.168.1.220", HttpServerPrefix = "http://127.0.0.1:5000/", ResponseTheme = "/LocalBacnetResponse", RequestTheme = "/LocalBacnetRequest" }; - this.EGSave().SetDataToFile(DataBacnetGatewaySetting); + this.EGSave().SetObjectToJson(DataBacnetGatewaySetting); }else{ - DataBacnetGatewaySetting = this.EGSave().GetDataByFile(); + DataBacnetGatewaySetting = this.EGSave().GetObjectFromJson(); } this.EGEnabledProtocolTool(); this.EGEnabledProtocolTool(); diff --git a/Example/Gateway/Script/View/ViewBacnetHttpServer.cs b/Example/Gateway/Script/View/ViewBacnetHttpServer.cs index 823f91e..61de3b1 100644 --- a/Example/Gateway/Script/View/ViewBacnetHttpServer.cs +++ b/Example/Gateway/Script/View/ViewBacnetHttpServer.cs @@ -13,16 +13,16 @@ namespace EGFramework.Examples.Gateway{ } } public void InitGateway(){ - if(this.EGSave().GetDataByFile() == null){ + if(this.EGSave().GetObjectFromJson() == null){ DataBacnetGatewaySetting = new DataBacnetGatewaySetting(){ MqttHost = "192.168.1.220", HttpServerPrefix = "http://127.0.0.1:5000/", ResponseTheme = "/LocalBacnetResponse", RequestTheme = "/LocalBacnetRequest" }; - this.EGSave().SetDataToFile(DataBacnetGatewaySetting); + this.EGSave().SetObjectToJson(DataBacnetGatewaySetting); }else{ - DataBacnetGatewaySetting = this.EGSave().GetDataByFile(); + DataBacnetGatewaySetting = this.EGSave().GetObjectFromJson(); } this.EGEnabledProtocolTool(); this.EGHttpServerListen(DataBacnetGatewaySetting.HttpServerPrefix+"WhoIs/",requestMsg=>{ diff --git a/Example/Gateway/Script/View/ViewModbusGateway.cs b/Example/Gateway/Script/View/ViewModbusGateway.cs index 26f12cc..761c804 100644 --- a/Example/Gateway/Script/View/ViewModbusGateway.cs +++ b/Example/Gateway/Script/View/ViewModbusGateway.cs @@ -43,11 +43,11 @@ namespace EGFramework.Examples.Gateway{ public void InitGateway() { - if(this.EGSave().GetDataByFile() == null){ + if(this.EGSave().GetObjectFromJson() == null){ InitSettings(); - this.EGSave().SetDataToFile(Setting); + this.EGSave().SetObjectToJson(Setting); }else{ - Setting = this.EGSave().GetDataByFile(); + Setting = this.EGSave().GetObjectFromJson(); } this.EGEnabledProtocolTool(); this.EGEnabledProtocolTool(); diff --git a/Example/Gateway/Script/View/ViewTcpGateway.cs b/Example/Gateway/Script/View/ViewTcpGateway.cs index dcfc024..ca47673 100644 --- a/Example/Gateway/Script/View/ViewTcpGateway.cs +++ b/Example/Gateway/Script/View/ViewTcpGateway.cs @@ -14,7 +14,7 @@ namespace EGFramework.Examples.Gateway{ } public void InitGateway(){ - if(this.EGSave().GetDataByFile() == null){ + if(this.EGSave().GetObjectFromJson() == null){ DataTcpGatewaySetting = new DataTcpGatewaySetting(); DataTcpGatewaySetting.DataTcpGatewayDevices.Add(new DataTcpGatewayDevice(){ Host = "127.0.0.1", @@ -23,9 +23,9 @@ namespace EGFramework.Examples.Gateway{ ResponseTheme = "/LocalTCPResponse", RequestTheme = "/LocalTCPRequest" }); - this.EGSave().SetDataToFile(DataTcpGatewaySetting); + this.EGSave().SetObjectToJson(DataTcpGatewaySetting); }else{ - DataTcpGatewaySetting = this.EGSave().GetDataByFile(); + DataTcpGatewaySetting = this.EGSave().GetObjectFromJson(); } this.EGEnabledProtocolTool(); this.EGEnabledProtocolTool(); diff --git a/Example/ModbusDebugTool/Script/ViewEdit.cs b/Example/ModbusDebugTool/Script/ViewEdit.cs index ee01e81..bc83e7e 100644 --- a/Example/ModbusDebugTool/Script/ViewEdit.cs +++ b/Example/ModbusDebugTool/Script/ViewEdit.cs @@ -75,7 +75,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ this.EGGetObject().Items.Remove(DataModbusItem.GetKey()); } this.EGGetObject().Items.Add(modbusItem.GetKey(),modbusItem); - this.EGSave().SetDataToFile(this.EGGetObject()); + this.EGSave().SetObjectToJson(this.EGGetObject()); this.Visible = false; this.ViewMenu.RefreshSaveData(); diff --git a/Example/ModbusDebugTool/Script/ViewMenu.cs b/Example/ModbusDebugTool/Script/ViewMenu.cs index 8f0e29e..49c072d 100644 --- a/Example/ModbusDebugTool/Script/ViewMenu.cs +++ b/Example/ModbusDebugTool/Script/ViewMenu.cs @@ -20,7 +20,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ Edit = this.GetNode("Edit"); Edit.Visible = false; this.EGEnabledProtocolTools(); - DataModbus dataModbus = this.EGSave().GetDataByFile(); + DataModbus dataModbus = this.EGSave().GetObjectFromJson(); if (dataModbus == null) { dataModbus = new DataModbus(); diff --git a/Example/ModbusDebugTool/Script/ViewModbusItem.cs b/Example/ModbusDebugTool/Script/ViewModbusItem.cs index 3a62827..3529d7b 100644 --- a/Example/ModbusDebugTool/Script/ViewModbusItem.cs +++ b/Example/ModbusDebugTool/Script/ViewModbusItem.cs @@ -66,7 +66,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ if(this.EGGetObject().Items.ContainsKey(DataModbusItem.GetKey())){ this.EGGetObject().Items.Remove(DataModbusItem.GetKey()); } - this.EGSave().SetDataToFile(this.EGGetObject()); + this.EGSave().SetObjectToJson(this.EGGetObject()); this.GetNode("/root/Menu").RefreshSaveData(); } diff --git a/Example/ModbusDebugTool/Script/ViewSettings.cs b/Example/ModbusDebugTool/Script/ViewSettings.cs index 113f49c..3d152db 100644 --- a/Example/ModbusDebugTool/Script/ViewSettings.cs +++ b/Example/ModbusDebugTool/Script/ViewSettings.cs @@ -14,7 +14,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ } public void LoadSettings(){ - DataModbusSettings settings = this.EGSave().GetDataByFile(); + DataModbusSettings settings = this.EGSave().GetObjectFromJson(); this.EGRegisterObject(settings); UpdateSettings(settings); EditBaudRate.Text = settings.BaudRate.ToString(); @@ -36,7 +36,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ }; UpdateSettings(settings); this.EGRegisterObject(settings); - this.EGSave().SetDataToFile(this.EGGetObject()); + this.EGSave().SetObjectToJson(this.EGGetObject()); this.Visible = false; } catch (System.Exception ex) diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index e33c98f..42a6735 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -9,9 +9,10 @@ namespace EGFramework.Examples.Test{ public override void _Ready() { base._Ready(); - 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"))); + this.EGSave().OpenUserPath(); + // 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(); } @@ -32,7 +33,8 @@ namespace EGFramework.Examples.Test{ using(var db = new LiteDatabase("SaveData/MyData.db")) { // 获取一个集合 (如果不存在创建) - var col = db.GetCollection("customers"); + LiteCollection col = (LiteCollection)db.GetCollection("customers"); + GD.Print(col); // // 创建新顾客实例 // var customer = new Customer diff --git a/addons/EGFramework/Module/SaveTools/EGByteSave.cs b/addons/EGFramework/Module/SaveTools/EGByteSave.cs index ffb266e..78f2f19 100644 --- a/addons/EGFramework/Module/SaveTools/EGByteSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGByteSave.cs @@ -7,7 +7,7 @@ using Godot; namespace EGFramework { - public class EGByteSave : EGModule//, IEGSave + public class EGByteSave : EGModule, IEGSave,IEGSaveObject { public void SaveToFile(string content) @@ -31,17 +31,19 @@ namespace EGFramework } - // public TData GetDataByFile() where TData : class, new() - // { - // throw new NotImplementedException(); - // } - // public void InitSaveData(string fileName) - // { - // throw new NotImplementedException(); - // } - public void SetDataToFile(TData data) + public void InitSaveFile(string path) { + //throw new NotImplementedException(); + } + public void SetObject(string objectKey , TObject obj) + { + throw new NotImplementedException(); + } + + public TObject GetObject(string objectKey) where TObject : new() + { + throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGTable.cs b/addons/EGFramework/Module/SaveTools/EGCsv.cs similarity index 84% rename from addons/EGFramework/Module/SaveTools/EGTable.cs rename to addons/EGFramework/Module/SaveTools/EGCsv.cs index 101f67d..85c8160 100644 --- a/addons/EGFramework/Module/SaveTools/EGTable.cs +++ b/addons/EGFramework/Module/SaveTools/EGCsv.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; namespace EGFramework { - public class EGTable : EGModule + public class EGCsv : EGModule { public override void Init() { diff --git a/addons/EGFramework/Module/SaveTools/EGJsonSave.cs b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs new file mode 100644 index 0000000..c9dd31a --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGJsonSave.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace EGFramework +{ + public class EGJsonSave : IEGSave,IEGSaveObject + { + private string DefaultPath { set; get; } + private JObject _SaveObject; + private JObject SaveObject{ + get { + if(_SaveObject == null){ + InitSaveFile(DefaultPath); + } + return _SaveObject; + } + } + + /// + /// Init a new save data file or load an other file with json suffix, if you want to load other save data, please use this function to reload; + /// + public void InitSaveFile(string path) + { + DefaultPath = path; + if(!File.Exists(path)){ + if (!Directory.Exists(DefaultPath)) + { + Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); + File.WriteAllText(DefaultPath,"{}"); + }else if(!File.Exists(DefaultPath)){ + File.WriteAllText(DefaultPath,"{}"); + } + } + using (StreamReader reader = File.OpenText(path)) + { + _SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); + } + } + + public void SetObject(string objectKey,TObject obj) + { + if(SaveObject.ContainsKey(typeof(TObject).ToString())){ + SaveObject[typeof(TObject).ToString()] = JToken.FromObject(obj); + }else{ + SaveObject.Add(typeof(TObject).ToString(),JToken.FromObject(obj)); + } + File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); + } + + /// + /// Get data from file, if your data is not in file, then throw an exception. + /// + public TObject GetObject(string objectKey) where TObject : new() + { + if(!SaveObject.ContainsKey(typeof(TObject).ToString())){ + throw new Exception("Key not found!"); + } + TObject data = SaveObject[typeof(TObject).ToString()].ToObject(); + return data; + } + } +} \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGSave.cs b/addons/EGFramework/Module/SaveTools/EGSave.cs index c7afafd..798b0a8 100644 --- a/addons/EGFramework/Module/SaveTools/EGSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGSave.cs @@ -1,131 +1,103 @@ using System; using System.IO; using System.Collections.Generic; -using Newtonsoft.Json; -using Newtonsoft.Json.Bson; -using Newtonsoft.Json.Linq; +using Godot; namespace EGFramework { - public interface IEGSave{ - void SetDataToFile(TData data); - TData GetDataByFile() where TData : class,new(); - void InitSaveData(string path); - } public enum TypeEGSave{ Json = 0, Bson = 1, Byte = 2, - Sqlite = 3, - LiteDB = 4, - XML = 5 + XML = 3 + } + public enum TypeDBSave{ + Csv = 0, + Sqlite = 1, + LiteDB = 2, } - public class EGSave : EGModule,IEGSave + public class EGSave : EGModule { - private string DefaultPath = "Default/SaveData.json"; - private JObject _SaveObject; - private JObject SaveObject{ - get { - if(_SaveObject == null){ - InitSaveObject(); - } - return _SaveObject; - } - } + #region About Godot File's PATH + // Godot's Path has res:// and user:// + // UserPath is used for every platform such as android. + // You can use ProjectSettings.GlobalizePath("") to convert a "local" path like res://path/to/file.txt to an absolute OS path. + #endregion + + private Dictionary DataBaseFiles = new Dictionary(); + private Dictionary ObjectFiles = new Dictionary(); public EGSave() {} public override void Init() { - if (!Directory.Exists(DefaultPath)) - { - Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath)); - File.WriteAllText(DefaultPath,"{}"); - }else if(!File.Exists(DefaultPath)){ - File.WriteAllText(DefaultPath,"{}"); + LoadObjectFile("Default/SaveData.json".GetGodotUserPath(),TypeEGSave.Json); + } + + public void LoadDataFile(string path,TypeDBSave type){ + switch(type){ + case TypeDBSave.Csv: + break; + default: + break; } } - private void InitSaveObject(){ - using (StreamReader reader = File.OpenText(DefaultPath)) - { - _SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); + public void LoadObjectFile(string path,TypeEGSave type){ + switch(type){ + case TypeEGSave.Json: + EGJsonSave newJsonFile = new EGJsonSave(); + newJsonFile.InitSaveFile(path); + ObjectFiles.Add(path, newJsonFile); + break; + default: + break; } } - /// - /// Push SaveObject data set to file - /// - public void SaveToFile(){ - SaveToFile(DefaultPath); + public void SetObject(string path,string objectKey,TObject obj){ + ObjectFiles[path].SetObject(objectKey,obj); } - private void SaveToFile(string fileName){ - File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); + public TObject GetObject(string path,string key) where TObject : new(){ + return ObjectFiles[path].GetObject(key); } - /// - /// Push data to SaveObject object cache, this function will not save data to file, if you hope not to IO operation frequently, you can use this with SaveToFile. - /// - /// - /// - public void SetData(TData data){ - //SaveObject = JObject.FromObject(data); - if(SaveObject.ContainsKey(typeof(TData).ToString())){ - SaveObject[typeof(TData).ToString()] = JToken.FromObject(data); - }else{ - SaveObject.Add(typeof(TData).ToString(),JToken.FromObject(data)); - } - } + //------------------------------------------------------------------------------// - /// - /// Get data from file, if your data is not in file, then get null. - /// - /// - public TData GetDataByFile() where TData : class,new(){ - if(!SaveObject.ContainsKey(typeof(TData).ToString())){ - return null; - } - TData data = SaveObject[typeof(TData).ToString()].ToObject(); - return data; + #region Default Json Operation + public void SetObjectToJson(TObject obj){ + ObjectFiles["Default/SaveData.json"].SetObject(typeof(TObject).ToString(),obj); } - - /// - /// Save data to file - /// - /// your any type of data - /// - public void SetDataToFile(TData data) - { - SetData(data); - SaveToFile(); + public TObject GetObjectFromJson() where TObject : new(){ + return ObjectFiles["Default/SaveData.json"].GetObject(typeof(TObject).ToString()); } - - #region About Godot File's PATH - // Godot's Path has res:// and user:// - // UserPath is used for every platform such as android. - // You can use ProjectSettings.GlobalizePath("") to convert a "local" path like res://path/to/file.txt to an absolute OS path. #endregion - /// - /// Init a new save data file or load an other file with json suffix, if you want to load other save data, please use this function to reload; - /// - /// - public void InitSaveData(string path) - { - DefaultPath = path; - if(!File.Exists(path)){ - File.WriteAllText(path,"{}"); - } - InitSaveObject(); + //------------------------------------------------------------------------------// + + + public void OpenResPath(){ + OS.ShellOpen("".GetGodotResPath()); } - } + public void OpenUserPath(){ + OS.ShellOpen("".GetGodotUserPath()); + } + + + } + public static class CanGetEGSaveExtension{ public static EGSave EGSave(this IEGFramework self){ return self.GetModule(); } - public static EGSave EGSave(this IEGFramework self,string path){ - self.GetModule().InitSaveData(path); - return self.GetModule(); + + public static string GetGodotResPath(this string absPath){ + return ProjectSettings.GlobalizePath("res://"+absPath); + } + + public static string GetGodotUserPath(this string absPath){ + return ProjectSettings.GlobalizePath("user://"+absPath); } + } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs new file mode 100644 index 0000000..bb538a1 --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EGFramework +{ + public interface IEGSave{ + void InitSaveFile(string path); + } + + public interface IEGSaveObject{ + void SetObject(string objectKey,TObject obj); + TObject GetObject(string objectKey) where TObject : new(); + } + + // + public interface IEGSaveData{ + void SetData(TData data,string dataKey,int id); + TData GetData(string dataKey,int id) where TData : new(); + IList QueryData(string dataKey,string sql) where TData : new(); + } +} \ No newline at end of file