Templates

This commit is contained in:
2026-01-26 10:24:16 -06:00
parent 5bde8db8cf
commit 523cd2104f
6 changed files with 181 additions and 62 deletions

View File

@@ -1,3 +1,4 @@
using System.ComponentModel.Design;
using SzCore.DataObjects;
namespace SzCore;
@@ -5,6 +6,7 @@ namespace SzCore;
public class SzDataHandler
{
private Dictionary<string, SzDataset> _loadedDatasets = [];
private Dictionary<string, ISzTemplate> _loadedTemplates = [];
public SzDataHandler(){}
@@ -26,7 +28,7 @@ public class SzDataHandler
{
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
{
if (value is not null) return value;
return value;
}
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
@@ -49,19 +51,62 @@ public class SzDataHandler
return parsedDataset;
}
// The client should periodically call this to free memory, probably
private void ClearLoadedDatasets()
public bool SaveTemplate(ISzTemplate template)
{
_loadedDatasets.Clear();
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
try
{
return SZ.LocalFileManager.SaveFile(templatePath, SZ.SzParser.SerializeTemplateToJson(template));
}
catch (Exception e)
{
SZ.Logger.LogError("Error saving template: " + e.Message);
return false;
}
}
// Only use this for test purposes, we probably don't want to load all datasets to memory in production
public T? LoadTemplate<T>(string templateId) where T : ISzTemplate
{
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? existing))
{
return (T)existing;
}
var templatePah = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId, "template.json");
var json = SZ.LocalFileManager.LoadFile(templatePah);
if (json is null)
{
SZ.Logger.LogWarning($"Could not load tetmplate with ID '{templateId}'");
return default;
}
var parsedTemplate = SZ.SzParser.DeserializeTemplate<T>(json);
if (parsedTemplate is null)
{
SZ.Logger.LogWarning($"Could not load template with ID '{templateId}'");
return default;
}
_loadedTemplates.TryAdd(parsedTemplate.Id, parsedTemplate);
return parsedTemplate;
}
// The client should periodically call this to free memory, probably
private void ClearCache()
{
_loadedDatasets.Clear();
_loadedTemplates.Clear();
}
// Only use these two methods for test purposes, we probably don't want to load all datasets and templates to memory in production
private void LoadAllDatasets()
{
ClearLoadedDatasets();
_loadedDatasets.Clear();
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
{
throw new Exception("Could not load datasets, the datase directory does not exist.");
throw new Exception("Could not load datasets, the dataset directory does not exist.");
}
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
@@ -82,4 +127,31 @@ public class SzDataHandler
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
}
}
private void LoadAllTemplates()
{
_loadedTemplates.Clear();
if (!Directory.Exists(SZ.LocalFileManager.TemplatesPath))
{
throw new Exception("Could not load templates, the template directory does not exist.");
}
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.TemplatesPath))
{
var templateFilePath = Path.Combine(dir, "template.json");
if (!File.Exists(templateFilePath)) continue;
var templateJson = SZ.LocalFileManager.LoadFile(templateFilePath);
if (templateJson == null) continue;
var parsedTemplate = SZ.SzParser.DeserializeTemplate<ISzTemplate>(templateJson);
if (parsedTemplate == null)
{
SZ.Logger.LogWarning($"Parse error: Could not parse file {templateFilePath} as an ISzTemplate");
continue;
}
_loadedTemplates.Add(parsedTemplate.Id, parsedTemplate);
}
}
}