More data object creation and test commands

This commit is contained in:
2025-11-30 23:19:18 -06:00
parent b40878a489
commit 678b762df5
8 changed files with 67 additions and 79 deletions

View File

@@ -29,7 +29,7 @@ public class Datapack
_path = Path.Combine(AppManager.DatapacksDirectory, Metadata.Id);
}
public void SavePack()
public void Save()
{
if (!Directory.Exists(_path))
@@ -48,12 +48,16 @@ public class Datapack
public void AddDataset(Dataset dataset)
{
dataset.Save();
dataset.Save($"{_path}/datasets/");
_datasetLookup.TryAdd(dataset.Metadata.Id, dataset);
}
public void AddTemplate(SzTemplate template)
{
_templateLookup.TryAdd(template.Id, template);
var success = _templateLookup.TryAdd(template.Id, template);
if (success)
{
File.WriteAllText($"{_path}/templates/{template.Id}.sztl", template.SztlText);
}
}
}

View File

@@ -8,7 +8,8 @@ public class Dataset
public DatasetMetadata Metadata { get; private set; }
private Dictionary<string, SzDataObject> _objectLookup = [];
private readonly string _path;
private string _path = "";
public Dataset(string name, string id, string dataType, string templateId, string iconPath = "", string description = "")
{
@@ -21,26 +22,31 @@ public class Dataset
ObjectTemplateId = templateId,
Icon = iconPath
};
_path = Path.Combine(AppManager.DatapacksDirectory, "datasets", id);
}
public void Save()
public void Save(string path)
{
if (!Directory.Exists(_path))
_path = path;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(_path);
Directory.CreateDirectory($"{_path}/objects");
Directory.CreateDirectory(path);
}
Directory.CreateDirectory(Path.Combine(path, "objects"));
if (!File.Exists($"{_path}/dataset.meta"))
if (!File.Exists($"{path}/dataset.meta"))
{
File.WriteAllText($"{_path}/dataset.meta", Metadata.ToString());
File.WriteAllText($"{path}/dataset.meta", Metadata.ToString());
}
}
public void AddObject(SzDataObject obj)
{
_objectLookup.TryAdd(obj.Id, obj);
var success = _objectLookup.TryAdd(obj.Id, obj);
if (success)
{
File.WriteAllText($"{_path}/objects/{obj.Id}.szo", obj.ToString());
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using SessionZero.Data.Sztl;
namespace SessionZero.Data;
@@ -93,4 +94,9 @@ public class SzObject
Fields[lastPart].Value = value;
}
}
public override string ToString()
{
return JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
}
}

View File

@@ -18,7 +18,7 @@ namespace SessionZero.Data
obj.TemplateId = template.Id;
obj.Id = ""; // TODO: This will be replaces with the top level "id" field whenever it gets filled in in the UI
obj.Uuid = "0"; // TODO: Generate true UUIDs later
obj.Uuid = Guid.NewGuid().ToString();
foreach (var field in template.Fields)
{

View File

@@ -13,9 +13,12 @@ public class SzTemplate
public List<string> CompatibleSystems { get; set; }
public string Description { get; set; }
public string SztlText { get; set; } = "";
public Dictionary<string, SztlField> Fields { get; set; } = new();
public Dictionary<string, SztlFieldGroup> SubGroups { get; set; } = new();
public virtual void ParseAdditionalMetaData(TomlTable table)
{

View File

@@ -30,6 +30,8 @@ public static class SztlParser
_ => throw new NotImplementedException($"Unknown template_type '{templateType}'")
};
template.SztlText = tomlText.Trim();
if (metadata.TryGetValue("id", out var idVal)) template.Id = idVal?.ToString()!;
else throw new Exception("Template missing id metadata");