/// <summary> /// Load a set of files from a path. /// <para>Should generally use <see cref="LoadDir(string)"/> instead.</para> /// </summary> /// <param name="pth">The path.</param> /// <param name="allfiles">A list of all files that need to be loaded.</param> public void Load(string pth, string[] allfiles) { List <PakFile> pfs_t = new List <PakFile>(); foreach (string tfile in allfiles) { string file = tfile.Replace('\\', '/'); if (file.Length == 0 || file[file.Length - 1] == '/') { continue; } if (file.EndsWith(".pak")) { pfs_t.Add(new PakFile(file.Replace(pth, "").ToLowerFast(), file)); } else { PakkedFile pf = new PakkedFile(file.Replace(pth, "").ToLowerFast(), file) { MainDirectory = pth, FileListIndex = Files.Count }; Files.Add(pf); FileMap[pf.Name] = pf; } } Paks.AddRange(pfs_t); int id = 0; foreach (PakFile pak in pfs_t) { IReadOnlyCollection <ZipArchiveEntry> zents = pak.Storer.Entries; pak.FileListIndex = Files.Count; foreach (ZipArchiveEntry zent in zents) { string name = CleanFileName(zent.FullName); if (name.Length == 0 || name[name.Length - 1] == '/') { continue; } PakkedFile pf = new PakkedFile(name, "", id, zent) { MainDirectory = pth, MainPak = pak, FileListIndex = Files.Count }; Files.Add(pf); FileMap[pf.Name] = pf; } id++; } }
/// <summary> /// Returns all the byte data in a file. /// </summary> /// <param name="filename">The name of the file to read.</param> /// <param name="journal">Whether to use a journalling read for file system files.</param> /// <param name="fref">Output file reference.</param> /// <returns>The file's data, as a byte array.</returns> public byte[] ReadBytes(string filename, out PakkedFile fref, bool journal = true) { string fname = CleanFileName(filename); int ind = FileIndex(fname); if (ind == -1) { byte[] b = ReadFromFS(fname); if (b != null) { fref = null; return(b); } if (journal) { b = ReadFromFS(fname + "~1") ?? ReadFromFS(fname + "~2"); if (b != null) { fref = null; return(b); } } throw new UnknownFileException(fname); } PakkedFile file = Files[ind]; fref = file; if (file.IsPakked) { Stream s = file.Entry.Open(); MemoryStream ms = new MemoryStream(); s.CopyTo(ms); s.Close(); byte[] toret = ms.ToArray(); ms.Close(); return(toret); } else // TODO: Should non-pakked "pakked file"s ever be a thing? { return(File.ReadAllBytes(file.Handle)); } }
/// <summary> /// Returns a stream of the byte data in a file. /// </summary> /// <param name="filename">The name of the file to read.</param> /// <param name="fref">Output file reference.</param> /// <returns>The file's data, as a stream.</returns> public Stream ReadToStream(string filename, out PakkedFile fref) { return(new MemoryStream(ReadBytes(filename, out fref))); }