Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rast1234 committed Jan 30, 2025
1 parent 67c176c commit 8df49ef
Show file tree
Hide file tree
Showing 61 changed files with 2,650 additions and 509 deletions.
42 changes: 38 additions & 4 deletions RFGM.Archiver/ArchiverUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Help;
using System.CommandLine.Hosting;
Expand All @@ -10,12 +9,47 @@
using NLog.Layouts;
using NLog.Targets;
using NLog.Targets.Wrappers;
using RFGM.Formats.Abstractions;


namespace RFGM.Archiver;

public static class ArchiverUtils
{
/// <summary>
/// Should fit into default console window when .exe is double-clicked
/// </summary>
public static readonly string Banner = $"""
___ ___ ___ __ __
| _ \ __/ __| \/ |
| / _| (_ | |\/| |
|_|_\_| \___|_| |_|
/_\ _ _ __| |_ (_)_ _____ _ _
/ _ \| '_/ _| ' \| \ V / -_) '_|
/_/ \_\_| \__|_||_|_|\_/\___|_|
🔨 Packer-Unpacker for RFG Re-MARS-tered 🔨
╔════════════════╤══════════════════════════════════════════════╗
║ BASIC USAGE ╪ drag&drop files on the .exe to unpack/pack ║
║ advanced ╪ use commandline for more features ║
╟──────────────────────── Pack Examples ────────────────────────╢
║ modfolder.vpp_pc/ + archiver.exe = .pack/modfolder.vpp_pc ║
║ textures.cpeg_pc/ + archiver.exe = .pack/textures.cpeg_pc ║
╟─────────────────────── Unpack Examples ───────────────────────╢
║ items.vpp_pc + archiver.exe = .unpack/items.vpp_pc/..... ║
║ test.cpeg_pc + archiver.exe = .unpack/test.cpeg_pc/*.png ║
╟────────────────┴──────────────────────────────────────────────╢
║ Help, feedback, bugreport: join Red Faction Community Discord ║
║ https://discord.gg/factionfiles - see #rfg-modding channel ║
╚═══════════════════════════════════════════════════════════════╝
Supported formats
Unpack: {string.Join(" ", FormatDescriptors.UnpackExt)}
Pack: {string.Join(" ", FormatDescriptors.PackExt)}
Decode: {string.Join(" ", FormatDescriptors.DecodeExt)}
Encode: {string.Join(" ", FormatDescriptors.EncodeExt)}
""";

public static IEnumerable<HelpSectionDelegate> HackHelpLayout(bool runStandalone)
{
var layout = HelpBuilder.Default.GetLayout();
Expand All @@ -29,7 +63,7 @@ public static IEnumerable<HelpSectionDelegate> HackHelpLayout(bool runStandalone
return layout;
}

public static void SetupLogs(ILoggingBuilder x)
public static void SetupLogs(ILoggingBuilder x, NLog.LogLevel consoleLogLevel)
{
x.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.None);

Expand Down Expand Up @@ -75,7 +109,7 @@ public static void SetupLogs(ILoggingBuilder x)
},
};
console.Layout = Layout.FromString("${date:format=HH\\:mm\\:ss} ${message}${onexception:${newline}${exception}}");
var rule1 = new LoggingRule("*", NLog.LogLevel.Debug, NLog.LogLevel.Off, new AsyncTargetWrapper(console, 10000, AsyncTargetWrapperOverflowAction.Discard));
var rule1 = new LoggingRule("*", consoleLogLevel, NLog.LogLevel.Off, new AsyncTargetWrapper(console, 10000, AsyncTargetWrapperOverflowAction.Discard));

var file = new FileTarget("file");
file.FileName = ".rfgm.archiver.log";
Expand Down Expand Up @@ -105,7 +139,7 @@ public static async Task LogExceptionMiddleware(InvocationContext context, Func<
var logPath = Path.Combine(Environment.CurrentDirectory, ".rfgm.archiver.log");
log.LogTrace(e, "Command failed");
log.LogCritical("Command failed!\n\t{message}\n\tCheck log for details:\n\t{logPath}", e.Message, logPath);
context.ExitCode = (int)ExitCode.UnhandledException;
}
}

}
50 changes: 50 additions & 0 deletions RFGM.Archiver/Commandline/AppRootCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.CommandLine;
using System.CommandLine.Hosting;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using Microsoft.Extensions.DependencyInjection;

namespace RFGM.Archiver.Commandline;

public class AppRootCommand : RootCommand
{
public override string Description => ArchiverUtils.Banner;
private readonly Argument<List<string>> inputArg = new("input", "Any supported file to unpack, or a directory to pack")
{
IsHidden = true,
Arity = ArgumentArity.OneOrMore
};

private readonly Option<bool> forceArg = new([
"-f",
"--force"
],
"Overwrite output if exists")
{
IsHidden = true
};

public AppRootCommand()
{
AddArgument(inputArg);
AddOption(forceArg);
AddCommand(new Unpack());
AddCommand(new Metadata());
AddCommand(new AppTest());
AddCommand(new Pack());

Handler = CommandHandler.Create(Handle);
}

/// <summary>
/// Simplified scenario where user drops files on the .exe
/// </summary>
private async Task<int> Handle(InvocationContext context, CancellationToken token)
{
var input = context.ParseResult.GetValueForArgument(inputArg);
var force = context.ParseResult.GetValueForOption(forceArg);
var archiver = context.GetHost().Services.GetRequiredService<Services.Archiver>();
return (int)await archiver.CommandDefault(input, force, token);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.CommandLine.NamingConventionBinder;
using Microsoft.Extensions.DependencyInjection;

namespace RFGM.Archiver.Commands;
namespace RFGM.Archiver.Commandline;

public class Test : Command
public class AppTest : Command
{
private readonly Argument<List<string>> inputArg = new("input", "test input"){
Arity = ArgumentArity.OneOrMore
Expand All @@ -24,7 +24,7 @@ public class Test : Command

public override string? Description => @"Run test logic for debugging";

public Test() : base(nameof(Test).ToLowerInvariant())
public AppTest() : base(nameof(AppTest).ToLowerInvariant())
{
IsHidden = true;
AddArgument(inputArg);
Expand All @@ -37,7 +37,6 @@ private async Task<int> Handle(InvocationContext context, CancellationToken toke
var input = context.ParseResult.GetValueForArgument(inputArg);
var parallel = context.ParseResult.GetValueForOption(parallelArg);
var archiver = context.GetHost().Services.GetRequiredService<Services.Archiver>();
await archiver.Test(input, parallel, token);
return 0;
return (int) await archiver.CommandTest(input, parallel, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileSystemGlobbing;
using RFGM.Archiver.Models;
using RFGM.Formats;

namespace RFGM.Archiver.Commands;
namespace RFGM.Archiver.Commandline;

public class Metadata : Command
{
Expand Down Expand Up @@ -48,7 +47,6 @@ private async Task<int> Handle(InvocationContext context, CancellationToken toke
var optimizeFor = context.ParseResult.GetValueForOption(optimizeArg);
var archiver = context.GetHost().Services.GetRequiredService<Services.Archiver>();
var settings = UnpackSettings.Meta with { OptimizeFor = optimizeFor };
await archiver.UnpackMetadata(input, parallel, settings, token);
return 0;
return (int) await archiver.CommandMetadata(input, parallel, settings, token);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System.CommandLine;
using System.CommandLine.Help;
using System.CommandLine.Hosting;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileSystemGlobbing;
using RFGM.Archiver.Models;
using RFGM.Archiver.Models.Messages;
using RFGM.Formats;
using RFGM.Formats.Abstractions;
using RFGM.Formats.Peg;

namespace RFGM.Archiver.Commands;
namespace RFGM.Archiver.Commandline;

public class Pack : Command
{
Expand Down Expand Up @@ -86,7 +80,6 @@ private async Task<int> Handle(InvocationContext context, CancellationToken toke
? PackSettings.Default
: new PackSettings(metadata, force);
var archiver = context.GetHost().Services.GetRequiredService<Services.Archiver>();
await archiver.Pack(input, output, parallel, settings, token);
return 0;
return (int) await archiver.CommandPack(input, output, parallel, settings, token);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using System.CommandLine;
using System.CommandLine.Help;
using System.CommandLine.Hosting;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileSystemGlobbing;
using RFGM.Archiver.Models;
using RFGM.Archiver.Models.Messages;
using RFGM.Archiver.Services.Handlers;
using RFGM.Formats;
using RFGM.Formats.Abstractions;
using RFGM.Formats.Peg;

namespace RFGM.Archiver.Commands;
namespace RFGM.Archiver.Commandline;

public class Unpack : Command
{
Expand All @@ -32,7 +28,7 @@ public class Unpack : Command
"-d",
"--default"
],
$"Use default unpack settings [ -r ]. Other flags will be ignored!");
$"Use default unpack settings [ -r -l ]. Other flags will be ignored!");

private readonly Option<string> filterArg = new([
"--filter",
Expand All @@ -49,6 +45,12 @@ public class Unpack : Command
],
"Format xml-like files (.xtbl .dtdox .gtdox) for readability. Some formatted files will crash game if packed!");

private readonly Option<bool> localizationArg = new([
"-l",
"--localization"
],
"Decode localization files (.rfglocatext) into editable xml");

private readonly Option<bool> recursiveArg = new([
"-r",
"--recursive"
Expand Down Expand Up @@ -119,6 +121,7 @@ public Unpack() : base(nameof(Unpack).ToLowerInvariant())
AddOption(defaultArg);
AddOption(filterArg);
AddOption(forceArg);
AddOption(localizationArg);
AddOption(metadataArg);
AddOption(noPropsArg);
AddOption(optimizeArg);
Expand Down Expand Up @@ -146,12 +149,12 @@ private async Task<int> Handle(InvocationContext context, CancellationToken toke
var parallel = context.ParseResult.GetValueForOption(parallelArg);
var optimize = context.ParseResult.GetValueForOption(optimizeArg);
var skip = context.ParseResult.GetValueForOption(skipArg);
var localization = context.ParseResult.GetValueForOption(localizationArg);
var matcher = new Matcher(StringComparison.OrdinalIgnoreCase).AddInclude(filter);
var settings = isDefault
? UnpackSettings.Default
: new UnpackSettings(matcher, texture, optimize, skip, !noProps, xmlFormat, recursive, metadata, force);
: new UnpackSettings(matcher, texture, optimize, skip, !noProps, xmlFormat, recursive, metadata, force, localization);
var archiver = context.GetHost().Services.GetRequiredService<Services.Archiver>();
await archiver.Unpack(input, output, parallel, settings, token);
return 0;
return (int) await archiver.CommandUnpack(input, output, parallel, settings, token);
}
}
73 changes: 0 additions & 73 deletions RFGM.Archiver/Commands/AppRoot.cs

This file was deleted.

2 changes: 1 addition & 1 deletion RFGM.Formats/Constants.cs → RFGM.Archiver/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RFGM.Formats;
namespace RFGM.Archiver;

public static class Constants
{
Expand Down
10 changes: 10 additions & 0 deletions RFGM.Archiver/ExitCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RFGM.Archiver;

public enum ExitCode
{
Ok,
UnhandledException,
FailedTasks,
NoOutput,
Rick = 42,
}
5 changes: 0 additions & 5 deletions RFGM.Archiver/Models/Messages/BuildMetadataMessage.cs

This file was deleted.

5 changes: 5 additions & 0 deletions RFGM.Archiver/Models/Messages/CollectMetadataMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using RFGM.Formats.Abstractions;

namespace RFGM.Archiver.Models.Messages;

public record CollectMetadataMessage(EntryInfo EntryInfo, Breadcrumbs Breadcrumbs, Stream Primary) : IMessage;
3 changes: 3 additions & 0 deletions RFGM.Archiver/Models/Messages/DecideMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace RFGM.Archiver.Models.Messages;

public record DecideMessage(string Input, string? Output, PackSettings? PackSettings, UnpackSettings? UnpackSettings) : IMessage;
6 changes: 6 additions & 0 deletions RFGM.Archiver/Models/Messages/DecodeFileMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.IO.Abstractions;
using RFGM.Formats.Abstractions;

namespace RFGM.Archiver.Models.Messages;

public record DecodeFileMessage(EntryInfo EntryInfo, Stream Primary, Breadcrumbs Breadcrumbs, IDirectoryInfo Destination, UnpackSettings Settings) : IMessage;
7 changes: 0 additions & 7 deletions RFGM.Archiver/Models/Messages/PackMessage.cs

This file was deleted.

Loading

0 comments on commit 8df49ef

Please sign in to comment.