diff --git a/README.md b/README.md index 1aa14c9..2602878 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,14 @@ Arguments (case-insensitive): - `-describe-target`: validate and describe the game without requiring a payload, copying files or creating a shortcut. Prints one JSON object with `GameDirectory` (absolute path), `ModDirectory` and `LauncherPath`. Assumes a complete payload containing both launchers; ordinary installation still falls back to the blob launcher when the normal payload launcher is absent. - `-include-debug-symbols`: also copy root `.pdb` files when installing, retaining their names even when `MetaHook.exe` is renamed to `svencoop.exe`. Normal installations continue to omit root symbols. - `-help`: print usage. +- `-plugins-only`: deploy a standalone plugin payload into an existing MetaHook installation. Accepts `-describe-target` to validate/query that installation, or `-include-debug-symbols` when installing. Cannot be combined with `-uninstall`. + +Plugin-only payloads require `install/output/svencoop/metahook/plugins/*.dll`, but no launcher. The existing launcher and `/metahook/` must exist. Common resources are mapped to the selected mod as in a full install, including plugin PDBs and dependency libraries under the mod directory. Root executables, DLLs and PDBs are never copied; plugin lists and their templates are neither overwritten, created nor deleted. No shortcut is created. Install MetaHook and enable the plugin in `plugins.lst` first; this mode does not install missing runtime dependencies or enable plugins automatically. + +```powershell +.\MetahookInstallerCLI.exe -appid 225840 -plugins-only -describe-target +.\MetahookInstallerCLI.exe -appid 225840 -plugins-only -include-debug-symbols +``` `-uninstall`, `-describe-target` and `-include-debug-symbols` are mutually exclusive. Query errors go to stderr and return exit code `1`. Omitting `-gamedir` also enables Steam discovery in query mode: diff --git a/src/MetahookInstallerCLI/CommandLineOptions.cs b/src/MetahookInstallerCLI/CommandLineOptions.cs index 12b7e86..cb7a2d6 100644 --- a/src/MetahookInstallerCLI/CommandLineOptions.cs +++ b/src/MetahookInstallerCLI/CommandLineOptions.cs @@ -12,16 +12,18 @@ public sealed record CommandLineOptions( bool Uninstall, bool ShowHelp, bool DescribeTarget = false, - bool IncludeDebugSymbols = false) + bool IncludeDebugSymbols = false, + bool PluginsOnly = false) { public const string Usage = - "Usage: MetahookInstallerCLI -appid [-gamedir ] [-moddir ] [-uninstall | -describe-target | -include-debug-symbols]\n" + + "Usage: MetahookInstallerCLI -appid [-gamedir ] [-moddir ] [-plugins-only] [-uninstall | -describe-target | -include-debug-symbols]\n" + " -appid Steam app ID of the game, e.g. 225840 (Sven Co-op) or 70 (Half-Life).\n" + " -gamedir Game root directory. Defaults to the Steam install directory of the app.\n" + " -moddir Mod directory under the game root. Defaults to the app's base mod.\n" + " -uninstall Remove MetaHook instead of installing it.\n" + " -describe-target Print target JSON without installing (assumes both launchers in payload).\n" + " -include-debug-symbols Also install root PDB files.\n" + + " -plugins-only Update plugins/resources in an existing MetaHook installation; keep root files and plugin lists.\n" + " -help Show this help."; private static readonly HashSet ValueKeys = new(StringComparer.OrdinalIgnoreCase) @@ -40,6 +42,7 @@ public static CommandLineOptions Parse(string[] args) var uninstall = false; var describeTarget = false; var includeDebugSymbols = false; + var pluginsOnly = false; for (var i = 0; i < args.Length; i++) { var arg = args[i]; @@ -72,6 +75,12 @@ public static CommandLineOptions Parse(string[] args) continue; } + if (key.Equals("plugins-only", StringComparison.OrdinalIgnoreCase)) + { + pluginsOnly = true; + continue; + } + if (!ValueKeys.Contains(key)) { throw new ArgumentException($"Unknown argument '{arg}'."); @@ -97,6 +106,8 @@ public static CommandLineOptions Parse(string[] args) if ((uninstall && (describeTarget || includeDebugSymbols)) || (describeTarget && includeDebugSymbols)) throw new ArgumentException("-uninstall, -describe-target and -include-debug-symbols cannot be combined."); + if (uninstall && pluginsOnly) + throw new ArgumentException("-plugins-only cannot be combined with -uninstall."); return new CommandLineOptions( appId, @@ -105,7 +116,8 @@ public static CommandLineOptions Parse(string[] args) uninstall, false, describeTarget, - includeDebugSymbols); + includeDebugSymbols, + pluginsOnly); } // findGameDirectory is only queried when -gamedir is omitted. diff --git a/src/MetahookInstallerCLI/Program.cs b/src/MetahookInstallerCLI/Program.cs index 0b2c166..fbe0057 100644 --- a/src/MetahookInstallerCLI/Program.cs +++ b/src/MetahookInstallerCLI/Program.cs @@ -27,7 +27,7 @@ if (options.DescribeTarget) { var gameDirectory = Path.GetFullPath(target.GameDirectory); - var launcherPath = MetahookSetup.DescribeLauncherPath(gameDirectory, target.ModDirectory); + var launcherPath = MetahookSetup.DescribeLauncherPath(gameDirectory, target.ModDirectory, options.PluginsOnly); Console.WriteLine(JsonSerializer.Serialize(new { GameDirectory = gameDirectory, @@ -58,9 +58,16 @@ return 0; } - var source = InstallPayload.FindApplicationSourceDirectory() + var source = InstallPayload.FindApplicationSourceDirectory(options.PluginsOnly) ?? throw new DirectoryNotFoundException( "The install/output folder cannot be located next to MetahookInstallerCLI.exe. Please extract the complete archive before running the programme."); + if (options.PluginsOnly) + { + var existingLauncher = MetahookSetup.InstallPlugins(source, target.GameDirectory, target.ModDirectory); + Console.WriteLine($"Updated plugins for {target.GameName}: {Path.Combine(target.GameDirectory, target.ModDirectory)}"); + Console.WriteLine($"Launcher: {existingLauncher}"); + return 0; + } var launcher = MetahookSetup.Install(source, target.GameDirectory, target.ModDirectory, options.IncludeDebugSymbols); var shortcut = MetahookSetup.CreateShortcut(shortcutDirectory, target.GameName, launcher, target.GameDirectory, target.ModDirectory); diff --git a/src/MetahookInstallerCore/InstallPayload.cs b/src/MetahookInstallerCore/InstallPayload.cs index 78f82b2..92e1bf7 100644 --- a/src/MetahookInstallerCore/InstallPayload.cs +++ b/src/MetahookInstallerCore/InstallPayload.cs @@ -7,23 +7,23 @@ namespace MetahookInstaller; public static class InstallPayload { // Release builds only look next to the executable; Debug builds also search its ancestors. - public static string? FindApplicationSourceDirectory() + public static string? FindApplicationSourceDirectory(bool pluginsOnly = false) { #if DEBUG - return FindSourceDirectory(AppContext.BaseDirectory, true); + return FindSourceDirectory(AppContext.BaseDirectory, true, pluginsOnly); #else - return FindSourceDirectory(AppContext.BaseDirectory, false); + return FindSourceDirectory(AppContext.BaseDirectory, false, pluginsOnly); #endif } - public static string? FindSourceDirectory(string applicationDirectory, bool searchParents) + public static string? FindSourceDirectory(string applicationDirectory, bool searchParents, bool pluginsOnly = false) { DirectoryInfo? directory = new(Path.GetFullPath(applicationDirectory)); while (directory != null) { var source = Path.Combine(directory.FullName, "install", "output"); - if (File.Exists(Path.Combine(source, "MetaHook.exe")) || - File.Exists(Path.Combine(source, "MetaHook_blob.exe"))) + if (pluginsOnly ? HasPlugins(source) : + File.Exists(Path.Combine(source, "MetaHook.exe")) || File.Exists(Path.Combine(source, "MetaHook_blob.exe"))) return source; if (!searchParents) break; @@ -39,20 +39,26 @@ public static string GetLauncherPath(string gameDirectory, string modDirectory, isNonBlobEngine ? (isSvenCoop ? "svencoop.exe" : "MetaHook.exe") : "MetaHook_blob.exe"); } - public static string Install(string source, string gameDirectory, string modDirectory, - bool isNonBlobEngine, bool needsSdl, bool includeDebugSymbols = false) + private static bool HasPlugins(string source) { - var normalLauncher = Path.Combine(source, "MetaHook.exe"); - isNonBlobEngine = isNonBlobEngine && File.Exists(normalLauncher); - var sourceLauncher = isNonBlobEngine ? normalLauncher : Path.Combine(source, "MetaHook_blob.exe"); - if (!File.Exists(sourceLauncher)) - throw new FileNotFoundException("Could not find the required MetaHook launcher.", sourceLauncher); + var plugins = Path.Combine(source, "svencoop", "metahook", "plugins"); + return Directory.Exists(plugins) && Directory.EnumerateFiles(plugins) + .Any(file => Path.GetExtension(file).Equals(".dll", StringComparison.OrdinalIgnoreCase)); + } + public static void InstallPlugins(string source, string gameDirectory, string modDirectory) + { + if (!HasPlugins(source)) + throw new DirectoryNotFoundException("Plugin payload must contain svencoop/metahook/plugins/*.dll."); + CopyResources(source, gameDirectory, modDirectory, preservePluginLists: true); + } + + private static void CopyResources(string source, string gameDirectory, string modDirectory, bool preservePluginLists = false) + { var isSvenCoop = modDirectory.Equals("svencoop", StringComparison.OrdinalIgnoreCase); - var targetLauncher = GetLauncherPath(gameDirectory, modDirectory, isNonBlobEngine); var commonResources = Path.Combine(source, "svencoop"); if (Directory.Exists(commonResources)) - CopyDirectory(commonResources, Path.Combine(gameDirectory, modDirectory)); + CopyDirectory(commonResources, Path.Combine(gameDirectory, modDirectory), preservePluginLists); foreach (var folder in Directory.GetDirectories(source).Where(directory => Path.GetFileName(directory).Equals(modDirectory, StringComparison.OrdinalIgnoreCase) || @@ -60,12 +66,26 @@ public static string Install(string source, string gameDirectory, string modDire { if (isSvenCoop && Path.GetFileName(folder).Equals("svencoop", StringComparison.OrdinalIgnoreCase)) continue; - CopyDirectory(folder, Path.Combine(gameDirectory, Path.GetFileName(folder))); + CopyDirectory(folder, Path.Combine(gameDirectory, Path.GetFileName(folder)), preservePluginLists); } var platform = Path.Combine(source, "platform"); if (isSvenCoop && Directory.Exists(platform)) - CopyDirectory(platform, Path.Combine(gameDirectory, "platform")); + CopyDirectory(platform, Path.Combine(gameDirectory, "platform"), preservePluginLists); + } + + public static string Install(string source, string gameDirectory, string modDirectory, + bool isNonBlobEngine, bool needsSdl, bool includeDebugSymbols = false) + { + var normalLauncher = Path.Combine(source, "MetaHook.exe"); + isNonBlobEngine = isNonBlobEngine && File.Exists(normalLauncher); + var sourceLauncher = isNonBlobEngine ? normalLauncher : Path.Combine(source, "MetaHook_blob.exe"); + if (!File.Exists(sourceLauncher)) + throw new FileNotFoundException("Could not find the required MetaHook launcher.", sourceLauncher); + + var isSvenCoop = modDirectory.Equals("svencoop", StringComparison.OrdinalIgnoreCase); + var targetLauncher = GetLauncherPath(gameDirectory, modDirectory, isNonBlobEngine); + CopyResources(source, gameDirectory, modDirectory); File.Copy(sourceLauncher, targetLauncher, true); foreach (var file in Directory.GetFiles(source).Where(file => @@ -99,12 +119,19 @@ public static string Install(string source, string gameDirectory, string modDire return targetLauncher; } - private static void CopyDirectory(string source, string destination) + private static void CopyDirectory(string source, string destination, bool preservePluginLists = false) { Directory.CreateDirectory(destination); foreach (var file in Directory.GetFiles(source)) + { + var name = Path.GetFileName(file); + if (preservePluginLists && (name.Equals("plugins.lst", StringComparison.OrdinalIgnoreCase) || + name.Equals("plugins_svencoop.lst", StringComparison.OrdinalIgnoreCase) || + name.Equals("plugins_goldsrc.lst", StringComparison.OrdinalIgnoreCase))) + continue; File.Copy(file, Path.Combine(destination, Path.GetFileName(file)), true); + } foreach (var directory in Directory.GetDirectories(source)) - CopyDirectory(directory, Path.Combine(destination, Path.GetFileName(directory))); + CopyDirectory(directory, Path.Combine(destination, Path.GetFileName(directory)), preservePluginLists); } } diff --git a/src/MetahookInstallerCore/MetahookSetup.cs b/src/MetahookInstallerCore/MetahookSetup.cs index ec768a8..f8bc39d 100644 --- a/src/MetahookInstallerCore/MetahookSetup.cs +++ b/src/MetahookInstallerCore/MetahookSetup.cs @@ -23,11 +23,21 @@ private static void EnsureValidInstallTarget(string gameDirectory, string modDir } // Describes a complete payload (both launchers) without requiring or deploying it. - public static string DescribeLauncherPath(string gameDirectory, string modDirectory) + public static string DescribeLauncherPath(string gameDirectory, string modDirectory, bool pluginsOnly = false) { EnsureValidInstallTarget(gameDirectory, modDirectory); - return InstallPayload.GetLauncherPath(Path.GetFullPath(gameDirectory), modDirectory, + var launcher = InstallPayload.GetLauncherPath(Path.GetFullPath(gameDirectory), modDirectory, PortableExecutable.IsLegitimatePE(Path.Combine(gameDirectory, "hw.dll"))); + if (pluginsOnly && (!File.Exists(launcher) || !Directory.Exists(Path.Combine(gameDirectory, modDirectory, "metahook")))) + throw new InvalidOperationException("Install MetaHook before deploying plugins: the expected launcher and mod's metahook directory must exist."); + return launcher; + } + + public static string InstallPlugins(string source, string gameDirectory, string modDirectory) + { + var launcher = DescribeLauncherPath(gameDirectory, modDirectory, pluginsOnly: true); + InstallPayload.InstallPlugins(source, gameDirectory, modDirectory); + return launcher; } // Returns the installed launcher path. diff --git a/tests/MetahookInstaller.FileTests/Program.cs b/tests/MetahookInstaller.FileTests/Program.cs index 4cfd913..f685991 100644 --- a/tests/MetahookInstaller.FileTests/Program.cs +++ b/tests/MetahookInstaller.FileTests/Program.cs @@ -3,6 +3,117 @@ var cases = new (string Name, Action Run)[] { + ("Plugin payload discovery does not require or accept only a launcher", fixture => + { + var source = fixture.Payload(normal: false, blob: false); + Equal(null, InstallPayload.FindSourceDirectory(fixture.Path("package"), false)); + Equal(source, InstallPayload.FindSourceDirectory(fixture.Path("package"), false, pluginsOnly: true)); + File.Delete(fixture.Path("package/install/output/svencoop/metahook/plugins/Plugin.dll")); + fixture.Write("package/install/output/MetaHook.exe", "normal"); + Equal(null, InstallPayload.FindSourceDirectory(fixture.Path("package"), false, pluginsOnly: true)); + }), + ("Plugin deployment maps resources and preserves launchers, runtime files and lists", fixture => + { + foreach (var mod in new[] { "svencoop", "valve" }) + { + var source = fixture.Payload(normal: false, blob: false); + var game = fixture.Game(mod); + fixture.NormalEngine(); + var launcher = fixture.Write("game/" + (mod == "svencoop" ? "svencoop.exe" : "MetaHook.exe"), "installed launcher"); + fixture.Write("game/libcurl.dll", "existing runtime"); + fixture.Write("game/MetaHook.pdb", "existing symbols"); + fixture.Write($"game/{mod}/metahook/configs/plugins.lst", "user list"); + fixture.Write($"game/{mod}/metahook/configs/plugins_svencoop.lst", "existing template"); + fixture.Write($"game/{mod}/metahook/plugins/Other.dll", "other plugin"); + fixture.Write("package/install/output/svencoop/metahook/configs/PLUGINS.LST", "must not overwrite"); + fixture.Write("package/install/output/svencoop/metahook/plugins/Plugin.pdb", "plugin symbols"); + fixture.Write("package/install/output/svencoop/metahook/dlls/dependency.dll", "dependency"); + Equal(launcher, MetahookSetup.InstallPlugins(source, game, mod)); + Equal("installed launcher", File.ReadAllText(launcher)); + Equal("existing runtime", File.ReadAllText(fixture.Path("game/libcurl.dll"))); + Equal("existing symbols", File.ReadAllText(fixture.Path("game/MetaHook.pdb"))); + Equal("user list", File.ReadAllText(fixture.Path($"game/{mod}/metahook/configs/plugins.lst"))); + Equal("existing template", File.ReadAllText(fixture.Path($"game/{mod}/metahook/configs/plugins_svencoop.lst"))); + Absent(fixture.Path($"game/{mod}/metahook/configs/plugins_goldsrc.lst")); + Equal("other plugin", File.ReadAllText(fixture.Path($"game/{mod}/metahook/plugins/Other.dll"))); + Equal("plugin", File.ReadAllText(fixture.Path($"game/{mod}/metahook/plugins/Plugin.dll"))); + Equal("plugin symbols", File.ReadAllText(fixture.Path($"game/{mod}/metahook/plugins/Plugin.pdb"))); + Exists(fixture.Path($"game/{mod}/metahook/dlls/dependency.dll")); + Exists(fixture.Path(mod == "svencoop" ? "game/svencoop_downloads/resource.dat" : "game/valve_hidpi/resource.dat")); + } + }), + ("Plugin query requires an installed launcher and MetaHook directory without writing", fixture => + { + var game = fixture.Game("valve"); + fixture.NormalEngine(); + Throws(() => MetahookSetup.DescribeLauncherPath(game, "valve", pluginsOnly: true)); + fixture.Directory("game/valve/metahook"); + Throws(() => MetahookSetup.DescribeLauncherPath(game, "valve", pluginsOnly: true)); + fixture.Write("game/MetaHook.exe", "normal"); + Equal(fixture.Path("game/MetaHook.exe"), MetahookSetup.DescribeLauncherPath(game, "valve", pluginsOnly: true)); + File.Delete(fixture.Path("game/hw.dll")); + fixture.Write("game/MetaHook_blob.exe", "blob"); + var before = fixture.Snapshot(); + Equal(fixture.Path("game/MetaHook_blob.exe"), MetahookSetup.DescribeLauncherPath(game, "valve", pluginsOnly: true)); + Equal(before, fixture.Snapshot()); + Throws(() => MetahookSetup.DescribeLauncherPath(game, "missing", pluginsOnly: true)); + }), + ("Plugin install rejects invalid payloads and missing installations before copying", fixture => + { + var source = fixture.Payload(normal: false, blob: false); + var game = fixture.Game("valve"); + Throws(() => MetahookSetup.InstallPlugins(source, game, "valve")); + Absent(fixture.Path("game/valve/metahook")); + fixture.Write("game/MetaHook_blob.exe", "blob"); + fixture.Directory("game/valve/metahook"); + Throws(() => MetahookSetup.InstallPlugins(fixture.Directory("empty payload"), game, "valve")); + Equal(fixture.Path("game/MetaHook_blob.exe"), MetahookSetup.InstallPlugins(source, game, "valve")); + Absent(fixture.Path("game/valve/metahook/configs/plugins.lst")); + Absent(fixture.Path("game/valve/metahook/configs/plugins_goldsrc.lst")); + }), + ("Plugin options compose with query and symbols but not uninstall", _ => + { + Equal(true, CommandLineOptions.Parse(["-appid", "70", "-PLUGINS-ONLY", "-describe-target"]).PluginsOnly); + Equal(true, CommandLineOptions.Parse(["-appid", "70", "-plugins-only", "-include-debug-symbols"]).IncludeDebugSymbols); + Throws(() => CommandLineOptions.Parse(["-appid", "70", "-plugins-only", "-uninstall"])); + }), + ("CLI plugin query and install work without payload launchers or shortcuts", fixture => + { + var game = fixture.Game("valve"); + fixture.Write("game/MetaHook_blob.exe", "installed blob"); + fixture.Directory("game/valve/metahook"); + fixture.CopyCli(); + var before = fixture.Snapshot(); + var query = fixture.RunCli("-appid", "70", "-gamedir", game, "-plugins-only", "-describe-target"); + Equal(0, query.ExitCode); + Equal("", query.Error); + using var json = System.Text.Json.JsonDocument.Parse(query.Output); + Equal(fixture.Path("game/MetaHook_blob.exe"), json.RootElement.GetProperty("LauncherPath").GetString()); + Equal(before, fixture.Snapshot()); + fixture.Payload(normal: false, blob: false); + var install = fixture.RunCli("-appid", "70", "-gamedir", game, "-plugins-only", "-include-debug-symbols"); + Equal("", install.Error); + Equal(0, install.ExitCode); + Exists(fixture.Path("game/valve/metahook/plugins/Plugin.dll")); + Equal("installed blob", File.ReadAllText(fixture.Path("game/MetaHook_blob.exe"))); + Absent(fixture.Path("working/MetaHook for Half-Life.lnk")); + Absent(fixture.Path("game/libcurl.dll")); + File.Delete(fixture.Path("game/MetaHook_blob.exe")); + var invalid = fixture.RunCli("-appid", "70", "-gamedir", game, "-plugins-only", "-describe-target"); + Equal(1, invalid.ExitCode); + Equal("", invalid.Output); + Equal(true, invalid.Error.Contains("Install MetaHook")); + }), + ("Locked plugin deployment reports failure", fixture => + { + var source = fixture.Payload(normal: false, blob: false); + var game = fixture.Game("valve"); + fixture.Write("game/MetaHook_blob.exe", "blob"); + var plugin = fixture.Write("game/valve/metahook/plugins/Plugin.dll", "old"); + using (new FileStream(plugin, FileMode.Open, FileAccess.Read, FileShare.None)) + Throws(() => MetahookSetup.InstallPlugins(source, game, "valve")); + Equal("old", File.ReadAllText(plugin)); + }), ("Describe target selects the same launcher without changing the game", fixture => { foreach (var mod in new[] { "svencoop", "valve" }) @@ -356,6 +467,30 @@ public string Game(string mod) Write($"game/{mod}/liblist.gam", "fixture"); return Path("game"); } + public string Snapshot() => string.Join("\n", System.IO.Directory.GetFiles(root, "*", SearchOption.AllDirectories) + .OrderBy(path => path).Select(path => path + ":" + Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(File.ReadAllBytes(path))))); + public void CopyCli() + { + var output = System.IO.Path.GetDirectoryName(typeof(CommandLineOptions).Assembly.Location)!; + Directory("package"); + foreach (var file in System.IO.Directory.GetFiles(output)) + File.Copy(file, Path("package/" + System.IO.Path.GetFileName(file)), true); + } + public (int ExitCode, string Output, string Error) RunCli(params string[] arguments) + { + var start = new System.Diagnostics.ProcessStartInfo("dotnet") + { + WorkingDirectory = Directory("working"), UseShellExecute = false, + RedirectStandardOutput = true, RedirectStandardError = true, + }; + start.ArgumentList.Add(Path("package/MetahookInstallerCLI.dll")); + foreach (var argument in arguments) start.ArgumentList.Add(argument); + using var process = System.Diagnostics.Process.Start(start)!; + var output = process.StandardOutput.ReadToEndAsync(); + var error = process.StandardError.ReadToEndAsync(); + process.WaitForExit(); + return (process.ExitCode, output.GetAwaiter().GetResult(), error.GetAwaiter().GetResult()); + } public void NormalEngine() { // Minimal PE signature sufficient for the installer's engine classifier.