Browse Source

add ByteSave as EGFileStream

master
jkpete 2 months ago
parent
commit
f03b8754c6
  1. 51
      Example/UsingTest/Script/EGSaveTest.cs
  2. 42
      addons/EGFramework/Module/SaveTools/EGByteSave.cs
  3. 2
      project.godot

51
Example/UsingTest/Script/EGSaveTest.cs

@ -33,7 +33,7 @@ namespace EGFramework.Examples.Test{ @@ -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{ @@ -48,19 +48,25 @@ namespace EGFramework.Examples.Test{
// csvSave.InitSaveFile("SaveData/TestCsv.csv");
// Customer testData = csvSave.GetData<Customer>("",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<EGCsvSave>(CardPath1,testCsv.GetAsText());
IEnumerable<Customer> allResult = this.EGSave().GetAllData<Customer>(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<EGCsvSave>(CardPath1,testCsv.GetAsText());
// IEnumerable<Customer> allResult = this.EGSave().GetAllData<Customer>(CardPath1,"");
// GD.Print("Get result " + allResult.Count());
// foreach(Customer customer in allResult){
// GD.Print(customer.Id +"|" + customer.Name);
// }
this.EGSave().LoadObjectFile<EGByteSave>("SaveData/testDat.dat");
// this.EGSave().SetObject("SaveData/testDat.dat","",testData);
CustomerByte testDat = this.EGSave().GetObject<CustomerByte>("SaveData/testDat.dat","");
GD.Print(testDat.Id);
// System.Linq.Expressions.Expression<Func<Customer, bool>> expr = i => i.Name == "Creature";
// IEnumerable<Customer> linqResult = csvSave.FindData<Customer>("",expr);
@ -100,4 +106,31 @@ namespace EGFramework.Examples.Test{ @@ -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;
}
}
}
}

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

@ -7,6 +7,7 @@ using System.IO; @@ -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 @@ -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 @@ -52,41 +53,26 @@ namespace EGFramework
public void SetObject<TObject>(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<TObject>(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);
}

2
project.godot

@ -27,7 +27,7 @@ project/assembly_name="EGFramework" @@ -27,7 +27,7 @@ project/assembly_name="EGFramework"
[editor_plugins]
enabled=PackedStringArray("res://addons/Tools/ItemImporter/plugin.cfg")
enabled=PackedStringArray()
[input]

Loading…
Cancel
Save