public void OnAdd(EntityRef key, Entity value) { var packet = new ViewPacket { PacketType = ViewPacket.ViewPacketType.CreateEntity, EntityType = value.GetType().ToString(), Data = JObject.FromObject(value), Entity = key, }; dispatcher.OnPacketGenerated?.Invoke(packet); foreach (var eventWrapper in value.SyncedObjects) { if (eventWrapper.Value == null) { continue; } eventWrapper.Value.AddSyncHandler(dispatcher, key, eventWrapper.Key); if (eventWrapper.Value is IEventCollection <string, ISyncField> withChildren) { foreach (var child in withChildren) { if (child.Value == null) { continue; } child.Value.AddSyncHandler(dispatcher, key, eventWrapper.Key + "." + child.Key); } } } }
public void Apply(ViewPacket packet) { Type entityType = null; if (!string.IsNullOrEmpty(packet.EntityType)) { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { entityType = assembly.GetType(packet.EntityType); if (entityType != null) { break; } } } switch (packet.PacketType) { case ViewPacket.ViewPacketType.CreateEntity: var clonedEntity = (Entity)packet.Data.ToObject(entityType); clonedEntity.Id = packet.Entity; Entities.Add(clonedEntity.Id, clonedEntity); break; case ViewPacket.ViewPacketType.DestroyEntity: Entities.Remove(packet.Entity); break; case ViewPacket.ViewPacketType.SetFieldValue: var targetEntity = Entities[packet.Entity]; string[] path = packet.FieldPath.Split('.'); var targetedEventWrapper = targetEntity.SyncedObjects.Where(s => s.Key == path[0]).First().Value; for (int i = 1; i < path.Length; i++) { if (targetedEventWrapper is IEventCollection <string, ISyncField> collection) { targetedEventWrapper = collection[path[i]]; } else { throw new InvalidOperationException("Something went wrong!"); } } if (targetedEventWrapper is IEventField field) { object newValue = packet.Data.ToObject(entityType); field.SetValue(newValue); } break; } }
public void OnRemove(TKey key, TValue value) { var packet = new ViewPacket { PacketType = ViewPacket.ViewPacketType.RemoveCollectionItem, Entity = parent, FieldPath = $"{path}.{key}" }; dispatcher.OnPacketGenerated?.Invoke(packet); }
public void OnRemove(EntityRef key, Entity value) { var packet = new ViewPacket { PacketType = ViewPacket.ViewPacketType.DestroyEntity, EntityType = value.GetType().ToString(), Entity = key }; dispatcher.OnPacketGenerated?.Invoke(packet); }
public void OnAfterChanged() { object value = field.GetValue(); var valueType = value.GetType(); var packet = new ViewPacket { PacketType = ViewPacket.ViewPacketType.SetFieldValue, EntityType = valueType.ToString(), Data = JToken.FromObject(value), Entity = entity, FieldPath = path }; dispatcher.OnPacketGenerated?.Invoke(packet); }
public void OnAdd(TKey key, TValue value) { var packet = new ViewPacket { PacketType = ViewPacket.ViewPacketType.AddCollectionItem, Data = JObject.FromObject(value), Entity = parent, FieldPath = $"{path}.{key}" }; dispatcher.OnPacketGenerated?.Invoke(packet); if (value is ISyncField wrapper) { wrapper.AddSyncHandler(dispatcher, parent, $"{path}.{key}"); } }