|
| 1 | +using System.Text; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.Text; |
| 4 | + |
| 5 | +namespace ObsWebSocket.Codegen.Tasks.Generation; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Contains emitter logic for generating a System.Text.Json source generation context. |
| 9 | +/// </summary> |
| 10 | +internal static partial class Emitter |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Generates a JsonSerializerContext containing all fixed and generated protocol types. |
| 14 | + /// </summary> |
| 15 | + public static void GenerateJsonSerializerContext( |
| 16 | + SourceProductionContext context, |
| 17 | + ProtocolDefinition protocol |
| 18 | + ) |
| 19 | + { |
| 20 | + try |
| 21 | + { |
| 22 | + StringBuilder builder = BuildSourceHeader("// Serialization Context: ObsWebSocketJsonContext"); |
| 23 | + |
| 24 | + _ = builder.AppendLine("using System.Collections.Generic;"); |
| 25 | + _ = builder.AppendLine("using System.Text.Json;"); |
| 26 | + _ = builder.AppendLine("using System.Text.Json.Serialization;"); |
| 27 | + _ = builder.AppendLine("using ObsWebSocket.Core.Protocol;"); |
| 28 | + _ = builder.AppendLine("using ObsWebSocket.Core.Protocol.Common;"); |
| 29 | + _ = builder.AppendLine("using ObsWebSocket.Core.Protocol.Events;"); |
| 30 | + _ = builder.AppendLine("using ObsWebSocket.Core.Protocol.Requests;"); |
| 31 | + _ = builder.AppendLine("using ObsWebSocket.Core.Protocol.Responses;"); |
| 32 | + _ = builder.AppendLine(); |
| 33 | + _ = builder.AppendLine("namespace ObsWebSocket.Core.Serialization;"); |
| 34 | + _ = builder.AppendLine(); |
| 35 | + _ = builder.AppendLine("[JsonSourceGenerationOptions("); |
| 36 | + _ = builder.AppendLine(" PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,"); |
| 37 | + _ = builder.AppendLine(" DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault)]"); |
| 38 | + |
| 39 | + // Fixed protocol wrapper and payload types. |
| 40 | + _ = builder.AppendLine("[JsonSerializable(typeof(OutgoingMessage<RequestPayload>))]"); |
| 41 | + _ = builder.AppendLine("[JsonSerializable(typeof(OutgoingMessage<IdentifyPayload>))]"); |
| 42 | + _ = builder.AppendLine("[JsonSerializable(typeof(OutgoingMessage<ReidentifyPayload>))]"); |
| 43 | + _ = builder.AppendLine("[JsonSerializable(typeof(OutgoingMessage<RequestBatchPayload>))]"); |
| 44 | + _ = builder.AppendLine("[JsonSerializable(typeof(IncomingMessage<JsonElement>))]"); |
| 45 | + _ = builder.AppendLine("[JsonSerializable(typeof(RequestResponsePayload<JsonElement>))]"); |
| 46 | + _ = builder.AppendLine("[JsonSerializable(typeof(RequestBatchResponsePayload<JsonElement>))]"); |
| 47 | + _ = builder.AppendLine("[JsonSerializable(typeof(EventPayloadBase<JsonElement>))]"); |
| 48 | + _ = builder.AppendLine("[JsonSerializable(typeof(HelloPayload))]"); |
| 49 | + _ = builder.AppendLine("[JsonSerializable(typeof(IdentifiedPayload))]"); |
| 50 | + _ = builder.AppendLine("[JsonSerializable(typeof(RequestStatus))]"); |
| 51 | + |
| 52 | + // Handwritten stub types. |
| 53 | + _ = builder.AppendLine("[JsonSerializable(typeof(SceneStub))]"); |
| 54 | + _ = builder.AppendLine("[JsonSerializable(typeof(SceneItemStub))]"); |
| 55 | + _ = builder.AppendLine("[JsonSerializable(typeof(SceneItemTransformStub))]"); |
| 56 | + _ = builder.AppendLine("[JsonSerializable(typeof(FilterStub))]"); |
| 57 | + _ = builder.AppendLine("[JsonSerializable(typeof(InputStub))]"); |
| 58 | + _ = builder.AppendLine("[JsonSerializable(typeof(TransitionStub))]"); |
| 59 | + _ = builder.AppendLine("[JsonSerializable(typeof(OutputStub))]"); |
| 60 | + _ = builder.AppendLine("[JsonSerializable(typeof(MonitorStub))]"); |
| 61 | + _ = builder.AppendLine("[JsonSerializable(typeof(PropertyItemStub))]"); |
| 62 | + |
| 63 | + // Common collection payload helpers. |
| 64 | + _ = builder.AppendLine("[JsonSerializable(typeof(List<JsonElement>))]"); |
| 65 | + _ = builder.AppendLine("[JsonSerializable(typeof(Dictionary<string, bool>))]"); |
| 66 | + _ = builder.AppendLine("[JsonSerializable(typeof(Dictionary<string, JsonElement>))]"); |
| 67 | + _ = builder.AppendLine( |
| 68 | + "[JsonSerializable(typeof(List<RequestResponsePayload<JsonElement>>))]" |
| 69 | + ); |
| 70 | + |
| 71 | + // Generated request/response/event DTOs. |
| 72 | + if (protocol.Requests is not null) |
| 73 | + { |
| 74 | + foreach (RequestDefinition request in protocol.Requests) |
| 75 | + { |
| 76 | + string baseName = SanitizeIdentifier(request.RequestType); |
| 77 | + if (request.RequestFields?.Count > 0) |
| 78 | + { |
| 79 | + _ = builder.AppendLine( |
| 80 | + $"[JsonSerializable(typeof({GeneratedRequestsNamespace}.{baseName}RequestData))]" |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if (request.ResponseFields?.Count > 0) |
| 85 | + { |
| 86 | + _ = builder.AppendLine( |
| 87 | + $"[JsonSerializable(typeof({GeneratedResponsesNamespace}.{baseName}ResponseData))]" |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (protocol.Events is not null) |
| 94 | + { |
| 95 | + foreach (OBSEvent eventDef in protocol.Events) |
| 96 | + { |
| 97 | + if (eventDef.DataFields?.Count > 0) |
| 98 | + { |
| 99 | + string payloadType = SanitizeIdentifier(eventDef.EventType) + "Payload"; |
| 100 | + _ = builder.AppendLine( |
| 101 | + $"[JsonSerializable(typeof({GeneratedEventsNamespace}.{payloadType}))]" |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + foreach (string nestedTypeName in s_generatedNestedTypes.Keys.OrderBy(k => k)) |
| 108 | + { |
| 109 | + _ = builder.AppendLine( |
| 110 | + $"[JsonSerializable(typeof({NestedTypesNamespace}.{nestedTypeName}))]" |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + _ = builder.AppendLine("internal sealed partial class ObsWebSocketJsonContext : JsonSerializerContext"); |
| 115 | + _ = builder.AppendLine("{"); |
| 116 | + _ = builder.AppendLine("}"); |
| 117 | + |
| 118 | + context.AddSource( |
| 119 | + "ObsWebSocketJsonContext.g.cs", |
| 120 | + SourceText.From(builder.ToString(), Encoding.UTF8) |
| 121 | + ); |
| 122 | + } |
| 123 | + catch (Exception ex) |
| 124 | + { |
| 125 | + context.ReportDiagnostic( |
| 126 | + Diagnostic.Create( |
| 127 | + Diagnostics.IdentifierGenerationError, |
| 128 | + Location.None, |
| 129 | + "ObsWebSocketJsonContext", |
| 130 | + "JsonSerializerContext generation", |
| 131 | + ex.ToString() |
| 132 | + ) |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments