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

@@ -17,7 +17,7 @@ public static class AppManager
public static ConsoleControl ConsoleControl { get; } = new();
public static AppSettings Settings { get; } = new();
public static string DataDirectory => Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "data");
public static string DataDirectory => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
public static string DatapacksDirectory => Path.Combine(DataDirectory, "datapacks");
public static void Init()

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");

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Cogwheel;
using SessionZero.Data;
@@ -9,18 +10,21 @@ namespace SessionZero;
public static class TestingCommands
{
private static Datapack pack;
private static Dataset set;
private static SzTemplate template;
private static SzDataObject obj;
[Command(Name = "createpack")]
private static void CreateTestPack()
{
var pack = new Datapack("SessionZero Core", "sz_core");
pack.SavePack();
}
// TODO: Remove this after testing
[Command(Name = "testdata")]
private static void TestParseTemplate()
{
pack = new Datapack("SessionZero Core", "sz_core");
pack.Save();
set = new Dataset("Core Items", "core_items", "item", "core_item");
pack.AddDataset(set);
string toml = @"
[metadata]
template_type = ""data""
@@ -53,75 +57,38 @@ public static class TestingCommands
try
{
var template = SztlParser.ParseTemplate(toml);
template = SztlParser.ParseTemplate(toml);
pack.AddTemplate(template);
var obj = SzObjectFactory.CreateObject(template) as SzDataObject;
obj.SetFieldValue("name", "Excalibur");
obj.SetFieldValue("consumable", true);
obj.SetFieldValue("stats.value", 10);
obj.SetFieldValue("stats.weight", 2);
obj.SetFieldValue("stats.modifiers.base", 5);
COGWHEEL.Log($"Template ID: {template.Id}");
COGWHEEL.Log("\nInstanced Object Field Values:");
foreach (var fieldPath in new[]
{ "name", "consumable", "stats.value", "stats.weight", "stats.modifiers.base" })
try
{
COGWHEEL.Log($"> {fieldPath} = {obj.GetFieldValue(fieldPath)}");
obj = SzObjectFactory.CreateObject(template) as SzDataObject;
obj.Id = "test_item";
set.AddObject(obj);
}
var jsonOptions = new JsonSerializerOptions
catch (Exception e)
{
WriteIndented = true
};
string json = JsonSerializer.Serialize(new
{
obj.Id,
obj.Uuid,
template_id = obj.TemplateId,
fields = obj.FieldsToDictionary()
}, jsonOptions);
COGWHEEL.Log("\nJSON Output:");
COGWHEEL.Log(json);
COGWHEEL.LogError($"Error parsing template: {e.Message}");
}
}
catch (Exception ex)
{
COGWHEEL.Log($"Error parsing template: {ex.Message}");
COGWHEEL.LogError($"Error adding template: {ex.Message}");
}
}
private static Dictionary<string, object> FieldsToDictionary(this SzObject obj)
[Command(Name = "testdata")]
private static void TestParseTemplate()
{
var result = new Dictionary<string, object>();
foreach (var kvp in obj.Fields)
try
{
result[kvp.Key] = kvp.Value.Value;
var objFromJson = JsonSerializer.Deserialize<SzObject>(File.ReadAllText($"{AppManager.DatapacksDirectory}/sz_core/datasets/objects/test_item.szo"));
COGWHEEL.Log($"The object's id is {objFromJson.Id}");
}
foreach (var group in obj.FieldGroups)
catch (Exception e)
{
result[group.Key] = GroupToDict(group.Value);
COGWHEEL.LogError($"Error deserializing object: {e.Message}");
}
return result;
}
private static Dictionary<string, object> GroupToDict(SzFieldGroup group)
{
var dict = new Dictionary<string, object>();
foreach (var kvp in group.Fields)
{
dict[kvp.Key] = kvp.Value.Value;
}
foreach (var subGroup in group.SubGroups)
{
dict[subGroup.Key] = GroupToDict(subGroup.Value);
}
return dict;
}
}