using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Uigenx.Protocol { /// Every bridge message on the wire: `{ v, t, id?, re?, ts, body }`. public sealed class UgxEnvelope { [JsonProperty("v")] public int V { get; set; } = UgxJson.ProtocolVersion; /// `family.name`, for example `data.delta`. [JsonProperty("t")] public string T { get; set; } /// Present on requests and events. [JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)] public string Id { get; set; } /// Echoes the id of the request this message answers. [JsonProperty("re", NullValueHandling = NullValueHandling.Ignore)] public string Re { get; set; } [JsonProperty("ts")] public long Ts { get; set; } [JsonProperty("body")] public JToken Body { get; set; } public T BodyAs() where T : class, new() => Body == null ? new T() : Body.ToObject(UgxJson.Serializer); public static UgxEnvelope Create(string type, object body, long ts, string id = null, string re = null) => new UgxEnvelope { V = UgxJson.ProtocolVersion, T = type, Id = id, Re = re, Ts = ts, Body = body == null ? new JObject() : JToken.FromObject(body, UgxJson.Serializer), }; } public sealed class UgxErrorBody { public string Code { get; set; } public string Message { get; set; } public Dictionary Details { get; set; } } public static class UgxErrorCodes { public const string VersionMismatch = "version_mismatch"; public const string Unauthorized = "unauthorized"; public const string UnknownMessage = "unknown_message"; public const string BadRequest = "bad_request"; public const string CompileFailed = "compile_failed"; public const string ImportFailed = "import_failed"; public const string RenderFailed = "render_failed"; public const string NotPlaying = "not_playing"; public const string Internal = "internal"; } /// Envelope `t` values. Unity sends the first group and receives the second. public static class UgxMessageTypes { public const string Hello = "hello"; public const string SyncResult = "sync.result"; public const string CompileResult = "compile.result"; public const string DataSchemaResult = "dataSchema.result"; public const string DataDelta = "data.delta"; public const string DataSnapshot = "data.snapshot"; public const string ActionFired = "action.fired"; public const string PreviewResult = "preview.result"; public const string HotReloadResult = "hotReload.result"; public const string ScreenState = "screen.state"; public const string Log = "log"; public const string Pong = "pong"; public const string Welcome = "welcome"; public const string SyncBundle = "sync.bundle"; public const string SyncFile = "sync.file"; public const string CompileRequest = "compile.request"; public const string DataSchemaRequest = "dataSchema.request"; public const string DataSubscribe = "data.subscribe"; public const string DataUnsubscribe = "data.unsubscribe"; public const string DataSet = "data.set"; public const string ActionEmit = "action.emit"; public const string PreviewRequest = "preview.request"; public const string HotReloadRequest = "hotReload.request"; public const string ScreenshotRequest = "screenshot.request"; public const string Ping = "ping"; /// Sent in both directions. public const string Error = "error"; } }