Added VDF Generator

This commit is contained in:
2026-05-31 11:06:50 +02:00
parent ec7031d9fd
commit ab391186f2
13 changed files with 720 additions and 17 deletions

View File

@@ -0,0 +1,99 @@
using System.IO;
using System.Text;
using EonaCat.FirstLight.SaveTransfer.VdfGenerator;
using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.Models;
public class RemoteCacheVdfFile(int appId)
{
public const string FileName = "remotecache";
public const string FileExtension = ".vdf";
public int AppId { get; set; } = appId;
public int ChangeNumber { get; set; }
public int OsType { get; set; }
public List<CachedFileMetadata> CachedFiles { get; set; } = [];
/// <summary>
/// Retrieves the application identifier from the specified file path.
/// </summary>
/// <param name="path">The file system path from which to extract the application identifier. Must contain a parent directory whose name is a valid integer.</param>
/// <returns>The application identifier parsed from the parent directory name of the specified path.</returns>
/// <exception cref="InvalidOperationException">Thrown if the parent directory name of the specified path is not a valid integer.</exception>
private static int GetAppIdFromPath(string path)
{
var parent = Path.GetFileName(Path.GetDirectoryName(path));
return int.TryParse(parent, out var result)
? result
: throw new InvalidOperationException("Invalid AppId in path");
}
/// <summary>
/// Initializes a new instance of the <see cref="RemoteCacheVdfFile"/> class using the specified remote folder path and loads metadata for all files within the folder and its subdirectories.
/// </summary>
/// <param name="remoteFolderPath">The full path to the remote folder containing the files to be cached. Must not be null or empty.</param>
public RemoteCacheVdfFile(string remoteFolderPath) : this(GetAppIdFromPath(remoteFolderPath))
{
var files = Directory.GetFiles(remoteFolderPath, "*", SearchOption.AllDirectories);
foreach (var file in files)
CachedFiles.Add(new CachedFileMetadata(file, remoteFolderPath));
}
/// <summary>
/// Initializes a new instance of the <see cref="RemoteCacheVdfFile"/> class based on the provided keyvalue (KV) group, copying the relevant metadata.
/// </summary>
/// <param name="group">The KV group from which metadata and the list of cached files are read. Must not be null.</param>
public RemoteCacheVdfFile(KeyValueGroup group) : this(group.Key)
{
foreach (var node in group.Nodes)
{
// If the node is a KvGroup, we create a new CachedFileMetadata object using the group and add it to the CachedFiles list.
if (node is KeyValueGroup fileGroup)
{
CachedFiles.Add(new CachedFileMetadata(fileGroup));
continue;
}
// If the node is not a KvGroup, we attempt to cast it to a KvPair to extract the key and value for the properties of RemoteCacheVdfFile.
var kvPair = node as KeyValue.Models.KeyValuePair;
switch (kvPair?.Key)
{
case "ChangeNumber":
ChangeNumber = NumberParser.ParseInt(kvPair.Value);
break;
case "OSType":
OsType = NumberParser.ParseInt(kvPair.Value);
break;
}
}
}
/// <summary>
/// Exports the current object and its cached files as a key-value group representation.
/// </summary>
/// <returns>A KvGroup containing the key-value pairs for the current object and its cached files.</returns>
public KeyValueGroup ExportAsKvGroup()
{
var kvGroup = new KeyValueGroup(AppId.ToString())
.Add("ChangeNumber", ChangeNumber.ToString())
.Add("OSType", OsType.ToString());
foreach (var cachedFile in CachedFiles)
kvGroup.Add(cachedFile.ExportAsKvGroup());
return kvGroup;
}
/// <summary>
/// Exports the current data to a file in the specified destination folder.
/// </summary>
/// <param name="destinationFolder">The path to the folder where the exported file will be created. Must be a valid, writable directory.</param>
public void ExportAsFile(string destinationFolder)
{
var kv = ExportAsKvGroup();
var serialized = KeyValueSerializer.Serialize(kv);
var outputFilePath = Path.Combine(destinationFolder, $"{FileName}{FileExtension}");
File.WriteAllText(outputFilePath, serialized, new UTF8Encoding());
}
}