Browse Source

rebuild egsave code

master
jkpete 2 months ago
parent
commit
2ee8c16731
  1. 6
      Example/Gateway/Script/View/ViewBacNetGateway.cs
  2. 6
      Example/Gateway/Script/View/ViewBacnetHttpServer.cs
  3. 6
      Example/Gateway/Script/View/ViewModbusGateway.cs
  4. 6
      Example/Gateway/Script/View/ViewTcpGateway.cs
  5. 2
      Example/ModbusDebugTool/Script/ViewEdit.cs
  6. 2
      Example/ModbusDebugTool/Script/ViewMenu.cs
  7. 2
      Example/ModbusDebugTool/Script/ViewModbusItem.cs
  8. 4
      Example/ModbusDebugTool/Script/ViewSettings.cs
  9. 10
      Example/UsingTest/Script/EGSaveTest.cs
  10. 22
      addons/EGFramework/Module/SaveTools/EGByteSave.cs
  11. 2
      addons/EGFramework/Module/SaveTools/EGCsv.cs
  12. 67
      addons/EGFramework/Module/SaveTools/EGJsonSave.cs
  13. 156
      addons/EGFramework/Module/SaveTools/EGSave.cs
  14. 23
      addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

6
Example/Gateway/Script/View/ViewBacNetGateway.cs

@ -20,16 +20,16 @@ namespace EGFramework.Examples.Gateway{ @@ -20,16 +20,16 @@ namespace EGFramework.Examples.Gateway{
public void InitGateway()
{
if(this.EGSave().GetDataByFile<DataBacnetGatewaySetting>() == null){
if(this.EGSave().GetObjectFromJson<DataBacnetGatewaySetting>() == 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>();
DataBacnetGatewaySetting = this.EGSave().GetObjectFromJson<DataBacnetGatewaySetting>();
}
this.EGEnabledProtocolTool<EGBacnet>();
this.EGEnabledProtocolTool<EGMqtt>();

6
Example/Gateway/Script/View/ViewBacnetHttpServer.cs

@ -13,16 +13,16 @@ namespace EGFramework.Examples.Gateway{ @@ -13,16 +13,16 @@ namespace EGFramework.Examples.Gateway{
}
}
public void InitGateway(){
if(this.EGSave().GetDataByFile<DataBacnetGatewaySetting>() == null){
if(this.EGSave().GetObjectFromJson<DataBacnetGatewaySetting>() == 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>();
DataBacnetGatewaySetting = this.EGSave().GetObjectFromJson<DataBacnetGatewaySetting>();
}
this.EGEnabledProtocolTool<EGBacnet>();
this.EGHttpServerListen(DataBacnetGatewaySetting.HttpServerPrefix+"WhoIs/",requestMsg=>{

6
Example/Gateway/Script/View/ViewModbusGateway.cs

@ -43,11 +43,11 @@ namespace EGFramework.Examples.Gateway{ @@ -43,11 +43,11 @@ namespace EGFramework.Examples.Gateway{
public void InitGateway()
{
if(this.EGSave().GetDataByFile<DataModbusGatewaySetting>() == null){
if(this.EGSave().GetObjectFromJson<DataModbusGatewaySetting>() == null){
InitSettings();
this.EGSave().SetDataToFile(Setting);
this.EGSave().SetObjectToJson(Setting);
}else{
Setting = this.EGSave().GetDataByFile<DataModbusGatewaySetting>();
Setting = this.EGSave().GetObjectFromJson<DataModbusGatewaySetting>();
}
this.EGEnabledProtocolTool<EGTCPClient>();
this.EGEnabledProtocolTool<EGSerialPort>();

6
Example/Gateway/Script/View/ViewTcpGateway.cs

@ -14,7 +14,7 @@ namespace EGFramework.Examples.Gateway{ @@ -14,7 +14,7 @@ namespace EGFramework.Examples.Gateway{
}
public void InitGateway(){
if(this.EGSave().GetDataByFile<DataTcpGatewaySetting>() == null){
if(this.EGSave().GetObjectFromJson<DataTcpGatewaySetting>() == null){
DataTcpGatewaySetting = new DataTcpGatewaySetting();
DataTcpGatewaySetting.DataTcpGatewayDevices.Add(new DataTcpGatewayDevice(){
Host = "127.0.0.1",
@ -23,9 +23,9 @@ namespace EGFramework.Examples.Gateway{ @@ -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>();
DataTcpGatewaySetting = this.EGSave().GetObjectFromJson<DataTcpGatewaySetting>();
}
this.EGEnabledProtocolTool<EGTCPClient>();
this.EGEnabledProtocolTool<EGMqtt>();

2
Example/ModbusDebugTool/Script/ViewEdit.cs

@ -75,7 +75,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ @@ -75,7 +75,7 @@ namespace EGFramework.Examples.ModbusDebugTool{
this.EGGetObject<DataModbus>().Items.Remove(DataModbusItem.GetKey());
}
this.EGGetObject<DataModbus>().Items.Add(modbusItem.GetKey(),modbusItem);
this.EGSave().SetDataToFile(this.EGGetObject<DataModbus>());
this.EGSave().SetObjectToJson(this.EGGetObject<DataModbus>());
this.Visible = false;
this.ViewMenu.RefreshSaveData();

2
Example/ModbusDebugTool/Script/ViewMenu.cs

@ -20,7 +20,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ @@ -20,7 +20,7 @@ namespace EGFramework.Examples.ModbusDebugTool{
Edit = this.GetNode<ViewEdit>("Edit");
Edit.Visible = false;
this.EGEnabledProtocolTools();
DataModbus dataModbus = this.EGSave().GetDataByFile<DataModbus>();
DataModbus dataModbus = this.EGSave().GetObjectFromJson<DataModbus>();
if (dataModbus == null)
{
dataModbus = new DataModbus();

2
Example/ModbusDebugTool/Script/ViewModbusItem.cs

@ -66,7 +66,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ @@ -66,7 +66,7 @@ namespace EGFramework.Examples.ModbusDebugTool{
if(this.EGGetObject<DataModbus>().Items.ContainsKey(DataModbusItem.GetKey())){
this.EGGetObject<DataModbus>().Items.Remove(DataModbusItem.GetKey());
}
this.EGSave().SetDataToFile(this.EGGetObject<DataModbus>());
this.EGSave().SetObjectToJson(this.EGGetObject<DataModbus>());
this.GetNode<ViewMenu>("/root/Menu").RefreshSaveData();
}

4
Example/ModbusDebugTool/Script/ViewSettings.cs

@ -14,7 +14,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ @@ -14,7 +14,7 @@ namespace EGFramework.Examples.ModbusDebugTool{
}
public void LoadSettings(){
DataModbusSettings settings = this.EGSave().GetDataByFile<DataModbusSettings>();
DataModbusSettings settings = this.EGSave().GetObjectFromJson<DataModbusSettings>();
this.EGRegisterObject(settings);
UpdateSettings(settings);
EditBaudRate.Text = settings.BaudRate.ToString();
@ -36,7 +36,7 @@ namespace EGFramework.Examples.ModbusDebugTool{ @@ -36,7 +36,7 @@ namespace EGFramework.Examples.ModbusDebugTool{
};
UpdateSettings(settings);
this.EGRegisterObject(settings);
this.EGSave().SetDataToFile(this.EGGetObject<DataModbusSettings>());
this.EGSave().SetObjectToJson(this.EGGetObject<DataModbusSettings>());
this.Visible = false;
}
catch (System.Exception ex)

10
Example/UsingTest/Script/EGSaveTest.cs

@ -9,9 +9,10 @@ namespace EGFramework.Examples.Test{ @@ -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{ @@ -32,7 +33,8 @@ namespace EGFramework.Examples.Test{
using(var db = new LiteDatabase("SaveData/MyData.db"))
{
// 获取一个集合 (如果不存在创建)
var col = db.GetCollection<Customer>("customers");
LiteCollection<Customer> col = (LiteCollection<Customer>)db.GetCollection<Customer>("customers");
GD.Print(col);
// // 创建新顾客实例
// var customer = new Customer

22
addons/EGFramework/Module/SaveTools/EGByteSave.cs

@ -7,7 +7,7 @@ using Godot; @@ -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 @@ -31,17 +31,19 @@ namespace EGFramework
}
// public TData GetDataByFile<TData>() where TData : class, new()
// {
// throw new NotImplementedException();
// }
// public void InitSaveData(string fileName)
// {
// throw new NotImplementedException();
// }
public void SetDataToFile<TData>(TData data)
public void InitSaveFile(string path)
{
//throw new NotImplementedException();
}
public void SetObject<TObject>(string objectKey , TObject obj)
{
throw new NotImplementedException();
}
public TObject GetObject<TObject>(string objectKey) where TObject : new()
{
throw new NotImplementedException();
}
}
}

2
addons/EGFramework/Module/SaveTools/EGTable.cs → addons/EGFramework/Module/SaveTools/EGCsv.cs

@ -5,7 +5,7 @@ using System.Threading.Tasks; @@ -5,7 +5,7 @@ using System.Threading.Tasks;
namespace EGFramework
{
public class EGTable : EGModule
public class EGCsv : EGModule
{
public override void Init()
{

67
addons/EGFramework/Module/SaveTools/EGJsonSave.cs

@ -0,0 +1,67 @@ @@ -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;
}
}
/// <summary>
/// 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;
/// </summary>
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<TObject>(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));
}
/// <summary>
/// Get data from file, if your data is not in file, then throw an exception.
/// </summary>
public TObject GetObject<TObject>(string objectKey) where TObject : new()
{
if(!SaveObject.ContainsKey(typeof(TObject).ToString())){
throw new Exception("Key not found!");
}
TObject data = SaveObject[typeof(TObject).ToString()].ToObject<TObject>();
return data;
}
}
}

156
addons/EGFramework/Module/SaveTools/EGSave.cs

@ -1,131 +1,103 @@ @@ -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>(TData data);
TData GetDataByFile<TData>() 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<string,IEGSaveData> DataBaseFiles = new Dictionary<string,IEGSaveData>();
private Dictionary<string,IEGSaveObject> ObjectFiles = new Dictionary<string,IEGSaveObject>();
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;
}
}
/// <summary>
/// Push SaveObject data set to file
/// </summary>
public void SaveToFile(){
SaveToFile(DefaultPath);
public void SetObject<TObject>(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<TObject>(string path,string key) where TObject : new(){
return ObjectFiles[path].GetObject<TObject>(key);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="data"></param>
/// <typeparam name="TData"></typeparam>
public void SetData<TData>(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));
}
}
//------------------------------------------------------------------------------//
/// <summary>
/// Get data from file, if your data is not in file, then get null.
/// </summary>
/// <typeparam name="TData"></typeparam>
public TData GetDataByFile<TData>() where TData : class,new(){
if(!SaveObject.ContainsKey(typeof(TData).ToString())){
return null;
}
TData data = SaveObject[typeof(TData).ToString()].ToObject<TData>();
return data;
#region Default Json Operation
public void SetObjectToJson<TObject>(TObject obj){
ObjectFiles["Default/SaveData.json"].SetObject(typeof(TObject).ToString(),obj);
}
/// <summary>
/// Save data to file
/// </summary>
/// <param name="data">your any type of data</param>
/// <typeparam name="TData"></typeparam>
public void SetDataToFile<TData>(TData data)
{
SetData(data);
SaveToFile();
public TObject GetObjectFromJson<TObject>() where TObject : new(){
return ObjectFiles["Default/SaveData.json"].GetObject<TObject>(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
/// <summary>
/// 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;
/// </summary>
/// <param name="fileName"></param>
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<EGSave>();
}
public static EGSave EGSave(this IEGFramework self,string path){
self.GetModule<EGSave>().InitSaveData(path);
return self.GetModule<EGSave>();
public static string GetGodotResPath(this string absPath){
return ProjectSettings.GlobalizePath("res://"+absPath);
}
public static string GetGodotUserPath(this string absPath){
return ProjectSettings.GlobalizePath("user://"+absPath);
}
}
}

23
addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

@ -0,0 +1,23 @@ @@ -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<TObject>(string objectKey,TObject obj);
TObject GetObject<TObject>(string objectKey) where TObject : new();
}
//
public interface IEGSaveData{
void SetData<TData>(TData data,string dataKey,int id);
TData GetData<TData>(string dataKey,int id) where TData : new();
IList<TData> QueryData<TData>(string dataKey,string sql) where TData : new();
}
}
Loading…
Cancel
Save