From 89c2c7cd3e1f891b0cf6af0515c1bef176ef3c49 Mon Sep 17 00:00:00 2001
From: jkpete <1031139173@qq.com>
Date: Sun, 29 Sep 2024 17:08:24 +0800
Subject: [PATCH] rewrite egSave Module
---
EGFramework.csproj | 1 +
Example/UsingTest/Script/EGSaveTest.cs | 78 ++++++++++++++++++-
.../EGFramework/Module/SaveTools/EGLiteDB.cs | 16 ++++
addons/EGFramework/Module/SaveTools/EGSave.cs | 63 ++++++++-------
.../Module/{SQL => SaveTools}/EGSqlite.cs | 2 +-
5 files changed, 128 insertions(+), 32 deletions(-)
create mode 100644 addons/EGFramework/Module/SaveTools/EGLiteDB.cs
rename addons/EGFramework/Module/{SQL => SaveTools}/EGSqlite.cs (99%)
diff --git a/EGFramework.csproj b/EGFramework.csproj
index da44c03..2eb842e 100644
--- a/EGFramework.csproj
+++ b/EGFramework.csproj
@@ -16,5 +16,6 @@
+
\ No newline at end of file
diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs
index 27aae3f..e33c98f 100644
--- a/Example/UsingTest/Script/EGSaveTest.cs
+++ b/Example/UsingTest/Script/EGSaveTest.cs
@@ -1,5 +1,7 @@
using Godot;
+using LiteDB;
using System;
+using System.IO;
namespace EGFramework.Examples.Test{
public partial class EGSaveTest : Node,IEGFramework
@@ -7,7 +9,10 @@ namespace EGFramework.Examples.Test{
public override void _Ready()
{
base._Ready();
- TestSqlite();
+ 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();
}
public void TestSqlite(){
@@ -21,10 +26,75 @@ namespace EGFramework.Examples.Test{
// var properties = typeof(SqliteBackpackItem).GetFields();
// Godot.GD.Print(properties.Count() + " Readed ");
}
+
+ public void TestLiteDB(){
+ // 打开数据库 (如果不存在自动创建)
+ using(var db = new LiteDatabase("SaveData/MyData.db"))
+ {
+ // 获取一个集合 (如果不存在创建)
+ var col = db.GetCollection("customers");
+
+ // // 创建新顾客实例
+ // var customer = new Customer
+ // {
+ // Id = 200,
+ // Name = "Alexander King",
+ // Phones = new string[] { "8000-0000", "9000-0000" },
+ // IsActive = true
+ // };
+ // // 插入新顾客文档 (Id 自增)
+ // for (int i = 0; i < 10000; i++)
+ // {
+ // customer.Id ++;
+ // col.Insert(customer);
+ // }
+ // // 更新集合中的一个文档
+ // customer.Name = "Joana Doe";
+ // col.Update(customer);
+ // // 使用文档的 Name 属性为文档建立索引
+ // col.EnsureIndex(x => x.Name);
+ // 使用 LINQ 查询文档
+ // var results = col.Find(x => x.Name.StartsWith("Al"));
+ // GD.Print("Find:"+results.Count());
+ // string ids = "";
+ // foreach(var item in results){
+ // ids += "["+item.Id.ToString()+"]";
+
+ // }
+ // GD.Print(ids);
+ // // 让我们创建在电话号码字段上创建一个索引 (使用表达式). 它是一个多键值索引
+ // //col.EnsureIndex(x => x.Phones, "$.Phones[*]");
+ // col.EnsureIndex(x => x.Phones);
+ // // 现在我们可以查询电话号码
+ // var r = col.FindOne(x => x.Phones.Contains("8888-5555"));\
+
+ // Test Other
+ // ILiteCollection col = db.GetCollection("SqliteBackpackItem");
+ // var item = new SqliteBackpackItem{
+ // ItemID = 10,
+ // ItemCount = 1,
+ // BackpackID = 1,
+ // };
+ // for (int i = 0; i < 100; i++)
+ // {
+ // col.Insert(item);
+ // }
+ }
+ }
+
}
public struct SqliteBackpackItem{
- public int ItemID;
- public int ItemCount;
- public int BackpackID;
+ public int Id { get; set; }
+ public int ItemID { get; set; }
+ public int ItemCount { get; set; }
+ public int BackpackID { get; set; }
+ }
+
+ public class Customer
+ {
+ public int Id { get; set; }
+ public string Name { get; set; }
+ public string[] Phones { get; set; }
+ public bool IsActive { get; set; }
}
}
\ No newline at end of file
diff --git a/addons/EGFramework/Module/SaveTools/EGLiteDB.cs b/addons/EGFramework/Module/SaveTools/EGLiteDB.cs
new file mode 100644
index 0000000..999416c
--- /dev/null
+++ b/addons/EGFramework/Module/SaveTools/EGLiteDB.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using LiteDB;
+
+namespace EGFramework
+{
+ public class EGLiteDB : EGModule
+ {
+ public override void Init()
+ {
+ // throw new NotImplementedException();
+ }
+ }
+}
\ No newline at end of file
diff --git a/addons/EGFramework/Module/SaveTools/EGSave.cs b/addons/EGFramework/Module/SaveTools/EGSave.cs
index 34e4b77..c7afafd 100644
--- a/addons/EGFramework/Module/SaveTools/EGSave.cs
+++ b/addons/EGFramework/Module/SaveTools/EGSave.cs
@@ -2,6 +2,7 @@ using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
+using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;
namespace EGFramework
@@ -9,12 +10,20 @@ namespace EGFramework
public interface IEGSave{
void SetDataToFile(TData data);
TData GetDataByFile() where TData : class,new();
- void InitSaveData(string fileName);
+ void InitSaveData(string path);
}
+ public enum TypeEGSave{
+ Json = 0,
+ Bson = 1,
+ Byte = 2,
+ Sqlite = 3,
+ LiteDB = 4,
+ XML = 5
+ }
+
public class EGSave : EGModule,IEGSave
{
- private string DefaultSaveFile = "Default";
- private string DefaultSaveFolder = "SaveData";
+ private string DefaultPath = "Default/SaveData.json";
private JObject _SaveObject;
private JObject SaveObject{
get {
@@ -24,30 +33,20 @@ namespace EGFramework
return _SaveObject;
}
}
-
- public EGSave(){
-
- }
- ///
- /// if you want to define default save data file name, please use "this.RegisterModule(new EGSave("FileName"))"in your architecture code(Init function);
- ///
- ///
- public EGSave(string fileName){
- this.DefaultSaveFile = fileName;
- }
+ public EGSave() {}
public override void Init()
{
- if (!Directory.Exists(DefaultSaveFolder))
+ if (!Directory.Exists(DefaultPath))
{
- Directory.CreateDirectory(DefaultSaveFolder);
- File.WriteAllText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json","{}");
- }else if(!File.Exists(DefaultSaveFolder + "/" + DefaultSaveFile + ".json")){
- File.WriteAllText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json","{}");
+ Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath));
+ File.WriteAllText(DefaultPath,"{}");
+ }else if(!File.Exists(DefaultPath)){
+ File.WriteAllText(DefaultPath,"{}");
}
}
private void InitSaveObject(){
- using (StreamReader reader = File.OpenText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json"))
+ using (StreamReader reader = File.OpenText(DefaultPath))
{
_SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
}
@@ -57,10 +56,10 @@ namespace EGFramework
/// Push SaveObject data set to file
///
public void SaveToFile(){
- SaveToFile(DefaultSaveFile);
+ SaveToFile(DefaultPath);
}
private void SaveToFile(string fileName){
- File.WriteAllText(DefaultSaveFolder + "/" + fileName + ".json",JsonConvert.SerializeObject(SaveObject,Formatting.Indented));
+ File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented));
}
///
@@ -100,15 +99,21 @@ namespace EGFramework
SaveToFile();
}
+ #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 fileName)
+ public void InitSaveData(string path)
{
- DefaultSaveFile = fileName;
- if(!File.Exists(DefaultSaveFolder + "/" + DefaultSaveFile + ".json")){
- File.WriteAllText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json","{}");
+ DefaultPath = path;
+ if(!File.Exists(path)){
+ File.WriteAllText(path,"{}");
}
InitSaveObject();
}
@@ -116,7 +121,11 @@ namespace EGFramework
public static class CanGetEGSaveExtension{
public static EGSave EGSave(this IEGFramework self){
- return EGArchitectureImplement.Interface.GetModule();
+ return self.GetModule();
+ }
+ public static EGSave EGSave(this IEGFramework self,string path){
+ self.GetModule().InitSaveData(path);
+ return self.GetModule();
}
}
}
\ No newline at end of file
diff --git a/addons/EGFramework/Module/SQL/EGSqlite.cs b/addons/EGFramework/Module/SaveTools/EGSqlite.cs
similarity index 99%
rename from addons/EGFramework/Module/SQL/EGSqlite.cs
rename to addons/EGFramework/Module/SaveTools/EGSqlite.cs
index 004155b..6a40288 100644
--- a/addons/EGFramework/Module/SQL/EGSqlite.cs
+++ b/addons/EGFramework/Module/SaveTools/EGSqlite.cs
@@ -10,7 +10,7 @@ namespace EGFramework{
List GetDataSet() where TData : new();
void InitDatabase(string dataBaseName);
}
- public class EGSqlite : EGModule,IEGSqlite
+ public class EGSqlite : EGModule
{
public string DBName = "Default";
private string DefaultDBFolder = "SaveData";