382 lines
12 KiB
Plaintext
382 lines
12 KiB
Plaintext
@page "/szf-parser-test"
|
|
@using SessionZero.Data
|
|
@using Microsoft.Extensions.Logging
|
|
@inject SzfParser SzfParser
|
|
@inject ILogger<SzfParser> Logger
|
|
@inject SzfGenerator SzfGenerator
|
|
|
|
<h1>SZF Parser and Generator Test Page</h1>
|
|
|
|
<div class="test-container">
|
|
<div class="input-section">
|
|
<h2>SZF Input</h2>
|
|
<div>
|
|
<textarea id="szf-input" @bind="szfCode" rows="20"></textarea>
|
|
</div>
|
|
<button @onclick="ParseSzf">Parse SZF</button>
|
|
@if (parsedObject != null)
|
|
{
|
|
<button @onclick="GenerateSzf">Generate SZF from Parsed Object</button>
|
|
}
|
|
</div>
|
|
<div class="output-section">
|
|
<h2>Parsed Object Data</h2>
|
|
@if (parsedObject != null)
|
|
{
|
|
<table>
|
|
<tbody>
|
|
<tr><th>Object Type</th><td>@parsedObject.TypeIdentifier</td></tr>
|
|
@if (!string.IsNullOrEmpty(parsedSchemaVersion))
|
|
{
|
|
<tr><th>Schema Version</th><td>@parsedSchemaVersion</td></tr>
|
|
}
|
|
<tr><th>Name</th><td>@parsedObject.Metadata.Name</td></tr>
|
|
<tr><th>Version</th><td>@parsedObject.Metadata.Version</td></tr>
|
|
<tr><th>ID</th><td>@parsedObject.Metadata.Guid</td></tr>
|
|
<tr><th>Author</th><td>@parsedObject.Metadata.Author</td></tr>
|
|
<tr><th>Description</th><td>@parsedObject.Metadata.Description</td></tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
@* Display Dataset specific data *@
|
|
@if (parsedObject is Dataset dataset)
|
|
{
|
|
@if (dataset.Metadata.Any()) // Display Dataset's specific metadata
|
|
{
|
|
<h3>Dataset Specific Metadata</h3>
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Key</th>
|
|
<th>Value</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var meta in dataset.Metadata)
|
|
{
|
|
<tr>
|
|
<td>@meta.Key</td>
|
|
<td>@meta.Value</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
|
|
@if (dataset.Entries.Any())
|
|
{
|
|
<h3>Dataset Entries</h3>
|
|
@foreach (var entry in dataset.Entries)
|
|
{
|
|
<div>
|
|
<h4>Entry: @entry.Name</h4>
|
|
@if (entry.Fields.Any())
|
|
{
|
|
<table>
|
|
<tbody>
|
|
@foreach (var field in entry.Fields)
|
|
{
|
|
<tr>
|
|
<td><strong>@field.Name</strong></td>
|
|
<td>@field.Value</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
@foreach (var section in entry.Sections)
|
|
{
|
|
@RenderDatasetSection(section)
|
|
}
|
|
</div>
|
|
}
|
|
}
|
|
}
|
|
|
|
@* Display CharacterTemplate specific data *@
|
|
@if (parsedObject is CharacterTemplate characterTemplate)
|
|
{
|
|
@if (characterTemplate.RequiredDatasets.Any())
|
|
{
|
|
<h3>Required Datasets</h3>
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Alias</th>
|
|
<th>Name</th>
|
|
<th>GUID</th>
|
|
<th>Version</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var reqDataset in characterTemplate.RequiredDatasets)
|
|
{
|
|
<tr>
|
|
<td>@reqDataset.Alias</td>
|
|
<td>@reqDataset.Reference?.Name</td>
|
|
<td>@reqDataset.Reference?.Guid</td>
|
|
<td>@reqDataset.Reference?.Version</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
|
|
@if (characterTemplate.Sections.Any())
|
|
{
|
|
<h3>Template Sections</h3>
|
|
@foreach (var section in characterTemplate.Sections)
|
|
{
|
|
@RenderTemplateSection(section)
|
|
}
|
|
}
|
|
}
|
|
|
|
@* Display Character specific data *@
|
|
@if (parsedObject is Character character)
|
|
{
|
|
@if (character.CharacterSections.Any())
|
|
{
|
|
<h3>Character Sections</h3>
|
|
@foreach (var section in character.CharacterSections)
|
|
{
|
|
@RenderCharacterSection(section)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@if (errorMessages.Any())
|
|
{
|
|
<div>
|
|
<h4>Errors</h4>
|
|
<ul>
|
|
@foreach (var error in errorMessages)
|
|
{
|
|
<li>@error</li>
|
|
}
|
|
</ul>
|
|
</div>
|
|
}
|
|
</div>
|
|
<div class="generated-output-section">
|
|
<h2>Generated SZF Output</h2>
|
|
@if (!string.IsNullOrEmpty(generatedSzfOutput))
|
|
{
|
|
<textarea id="generated-szf-output" @bind="generatedSzfOutput" rows="20" readonly></textarea>
|
|
}
|
|
else
|
|
{
|
|
<p>No SZF has been generated yet.</p>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
<div class="logs-section">
|
|
<h2>Logs</h2>
|
|
<pre>@string.Join("\n", logMessages)</pre>
|
|
</div>
|
|
|
|
<style>
|
|
.test-container {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr 1fr; /* Three columns for input, parsed data, generated output */
|
|
gap: 20px;
|
|
}
|
|
|
|
.input-section, .output-section, .generated-output-section {
|
|
border: 1px solid #ccc;
|
|
padding: 15px;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
textarea {
|
|
width: 100%;
|
|
box-sizing: border-box; /* Include padding and border in the element's total width and height */
|
|
}
|
|
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin-top: 10px;
|
|
}
|
|
|
|
th, td {
|
|
border: 1px solid #ddd;
|
|
padding: 8px;
|
|
text-align: left;
|
|
}
|
|
|
|
th {
|
|
background-color: #f2f2f2;
|
|
}
|
|
|
|
.logs-section {
|
|
margin-top: 20px;
|
|
border: 1px solid #ccc;
|
|
padding: 15px;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
pre {
|
|
white-space: pre-wrap; /* Ensures logs wrap */
|
|
word-wrap: break-word;
|
|
}
|
|
</style>
|
|
|
|
@code {
|
|
private string szfCode = string.Empty;
|
|
private SzfObject? parsedObject;
|
|
private string? parsedSchemaVersion; // To store the schema version from parsing
|
|
private string generatedSzfOutput = string.Empty; // New field for generated output
|
|
private List<string> logMessages = new List<string>();
|
|
private List<string> errorMessages = new List<string>();
|
|
|
|
private void ParseSzf()
|
|
{
|
|
ClearState(); // Clear previous states
|
|
Log("Starting SZF parsing...");
|
|
|
|
if (string.IsNullOrWhiteSpace(szfCode))
|
|
{
|
|
const string message = "SZF code input is empty.";
|
|
LogError(message);
|
|
errorMessages.Add(message);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var parseResult = SzfParser.ParseSzfStructure(szfCode.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList());
|
|
|
|
// Create the SzfObject instance from the parseResult
|
|
parsedObject = SzfParser.CreateInstanceFromTypeIdentifier(parseResult.ObjectTypeIdentifier);
|
|
SzfParser.PopulateSzfObject(parsedObject, parseResult);
|
|
|
|
parsedSchemaVersion = parseResult.SchemaVersion; // Store the parsed schema version
|
|
|
|
Log("SZF content parsed successfully.");
|
|
Log($"Object Type: {parsedObject.TypeIdentifier}");
|
|
Log($"Schema Version: {parsedSchemaVersion}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
const string message = "An error occurred during parsing.";
|
|
LogError(message, ex);
|
|
errorMessages.Add($"{message} See logs for details.");
|
|
}
|
|
}
|
|
|
|
private void GenerateSzf()
|
|
{
|
|
generatedSzfOutput = string.Empty; // Clear previous generated output
|
|
if (parsedObject == null)
|
|
{
|
|
Log("No object to generate from. Please parse an SZF first.");
|
|
return;
|
|
}
|
|
|
|
Log("Starting SZF generation...");
|
|
try
|
|
{
|
|
// Use the SzfGenerator to generate the string
|
|
generatedSzfOutput = SzfGenerator.Generate(parsedObject);
|
|
Log("SZF content generated successfully.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
const string message = "An error occurred during generation.";
|
|
LogError(message, ex);
|
|
errorMessages.Add($"{message} See logs for details.");
|
|
}
|
|
}
|
|
|
|
private void ClearState()
|
|
{
|
|
logMessages.Clear();
|
|
errorMessages.Clear();
|
|
parsedObject = null;
|
|
parsedSchemaVersion = null; // Clear parsed schema version
|
|
generatedSzfOutput = string.Empty;
|
|
}
|
|
|
|
private void Log(string message)
|
|
{
|
|
Logger.LogInformation(message);
|
|
logMessages.Add($"[INFO] {message}");
|
|
}
|
|
|
|
private void LogError(string message, Exception? ex = null)
|
|
{
|
|
Logger.LogError(ex, message);
|
|
logMessages.Add($"[ERROR] {message}{(ex is not null ? $": {ex.Message}" : "")}");
|
|
}
|
|
|
|
// Helper for rendering Dataset sections (retained from original, adapted for basic HTML)
|
|
private RenderFragment RenderDatasetSection(DatasetSection dataSection) =>@<div>
|
|
<strong>@dataSection.Name</strong>
|
|
@if (dataSection.Fields.Any())
|
|
{
|
|
<table>
|
|
<tbody>
|
|
@foreach (var field in dataSection.Fields)
|
|
{
|
|
<tr>
|
|
<td><strong>@field.Name</strong></td>
|
|
<td>@field.Value</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
@foreach (var subSection in dataSection.Subsections)
|
|
{
|
|
@RenderDatasetSection(subSection)
|
|
}
|
|
</div>;
|
|
|
|
// Helper for rendering CharacterTemplate sections
|
|
private RenderFragment RenderTemplateSection(TemplateSection templateSection) =>@<div>
|
|
<strong>@templateSection.Name</strong>
|
|
@if (templateSection.Fields.Any())
|
|
{
|
|
<table>
|
|
<tbody>
|
|
@foreach (var field in templateSection.Fields)
|
|
{
|
|
<tr>
|
|
<td><strong>@field.Name</strong></td>
|
|
<td>@field.Value</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
@foreach (var subSection in templateSection.Subsections)
|
|
{
|
|
@RenderTemplateSection(subSection)
|
|
}
|
|
</div>;
|
|
|
|
// New helper for rendering Character sections
|
|
private RenderFragment RenderCharacterSection(CharacterSection characterSection) =>@<div>
|
|
<h4>@characterSection.Name</h4>
|
|
@if (characterSection.Fields.Any())
|
|
{
|
|
<table>
|
|
<tbody>
|
|
@foreach (var field in characterSection.Fields)
|
|
{
|
|
<tr>
|
|
<td><strong>@field.Name</strong></td>
|
|
<td>@field.Value</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
@foreach (var subSection in characterSection.Subsections)
|
|
{
|
|
@RenderCharacterSection(subSection)
|
|
}
|
|
</div>;
|
|
} |