From 0326bb21802d49fb228ad9f6fe12e10d1169db6c Mon Sep 17 00:00:00 2001 From: "Michael A. Sinclair" Date: Wed, 18 Oct 2023 15:23:34 -0400 Subject: [PATCH] feat: add --onMultipleSources flag for unpack ops The unpack operation now supports the `--onMultipleSources` flag just like the pack operation. --- src/nasher/unpack.nim | 12 +++++------- src/nasher/utils/shared.nim | 4 ++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/nasher/unpack.nim b/src/nasher/unpack.nim index 6303649..4ef3d53 100644 --- a/src/nasher/unpack.nim +++ b/src/nasher/unpack.nim @@ -50,7 +50,7 @@ proc genSrcMap(files: seq[string]): Table[string, seq[string]] = if result.hasKeyOrPut(fileName, @[dir]): result[fileName].add(dir) -proc mapSrc(file: string, srcMap: Table[string, seq[string]], rules: seq[Rule]): string = +proc mapSrc(file: string, srcMap: Table[string, seq[string]], rules: seq[Rule], action: MultiSrcAction): string = ## Maps a file to the proper directory, first searching existing source files ## and then matching it to each pattern in rules. Returns the directory. var choices = srcMap.getOrDefault(file) @@ -65,15 +65,13 @@ proc mapSrc(file: string, srcMap: Table[string, seq[string]], rules: seq[Rule]): debug("Matched", file & " to pattern " & pattern.escape) break else: - choices.add("unknown") - result = - choose(fmt"Cannot decide where to extract {file}. Please choose:", - choices) + result = chooseFile(file, choices, action) proc unpack*(opts: Options, target: Target) = let dir = opts.get("directory", getPackageRoot()) precision = opts.get("truncateFloats", 4) + multiSrcAction = opts.get("onMultipleSources", MultiSrcAction.None) if precision notin 1..32: fatal("Invalid value: --truncateFloats must be between 1 and 32") @@ -174,7 +172,7 @@ proc unpack*(opts: Options, target: Target) = for fileName in manifest.keys: let ext = fileName.getFileExt - dir = mapSrc(fileName, srcMap, target.rules) + dir = mapSrc(fileName, srcMap, target.rules, multiSrcAction) var sourceName = dir / filename if ext in GffExtensions: @@ -223,7 +221,7 @@ proc unpack*(opts: Options, target: Target) = let ext = file.fileName.getFileExt filePath = tmpDir / file.fileName - dir = mapSrc(file.fileName, srcMap, target.rules) + dir = mapSrc(file.fileName, srcMap, target.rules, multiSrcAction) if dir == "unknown": warning("cannot decide where to extract " & file.fileName) diff --git a/src/nasher/utils/shared.nim b/src/nasher/utils/shared.nim index 43bba71..8616b9d 100644 --- a/src/nasher/utils/shared.nim +++ b/src/nasher/utils/shared.nim @@ -122,6 +122,10 @@ const UnpackOpts* = """ have [range: 0-32, default: 4] --removeDeleted Remove source files not present in the file being unpacked [default: false] + --onMultipleSources: + How to handle multiple sources for the same file + [choices: choose (default), default (accept the first), + error (fail)]. Overrides --yes/--no if explicitly set. --branch: Select git branch before operation --installDir: Location for installed files (i.e., dir containing erf, hak, modules, and tlk dirs) [default: $NWN_HOME]