adding saving to Datapacks and datasets
This commit is contained in:
59
SessionZero/Data/Datapack.cs
Normal file
59
SessionZero/Data/Datapack.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Avalonia.Markup.Xaml.Templates;
|
||||
|
||||
namespace SessionZero.Data;
|
||||
|
||||
public class Datapack
|
||||
{
|
||||
public DatapackMetadata Metadata { get; set; }
|
||||
|
||||
private readonly string _path;
|
||||
private Dictionary<string, Dataset> _datasetLookup = [];
|
||||
private Dictionary<string, SzTemplate> _templateLookup = [];
|
||||
|
||||
|
||||
public Datapack(string name, string id, string version = "1.0.0", string[]? compatibleSystems = null, string description = "")
|
||||
{
|
||||
Metadata = new()
|
||||
{
|
||||
Name = name,
|
||||
Id = id,
|
||||
Version = version,
|
||||
CompatibleSystems = compatibleSystems,
|
||||
Description = description,
|
||||
Uuid = Guid.NewGuid().ToString()
|
||||
};
|
||||
|
||||
_path = Path.Combine(AppManager.DatapacksDirectory, Metadata.Id);
|
||||
}
|
||||
|
||||
public void SavePack()
|
||||
{
|
||||
|
||||
if (!Directory.Exists(_path))
|
||||
{
|
||||
Directory.CreateDirectory(_path);
|
||||
Directory.CreateDirectory($"{_path}/resources");
|
||||
Directory.CreateDirectory($"{_path}/datasets");
|
||||
Directory.CreateDirectory($"{_path}/templates");
|
||||
}
|
||||
|
||||
if (!File.Exists($"{_path}/datapack.meta"))
|
||||
{
|
||||
File.WriteAllText($"{_path}/datapack.meta", Metadata.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void AddDataset(Dataset dataset)
|
||||
{
|
||||
dataset.Save();
|
||||
_datasetLookup.TryAdd(dataset.Metadata.Id, dataset);
|
||||
}
|
||||
|
||||
public void AddTemplate(SzTemplate template)
|
||||
{
|
||||
_templateLookup.TryAdd(template.Id, template);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SessionZero.Data;
|
||||
|
||||
@@ -10,4 +12,10 @@ public class DatapackMetadata
|
||||
public required string Version { get; set; }
|
||||
public required string[] CompatibleSystems { get; set; }
|
||||
public string Uuid { get; set; } = string.Empty;
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
||||
}
|
||||
}
|
||||
46
SessionZero/Data/Dataset.cs
Normal file
46
SessionZero/Data/Dataset.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace SessionZero.Data;
|
||||
|
||||
public class Dataset
|
||||
{
|
||||
public DatasetMetadata Metadata { get; private set; }
|
||||
|
||||
private Dictionary<string, SzDataObject> _objectLookup = [];
|
||||
private readonly string _path;
|
||||
|
||||
public Dataset(string name, string id, string dataType, string templateId, string iconPath = "", string description = "")
|
||||
{
|
||||
Metadata = new()
|
||||
{
|
||||
Name = name,
|
||||
Id = id,
|
||||
DatasetType = dataType,
|
||||
Description = description,
|
||||
ObjectTemplateId = templateId,
|
||||
Icon = iconPath
|
||||
};
|
||||
|
||||
_path = Path.Combine(AppManager.DatapacksDirectory, "datasets", id);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
if (!Directory.Exists(_path))
|
||||
{
|
||||
Directory.CreateDirectory(_path);
|
||||
Directory.CreateDirectory($"{_path}/objects");
|
||||
}
|
||||
|
||||
if (!File.Exists($"{_path}/dataset.meta"))
|
||||
{
|
||||
File.WriteAllText($"{_path}/dataset.meta", Metadata.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void AddObject(SzDataObject obj)
|
||||
{
|
||||
_objectLookup.TryAdd(obj.Id, obj);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SessionZero.Data;
|
||||
|
||||
public class DatasetMetadata
|
||||
@@ -6,6 +8,11 @@ public class DatasetMetadata
|
||||
public required string Id { get; set; }
|
||||
public required string Description { get; set; }
|
||||
public required string DatasetType { get; set; }
|
||||
public required string Icon { get; set; }
|
||||
public required string ObjectTemplate { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public required string ObjectTemplateId { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user