Browse Source

rewrite egSave Module

master
jkpete 2 months ago
parent
commit
89c2c7cd3e
  1. 1
      EGFramework.csproj
  2. 78
      Example/UsingTest/Script/EGSaveTest.cs
  3. 16
      addons/EGFramework/Module/SaveTools/EGLiteDB.cs
  4. 63
      addons/EGFramework/Module/SaveTools/EGSave.cs
  5. 2
      addons/EGFramework/Module/SaveTools/EGSqlite.cs

1
EGFramework.csproj

@ -16,5 +16,6 @@
<PackageReference Include="PacketDotNet" Version="0.19.3" /> <PackageReference Include="PacketDotNet" Version="0.19.3" />
<PackageReference Include="SharpPcap" Version="4.5.0" /> <PackageReference Include="SharpPcap" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="8.0.0" />
<PackageReference Include="LiteDB" Version="5.0.21" />
</ItemGroup> </ItemGroup>
</Project> </Project>

78
Example/UsingTest/Script/EGSaveTest.cs

@ -1,5 +1,7 @@
using Godot; using Godot;
using LiteDB;
using System; using System;
using System.IO;
namespace EGFramework.Examples.Test{ namespace EGFramework.Examples.Test{
public partial class EGSaveTest : Node,IEGFramework public partial class EGSaveTest : Node,IEGFramework
@ -7,7 +9,10 @@ namespace EGFramework.Examples.Test{
public override void _Ready() public override void _Ready()
{ {
base._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(){ public void TestSqlite(){
@ -21,10 +26,75 @@ namespace EGFramework.Examples.Test{
// var properties = typeof(SqliteBackpackItem).GetFields(); // var properties = typeof(SqliteBackpackItem).GetFields();
// Godot.GD.Print(properties.Count() + " Readed "); // Godot.GD.Print(properties.Count() + " Readed ");
} }
public void TestLiteDB(){
// 打开数据库 (如果不存在自动创建)
using(var db = new LiteDatabase("SaveData/MyData.db"))
{
// 获取一个集合 (如果不存在创建)
var col = db.GetCollection<Customer>("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<SqliteBackpackItem> col = db.GetCollection<SqliteBackpackItem>("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 struct SqliteBackpackItem{
public int ItemID; public int Id { get; set; }
public int ItemCount; public int ItemID { get; set; }
public int BackpackID; 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; }
} }
} }

16
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();
}
}
}

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

@ -2,6 +2,7 @@ using System;
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace EGFramework namespace EGFramework
@ -9,12 +10,20 @@ namespace EGFramework
public interface IEGSave{ public interface IEGSave{
void SetDataToFile<TData>(TData data); void SetDataToFile<TData>(TData data);
TData GetDataByFile<TData>() where TData : class,new(); TData GetDataByFile<TData>() 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 public class EGSave : EGModule,IEGSave
{ {
private string DefaultSaveFile = "Default"; private string DefaultPath = "Default/SaveData.json";
private string DefaultSaveFolder = "SaveData";
private JObject _SaveObject; private JObject _SaveObject;
private JObject SaveObject{ private JObject SaveObject{
get { get {
@ -24,30 +33,20 @@ namespace EGFramework
return _SaveObject; return _SaveObject;
} }
} }
public EGSave() {}
public EGSave(){
}
/// <summary>
/// if you want to define default save data file name, please use "this.RegisterModule(new EGSave("FileName"))"in your architecture code(Init function);
/// </summary>
/// <param name="fileName"></param>
public EGSave(string fileName){
this.DefaultSaveFile = fileName;
}
public override void Init() public override void Init()
{ {
if (!Directory.Exists(DefaultSaveFolder)) if (!Directory.Exists(DefaultPath))
{ {
Directory.CreateDirectory(DefaultSaveFolder); Directory.CreateDirectory(Path.GetDirectoryName(DefaultPath));
File.WriteAllText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json","{}"); File.WriteAllText(DefaultPath,"{}");
}else if(!File.Exists(DefaultSaveFolder + "/" + DefaultSaveFile + ".json")){ }else if(!File.Exists(DefaultPath)){
File.WriteAllText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json","{}"); File.WriteAllText(DefaultPath,"{}");
} }
} }
private void InitSaveObject(){ private void InitSaveObject(){
using (StreamReader reader = File.OpenText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json")) using (StreamReader reader = File.OpenText(DefaultPath))
{ {
_SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); _SaveObject = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
} }
@ -57,10 +56,10 @@ namespace EGFramework
/// Push SaveObject data set to file /// Push SaveObject data set to file
/// </summary> /// </summary>
public void SaveToFile(){ public void SaveToFile(){
SaveToFile(DefaultSaveFile); SaveToFile(DefaultPath);
} }
private void SaveToFile(string fileName){ private void SaveToFile(string fileName){
File.WriteAllText(DefaultSaveFolder + "/" + fileName + ".json",JsonConvert.SerializeObject(SaveObject,Formatting.Indented)); File.WriteAllText(DefaultPath,JsonConvert.SerializeObject(SaveObject,Formatting.Indented));
} }
/// <summary> /// <summary>
@ -100,15 +99,21 @@ namespace EGFramework
SaveToFile(); 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
/// <summary> /// <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; /// 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> /// </summary>
/// <param name="fileName"></param> /// <param name="fileName"></param>
public void InitSaveData(string fileName) public void InitSaveData(string path)
{ {
DefaultSaveFile = fileName; DefaultPath = path;
if(!File.Exists(DefaultSaveFolder + "/" + DefaultSaveFile + ".json")){ if(!File.Exists(path)){
File.WriteAllText(DefaultSaveFolder + "/" + DefaultSaveFile + ".json","{}"); File.WriteAllText(path,"{}");
} }
InitSaveObject(); InitSaveObject();
} }
@ -116,7 +121,11 @@ namespace EGFramework
public static class CanGetEGSaveExtension{ public static class CanGetEGSaveExtension{
public static EGSave EGSave(this IEGFramework self){ public static EGSave EGSave(this IEGFramework self){
return EGArchitectureImplement.Interface.GetModule<EGSave>(); return self.GetModule<EGSave>();
}
public static EGSave EGSave(this IEGFramework self,string path){
self.GetModule<EGSave>().InitSaveData(path);
return self.GetModule<EGSave>();
} }
} }
} }

2
addons/EGFramework/Module/SQL/EGSqlite.cs → addons/EGFramework/Module/SaveTools/EGSqlite.cs

@ -10,7 +10,7 @@ namespace EGFramework{
List<TData> GetDataSet<TData>() where TData : new(); List<TData> GetDataSet<TData>() where TData : new();
void InitDatabase(string dataBaseName); void InitDatabase(string dataBaseName);
} }
public class EGSqlite : EGModule,IEGSqlite public class EGSqlite : EGModule
{ {
public string DBName = "Default"; public string DBName = "Default";
private string DefaultDBFolder = "SaveData"; private string DefaultDBFolder = "SaveData";
Loading…
Cancel
Save