More data object creation and test commands
This commit is contained in:
@@ -17,7 +17,7 @@ public static class AppManager
|
|||||||
public static ConsoleControl ConsoleControl { get; } = new();
|
public static ConsoleControl ConsoleControl { get; } = new();
|
||||||
public static AppSettings Settings { 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 string DatapacksDirectory => Path.Combine(DataDirectory, "datapacks");
|
||||||
|
|
||||||
public static void Init()
|
public static void Init()
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class Datapack
|
|||||||
_path = Path.Combine(AppManager.DatapacksDirectory, Metadata.Id);
|
_path = Path.Combine(AppManager.DatapacksDirectory, Metadata.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SavePack()
|
public void Save()
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!Directory.Exists(_path))
|
if (!Directory.Exists(_path))
|
||||||
@@ -48,12 +48,16 @@ public class Datapack
|
|||||||
|
|
||||||
public void AddDataset(Dataset dataset)
|
public void AddDataset(Dataset dataset)
|
||||||
{
|
{
|
||||||
dataset.Save();
|
dataset.Save($"{_path}/datasets/");
|
||||||
_datasetLookup.TryAdd(dataset.Metadata.Id, dataset);
|
_datasetLookup.TryAdd(dataset.Metadata.Id, dataset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddTemplate(SzTemplate template)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,8 @@ public class Dataset
|
|||||||
public DatasetMetadata Metadata { get; private set; }
|
public DatasetMetadata Metadata { get; private set; }
|
||||||
|
|
||||||
private Dictionary<string, SzDataObject> _objectLookup = [];
|
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 = "")
|
public Dataset(string name, string id, string dataType, string templateId, string iconPath = "", string description = "")
|
||||||
{
|
{
|
||||||
@@ -21,26 +22,31 @@ public class Dataset
|
|||||||
ObjectTemplateId = templateId,
|
ObjectTemplateId = templateId,
|
||||||
Icon = iconPath
|
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);
|
||||||
Directory.CreateDirectory($"{_path}/objects");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!File.Exists($"{_path}/dataset.meta"))
|
Directory.CreateDirectory(Path.Combine(path, "objects"));
|
||||||
|
|
||||||
|
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)
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json;
|
||||||
using SessionZero.Data.Sztl;
|
using SessionZero.Data.Sztl;
|
||||||
|
|
||||||
namespace SessionZero.Data;
|
namespace SessionZero.Data;
|
||||||
@@ -93,4 +94,9 @@ public class SzObject
|
|||||||
Fields[lastPart].Value = value;
|
Fields[lastPart].Value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ namespace SessionZero.Data
|
|||||||
|
|
||||||
obj.TemplateId = template.Id;
|
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.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)
|
foreach (var field in template.Fields)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,9 +13,12 @@ public class SzTemplate
|
|||||||
public List<string> CompatibleSystems { get; set; }
|
public List<string> CompatibleSystems { get; set; }
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
public string SztlText { get; set; } = "";
|
||||||
|
|
||||||
public Dictionary<string, SztlField> Fields { get; set; } = new();
|
public Dictionary<string, SztlField> Fields { get; set; } = new();
|
||||||
public Dictionary<string, SztlFieldGroup> SubGroups { get; set; } = new();
|
public Dictionary<string, SztlFieldGroup> SubGroups { get; set; } = new();
|
||||||
|
|
||||||
|
|
||||||
public virtual void ParseAdditionalMetaData(TomlTable table)
|
public virtual void ParseAdditionalMetaData(TomlTable table)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ public static class SztlParser
|
|||||||
_ => throw new NotImplementedException($"Unknown template_type '{templateType}'")
|
_ => throw new NotImplementedException($"Unknown template_type '{templateType}'")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template.SztlText = tomlText.Trim();
|
||||||
|
|
||||||
if (metadata.TryGetValue("id", out var idVal)) template.Id = idVal?.ToString()!;
|
if (metadata.TryGetValue("id", out var idVal)) template.Id = idVal?.ToString()!;
|
||||||
else throw new Exception("Template missing id metadata");
|
else throw new Exception("Template missing id metadata");
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Cogwheel;
|
using Cogwheel;
|
||||||
using SessionZero.Data;
|
using SessionZero.Data;
|
||||||
@@ -9,18 +10,21 @@ namespace SessionZero;
|
|||||||
|
|
||||||
public static class TestingCommands
|
public static class TestingCommands
|
||||||
{
|
{
|
||||||
|
private static Datapack pack;
|
||||||
|
private static Dataset set;
|
||||||
|
private static SzTemplate template;
|
||||||
|
private static SzDataObject obj;
|
||||||
|
|
||||||
|
|
||||||
[Command(Name = "createpack")]
|
[Command(Name = "createpack")]
|
||||||
private static void CreateTestPack()
|
private static void CreateTestPack()
|
||||||
{
|
{
|
||||||
var pack = new Datapack("SessionZero Core", "sz_core");
|
pack = new Datapack("SessionZero Core", "sz_core");
|
||||||
pack.SavePack();
|
pack.Save();
|
||||||
}
|
|
||||||
|
|
||||||
|
set = new Dataset("Core Items", "core_items", "item", "core_item");
|
||||||
|
pack.AddDataset(set);
|
||||||
|
|
||||||
// TODO: Remove this after testing
|
|
||||||
[Command(Name = "testdata")]
|
|
||||||
private static void TestParseTemplate()
|
|
||||||
{
|
|
||||||
string toml = @"
|
string toml = @"
|
||||||
[metadata]
|
[metadata]
|
||||||
template_type = ""data""
|
template_type = ""data""
|
||||||
@@ -53,75 +57,38 @@ public static class TestingCommands
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var template = SztlParser.ParseTemplate(toml);
|
template = SztlParser.ParseTemplate(toml);
|
||||||
|
pack.AddTemplate(template);
|
||||||
|
|
||||||
var obj = SzObjectFactory.CreateObject(template) as SzDataObject;
|
try
|
||||||
|
|
||||||
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" })
|
|
||||||
{
|
{
|
||||||
COGWHEEL.Log($"> {fieldPath} = {obj.GetFieldValue(fieldPath)}");
|
obj = SzObjectFactory.CreateObject(template) as SzDataObject;
|
||||||
|
obj.Id = "test_item";
|
||||||
|
set.AddObject(obj);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
var jsonOptions = new JsonSerializerOptions
|
|
||||||
{
|
{
|
||||||
WriteIndented = true
|
COGWHEEL.LogError($"Error parsing template: {e.Message}");
|
||||||
};
|
}
|
||||||
string json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
obj.Id,
|
|
||||||
obj.Uuid,
|
|
||||||
template_id = obj.TemplateId,
|
|
||||||
fields = obj.FieldsToDictionary()
|
|
||||||
}, jsonOptions);
|
|
||||||
|
|
||||||
COGWHEEL.Log("\nJSON Output:");
|
|
||||||
COGWHEEL.Log(json);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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)
|
|
||||||
{
|
|
||||||
var result = new Dictionary<string, object>();
|
|
||||||
foreach (var kvp in obj.Fields)
|
|
||||||
{
|
|
||||||
result[kvp.Key] = kvp.Value.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var group in obj.FieldGroups)
|
[Command(Name = "testdata")]
|
||||||
|
private static void TestParseTemplate()
|
||||||
{
|
{
|
||||||
result[group.Key] = GroupToDict(group.Value);
|
try
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Dictionary<string, object> GroupToDict(SzFieldGroup group)
|
|
||||||
{
|
{
|
||||||
var dict = new Dictionary<string, object>();
|
var objFromJson = JsonSerializer.Deserialize<SzObject>(File.ReadAllText($"{AppManager.DatapacksDirectory}/sz_core/datasets/objects/test_item.szo"));
|
||||||
foreach (var kvp in group.Fields)
|
COGWHEEL.Log($"The object's id is {objFromJson.Id}");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
dict[kvp.Key] = kvp.Value.Value;
|
COGWHEEL.LogError($"Error deserializing object: {e.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var subGroup in group.SubGroups)
|
|
||||||
{
|
|
||||||
dict[subGroup.Key] = GroupToDict(subGroup.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return dict;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user