CodeWalker/CodeWalker.Core/GameFiles/RpfManager.cs

579 lines
21 KiB
C#
Raw Normal View History

2023-11-12 01:59:17 +08:00

using CodeWalker.Core.Utils;
using System;
using System.Collections.Concurrent;
2017-09-21 18:33:05 +08:00
using System.Collections.Generic;
using System.Diagnostics;
2017-09-21 18:33:05 +08:00
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace CodeWalker.GameFiles
{
public class RpfManager
{
private static RpfManager _instance = new RpfManager();
public static RpfManager GetInstance()
{
return _instance ??= new RpfManager();
}
2017-09-21 18:33:05 +08:00
//for caching and management of RPF file data.
public string Folder { get; private set; }
public string[] ExcludePaths { get; set; }
public bool EnableMods { get; set; }
2019-01-11 11:24:50 +08:00
public bool BuildExtendedJenkIndex { get; set; } = true;
public event Action<string> UpdateStatus;
public event Action<string> ErrorLog;
2017-09-21 18:33:05 +08:00
public List<RpfFile> BaseRpfs { get; private set; }
public List<RpfFile> ModRpfs { get; private set; }
public List<RpfFile> DlcRpfs { get; private set; }
public List<RpfFile> AllRpfs { get; private set; }
public List<RpfFile> DlcNoModRpfs { get; private set; }
public List<RpfFile> AllNoModRpfs { get; private set; }
public Dictionary<string, RpfFile> RpfDict { get; private set; }
public Dictionary<string, RpfEntry> EntryDict { get; private set; }
public Dictionary<string, RpfFile> ModRpfDict { get; private set; }
public Dictionary<string, RpfEntry> ModEntryDict { get; private set; }
public volatile bool IsInited = false;
public void Init(string folder, Action<string> updateStatus, Action<string> errorLog, bool rootOnly = false, bool buildIndex = true)
2017-09-21 18:33:05 +08:00
{
using var _ = new DisposableTimer("RpfManager.Init");
UpdateStatus += updateStatus;
ErrorLog += errorLog;
2017-09-21 18:33:05 +08:00
string replpath = folder + "\\";
var sopt = rootOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories;
string[] allfiles = Directory.GetFiles(folder, "*.rpf", sopt);
BaseRpfs = new List<RpfFile>();
ModRpfs = new List<RpfFile>();
DlcRpfs = new List<RpfFile>();
AllRpfs = new List<RpfFile>();
DlcNoModRpfs = new List<RpfFile>();
AllNoModRpfs = new List<RpfFile>();
2023-11-12 01:59:17 +08:00
RpfDict = new Dictionary<string, RpfFile>(StringComparer.OrdinalIgnoreCase);
EntryDict = new Dictionary<string, RpfEntry>(StringComparer.OrdinalIgnoreCase);
ModRpfDict = new Dictionary<string, RpfFile>(StringComparer.OrdinalIgnoreCase);
ModEntryDict = new Dictionary<string, RpfEntry>(StringComparer.OrdinalIgnoreCase);
2017-09-21 18:33:05 +08:00
var rpfs = new ConcurrentBag<RpfFile>();
Parallel.ForEach(allfiles, (rpfpath) =>
2017-09-21 18:33:05 +08:00
{
try
{
RpfFile rf = new RpfFile(rpfpath, rpfpath.Replace(replpath, ""));
if (ExcludePaths != null)
{
bool excl = false;
for (int i = 0; i < ExcludePaths.Length; i++)
{
2023-11-12 01:59:17 +08:00
if (rf.Path.StartsWith(ExcludePaths[i], StringComparison.OrdinalIgnoreCase))
2017-09-21 18:33:05 +08:00
{
excl = true;
break;
}
}
if (excl) return; //skip files in exclude paths.
2017-09-21 18:33:05 +08:00
}
rf.ScanStructure(updateStatus, errorLog);
if (rf.LastException != null) //incase of corrupted rpf (or renamed NG encrypted RPF)
{
return;
}
rpfs.Add(rf);
2017-09-21 18:33:05 +08:00
}
catch (Exception ex)
{
errorLog(rpfpath + ": " + ex.ToString());
}
});
var calculateSum = (RpfFile rpf) => { return 0; };
calculateSum = (RpfFile rpf) =>
{
return rpf.AllEntries?.Count ?? 0 + rpf.Children?.Sum(calculateSum) ?? 0;
};
var minCapacity = rpfs.Sum(calculateSum);
if (minCapacity > AllRpfs.Capacity)
{
AllRpfs.Capacity = minCapacity;
2017-09-21 18:33:05 +08:00
}
foreach(var rpf in rpfs)
{
AddRpfFile(rpf, false, false);
}
if (buildIndex)
{
updateStatus?.Invoke("Building jenkindex...");
Task.Run(() =>
{
BuildBaseJenkIndex();
IsInited = true;
});
updateStatus?.Invoke("Scan complete");
}
else
{
updateStatus?.Invoke("Scan complete");
IsInited = true;
}
2017-09-21 18:33:05 +08:00
2017-09-21 18:33:05 +08:00
}
public void Init(List<RpfFile> allRpfs)
{
using var _ = new DisposableTimer("RpfManager.Init");
2017-09-21 18:33:05 +08:00
//fast init used by RPF explorer's File cache
AllRpfs = allRpfs;
BaseRpfs = new List<RpfFile>();
ModRpfs = new List<RpfFile>();
DlcRpfs = new List<RpfFile>();
DlcNoModRpfs = new List<RpfFile>();
AllNoModRpfs = new List<RpfFile>();
2023-11-12 01:59:17 +08:00
RpfDict = new Dictionary<string, RpfFile>(StringComparer.OrdinalIgnoreCase);
EntryDict = new Dictionary<string, RpfEntry>(StringComparer.OrdinalIgnoreCase);
ModRpfDict = new Dictionary<string, RpfFile>(StringComparer.OrdinalIgnoreCase);
ModEntryDict = new Dictionary<string, RpfEntry>(StringComparer.OrdinalIgnoreCase);
2017-09-21 18:33:05 +08:00
foreach (var rpf in allRpfs)
{
RpfDict[rpf.Path] = rpf;
if (rpf.AllEntries == null) continue;
foreach (var entry in rpf.AllEntries)
{
EntryDict[entry.Path] = entry;
}
}
Task.Run(() =>
{
BuildBaseJenkIndex();
IsInited = true;
});
2017-09-21 18:33:05 +08:00
}
private void AddRpfFile(RpfFile file, bool isdlc, bool ismod)
{
2023-11-12 01:59:17 +08:00
isdlc = isdlc || file.Name.Equals("update.rpf", StringComparison.OrdinalIgnoreCase) || (file.Name.StartsWith("dlc", StringComparison.OrdinalIgnoreCase) && file.Name.EndsWith(".rpf", StringComparison.OrdinalIgnoreCase));
ismod = ismod || (file.Path.StartsWith("mods\\", StringComparison.OrdinalIgnoreCase));
2017-09-21 18:33:05 +08:00
if (file.AllEntries != null)
{
AllRpfs.Add(file);
if (!ismod)
{
AllNoModRpfs.Add(file);
}
if (isdlc)
{
DlcRpfs.Add(file);
if (!ismod)
{
DlcNoModRpfs.Add(file);
}
}
else
{
if (ismod)
{
ModRpfs.Add(file);
}
else
{
BaseRpfs.Add(file);
}
}
if (ismod)
{
ModRpfDict[file.Path.Substring(5)] = file;
}
RpfDict[file.Path] = file;
foreach (RpfEntry entry in file.AllEntries)
{
try
{
if (!string.IsNullOrEmpty(entry.Name))
{
if (ismod)
{
ModEntryDict[entry.Path] = entry;
ModEntryDict[entry.Path.Substring(5)] = entry;
}
else
{
EntryDict[entry.Path] = entry;
}
}
}
catch (Exception ex)
{
file.LastError = ex.ToString();
file.LastException = ex;
ErrorLog?.Invoke(entry.Path + ": " + ex.ToString());
2017-09-21 18:33:05 +08:00
}
}
}
if (file.Children != null)
{
foreach (RpfFile cfile in file.Children)
{
AddRpfFile(cfile, isdlc, ismod);
}
}
}
public RpfFile FindRpfFile(string path) => FindRpfFile(path, false);
2017-09-21 18:33:05 +08:00
public RpfFile FindRpfFile(string path, bool exactPathOnly)
2017-09-21 18:33:05 +08:00
{
2023-11-12 01:59:17 +08:00
RpfFile file;
2017-09-21 18:33:05 +08:00
if (EnableMods && ModRpfDict.TryGetValue(path, out file))
{
return file;
}
if (RpfDict.TryGetValue(path, out file))
{
return file;
}
foreach (RpfFile tfile in AllRpfs)
{
2023-11-12 01:59:17 +08:00
if (!exactPathOnly && tfile.Name.Equals(path, StringComparison.OrdinalIgnoreCase))
2017-09-21 18:33:05 +08:00
{
return tfile;
}
2023-11-12 01:59:17 +08:00
if (tfile.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
2017-09-21 18:33:05 +08:00
{
return tfile;
}
}
return file;
}
public RpfEntry GetEntry(string path)
{
RpfEntry entry;
2023-11-12 01:59:17 +08:00
if (EnableMods && ModEntryDict.TryGetValue(path, out entry))
2017-09-21 18:33:05 +08:00
{
return entry;
}
2023-11-12 01:59:17 +08:00
EntryDict.TryGetValue(path, out entry);
2017-09-21 18:33:05 +08:00
if (entry == null)
{
2023-11-12 01:59:17 +08:00
path = path.Replace("/", "\\");
path = path.Replace("common:", "common.rpf");
if (EnableMods && ModEntryDict.TryGetValue(path, out entry))
2017-09-21 18:33:05 +08:00
{
return entry;
}
2023-11-12 01:59:17 +08:00
EntryDict.TryGetValue(path, out entry);
2017-09-21 18:33:05 +08:00
}
return entry;
}
public byte[] GetFileData(string path)
{
byte[] data = null;
RpfFileEntry entry = GetEntry(path) as RpfFileEntry;
if (entry != null)
{
data = entry.File.ExtractFile(entry);
}
return data;
}
public string GetFileUTF8Text(string path)
{
byte[] bytes = GetFileData(path);
return TextUtil.GetUTF8Text(bytes);
2017-09-21 18:33:05 +08:00
}
public XmlDocument GetFileXml(string path)
{
XmlDocument doc = new XmlDocument();
string text = GetFileUTF8Text(path);
if (!string.IsNullOrEmpty(text))
{
doc.LoadXml(text);
}
return doc;
}
public T GetFile<T>(string path) where T : class, PackedFile, new()
{
T file = null;
byte[] data = null;
RpfFileEntry entry = GetEntry(path) as RpfFileEntry;
if (entry != null)
{
data = entry.File.ExtractFile(entry);
}
if (data != null)
{
file = new T();
file.Load(data, entry);
}
return file;
}
public T GetFile<T>(RpfEntry e) where T : class, PackedFile, new()
{
T file = null;
byte[] data = null;
RpfFileEntry entry = e as RpfFileEntry;
if (entry != null)
{
data = entry.File.ExtractFile(entry);
}
if (data != null)
{
file = new T();
file.Load(data, entry);
}
return file;
}
2023-11-12 01:59:17 +08:00
public async Task<T> GetFileAsync<T>(RpfEntry e) where T : class, PackedFile, new()
{
try
{
T file = null;
byte[] data = null;
RpfFileEntry entry = e as RpfFileEntry;
if (entry != null)
{
data = await entry.File.ExtractFileAsync(entry).ConfigureAwait(false);
}
if (data != null)
{
file = new T();
file.Load(data, entry);
}
return file;
} catch(Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
2017-09-21 18:33:05 +08:00
public bool LoadFile<T>(T file, RpfEntry e) where T : class, PackedFile
{
byte[] data = null;
RpfFileEntry entry = e as RpfFileEntry;
if (entry != null)
{
data = entry.File.ExtractFile(entry);
}
if (data != null)
{
2023-11-12 01:59:17 +08:00
try
{
file.Load(data, entry);
return true;
}
catch(Exception ex)
{
Console.WriteLine($"Error occured while loading {entry.Name} at {entry.Path}:\n{ex}");
throw;
}
}
return false;
}
public async ValueTask<bool> LoadFileAsync<T>(T file, RpfEntry e) where T : class, PackedFile
{
byte[] data = null;
RpfFileEntry entry = e as RpfFileEntry;
if (entry != null)
{
data = await entry.File.ExtractFileAsync(entry).ConfigureAwait(false);
}
if (data != null && data.Length > 0)
{
try
{
file.Load(data, entry);
return true;
}
catch(Exception ex)
{
Console.WriteLine($"Error occured while loading {entry.Name} at {entry.Path}:\n{ex}");
throw;
}
2017-09-21 18:33:05 +08:00
}
return false;
}
public void BuildBaseJenkIndex()
{
using var _ = new DisposableTimer("BuildBaseJenkIndex");
Parallel.ForEach(AllRpfs, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (file) =>
2017-09-21 18:33:05 +08:00
{
try
{
StringBuilder sb = new StringBuilder();
2017-09-21 18:33:05 +08:00
JenkIndex.Ensure(file.Name);
foreach (RpfEntry entry in file.AllEntries)
{
2023-11-12 01:59:17 +08:00
var name = entry.Name;
if (string.IsNullOrEmpty(name))
continue;
//JenkIndex.Ensure(entry.Name);
//JenkIndex.Ensure(nlow);
2023-11-12 01:59:17 +08:00
var nameWithoutExtension = entry.ShortName;
JenkIndex.EnsureBoth(nameWithoutExtension);
//if (ind < entry.Name.Length - 2)
//{
// JenkIndex.Ensure(entry.Name.Substring(0, ind) + ".#" + entry.Name.Substring(ind + 2));
// JenkIndex.Ensure(entry.NameLower.Substring(0, ind) + ".#" + entry.NameLower.Substring(ind + 2));
//}
2019-01-11 11:24:50 +08:00
if (BuildExtendedJenkIndex)
2017-09-21 18:33:05 +08:00
{
2023-11-12 01:59:17 +08:00
if (name.EndsWith(".ydr", StringComparison.OrdinalIgnoreCase))// || nlow.EndsWith(".yft")) //do yft's get lods?
2017-09-21 18:33:05 +08:00
{
2023-11-12 01:59:17 +08:00
var sname = entry.ShortName;
var nameLod = sname + "_lod";
JenkIndex.EnsureLower(nameLod);
JenkIndex.EnsureLower(nameLod + 'a');
JenkIndex.EnsureLower(nameLod + 'b');
2017-09-21 18:33:05 +08:00
}
2023-11-12 01:59:17 +08:00
else if (name.EndsWith(".ydd", StringComparison.OrdinalIgnoreCase))
2017-09-21 18:33:05 +08:00
{
2023-11-12 01:59:17 +08:00
if (name.EndsWith("_children.ydd", StringComparison.OrdinalIgnoreCase))
2017-09-21 18:33:05 +08:00
{
2023-11-12 01:59:17 +08:00
var strn = entry.Name.Substring(0, name.Length - 13);
JenkIndex.EnsureLower(strn);
var nameChildrenLod = strn + "_lod";
JenkIndex.EnsureLower(nameChildrenLod);
JenkIndex.EnsureLower(nameChildrenLod + 'a');
JenkIndex.EnsureLower(nameChildrenLod + 'b');
2019-01-11 11:24:50 +08:00
}
2023-11-12 01:59:17 +08:00
var idx = name.LastIndexOf('_');
2019-01-11 11:24:50 +08:00
if (idx > 0)
{
2023-11-12 01:59:17 +08:00
var str1 = name.Substring(0, idx);
2019-01-11 11:24:50 +08:00
var idx2 = str1.LastIndexOf('_');
if (idx2 > 0)
2017-09-21 18:33:05 +08:00
{
2019-01-11 11:24:50 +08:00
var str2 = str1.Substring(0, idx2);
2023-11-12 01:59:17 +08:00
JenkIndex.EnsureLower(str2 + "_lod");
2019-01-11 11:24:50 +08:00
var maxi = 100;
for (int i = 1; i <= maxi; i++)
{
2023-11-12 01:59:17 +08:00
var str3 = str2 + '_' + i.ToString().PadLeft(2, '0') + "_lod";
2019-01-11 11:24:50 +08:00
//JenkIndex.Ensure(str3);
2023-11-12 01:59:17 +08:00
JenkIndex.EnsureLower(str3);
2019-01-11 11:24:50 +08:00
}
2017-09-21 18:33:05 +08:00
}
}
}
2023-11-12 01:59:17 +08:00
else if(name.EndsWith(".sps", StringComparison.OrdinalIgnoreCase))
2019-03-21 22:29:37 +08:00
{
2023-11-12 01:59:17 +08:00
JenkIndex.EnsureLower(entry.Name);//for shader preset filename hashes!
2019-03-21 22:29:37 +08:00
}
2023-11-12 01:59:17 +08:00
else if(name.EndsWith(".awc", StringComparison.OrdinalIgnoreCase)) //create audio container path hashes...
{
2019-01-11 11:24:50 +08:00
string[] parts = entry.Path.Split('\\');
int pl = parts.Length;
if (pl > 2)
{
2019-01-11 11:24:50 +08:00
string fn = parts[pl - 1];
string fd = parts[pl - 2];
string hpath = fn.Substring(0, fn.Length - 4);
2023-11-12 01:59:17 +08:00
if (fd.EndsWith(".rpf", StringComparison.OrdinalIgnoreCase))
2019-01-11 11:24:50 +08:00
{
fd = fd.Substring(0, fd.Length - 4);
}
2023-11-12 01:59:17 +08:00
hpath = fd + '/' + hpath;
2017-09-21 18:33:05 +08:00
2023-11-12 01:59:17 +08:00
JenkIndex.EnsureLower(hpath);
2019-01-11 11:24:50 +08:00
}
}
2023-11-12 01:59:17 +08:00
else if(name.EndsWith(".nametable", StringComparison.OrdinalIgnoreCase))
{
2023-11-12 01:59:17 +08:00
if (entry is RpfBinaryFileEntry binfe)
{
2019-01-11 11:24:50 +08:00
byte[] data = file.ExtractFile(binfe);
if (data != null)
{
2019-01-11 11:24:50 +08:00
sb.Clear();
for (int i = 0; i < data.Length; i++)
{
2019-01-11 11:24:50 +08:00
byte c = data[i];
if (c == 0)
{
2019-01-11 11:24:50 +08:00
string str = sb.ToString();
if (!string.IsNullOrEmpty(str))
{
//JenkIndex.Ensure(str);
2023-11-12 01:59:17 +08:00
JenkIndex.EnsureLower(str);
2019-01-11 11:24:50 +08:00
////DirMod_Sounds_ entries apparently can be used to infer SP audio strings
////no luck here yet though
//if (strl.StartsWith("dirmod_sounds_") && (strl.Length > 14))
//{
// strl = strl.Substring(14);
// JenkIndex.Ensure(strl);
//}
}
sb.Clear();
}
else
{
sb.Append((char)c);
}
}
}
}
}
}
2017-09-21 18:33:05 +08:00
}
}
catch(Exception err)
2017-09-21 18:33:05 +08:00
{
ErrorLog?.Invoke(err.ToString());
2017-09-21 18:33:05 +08:00
//failing silently!! not so good really
}
});
//foreach (RpfFile file in AllRpfs)
//{
//}
2020-02-08 03:24:04 +08:00
for (int i = 0; i < 100; i++)
{
JenkIndex.Ensure(i.ToString("00"));
}
2017-09-21 18:33:05 +08:00
}
}
}