summaryrefslogtreecommitdiff
path: root/modules/mono/editor/GodotTools/GodotTools.IdeMessaging/MessageDecoder.cs
blob: a00575a2a1d003eca423aa2804ea917ca5a9db60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Text;

namespace GodotTools.IdeMessaging
{
    public class MessageDecoder
    {
        private class DecodedMessage
        {
            public MessageKind? Kind;
            public string Id;
            public MessageStatus? Status;
            public readonly StringBuilder Body = new StringBuilder();
            public uint? PendingBodyLines;

            public void Clear()
            {
                Kind = null;
                Id = null;
                Status = null;
                Body.Clear();
                PendingBodyLines = null;
            }

            public Message ToMessage()
            {
                if (!Kind.HasValue || Id == null || !Status.HasValue ||
                    !PendingBodyLines.HasValue || PendingBodyLines.Value > 0)
                    throw new InvalidOperationException();

                return new Message(Kind.Value, Id, new MessageContent(Status.Value, Body.ToString()));
            }
        }

        public enum State
        {
            Decoding,
            Decoded,
            Errored
        }

        private readonly DecodedMessage decodingMessage = new DecodedMessage();

        public State Decode(string messageLine, out Message decodedMessage)
        {
            decodedMessage = null;

            if (!decodingMessage.Kind.HasValue)
            {
                if (!Enum.TryParse(messageLine, ignoreCase: true, out MessageKind kind))
                {
                    decodingMessage.Clear();
                    return State.Errored;
                }

                decodingMessage.Kind = kind;
            }
            else if (decodingMessage.Id == null)
            {
                decodingMessage.Id = messageLine;
            }
            else if (decodingMessage.Status == null)
            {
                if (!Enum.TryParse(messageLine, ignoreCase: true, out MessageStatus status))
                {
                    decodingMessage.Clear();
                    return State.Errored;
                }

                decodingMessage.Status = status;
            }
            else if (decodingMessage.PendingBodyLines == null)
            {
                if (!uint.TryParse(messageLine, out uint pendingBodyLines))
                {
                    decodingMessage.Clear();
                    return State.Errored;
                }

                decodingMessage.PendingBodyLines = pendingBodyLines;
            }
            else
            {
                if (decodingMessage.PendingBodyLines > 0)
                {
                    decodingMessage.Body.AppendLine(messageLine);
                    decodingMessage.PendingBodyLines -= 1;
                }
                else
                {
                    decodedMessage = decodingMessage.ToMessage();
                    decodingMessage.Clear();
                    return State.Decoded;
                }
            }

            return State.Decoding;
        }
    }
}