2023-11-10 00:29:16 -06:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
|
using YamlDotNet.Serialization;
|
2024-08-26 20:18:54 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2025-01-30 23:57:12 -06:00
|
|
|
|
using LANCommander.SDK.Models;
|
2024-10-04 23:37:49 -05:00
|
|
|
|
using LANCommander.SDK.Services;
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.SDK.Helpers
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ManifestHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly ILogger Logger;
|
|
|
|
|
|
|
2024-01-09 01:22:56 -06:00
|
|
|
|
public const string ManifestFilename = "Manifest.yml";
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static bool Exists(string installDirectory, Guid id)
|
2023-11-17 11:48:45 -06:00
|
|
|
|
{
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var path = GetPath(installDirectory, id);
|
2023-11-17 11:48:45 -06:00
|
|
|
|
|
|
|
|
|
|
return File.Exists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static T Read<T>(string installDirectory)
|
2024-01-25 21:24:35 -06:00
|
|
|
|
{
|
|
|
|
|
|
var source = Path.Combine(installDirectory, ManifestFilename);
|
|
|
|
|
|
var yaml = File.ReadAllText(source);
|
|
|
|
|
|
|
|
|
|
|
|
Logger?.LogTrace("Deserializing manifest");
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var manifest = Deserialize<T>(yaml);
|
2024-01-25 21:24:35 -06:00
|
|
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static T Read<T>(string installDirectory, Guid id)
|
|
|
|
|
|
where T : class
|
2023-11-10 00:29:16 -06:00
|
|
|
|
{
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var source = GetPath(installDirectory, id);
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
2024-08-08 18:28:33 -05:00
|
|
|
|
if (File.Exists(source))
|
|
|
|
|
|
{
|
|
|
|
|
|
var yaml = File.ReadAllText(source);
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
2024-08-08 18:28:33 -05:00
|
|
|
|
Logger?.LogTrace("Deserializing manifest");
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var manifest = Deserialize<T>(yaml);
|
2024-08-08 18:28:33 -05:00
|
|
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2023-11-10 00:29:16 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static async Task<T> ReadAsync<T>(string installDirectory)
|
2024-08-26 20:18:54 -05:00
|
|
|
|
{
|
|
|
|
|
|
var source = Path.Combine(installDirectory, ManifestFilename);
|
|
|
|
|
|
var yaml = await File.ReadAllTextAsync(source);
|
|
|
|
|
|
|
|
|
|
|
|
Logger?.LogTrace("Deserializing manifest from path {ManifestPath}", source);
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var manifest = Deserialize<T>(yaml);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static async Task<T> ReadAsync<T>(string installDirectory, Guid id)
|
|
|
|
|
|
where T : class
|
2024-08-26 20:18:54 -05:00
|
|
|
|
{
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var source = GetPath(installDirectory, id);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
|
|
|
|
|
if (File.Exists(source))
|
|
|
|
|
|
{
|
|
|
|
|
|
var yaml = await File.ReadAllTextAsync(source);
|
|
|
|
|
|
|
|
|
|
|
|
Logger?.LogTrace("Deserializing manifest from {ManifestPath}", source);
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
var manifest = Deserialize<T>(yaml);
|
2024-08-26 20:18:54 -05:00
|
|
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-01-30 23:57:12 -06:00
|
|
|
|
|
|
|
|
|
|
public static string Write<T>(T manifest, string installDirectory)
|
|
|
|
|
|
where T : IKeyedModel
|
2023-11-10 00:29:16 -06:00
|
|
|
|
{
|
2024-01-09 01:22:56 -06:00
|
|
|
|
var destination = GetPath(installDirectory, manifest.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(destination)))
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(destination));
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
2023-11-10 20:53:28 -06:00
|
|
|
|
Logger?.LogTrace("Attempting to write manifest to path {Destination}", destination);
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
2023-11-30 18:25:37 -06:00
|
|
|
|
var yaml = Serialize(manifest);
|
|
|
|
|
|
|
|
|
|
|
|
Logger?.LogTrace("Writing manifest file");
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(destination, yaml);
|
|
|
|
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static async Task<string> WriteAsync<T>(T manifest, string installDirectory)
|
|
|
|
|
|
where T : IKeyedModel
|
|
|
|
|
|
{
|
|
|
|
|
|
var destination = GetPath(installDirectory, manifest.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(destination)))
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(destination));
|
|
|
|
|
|
|
|
|
|
|
|
Logger?.LogTrace("Attempting to write manifest to path {Destination}", destination);
|
|
|
|
|
|
|
|
|
|
|
|
var yaml = Serialize(manifest);
|
|
|
|
|
|
|
|
|
|
|
|
Logger?.LogTrace("Writing manifest file");
|
|
|
|
|
|
|
|
|
|
|
|
await File.WriteAllTextAsync(destination, yaml);
|
|
|
|
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-10 19:45:41 -05:00
|
|
|
|
public static bool TryDeserialize<T>(string serializedManifest, out T manifest)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
manifest = Deserialize<T>(serializedManifest);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
manifest = default(T);
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-15 17:45:39 -06:00
|
|
|
|
public static T Deserialize<T>(string serializedManifest)
|
2023-12-14 19:09:00 -06:00
|
|
|
|
{
|
|
|
|
|
|
var deserializer = new DeserializerBuilder()
|
2024-05-13 18:41:40 -05:00
|
|
|
|
.IgnoreUnmatchedProperties()
|
2023-12-14 19:09:00 -06:00
|
|
|
|
.WithNamingConvention(new PascalCaseNamingConvention())
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
2024-01-15 17:45:39 -06:00
|
|
|
|
return deserializer.Deserialize<T>(serializedManifest);
|
2023-12-14 19:09:00 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-15 17:45:39 -06:00
|
|
|
|
public static string Serialize<T>(T manifest)
|
2023-11-30 18:25:37 -06:00
|
|
|
|
{
|
2023-11-10 00:29:16 -06:00
|
|
|
|
var serializer = new SerializerBuilder()
|
2023-11-10 21:37:02 -06:00
|
|
|
|
.WithNamingConvention(new PascalCaseNamingConvention())
|
2023-11-10 00:29:16 -06:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
2023-11-10 20:53:28 -06:00
|
|
|
|
Logger?.LogTrace("Serializing manifest");
|
2023-11-10 00:29:16 -06:00
|
|
|
|
|
|
|
|
|
|
var yaml = serializer.Serialize(manifest);
|
|
|
|
|
|
|
2023-11-30 18:25:37 -06:00
|
|
|
|
return yaml;
|
2023-11-10 00:29:16 -06:00
|
|
|
|
}
|
2023-11-10 01:32:30 -06:00
|
|
|
|
|
2025-01-30 23:57:12 -06:00
|
|
|
|
public static string GetPath(string installDirectory, Guid id)
|
2023-11-10 01:32:30 -06:00
|
|
|
|
{
|
2025-01-30 23:57:12 -06:00
|
|
|
|
return GameService.GetMetadataFilePath(installDirectory, id, ManifestFilename);
|
2023-11-10 01:32:30 -06:00
|
|
|
|
}
|
2023-11-10 00:29:16 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|