-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigParser.cs
217 lines (204 loc) · 7.71 KB
/
ConfigParser.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace HLNC.SourceGenerators
{
public class ConfigParser
{
public Dictionary<string, string> resourceToPathMap = new Dictionary<string, string>();
public class GdScene
{
public int LoadSteps { get; set; }
public int Format { get; set; }
public string Uid { get; set; }
}
public class ExtResource
{
public string Type { get; set; }
public string Path { get; set; }
public string Id { get; set; }
}
public class SubResource
{
public string Type { get; set; }
public string Id { get; set; }
public Dictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();
}
public class Node
{
public string Name { get; set; }
public string Type { get; set; }
#nullable enable
public string? Parent { get; set; }
public string? Instance { get; set; }
#nullable disable
public Dictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();
}
public class ParsedTscn
{
public GdScene GdScene { get; set; }
public List<ExtResource> ExtResources { get; set; } = new List<ExtResource>();
public List<SubResource> SubResources { get; set; } = new List<SubResource>();
public List<Node> Nodes { get; set; } = new List<Node>();
public Node RootNode { get; set; }
}
public ParsedTscn ParseTscnFile(string fileText)
{
var parsedTscn = new ParsedTscn();
var lines = fileText.Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
SubResource currentSubResource = null;
Node currentNode = null;
foreach (var line in lines)
{
if (line.StartsWith("[gd_scene"))
{
parsedTscn.GdScene = ParseGdScene(line);
}
else if (line.StartsWith("[ext_resource"))
{
parsedTscn.ExtResources.Add(ParseExtResource(line));
}
else if (line.StartsWith("[sub_resource"))
{
currentSubResource = ParseSubResource(line);
parsedTscn.SubResources.Add(currentSubResource);
currentNode = null;
}
else if (line.StartsWith("[node"))
{
currentNode = ParseNode(line);
parsedTscn.Nodes.Add(currentNode);
if (currentNode.Parent == null) {
parsedTscn.RootNode = currentNode;
}
currentSubResource = null;
}
else if (currentSubResource != null && line.Contains("="))
{
var parts = line.Split('=');
currentSubResource.Properties[parts[0].Trim()] = parts[1].Trim();
}
else if (currentNode != null && line.Contains("="))
{
var parts = line.Split('=');
var propName = parts[0].Trim();
var propValue = parts[1].Trim();
if (propName == "script")
{
var regex = new Regex(@"ExtResource\(""([^']+)""\)");
var match = regex.Match(propValue);
if (match.Success)
{
var resourceId = match.Groups[1].Value;
propValue = resourceToPathMap[resourceId];
Debug.WriteLine($"Found script: {propValue}");
}
}
currentNode.Properties[propName] = propValue;
}
}
return parsedTscn;
}
private GdScene ParseGdScene(string line)
{
var gdScene = new GdScene();
var parts = line.Split(' ');
foreach (var part in parts)
{
if (part.StartsWith("load_steps="))
{
gdScene.LoadSteps = int.Parse(part.Split('=')[1].Trim('"', ']'));
}
else if (part.StartsWith("format="))
{
gdScene.Format = int.Parse(part.Split('=')[1].Trim('"', ']'));
}
else if (part.StartsWith("uid="))
{
gdScene.Uid = part.Split('=')[1].Trim('"', ']');
}
}
return gdScene;
}
private ExtResource ParseExtResource(string line)
{
var extResource = new ExtResource();
var parts = line.Split(' ');
foreach (var part in parts)
{
if (part.StartsWith("type="))
{
extResource.Type = part.Split('=')[1].Trim('"', ']');
}
else if (part.StartsWith("path="))
{
extResource.Path = part.Split('=')[1].Trim('"', ']');
}
else if (part.StartsWith("id="))
{
var regex = new Regex(@"id=""?([^""]+)""?");
var match = regex.Match(part);
if (match.Success)
{
extResource.Id = match.Groups[1].Value;
}
}
}
resourceToPathMap[extResource.Id] = extResource.Path.Replace("res://", "");
return extResource;
}
private SubResource ParseSubResource(string line)
{
var subResource = new SubResource();
var parts = line.Split(' ');
foreach (var part in parts)
{
if (part.StartsWith("type="))
{
subResource.Type = part.Split('=')[1].Trim('"', ']');
}
else if (part.StartsWith("id="))
{
subResource.Id = part.Split('=')[1].Trim('"', ']');
}
}
return subResource;
}
private Node ParseNode(string line)
{
var node = new Node();
var parts = line.Split(' ');
foreach (var part in parts)
{
if (part.StartsWith("name="))
{
node.Name = part.Split('=')[1].Trim('"', ']');
}
else if (part.StartsWith("type="))
{
node.Type = part.Split('=')[1].Trim('"', ']');
}
else if (part.StartsWith("parent="))
{
node.Parent = part.Split('=')[1].Trim('"', ']');
} else if (part.StartsWith("instance="))
{
var inst = part.Split('=')[1].Trim('"', ']');
var regex = new Regex(@"ExtResource\(""([^']+)""\)");
var match = regex.Match(inst);
if (match.Success)
{
var resourceId = match.Groups[1].Value;
inst = resourceToPathMap[resourceId];
if (!inst.EndsWith(".tscn")) continue;
Debug.WriteLine($"Found instance: {inst}");
node.Instance = inst;
}
}
}
return node;
}
}
}