Files
SabreTools/RombaSharp/Features/Dir2Dat.cs

66 lines
2.4 KiB
C#
Raw Normal View History

2020-08-01 13:25:32 -07:00
using System.Collections.Generic;
using System.IO;
using SabreTools.DatFiles;
2020-12-10 23:24:09 -08:00
using SabreTools.DatTools;
using SabreTools.FileTypes;
2024-03-04 23:56:05 -05:00
using SabreTools.Hashing;
2020-12-07 13:57:26 -08:00
using SabreTools.Help;
2020-12-07 15:08:57 -08:00
using SabreTools.IO;
2020-08-01 13:25:32 -07:00
namespace RombaSharp.Features
{
internal class Dir2Dat : BaseFeature
{
public const string Value = "Dir2Dat";
public Dir2Dat()
{
Name = Value;
Flags = ["dir2dat"];
2020-08-01 13:25:32 -07:00
Description = "Creates a DAT file for the specified input directory and saves it to the -out filename.";
2020-12-07 13:57:26 -08:00
_featureType = ParameterType.Flag;
2020-08-01 13:25:32 -07:00
LongDescription = "Creates a DAT file for the specified input directory and saves it to the -out filename.";
Features = [];
2020-08-01 13:25:32 -07:00
2021-02-03 11:10:19 -08:00
// Common Features
AddCommonFeatures();
2020-08-01 13:25:32 -07:00
AddFeature(OutStringInput);
AddFeature(SourceStringInput);
AddFeature(NameStringInput); // Defaults to "untitled"
AddFeature(DescriptionStringInput);
}
public override bool ProcessFeatures(Dictionary<string, Feature?> features)
2020-08-01 13:25:32 -07:00
{
2021-03-19 20:52:11 -07:00
// If the base fails, just fail out
if (!base.ProcessFeatures(features))
return false;
2020-08-01 13:25:32 -07:00
// Get feature flags
string? name = GetString(features, NameStringValue);
string? description = GetString(features, DescriptionStringValue);
string? source = GetString(features, SourceStringValue);
string? outdat = GetString(features, OutStringValue);
2020-08-01 13:25:32 -07:00
// Ensure the output directory
2024-03-12 16:47:21 -04:00
outdat = outdat.Ensure(create: true);
2020-08-01 13:25:32 -07:00
// Check that all required directories exist
if (!Directory.Exists(source))
{
logger.Error($"File '{source}' does not exist!");
2021-03-19 20:52:11 -07:00
return false;
2020-08-01 13:25:32 -07:00
}
2020-08-02 13:34:35 -07:00
// Create and write the encapsulating datfile
2020-08-01 13:25:32 -07:00
DatFile datfile = DatFile.Create();
2024-03-10 04:10:37 -04:00
datfile.Header.SetFieldValue<string?>(SabreTools.Models.Metadata.Header.NameKey, string.IsNullOrWhiteSpace(name) ? "untitled" : name);
datfile.Header.SetFieldValue<string?>(SabreTools.Models.Metadata.Header.DescriptionKey, description);
2024-03-04 23:56:05 -05:00
DatFromDir.PopulateFromDir(datfile, source, asFiles: TreatAsFile.NonArchive, hashes: [HashType.CRC32, HashType.MD5, HashType.SHA1]);
Writer.Write(datfile, outdat!);
2021-03-19 20:52:11 -07:00
return true;
2020-08-01 13:25:32 -07:00
}
}
}