Newer
Older
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
101
102
103
104
105
106
107
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools.ProtocolGenerator
{
public class ProtocolProcessor
{
public static void ResolveTypeReferences(Protocol protocol)
{
foreach (var domain in protocol.Domains)
{
ResolveTypeReferences(protocol, domain);
}
}
public static void ResolveTypeReferences(Protocol protocol, Domain domain)
{
foreach (var command in domain.Commands)
{
ResolveTypeReferences(protocol, domain, command);
}
}
public static void ResolveTypeReferences(Protocol protocol, Domain domain, Command command)
{
foreach (var parameter in command.Parameters)
{
ResolveTypeReferences(protocol, domain, parameter);
}
foreach (var returnValue in command.Returns)
{
ResolveTypeReferences(protocol, domain, returnValue);
}
}
public static void ResolveTypeReferences(Protocol protocol, Domain domain, Property property)
{
if (property.TypeReference != null)
{
// Find the type which is being referenced
var referenceParts = property.TypeReference.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
Domain referencedDomain = null;
Type referencedType = null;
if (referenceParts.Length == 1)
{
referencedDomain = domain;
referencedType = domain.GetType(referenceParts[0]);
}
else if (referenceParts.Length == 2)
{
referencedDomain = protocol.GetDomain(referenceParts[0]);
referencedType = referencedDomain.GetType(referenceParts[1]);
}
else
{
throw new ArgumentOutOfRangeException();
}
// If it is a string, it can be resolved easily
if(referencedType.IsString())
{
property.Kind = "string";
property.TypeReference = null;
}
}
}
public static Protocol LoadProtocol(string path, string alias)
{
string json = File.ReadAllText(path);
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MissingMemberHandling = MissingMemberHandling.Error;
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
Protocol p = JsonConvert.DeserializeObject<Protocol>(json, settings);
p.SourceFile = path;
p.Alias = alias;
foreach (var domain in p.Domains)
{
foreach (var command in domain.Commands)
{
command.SupportedBy.Add(alias);
}
foreach (var @event in domain.Events)
{
@event.SupportedBy.Add(alias);
}
foreach (var type in domain.Types)
{
type.SupportedBy.Add(alias);
}
}
return p;
}
}
}