forked from ZeraGmbH/Blockly.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParser.cs
More file actions
179 lines (124 loc) · 4.27 KB
/
JsonParser.cs
File metadata and controls
179 lines (124 loc) · 4.27 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using BlocklyNet.Core.Blocks.Variables;
using Newtonsoft.Json.Linq;
using BlocklyNet.Core.Model;
namespace BlocklyNet.Core;
/// <summary>
///
/// </summary>
public class JsonParser : Parser<JsonParser>
{
private readonly Dictionary<string, string> _variables = [];
/// <inheritdoc/>
public override Workspace Parse(string json, bool preserveWhitespace = false)
{
var workspace = new Workspace();
var jdoc = JObject.Parse(json);
ParseVariables(jdoc, workspace);
ParseBlocks(jdoc, workspace);
return workspace;
}
private void ParseVariables(JToken jdoc, Workspace workspace)
{
var variables = jdoc["variables"];
if (variables == null)
return;
foreach (var variable in variables)
{
var id = (string)variable["id"]!;
var name = (string)variable["name"]!;
_variables[id] = name;
GlobalVariablesSet setVariable = new();
setVariable.Fields.Add(new() { Name = "VAR", Value = name });
setVariable.Values.Add(new() { Name = "VALUE", Block = null });
workspace.Blocks.Add(setVariable);
}
}
private void ParseBlocks(JToken jdoc, Workspace workspace)
{
var blocks = jdoc["blocks"]?["blocks"];
if (blocks == null)
return;
foreach (var jblock in blocks)
{
var block = ParseBlock(jblock);
if (block != null)
workspace.Blocks.Add(block);
}
}
private Block? ParseBlock(JToken jblock)
{
if (((bool?)jblock["disabled"]).GetValueOrDefault() == true)
return null;
var type = (string)jblock["type"]!;
if (!blocks.TryGetValue(type, out var factory))
throw new ApplicationException($"block type not registered: '{type}'");
var block = factory();
block.Type = type;
block.Id = (string)jblock["id"]!;
var fields = jblock["fields"];
if (fields != null)
foreach (var field in fields)
ParseField(field, block);
var inputs = jblock["inputs"];
if (inputs != null)
foreach (var input in inputs)
ParseInput(input, block);
var icons = jblock["icons"];
if (icons != null)
foreach (var icon in icons)
ParseComment(icon, block);
var extra = jblock["extraState"];
if (extra != null)
ParseMutation(extra, block);
var next = jblock["next"];
if (next != null)
block.Next = ParseBlock(next["block"]!);
return block;
}
private void ParseField(JToken jfield, Block block)
{
var first = jfield.First!;
block.Fields.Add(new() { Name = jfield.Path.Split(".")[^1], Value = first.HasValues ? _variables[(string)first["id"]!] : first.Value<string>()! });
}
private void ParseInput(JToken jvalue, Block block)
{
var jBlock = jvalue.First!["block"] ?? jvalue.First["shadow"];
if (jBlock == null)
return;
var key = jvalue.Path.Split(".")[^1];
var inputBlock = ParseBlock(jBlock)!;
block.Values.Add(new() { Name = key, Block = inputBlock });
block.Statements.Add(new() { Name = key, Block = inputBlock });
}
private static void ParseComment(JToken jicon, Block block)
{
var key = jicon.Path.Split(".")[^1];
if (key != "comment")
return;
block.Comments.Add(new Comment((string)jicon.First!["text"]!));
}
private void ParseMutation(JToken jextra, Block block)
{
if (!jextra.HasValues)
return;
var name = (string?)jextra["name"];
if (name != null)
block.Mutations.Add(new Mutation("mutation", "name", name));
var hasElse = (string?)jextra["hasElse"];
if (hasElse != null)
block.Mutations.Add(new Mutation("mutation", "else", "1"));
var elseIfCount = (string?)jextra["elseIfCount"];
if (elseIfCount != null)
block.Mutations.Add(new Mutation("mutation", "elseIfCount", elseIfCount));
var itemCount = (string?)jextra["itemCount"];
if (itemCount != null)
block.Mutations.Add(new Mutation("mutation", "items", itemCount));
var hasStatements = (bool?)jextra["hasStatements"];
if (hasStatements.HasValue)
block.Mutations.Add(new Mutation("mutation", "statements", hasStatements.ToString()!.ToLower()));
var args = jextra["params"];
if (args != null)
foreach (var arg in args)
block.Mutations.Add(new Mutation("arg", "name", arg.HasValues ? _variables[(string)arg["id"]!]! : arg.Value<string>()!));
}
}