Skip to content

Commit

Permalink
feat: add --onMultipleSources flag for unpack ops
Browse files Browse the repository at this point in the history
The unpack operation now supports the `--onMultipleSources` flag just
like the pack operation.
  • Loading branch information
squattingmonk committed Oct 18, 2023
1 parent 9e1c79b commit 0326bb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/nasher/unpack.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/nasher/utils/shared.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:<method>
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:<branch> Select git branch <branch> before operation
--installDir:<dir> Location for installed files (i.e., dir containing erf,
hak, modules, and tlk dirs) [default: $NWN_HOME]
Expand Down

0 comments on commit 0326bb2

Please sign in to comment.