|
@@ -2,6 +2,8 @@
|
|
|
using Expressive;
|
|
|
using InABox.Core;
|
|
|
using InABox.Database;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using Newtonsoft.Json.Serialization;
|
|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
@@ -13,7 +15,7 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace PRS.Shared.Events;
|
|
|
|
|
|
-public interface IEventData : ISerializeBinary
|
|
|
+public interface IEventData
|
|
|
{
|
|
|
IEvent Event { get; }
|
|
|
|
|
@@ -48,113 +50,37 @@ public class EventData<T, TDataModel> : IEventData
|
|
|
}
|
|
|
|
|
|
public Notification GenerateNotification(TDataModel model) => Event.GenerateNotification(model);
|
|
|
-
|
|
|
- public void SerializeBinary(CoreBinaryWriter writer)
|
|
|
- {
|
|
|
- Event.SerializeBinary(writer);
|
|
|
- writer.Write(Triggers.Count);
|
|
|
- foreach(var trigger in Triggers)
|
|
|
- {
|
|
|
- EventUtils.SerializeObject(trigger, writer);
|
|
|
- }
|
|
|
- writer.Write(Actions.Count);
|
|
|
- foreach(var action in Actions)
|
|
|
- {
|
|
|
- EventUtils.SerializeObject(action, writer);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void DeserializeBinary(CoreBinaryReader reader)
|
|
|
- {
|
|
|
- Event.DeserializeBinary(reader);
|
|
|
- var nTriggers = reader.ReadInt32();
|
|
|
- for(int i = 0; i < nTriggers; ++i)
|
|
|
- {
|
|
|
- var trigger = EventUtils.DeserializeObject<IEventTrigger<T, TDataModel>>(EventUtils.GetTriggerType, reader);
|
|
|
- Triggers.Add(trigger);
|
|
|
- }
|
|
|
- var nActions = reader.ReadInt32();
|
|
|
- for(int i = 0; i < nActions; ++i)
|
|
|
- {
|
|
|
- var action = EventUtils.DeserializeObject<IEventAction<T>>(EventUtils.GetActionType, reader);
|
|
|
- Actions.Add(action);
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
public static class EventUtils
|
|
|
{
|
|
|
#region Serialisation
|
|
|
|
|
|
- public static void SerializeObject(ISerializeBinary obj, CoreBinaryWriter writer)
|
|
|
+ private class WritablePropertiesOnlyResolver : DefaultContractResolver
|
|
|
{
|
|
|
- writer.Write(obj.GetType().Name);
|
|
|
- foreach(var arg in obj.GetType().GenericTypeArguments)
|
|
|
+ protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
|
|
{
|
|
|
- writer.Write(CoreUtils.EntityName(arg));
|
|
|
+ IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
|
|
|
+ return props.Where(p => p.Writable).ToList();
|
|
|
}
|
|
|
- obj.SerializeBinary(writer);
|
|
|
}
|
|
|
- public static T DeserializeObject<T>(Func<string, Type> typeSource, CoreBinaryReader reader)
|
|
|
- where T : class, ISerializeBinary
|
|
|
- {
|
|
|
- var eventTypeName = reader.ReadString();
|
|
|
- var objType = typeSource(eventTypeName);
|
|
|
|
|
|
- var typeParams = objType.GetTypeInfo().GenericTypeParameters;
|
|
|
- var typeArgs = new Type[typeParams.Length];
|
|
|
- for(int i = 0; i < typeParams.Length; ++i)
|
|
|
- {
|
|
|
- var entityType = CoreUtils.GetEntity(reader.ReadString());
|
|
|
- typeArgs[i] = entityType;
|
|
|
- }
|
|
|
- if(typeArgs.Length > 0)
|
|
|
- {
|
|
|
- objType = objType.MakeGenericType(typeArgs);
|
|
|
- }
|
|
|
- var obj = (Activator.CreateInstance(objType) as T)!;
|
|
|
- obj.DeserializeBinary(reader);
|
|
|
-
|
|
|
- return obj;
|
|
|
+ private static JsonSerializerSettings SerializationSettings()
|
|
|
+ {
|
|
|
+ var settings = Serialization.CreateSerializerSettings();
|
|
|
+ settings.TypeNameHandling = TypeNameHandling.Auto;
|
|
|
+ settings.ContractResolver = new WritablePropertiesOnlyResolver();
|
|
|
+ return settings;
|
|
|
}
|
|
|
|
|
|
- public static void Serialize(IEventData data, CoreBinaryWriter writer)
|
|
|
+ public static string Serialize(IEventData data)
|
|
|
{
|
|
|
- writer.Write(data.Event.GetType().Name);
|
|
|
- foreach(var arg in data.Event.GetType().GenericTypeArguments)
|
|
|
- {
|
|
|
- writer.Write(CoreUtils.EntityName(arg));
|
|
|
- }
|
|
|
- data.SerializeBinary(writer);
|
|
|
+ return JsonConvert.SerializeObject(data, typeof(IEventData), SerializationSettings());
|
|
|
}
|
|
|
|
|
|
- public static IEventData Deserialize(CoreBinaryReader reader)
|
|
|
+ public static IEventData Deserialize(string json)
|
|
|
{
|
|
|
- var eventTypeName = reader.ReadString();
|
|
|
- var eventType = EventUtils.GetEventType(eventTypeName);
|
|
|
-
|
|
|
- var typeParams = eventType.GetTypeInfo().GenericTypeParameters;
|
|
|
- var typeArgs = new Type[typeParams.Length];
|
|
|
- for(int i = 0; i < typeParams.Length; ++i)
|
|
|
- {
|
|
|
- var entityType = CoreUtils.GetEntity(reader.ReadString());
|
|
|
- typeArgs[i] = entityType;
|
|
|
- }
|
|
|
- if(typeArgs.Length > 0)
|
|
|
- {
|
|
|
- eventType = eventType.MakeGenericType(typeArgs);
|
|
|
- }
|
|
|
- var ev = (Activator.CreateInstance(eventType) as IEvent)!;
|
|
|
-
|
|
|
- var dataModelType = ev.GetType().GetInterfaceDefinition(typeof(IEvent<>))!.GenericTypeArguments[0];
|
|
|
-
|
|
|
- var eventDataType = typeof(EventData<,>).MakeGenericType(ev.GetType(), dataModelType);
|
|
|
-
|
|
|
- var eventData = (Activator.CreateInstance(eventDataType, ev) as IEventData)!;
|
|
|
-
|
|
|
- eventData.DeserializeBinary(reader);
|
|
|
-
|
|
|
- return eventData;
|
|
|
+ return JsonConvert.DeserializeObject<IEventData>(json, SerializationSettings())!;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -336,9 +262,7 @@ public static class EventUtils
|
|
|
{
|
|
|
if(ev.Data is not null && ev.Data.Length != 0)
|
|
|
{
|
|
|
- using var stream = new MemoryStream(ev.Data);
|
|
|
- var reader = new CoreBinaryReader(stream, BinarySerializationSettings.Latest);
|
|
|
- var data = Deserialize(reader);
|
|
|
+ var data = Deserialize(ev.Data);
|
|
|
|
|
|
var entityType = data.Event.GetType().GenericTypeArguments[0];
|
|
|
var list = _entityEvents.GetValueOrAdd(ev.EventType).GetValueOrAdd(entityType);
|
|
@@ -523,13 +447,12 @@ public class ChildEventDataModel : IChildEventDataModel
|
|
|
|
|
|
public void Init(IEventData data)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
-public interface IEvent : ISerializeBinary
|
|
|
+public interface IEvent
|
|
|
{
|
|
|
IEventDataModelDefinition DataModelDefinition();
|
|
|
|
|
@@ -601,14 +524,14 @@ public interface IEventTrigger
|
|
|
string Description { get; }
|
|
|
}
|
|
|
|
|
|
-public interface IEventTrigger<TEvent, TDataModel> : ISerializeBinary, IEventTrigger
|
|
|
+public interface IEventTrigger<TEvent, TDataModel> : IEventTrigger
|
|
|
where TEvent : IEvent<TDataModel>
|
|
|
where TDataModel : IEventDataModel
|
|
|
{
|
|
|
bool Check(TDataModel dataModel);
|
|
|
}
|
|
|
|
|
|
-public interface IEventAction : ISerializeBinary
|
|
|
+public interface IEventAction
|
|
|
{
|
|
|
IEnumerable<string> ReferencedVariables { get; }
|
|
|
|