From 8c05eac5b5b997c202c4aa1f236e2a3bd7881531 Mon Sep 17 00:00:00 2001 From: PNWParksFan Date: Mon, 3 Jun 2019 00:12:52 -0700 Subject: [PATCH] Added method to copy RpfFile to Mods folder, and some helper functions --- CodeWalker.Core/GameFiles/RpfFile.cs | 46 +++++++++++++++++++++++-- CodeWalker.Core/GameFiles/RpfManager.cs | 6 ++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CodeWalker.Core/GameFiles/RpfFile.cs b/CodeWalker.Core/GameFiles/RpfFile.cs index 3a0fcc7..a7fdcca 100644 --- a/CodeWalker.Core/GameFiles/RpfFile.cs +++ b/CodeWalker.Core/GameFiles/RpfFile.cs @@ -75,15 +75,57 @@ namespace CodeWalker.GameFiles FileSize = filesize; } + // Returns string to new path + public string CopyToModsFolder(out string status) + { + RpfFile parentFile = GetTopParent(); + string rel_parent_path = parentFile.Path; + string full_parent_path = parentFile.FilePath; - public string GetPhysicalFilePath() + if(rel_parent_path.StartsWith(@"mods\")) + { + status = "already in mods folder"; + return null; + } + + if(!full_parent_path.EndsWith(rel_parent_path)) + { + throw new DirectoryNotFoundException("Expected full parent path to end with relative path"); + } + + string mods_base_path = full_parent_path.Replace(rel_parent_path, @"mods\"); + string dest_path = mods_base_path + rel_parent_path; + + try + { + File.Copy(full_parent_path, dest_path); + status = $"copied \"{parentFile.Name}\" from \"{full_parent_path}\" to \"{dest_path}\""; + return dest_path; + } catch (IOException e) + { + status = $"unable to copy \"{parentFile.Name}\" from \"{full_parent_path}\" to \"{dest_path}\": {e.Message}"; + return null; + } + } + + public bool IsInModsFolder() + { + return GetTopParent().Path.StartsWith(@"mods\"); + } + + public RpfFile GetTopParent() { RpfFile pfile = this; while (pfile.Parent != null) { pfile = pfile.Parent; } - return pfile.FilePath; + return pfile; + } + + public string GetPhysicalFilePath() + { + return GetTopParent().FilePath; } diff --git a/CodeWalker.Core/GameFiles/RpfManager.cs b/CodeWalker.Core/GameFiles/RpfManager.cs index 3a0da21..3f7d8fc 100644 --- a/CodeWalker.Core/GameFiles/RpfManager.cs +++ b/CodeWalker.Core/GameFiles/RpfManager.cs @@ -215,8 +215,10 @@ namespace CodeWalker.GameFiles } + public RpfFile FindRpfFile(string path) => FindRpfFile(path, false); - public RpfFile FindRpfFile(string path) + + public RpfFile FindRpfFile(string path, bool exactPathOnly) { RpfFile file = null; //check the dictionary @@ -233,7 +235,7 @@ namespace CodeWalker.GameFiles string lpath = path.ToLowerInvariant(); //try look at names etc foreach (RpfFile tfile in AllRpfs) { - if (tfile.NameLower == lpath) + if (!exactPathOnly && tfile.NameLower == lpath) { return tfile; }