From f03b8754c67dbe56720456b4df72b3081a24f3e2 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Tue, 15 Oct 2024 19:26:42 +0800 Subject: [PATCH] add ByteSave as EGFileStream --- Example/UsingTest/Script/EGSaveTest.cs | 51 +++++++++++++++---- .../Module/SaveTools/EGByteSave.cs | 42 +++++---------- project.godot | 2 +- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index a54964b..2556a2f 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -33,7 +33,7 @@ namespace EGFramework.Examples.Test{ // // this.EGSave().SetObject(CardPath1,"Customer1",new Customer() { Name = "Andy" }); // // this.EGSave().SetObject(CardPath1,"Customer3",new Customer() { Name = "Terry" }); - string CardPath1 = "Card1"; + // 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); @@ -48,19 +48,25 @@ namespace EGFramework.Examples.Test{ // csvSave.InitSaveFile("SaveData/TestCsv.csv"); // Customer testData = csvSave.GetData("",1); // GD.Print("Name = "+testData.Name +" || ID = "+testData.Id); - // Customer testData = new Customer(){ + // CustomerByte testData = new CustomerByte(){ // Id = 1008, // Name = "AddDataDefault", // IsActive = true // }; // csvSave.SetData("",testData,2) - FileAccess testCsv = FileAccess.Open("res://SaveData/TestCsv.csv", FileAccess.ModeFlags.Read); - this.EGSave().ReadData(CardPath1,testCsv.GetAsText()); - IEnumerable allResult = this.EGSave().GetAllData(CardPath1,""); - GD.Print("Get result " + allResult.Count()); - foreach(Customer customer in allResult){ - GD.Print(customer.Id +"|" + customer.Name); - } + + // FileAccess testCsv = FileAccess.Open("res://SaveData/TestCsv.csv", FileAccess.ModeFlags.Read); + // this.EGSave().ReadData(CardPath1,testCsv.GetAsText()); + // IEnumerable allResult = this.EGSave().GetAllData(CardPath1,""); + // GD.Print("Get result " + allResult.Count()); + // foreach(Customer customer in allResult){ + // GD.Print(customer.Id +"|" + customer.Name); + // } + + this.EGSave().LoadObjectFile("SaveData/testDat.dat"); + // this.EGSave().SetObject("SaveData/testDat.dat","",testData); + CustomerByte testDat = this.EGSave().GetObject("SaveData/testDat.dat",""); + GD.Print(testDat.Id); // System.Linq.Expressions.Expression> expr = i => i.Name == "Creature"; // IEnumerable linqResult = csvSave.FindData("",expr); @@ -100,4 +106,31 @@ namespace EGFramework.Examples.Test{ [CsvParam("是否启用")] public bool IsActive { get; set; } } + + public class CustomerByte : Customer, IResponse, IRequest + { + public byte[] ToProtocolByteData() + { + byte[] data = new byte[0]; + data = data.Concat(((uint)Id).ToBytesLittleEndian()).ToArray(); + return data; + } + + public string ToProtocolData() + { + return ""; + } + + + public bool TrySetData(string protocolData, byte[] protocolBytes) + { + if(protocolBytes != null && protocolBytes.Length >= 4){ + Id = (int)protocolBytes.ToUINTLittleEndian(); + return true; + }else{ + return false; + } + } + + } } \ No newline at end of file diff --git a/addons/EGFramework/Module/SaveTools/EGByteSave.cs b/addons/EGFramework/Module/SaveTools/EGByteSave.cs index 9996941..8d0d518 100644 --- a/addons/EGFramework/Module/SaveTools/EGByteSave.cs +++ b/addons/EGFramework/Module/SaveTools/EGByteSave.cs @@ -7,6 +7,7 @@ using System.IO; namespace EGFramework { + [Obsolete("this idea can be replaced by EGFileStream")] public class EGByteSave : IEGSave,IEGSaveObject { public Encoding StringEncoding { set; get; } = Encoding.ASCII; @@ -17,7 +18,7 @@ namespace EGFramework DefaultPath = path; try { - FileStream fileStream = new FileStream(path,FileMode.Open); + FileStream fileStream = new FileStream(path,FileMode.OpenOrCreate); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); fileStream.Close(); @@ -52,41 +53,26 @@ namespace EGFramework public void SetObject(string objectKey , TObject obj) { - // throw new NotImplementedException(); + if(typeof(TObject).GetInterfaces().Contains(typeof(IRequest))){ + Data = ((IRequest)obj).ToProtocolByteData(); + }else{ + throw new Exception("This byte class cannot be serialized! you should implement IRequest first!"); + } + WriteDataBlock(DefaultPath); } public TObject GetObject(string objectKey) where TObject : new() { - throw new NotImplementedException(); - } - - public int GetDataLength(Type type){ - - switch(type){ - - default: return 0 ; + if(typeof(TObject).GetInterfaces().Contains(typeof(IResponse))){ + TObject result = new TObject(); + ((IResponse)result).TrySetData(StringEncoding.GetString(Data),Data); + return result; + }else{ + throw new Exception("This byte class cannot be serialized! you should implement IRequest first!"); } } } - [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] - public class ByteParamAttribute: Attribute{ - public int _pointer { set; get; } - public int _length { set; get; } - public ByteParamAttribute(int pointer,int length = 0){ - this._pointer = pointer; - this._length = length; - } - } - - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] - public class ByteClassAttribute:Attribute{ - public int _length { set; get; } - public ByteClassAttribute(int length){ - this._length = length; - } - } - public interface IEGByteInit{ void Init(byte[] data); } diff --git a/project.godot b/project.godot index 23c921a..a01ff7f 100644 --- a/project.godot +++ b/project.godot @@ -27,7 +27,7 @@ project/assembly_name="EGFramework" [editor_plugins] -enabled=PackedStringArray("res://addons/Tools/ItemImporter/plugin.cfg") +enabled=PackedStringArray() [input]