-
Notifications
You must be signed in to change notification settings - Fork 10
/
Entities.cs
242 lines (204 loc) · 6.25 KB
/
Entities.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Sledge.Formats.Bsp.Objects;
namespace Sledge.Formats.Bsp.Lumps
{
public class Entities : ILump, IList<Entity>
{
private readonly IList<Entity> _entities;
public Entities()
{
_entities = new List<Entity>();
}
public void Read(BinaryReader br, Blob blob, Version version)
{
var text = Encoding.ASCII.GetString(br.ReadBytes(blob.Length));
// Remove comments
var cleaned = new StringBuilder();
foreach (var line in text.Split('\n'))
{
var l = line;
var idx = l.IndexOf("//", StringComparison.Ordinal);
if (idx >= 0) l = l.Substring(0, idx);
l = l.Trim();
cleaned.Append(l).Append('\n');
}
var data = cleaned.ToString();
Entity cur = null;
int i;
string key = null;
for (i = 0; i < data.Length; i++)
{
var token = GetToken();
if (token == "{")
{
// Start of new entity
cur = new Entity();
_entities.Add(cur);
key = null;
}
else if (token == "}")
{
// End of entity
cur = null;
key = null;
}
else if (cur != null && key != null)
{
// KeyValue value
SetKeyValue(cur, key, token);
key = null;
}
else if (cur != null)
{
// KeyValue key
key = token;
}
else if (token == null)
{
// End of file
break;
}
else
{
// Invalid
}
}
string GetToken()
{
if (!ScanToNonWhitespace()) return null;
if (data[i] == '{' || data[i] == '}')
{
// Start/end entity
return data[i].ToString();
}
if (data[i] == '"')
{
// Quoted string, find end quote
var idx = data.IndexOf('"', i + 1);
if (idx < 0) return null;
var tok = data.Substring(i + 1, idx - i - 1);
i = idx + 1;
return tok;
}
if (data[i] > 32)
{
// Not whitespace
var s = "";
while (data[i] > 32)
{
s += data[i++];
}
return s;
}
return null;
}
bool ScanToNonWhitespace()
{
while (i < data.Length)
{
if (data[i] == ' ' || data[i] == '\n') i++;
else return true;
}
return false;
}
void SetKeyValue(Entity e, string k, string v)
{
e.KeyValues[k] = v;
switch (k)
{
case "classname":
e.ClassName = v;
break;
case "model":
if (int.TryParse(v.Substring(1), out var m)) e.Model = m;
break;
}
}
}
public void PostReadProcess(BspFile bsp)
{
}
public void PreWriteProcess(BspFile bsp, Version version)
{
}
public int Write(BinaryWriter bw, Version version)
{
var pos = bw.BaseStream.Position;
var sb = new StringBuilder();
foreach (var entity in _entities)
{
sb.Append("{\n");
if (entity.Model > 0)
{
sb.Append($"\"model\" \"*{entity.Model}\"\n");
}
foreach (var kv in entity.KeyValues.Where(x => x.Key?.Length > 0 && x.Value?.Length > 0))
{
sb.Append($"\"{kv.Key}\" \"{kv.Value}\"\n");
}
if (entity.ClassName?.Length > 0)
{
sb.Append($"\"classname\" \"{entity.ClassName}\"\n");
}
sb.Append("}\n");
}
bw.Write(Encoding.ASCII.GetBytes(sb.ToString()));
return (int)(bw.BaseStream.Position - pos);
}
#region IList
public IEnumerator<Entity> GetEnumerator()
{
return _entities.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _entities).GetEnumerator();
}
public void Add(Entity item)
{
_entities.Add(item);
}
public void Clear()
{
_entities.Clear();
}
public bool Contains(Entity item)
{
return _entities.Contains(item);
}
public void CopyTo(Entity[] array, int arrayIndex)
{
_entities.CopyTo(array, arrayIndex);
}
public bool Remove(Entity item)
{
return _entities.Remove(item);
}
public int Count => _entities.Count;
public bool IsReadOnly => _entities.IsReadOnly;
public int IndexOf(Entity item)
{
return _entities.IndexOf(item);
}
public void Insert(int index, Entity item)
{
_entities.Insert(index, item);
}
public void RemoveAt(int index)
{
_entities.RemoveAt(index);
}
public Entity this[int index]
{
get => _entities[index];
set => _entities[index] = value;
}
#endregion
}
}