using System; using System.Collections.Generic; using System.Text; using AppGame; using Google.Protobuf; using JNGame.Math; using JNGame.Sync.Entity; using JNGame.Sync.State.Tile.Entity; using JNGame.Sync.System; using JNGame.Sync.System.Data; using Newtonsoft.Json; using Plugins.JNGame.Network.Action; namespace Game.JNGState.Logic.Data { [Serializable] public class GDataValue { public DValuePosition Position = null; } public class GDataBase : ISTileData where Self : GDataBase where T : GDataValue,new() where N : JNTileEntity { public readonly T Value = new (); public N Node => Entity as N; public override bool IsHost => Node is not null && Node.IsHost; public GDataBase() { } public GDataBase(N node) { Id = node.Id; Value.Position = new DValuePosition() { x = node.Position.x.rawValue, y = node.Position.y.rawValue, z = node.Position.z.rawValue, }; BindEntity(node); } public override bool IsEquals(ISData data) { var node = data as Self; if (node is null) return false; return Value.Position.Equals(node.Value.Position); } public sealed override byte[] GetByte() { return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Value)); } public sealed override byte[] GetByteDifference(ISData diffValue = null) { var diff = GetDifference(diffValue); if (diff is not null) { return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(diff)); } else { return Array.Empty(); } } public virtual T GetDifference(ISData diffValue = null) { var diff = diffValue as Self; if (diff is null || IsEquals(diffValue)) return null; var value = new T(); if (diff.Value.Position is not null) value.Position = diff.Value.Position; return value; } public sealed override void UByte(byte[] bytes) { if (bytes.Length == 0) return; var value = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bytes)); UData(value); } /// /// 更新数据 /// public virtual void UData(T data) { if (data.Position is not null) Value.Position = data.Position; } public override LVector3 GetDataPosition() { return Value.Position.ToLVector3(); } } public abstract class GDataBaseSystem : STileDataSystem where T : ISTileData,new() where E : JNTileEntity, new() { protected GDataBaseSystem(SStateDataEnum type) : base(type) { } public override void OnSendUBytes(Dictionary bytes) { JNStateItemData data = new JNStateItemData(); data.NetID = NetID; foreach (var byteItem in bytes) { data.Messages.Add(byteItem.Key,new JNStateData() { Data = ByteString.CopyFrom(byteItem.Value) }); } App.Server.AllSend((int)NActionEnum.NSyncStateDataUpdate,data); } } }