Merge pull request #48 from pnwparksfan/copy-rpf-to-mods

Added method to copy RpfFile to Mods folder
This commit is contained in:
dexyfex 2019-10-30 22:36:20 +11:00 committed by GitHub
commit 0888cde9ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 4 deletions

View File

@ -75,15 +75,57 @@ namespace CodeWalker.GameFiles
FileSize = filesize; 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; RpfFile pfile = this;
while (pfile.Parent != null) while (pfile.Parent != null)
{ {
pfile = pfile.Parent; pfile = pfile.Parent;
} }
return pfile.FilePath; return pfile;
}
public string GetPhysicalFilePath()
{
return GetTopParent().FilePath;
} }

View File

@ -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 RpfFile file = null; //check the dictionary
@ -233,7 +235,7 @@ namespace CodeWalker.GameFiles
string lpath = path.ToLowerInvariant(); //try look at names etc string lpath = path.ToLowerInvariant(); //try look at names etc
foreach (RpfFile tfile in AllRpfs) foreach (RpfFile tfile in AllRpfs)
{ {
if (tfile.NameLower == lpath) if (!exactPathOnly && tfile.NameLower == lpath)
{ {
return tfile; return tfile;
} }