adding saving to Datapacks and datasets
This commit is contained in:
@@ -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.Cogwheel;
|
using SessionZero.Cogwheel;
|
||||||
@@ -15,6 +16,27 @@ public static class AppManager
|
|||||||
public static CommandsManager CommandsManager { get; } = new();
|
public static CommandsManager CommandsManager { get; } = new();
|
||||||
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 DatapacksDirectory => Path.Combine(DataDirectory, "datapacks");
|
||||||
|
|
||||||
|
public static void Init()
|
||||||
|
{
|
||||||
|
VerifyDataDirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void VerifyDataDirs()
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(DataDirectory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(DataDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Directory.Exists(DatapacksDirectory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(DatapacksDirectory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void ShowConsole()
|
public static void ShowConsole()
|
||||||
{
|
{
|
||||||
|
|||||||
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.Collections.Generic;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace SessionZero.Data;
|
namespace SessionZero.Data;
|
||||||
|
|
||||||
@@ -10,4 +12,10 @@ public class DatapackMetadata
|
|||||||
public required string Version { get; set; }
|
public required string Version { get; set; }
|
||||||
public required string[] CompatibleSystems { get; set; }
|
public required string[] CompatibleSystems { get; set; }
|
||||||
public string Uuid { get; set; } = string.Empty;
|
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;
|
namespace SessionZero.Data;
|
||||||
|
|
||||||
public class DatasetMetadata
|
public class DatasetMetadata
|
||||||
@@ -6,6 +8,11 @@ public class DatasetMetadata
|
|||||||
public required string Id { get; set; }
|
public required string Id { get; set; }
|
||||||
public required string Description { get; set; }
|
public required string Description { get; set; }
|
||||||
public required string DatasetType { get; set; }
|
public required string DatasetType { get; set; }
|
||||||
public required string Icon { get; set; }
|
public string Icon { get; set; }
|
||||||
public required string ObjectTemplate { get; set; }
|
public required string ObjectTemplateId { get; set; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -9,8 +9,16 @@ namespace SessionZero;
|
|||||||
|
|
||||||
public static class TestingCommands
|
public static class TestingCommands
|
||||||
{
|
{
|
||||||
|
[Command(Name = "createpack")]
|
||||||
|
private static void CreateTestPack()
|
||||||
|
{
|
||||||
|
var pack = new Datapack("SessionZero Core", "sz_core");
|
||||||
|
pack.SavePack();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: Remove this after testing
|
// TODO: Remove this after testing
|
||||||
[Command(Name = "test")]
|
[Command(Name = "testdata")]
|
||||||
private static void TestParseTemplate()
|
private static void TestParseTemplate()
|
||||||
{
|
{
|
||||||
string toml = @"
|
string toml = @"
|
||||||
|
|||||||
Reference in New Issue
Block a user