LogicAndTrick/sledge-formats

BSP Entities lump writes "classname" and "model" keys twice on write

SamVanheer opened this issue · 0 comments

When a BSP file's entities lump is written it writes the classname and model keys twice. This is because it writes both all keyvalues and these keyvalues explicitly:

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");
}

Both keyvalues are added to the list of keyvalues on load:

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;
}
}

It should use one or the other to save these keyvalues. It might be better to remove the explicit keyvalue properties for classname and model and rely solely on the keyvalues dictionary, since users will likely have their own wrapper around this data.