SessionZero.SzfToolkit-Dotnet/SessionZero.SzfLib/Generator/SzfGenerator.cs

59 lines
1.5 KiB
C#

using System.Text;
using SessionZero.SzfLib.File;
using SessionZero.SzfLib.Objects;
using SessionZero.SzfLib.Parser;
namespace SessionZero.SzfLib.Generator;
public class SzfGenerator : ISzfGenerator
{
private StringBuilder? _builder;
public SzfFile GenerateSzfFile(SzfObject szfObj)
{
_builder = new();
_builder.AppendLine($"!type: {szfObj.SzfType}");
_builder.AppendLine($"!schema: {szfObj.SzfVersion}");
_builder.AppendLine();
foreach (var section in szfObj.Sections)
{
WriteSection(section);
}
SzfFile szfFile = new()
{
SzfType = szfObj.SzfType,
Schema = szfObj.SzfVersion,
Name = szfObj.GetMetadataField("Name"),
Guid = szfObj.GetMetadataField("Guid"),
Version = szfObj.GetMetadataField("Version"),
Content = _builder.ToString()
};
return szfFile;
}
private void WriteSection(SzfSection section, string parentPath = "")
{
var sectionName = string.IsNullOrEmpty(parentPath) ? section.Name : $"{parentPath}.{section.Name}";
_builder?.AppendLine($"[{sectionName}]");
foreach (var field in section.Fields)
{
_builder?.AppendLine($"{field.Key} ({field.Value.SzfType}) = {field.Value.Value}");
}
_builder?.AppendLine();
foreach (var subsection in section.Subsections.Values)
{
WriteSection(subsection, sectionName);
}
}
}