157 lines
5.1 KiB
C#
157 lines
5.1 KiB
C#
using System.ComponentModel.Design;
|
|
using SzCore.DataObjects;
|
|
|
|
namespace SzCore;
|
|
|
|
public class SzDataHandler
|
|
{
|
|
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
|
private Dictionary<string, ISzTemplate> _loadedTemplates = [];
|
|
|
|
public SzDataHandler(){}
|
|
|
|
public bool SaveDataset(SzDataset dataset)
|
|
{
|
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
|
try
|
|
{
|
|
return SZ.LocalFileManager.SaveFile(datasetPath, SZ.SzParser.SerializeDatasetToJson(dataset));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public SzDataset? LoadDataset(string datasetId)
|
|
{
|
|
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
|
|
|
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
|
if (json is null)
|
|
{
|
|
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
|
return null;
|
|
}
|
|
|
|
var parsedDataset = SZ.SzParser.DeserializeDataset(json);
|
|
if (parsedDataset is null)
|
|
{
|
|
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
|
return null;
|
|
}
|
|
|
|
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
|
return parsedDataset;
|
|
}
|
|
|
|
public bool SaveTemplate(ISzTemplate template)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
_loadedDatasets.Clear();
|
|
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
|
{
|
|
throw new Exception("Could not load datasets, the dataset directory does not exist.");
|
|
}
|
|
|
|
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
|
{
|
|
var datasetFilePath = Path.Combine(dir, "dataset.json");
|
|
if (!File.Exists(datasetFilePath)) continue;
|
|
|
|
var datasetJson = SZ.LocalFileManager.LoadFile(datasetFilePath);
|
|
if (datasetJson == null) continue;
|
|
|
|
var parsedDataset = SZ.SzParser.DeserializeDataset(datasetJson);
|
|
if (parsedDataset == null)
|
|
{
|
|
SZ.Logger.LogWarning($"Parse error: Could not parse file {datasetFilePath} as an SzDataset");
|
|
continue;
|
|
}
|
|
|
|
_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);
|
|
}
|
|
}
|
|
} |